diff --git a/src/pages.json b/src/pages.json index 7305f40..b119114 100644 --- a/src/pages.json +++ b/src/pages.json @@ -23,23 +23,29 @@ } }, { - "path": "pages/category/index", + "path": "pages/category/category", "style": { "navigationBarTitleText": "分类" } }, { - "path": "pages/cart/index", + "path": "pages/cart/cart", "style": { "navigationBarTitleText": "购物车" } }, { - "path": "pages/my/index", + "path": "pages/my/my", "style": { "navigationStyle": "custom", "navigationBarTitleText": "我的" } + }, + { + "path": "pages/login/login", + "style": { + "navigationBarTitleText": "登录" + } } ], // 全局样式 @@ -66,19 +72,19 @@ }, { "text": "分类", - "pagePath": "pages/category/index", + "pagePath": "pages/category/category", "iconPath": "static/tabs/category_default.png", "selectedIconPath": "static/tabs/category_selected.png" }, { "text": "购物车", - "pagePath": "pages/cart/index", + "pagePath": "pages/cart/cart", "iconPath": "static/tabs/cart_default.png", "selectedIconPath": "static/tabs/cart_selected.png" }, { "text": "我的", - "pagePath": "pages/my/index", + "pagePath": "pages/my/my", "iconPath": "static/tabs/user_default.png", "selectedIconPath": "static/tabs/user_selected.png" } diff --git a/src/pages/cart/index.vue b/src/pages/cart/cart.vue similarity index 100% rename from src/pages/cart/index.vue rename to src/pages/cart/cart.vue diff --git a/src/pages/category/index.vue b/src/pages/category/category.vue similarity index 100% rename from src/pages/category/index.vue rename to src/pages/category/category.vue diff --git a/src/pages/login/login.vue b/src/pages/login/login.vue new file mode 100644 index 0000000..b6f924c --- /dev/null +++ b/src/pages/login/login.vue @@ -0,0 +1,7 @@ + + + + + diff --git a/src/pages/my/index.vue b/src/pages/my/index.vue deleted file mode 100644 index e1adcc3..0000000 --- a/src/pages/my/index.vue +++ /dev/null @@ -1,7 +0,0 @@ - - - - - diff --git a/src/pages/my/my.vue b/src/pages/my/my.vue new file mode 100644 index 0000000..ef98063 --- /dev/null +++ b/src/pages/my/my.vue @@ -0,0 +1,47 @@ + + + + + diff --git a/src/utils/http.ts b/src/utils/http.ts index 189e499..c68988b 100644 --- a/src/utils/http.ts +++ b/src/utils/http.ts @@ -37,3 +37,61 @@ const interceptorOptions = { uni.addInterceptor('request', interceptorOptions) uni.addInterceptor('uploadFile', interceptorOptions) + +/** + * 请求函数 + * @param UniApp.RequestOptions + * @returns Promise + * 1. 返回 Promise 对象 + * 2. 获取数据成功 + * 2.1 提取核心数据 res.data + * 2.2 添加类型,支持泛型 + * 3. 获取数据失败 + * 3.1 401错误 -> 清理用户信息,跳转到登录页 + * 3.2 其他错误 -> 根据后端错误信息轻提示 + * 3.3 网络错误 -> 提示用户换网络 + */ +interface Data { + code: number + msg: string + result: T +} + +export const http = (options: UniApp.RequestOptions) => { + return new Promise>((resolve, reject) => { + uni.request({ + ...options, + // 1. 响应成功 + success: (res) => { + if (res.statusCode >= 200 && res.statusCode < 300) { + // 2xx 成功, 提取核心数据 res.data + resolve(res.data as Data) + return + } + if (res.statusCode === 401) { + // 401错误 -> 清理用户信息,跳转到登录页 + useMemberStore().clearProfile() + uni.navigateTo({ + url: '/pages/login/login', + }) + reject(res.data as Data) + return + } + // 其他错误 -> 根据后端错误信息轻提示 + uni.showToast({ + title: (res.data as Data).msg || '请求失败', + icon: 'none', + }) + reject(res.data as Data) + }, + // 2. 响应失败 + fail: (err) => { + reject(err.errMsg) + uni.showToast({ + title: '网络错误,请稍后再试', + icon: 'none', + }) + }, + }) + }) +}