新增:私密分类的访问

This commit is contained in:
cuolv 2023-08-02 05:30:13 +00:00 committed by Gitee
parent f838da43d8
commit f6feecb324
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
2 changed files with 68 additions and 17 deletions

View File

@ -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)
},
}

View File

@ -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);
}