From f6feecb324836694df9dac4a2fb583cebe337f61 Mon Sep 17 00:00:00 2001 From: cuolv Date: Wed, 2 Aug 2023 05:30:13 +0000 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9A=E7=A7=81=E5=AF=86?= =?UTF-8?q?=E5=88=86=E7=B1=BB=E7=9A=84=E8=AE=BF=E9=97=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/category.js | 3 ++ common/http/interceptors.js | 82 +++++++++++++++++++++++++++++-------- 2 files changed, 68 insertions(+), 17 deletions(-) diff --git a/api/category.js b/api/category.js index 58cf89e..7edc4a9 100644 --- a/api/category.js +++ b/api/category.js @@ -4,6 +4,7 @@ */ import HttpHandler from '@/common/http/request.js' +import { getCache } from '@/utils/storage.js' export default { /** * 查询分类列表 @@ -19,6 +20,8 @@ export default { * @param {Object} params 查询参数 */ getCategoryPostList: (slug, params) => { + // 从缓存中根据分类获取密码,如果获取到了说明本分类需要密码,避免多个分类需要密码在输入密码后刷新页面点错了分类 + params.password = getCache('APP_CATEGORY_PWD_' + slug) return HttpHandler.Get(`/api/content/categories/${slug}/posts`, params) }, } diff --git a/common/http/interceptors.js b/common/http/interceptors.js index cc08442..23e9ff2 100644 --- a/common/http/interceptors.js +++ b/common/http/interceptors.js @@ -13,7 +13,8 @@ import { getAdminAccessToken } from "@/utils/auth.js"; import { - delCache + delCache, + setCache } from "@/utils/storage"; export const setInterceptors = (http) => { http.interceptors.request.use( @@ -57,24 +58,71 @@ export const setInterceptors = (http) => { message: 'API接口服务异常!' }) } else if (response.data.status == 401) { - delCache('APP_ADMIN_LOGIN_TOKEN'); - uni.$eShowModal({ - title: '提示', - content: '您未登录超管账号或登录已过期,是否重新登录?', - showCancel: true, - cancelText: '否', - cancelColor: '#999999', - confirmText: '是', - confirmColor: '#03a9f4' - }).then(res => { - uni.navigateTo({ - url: '/pagesB/login/login' + uni.$tm.toast(response.data.message); + // 如果是请求分类之后报401说明密码错误,那么清除该密码,下次点击会报403弹窗再次输入密码 + if (response.config.url.indexOf('/api/content/categories') >= 0) { + let requestUrl = response.config.url; + var reg = '(?<=/api/content/categories/).+(?=/posts)' + let category = requestUrl.match(reg)[0] + delCache('APP_CATEGORY_PWD_' + category) + } else { + // 其他情况维持原来的逻辑 + delCache('APP_ADMIN_LOGIN_TOKEN'); + uni.$eShowModal({ + title: '提示', + content: '您未登录超管账号或登录已过期,是否重新登录?', + showCancel: true, + cancelText: '否', + cancelColor: '#999999', + confirmText: '是', + confirmColor: '#03a9f4' + }).then(res => { + uni.navigateTo({ + url: '/pagesB/login/login' + }) + }).catch(err => { + uni.switchTab({ + url: '/pages/tabbar/about/about' + }) }) - }).catch(err => { - uni.switchTab({ - url: '/pages/tabbar/about/about' + } + } else if (response.data.status == 403) { + // 如果报403是请求分类文章接口(您没有该分类的访问权限)的话说明是私密分类,需要输入密码请求 + if (response.config.url.indexOf('/api/content/categories') >= 0) { + uni.showModal({ + title: '私密分类', // TODO 这里应该获取分类的名字,可以在弹窗之前请求后台拿到所有分类根据分类code拿到名称,但是不会在这之前发送请求 + content: '', + editable: true, + placeholderText: '请输入密码', + confirmText: '确认', + cancelText: '取消', + showCancel: true, + cancelColor: '#000000', + confirmColor: '#007aff', + success: (res) => { + if (res.confirm) { + // TODO 这里如果没有输入密码点击确认应该阻止窗口关闭,但是没找到方法 + if (!res.content) { + uni.$tm.toast('请输入密码'); + return + } + // 根据请求URL正则匹配分类code,然后把输入的密码根据分类code放入缓存,然后在category.getCategoryPostList中获取,解决多个分类加密输入密码后点错的问题 + // 目前存在一个问题,比如前两个都需要密码,如果先输入第二个的密码之后,重新进来默认打开第一个还会弹窗,所以想在弹窗标题上增加分类名字 + // 另外有以下两种方式科技解决 + // TODO 1.其实这里获取到密码之后可以直接发送一个请求追加上password参数,因为后台会缓存权限,后续不输入密码也可以访问,可惜不会 + // TODO 2.另外也可以拿到密码之后,直接选中该分类追加password参数,重新请求,可惜也不会 + let requestUrl = response.config.url; + var reg = '(?<=/api/content/categories/).+(?=/posts)' + let category = requestUrl.match(reg)[0] + setCache('APP_CATEGORY_PWD_' + category, res.content) + uni.reLaunch({ + url: '/pages/tabbar/category/category' + }); + } else if (res.cancel) { + } + }, }) - }) + } } else { return Promise.reject(response.data); } -- Gitee