From 54084b4d0761d8e3ae6c74866c59a632bc48d357 Mon Sep 17 00:00:00 2001 From: RyRh <3123294924@qq.com> Date: Fri, 21 Aug 2026 14:21:35 +0800 Subject: [PATCH] =?UTF-8?q?'=E9=A1=B9=E7=9B=AE=E9=85=8D=E7=BD=AE'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + api/public.js | 79 +++++++++++++++++++++++++++ api/request.js | 67 +++++++++++++++++++++++ manifest.json | 144 +++++++++++++++++++++++++------------------------ 4 files changed, 222 insertions(+), 69 deletions(-) create mode 100644 .gitignore create mode 100644 api/public.js create mode 100644 api/request.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..99057ba --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +unpackage/ \ No newline at end of file diff --git a/api/public.js b/api/public.js new file mode 100644 index 0000000..45c78ac --- /dev/null +++ b/api/public.js @@ -0,0 +1,79 @@ +/** + * 公共路由接口封装(/public/*) + * 注:响应拦截器暂未做业务规范拦截,调用方拿到的是 uni.request 原始 response 结构 { statusCode, data, header ... } + */ +import request from './request.js'; + +const PREFIX = '/public'; + +/** + * 获取商品分类树 + * @param {Object} [params] + * @param {string|number} [params.countryId] 国家 ID,不传时返回全部国家 + */ +export function getCategories(params) { + return request.get(`${PREFIX}/categories`, params); +} + +/** + * 获取有商品的国家列表 + */ +export function getCountries() { + return request.get(`${PREFIX}/countries`); +} + +/** + * 获取有商品的标签列表 + */ +export function getTags() { + return request.get(`${PREFIX}/tags`); +} + +/** + * 获取标签组及标签筛选项 + * @param {Object} [params] + * @param {string|number} [params.countryId] 国家 ID,不传时返回全部国家 + */ +export function getTagGroups(params) { + return request.get(`${PREFIX}/tag-groups`, params); +} + +/** + * 分页获取商品 + * @param {Object} params + * @param {number} [params.page] + * @param {number} [params.pageSize] + * @param {string|number} [params.countryId] + * @param {string|number} [params.categoryId] + * @param {string} [params.keyword] + */ +export function getGoods(params) { + return request.get(`${PREFIX}/goods`, params); +} + +/** + * 获取商品完整详情 + * @param {string|number} goodId 商品 ID(替换路径变量 {goodId}) + */ +export function getGoodsDetail(goodId) { + return request.get(`${PREFIX}/goods/${goodId}`); +} + +/** + * 获取首页商品(可按国家返回) + * @param {Object} [params] + * @param {string|number} [params.countryId] + */ +export function getHomeGoods(params) { + return request.get(`${PREFIX}/home-goods`, params); +} + +export default { + getCategories, + getCountries, + getTags, + getTagGroups, + getGoods, + getGoodsDetail, + getHomeGoods +}; diff --git a/api/request.js b/api/request.js new file mode 100644 index 0000000..5ebd706 --- /dev/null +++ b/api/request.js @@ -0,0 +1,67 @@ +/** + * uni-app 简易请求封装 + * - 统一拼接 BASE_URL + path + * - 注入 token header + * - Promise 化,提供 get/post/put/delete 快捷方法 + * + * 用法: + * import request from '@/api/request.js' + * request.get('/user/info', { id: 1 }) + * request.post('/user/login', { username, password }) + */ + +// 基础路径:修改此处即可切换后端地址 +const BASE_URL = 'http://192.168.124.137:3001'; + +// 默认配置 +const TIMEOUT = 30000; +const DEFAULT_HEADER = { 'Content-Type': 'application/json' }; + +// 统一拼装 URL:完整地址直接返回,否则 BASE_URL + path +function buildUrl(url) { + if (!url) return ''; + if (/^https?:\/\//i.test(url)) return url; + const base = BASE_URL.replace(/\/+$/, ''); + const path = url.replace(/^\/+/, ''); + return path ? `${base}/${path}` : base; +} + +// 统一取 header(注入 token) +function buildHeader(extraHeader) { + const header = Object.assign({}, DEFAULT_HEADER, extraHeader || {}); + const token = uni.getStorageSync('token'); + if (token) header['Authorization'] = `Bearer ${token}`; + return header; +} + +/** + * 核心请求方法 + * @param {Object} options + * @param {string} options.url 相对路径或完整 URL + * @param {string} [options.method='GET'] + * @param {Object} [options.data] 请求参数 / body + * @param {Object} [options.header] 额外 header + * @returns {Promise} + */ +function request(options = {}) { + return new Promise((resolve, reject) => { + uni.request({ + url: buildUrl(options.url || ''), + method: (options.method || 'GET').toUpperCase(), + data: options.data || {}, + header: buildHeader(options.header), + timeout: TIMEOUT, + dataType: 'json', + success: resolve, + fail: reject + }); + }); +} + +// 快捷方法 +request.get = (url, data, options) => request({ url, method: 'GET', data, ...options }); +request.post = (url, data, options) => request({ url, method: 'POST', data, ...options }); +request.put = (url, data, options) => request({ url, method: 'PUT', data, ...options }); +request.delete = (url, data, options) => request({ url, method: 'DELETE', data, ...options }); + +export default request; diff --git a/manifest.json b/manifest.json index 210f49a..14336ad 100644 --- a/manifest.json +++ b/manifest.json @@ -1,70 +1,76 @@ { - "name": "inkreach-miniprogram", - "appid": "", - "description": "", - "versionName": "1.0.0", - "versionCode": "100", - "transformPx": false, - "app-plus": { - "usingComponents": true, - "nvueStyleCompiler": "uni-app", - "compilerVersion": 3, - "splashscreen": { - "alwaysShowBeforeRender": true, - "waiting": true, - "autoclose": true, - "delay": 0 - }, - "modules": {}, - "distribute": { - "android": { - "permissions": [ - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "" - ] - }, - "ios": {}, - "sdkConfigs": {} - } - }, - "quickapp": {}, - "mp-weixin": { - "appid": "wxd0e6eff3c220337b", - "setting": { - "urlCheck": false - }, - "usingComponents": true, - "permission": { - "scope.userLocation": { - "desc": "你的位置信息将用于小程序位置接口的效果展示" - } - }, - "plugins": {} - }, - "mp-alipay": { - "usingComponents": true - }, - "mp-baidu": { - "usingComponents": true - }, - "mp-toutiao": { - "usingComponents": true - }, - "uniStatistics": { - "enable": false - }, - "vueVersion": "2" -} \ No newline at end of file + "name" : "inkreach-miniprogram", + "appid" : "__UNI__73E5929", + "description" : "", + "versionName" : "1.0.0", + "versionCode" : "100", + "transformPx" : false, + "app-plus" : { + "usingComponents" : true, + "nvueStyleCompiler" : "uni-app", + "compilerVersion" : 3, + "splashscreen" : { + "alwaysShowBeforeRender" : true, + "waiting" : true, + "autoclose" : true, + "delay" : 0 + }, + "modules" : {}, + "distribute" : { + "android" : { + "permissions" : [ + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "" + ] + }, + "ios" : {}, + "sdkConfigs" : {} + } + }, + "quickapp" : {}, + "mp-weixin" : { + "appid" : "wxd0e6eff3c220337b", + "setting" : { + "urlCheck" : false + }, + "usingComponents" : true, + "permission" : { + "scope.userLocation" : { + "desc" : "你的位置信息将用于小程序位置接口的效果展示" + } + }, + "plugins" : {} + }, + "mp-alipay" : { + "usingComponents" : true + }, + "mp-baidu" : { + "usingComponents" : true + }, + "mp-toutiao" : { + "usingComponents" : true + }, + "uniStatistics" : { + "enable" : false + }, + "vueVersion" : "2", + "h5" : { + "devServer" : { + "https" : false, + "host" : "192.168.124.19" + } + } +}