1
0
mirror of https://github.com/ialley-workshop-open/uni-halo.git synced 2026-06-10 11:59:28 +08:00

refactor: 对token配置进行重构、同时修复token获取失败的问题

This commit is contained in:
小莫唐尼
2025-07-01 20:09:05 +08:00
parent 4246d8cdcb
commit d521a641de
14 changed files with 236 additions and 195 deletions
+1 -1
View File
@@ -1,5 +1,5 @@
<script>
import HaloTokenConfig from '@/config/token.config.js';
import HaloTokenConfig from '@/config/uhalo.config.js';
// app升级检测(搭配:https://ext.dcloud.net.cn/plugin?id=4470 升级中心)
import CheckAppUpdate from '@/uni_modules/uni-upgrade-center-app/utils/check-update';
+7 -9
View File
@@ -1,13 +1,11 @@
/**
* 所有的接口
*/
import HaloTokenConfig from '@/config/token.config.js'
import {getPersonalToken} from '@/utils/token.js'
import HttpHandler from '@/common/http/request.js'
import qs from 'qs'
import {
getAppConfigs
} from '@/config/index.js'
import {getAppConfigs} from '@/config/index.js'
export default {
/**
@@ -51,7 +49,7 @@ export default {
encode: false,
arrayFormat: 'repeat'
})
return HttpHandler.Get(`/apis/api.content.halo.run/v1alpha1/categories?${param}`,{})
return HttpHandler.Get(`/apis/api.content.halo.run/v1alpha1/categories?${param}`, {})
},
/**
* 查询分类下的文章
@@ -112,7 +110,7 @@ export default {
getMomentList: (params) => {
return HttpHandler.Get(`/apis/moment.halo.run/v1alpha1/moments`, params, {
custom: {
systemToken: HaloTokenConfig.systemToken
personalToken: getPersonalToken()
}
})
},
@@ -131,7 +129,7 @@ export default {
getPhotoGroupList: (params) => {
return HttpHandler.Get(`/apis/core.halo.run/v1alpha1/photogroups`, params, {
custom: {
systemToken: HaloTokenConfig.systemToken
personalToken: getPersonalToken()
}
})
},
@@ -143,7 +141,7 @@ export default {
getPhotoListByGroupName: (params) => {
return HttpHandler.Get(`/apis/console.api.photo.halo.run/v1alpha1/photos`, params, {
custom: {
systemToken: HaloTokenConfig.systemToken
personalToken: getPersonalToken()
}
})
},
@@ -154,7 +152,7 @@ export default {
getFriendLinkGroupList: (params) => {
return HttpHandler.Get(`/apis/core.halo.run/v1alpha1/linkgroups`, params, {
custom: {
systemToken: HaloTokenConfig.systemToken
personalToken: getPersonalToken()
}
})
},
+1 -1
View File
@@ -10,7 +10,7 @@
* 修改时间:
*/
import HaloTokenConfig from '@/config/token.config.js'
import HaloTokenConfig from '@/config/uhalo.config.js'
import {
setInterceptors
} from "./interceptors.js";
+14 -30
View File
@@ -9,13 +9,7 @@
* 修改人员:
* 修改时间:
*/
import {
getAdminAccessToken
} from "@/utils/auth.js";
import {
delCache,
setCache
} from "@/utils/storage";
import {delCache, setCache} from "@/utils/storage";
const getCategoryNameByUrl = (url) => {
const reg = '(?<=/api/content/categories/).+(?=/posts)'
@@ -37,9 +31,7 @@ const showCategoryInputPasswordModal = (response, category) => {
// TODO 这里如果没有输入密码点击确认应该阻止窗口关闭,但是没找到方法
if (!res.content) {
uni.showToast({
title: '提示:请输入密码',
icon: 'none',
success: () => {
title: '提示:请输入密码', icon: 'none', success: () => {
setTimeout(() => {
showCategoryInputPasswordModal(response, category);
}, 800)
@@ -56,14 +48,14 @@ const showCategoryInputPasswordModal = (response, category) => {
uni.reLaunch({
url: '/pages/tabbar/category/category'
});
} else if (res.cancel) {}
} else if (res.cancel) {
}
},
})
}
export const setInterceptors = (http) => {
http.interceptors.request.use(
(config) => {
http.interceptors.request.use((config) => {
console.log("config", config)
// 可使用async await 做异步操作
@@ -71,19 +63,16 @@ export const setInterceptors = (http) => {
...config.header
// ... 可以直接加参数
};
if (config.custom.systemToken) {
config.header['Authorization'] = `Bearer ${config.custom.systemToken}`
if (config.custom.personalToken) {
config.header['Authorization'] = `Bearer ${config.custom.personalToken}`
}
return config;
},
(config) => {
}, (config) => {
// 可使用async await 做异步操作
return Promise.reject(config);
}
);
});
http.interceptors.response.use(
(response) => {
http.interceptors.response.use((response) => {
/* 对响应成功做点什么 可使用async await 做异步操作*/
// if (response.data.code !== 200) { // 服务端返回的状态码不等于200,则reject()
// return Promise.reject(response) // return Promise.reject 可使promise状态进入catch
@@ -96,13 +85,11 @@ export const setInterceptors = (http) => {
} else {
return Promise.reject(response);
}
},
(response) => {
}, (response) => {
/* 对响应错误做点什么 statusCode !== 200*/
if (!response.data) {
return Promise.reject({
status: 500,
message: 'API接口服务异常!'
status: 500, message: 'API接口服务异常!'
})
} else if (response.data.status == 401) {
uni.$tm.toast(response.data.message);
@@ -111,9 +98,7 @@ export const setInterceptors = (http) => {
const category = getCategoryNameByUrl(response.config.url)
delCache('APP_CATEGORY_PWD_' + category);
uni.showToast({
title: '提示:密码不正确',
icon: 'none',
success: () => {
title: '提示:密码不正确', icon: 'none', success: () => {
setTimeout(() => {
showCategoryInputPasswordModal(response, category);
}, 800)
@@ -152,6 +137,5 @@ export const setInterceptors = (http) => {
return Promise.reject(response.data);
}
}
);
});
};
+1 -1
View File
@@ -3,7 +3,7 @@
*/
import {getAppConfigs} from '@/config/index.js'
import HaloTokenConfig from '@/config/token.config.js'
import HaloTokenConfig from '@/config/uhalo.config.js'
import utils from '@/utils/index.js'
export default {
+5 -1
View File
@@ -4,7 +4,11 @@ export const _AppMockJsonKey = 'APP_GLOBAL_MOCK_JSON';
// 默认的应用设置
export const DefaultAppConfigs = {
basicConfig: {},
basicConfig: {
tokenConfig: {
personalToken: "",
}
},
loveConfig: {},
imagesConfig: {},
authorConfig: {},
-15
View File
@@ -1,15 +0,0 @@
import {getAppConfigs} from "@/config/index";
/** 应用配置 */
const UniHaloConfig = Object.freeze({
/** 基础请求域名:你的Halo博客基础域名 */
BASE_API: "",
// 比如:
// BASE_API: "https://blog.xiaoxiaomo.cn",
// token
systemToken: getAppConfigs()?.basicConfig?.tokenConfig?.personalToken,
})
export default UniHaloConfig;
+12
View File
@@ -0,0 +1,12 @@
/** 应用基础配置 */
const UHaloBaseConfig = Object.freeze({
/** 基础请求域名:你的Halo博客基础域名 */
BASE_API: "",
// 示例:
// BASE_API: "https://blog.xiaoxiaomo.cn",
// todo:可能有其他配置
})
export default UHaloBaseConfig;
+1 -1
View File
@@ -58,7 +58,7 @@ Vue.prototype.$http = http;
import ApiManager from '@/api/index.js'
Vue.use(ApiManager);
import HaloTokenConfig from '@/config/token.config.js'
import HaloTokenConfig from '@/config/uhalo.config.js'
Vue.prototype.$baseApiUrl = HaloTokenConfig.BASE_API
// 由于微信小程序的运行机制问题,需声明如下一行,H5和APP非必填
+1 -1
View File
@@ -316,7 +316,7 @@ import {
getRestrictReadTypeName,
getShowableContent
} from "@/utils/restrictRead";
import HaloTokenConfig from "@/config/token.config";
import HaloTokenConfig from "@/config/uhalo.config";
import RestrictReadSkeleton from "@/components/restrict-read-skeleton/restrict-read-skeleton.vue";
import TmImages from "@/tm-vuetify/components/tm-images/tm-images.vue";
import TmInput from "@/tm-vuetify/components/tm-input/tm-input.vue";
+6
View File
@@ -8,6 +8,7 @@
import {DefaultAppConfigs, getAppConfigs, getAppMockJson, setAppConfigs, setAppMockJson} from '@/config/index.js'
import v2Config from '@/api/v2/all.config.js'
import utils from '@/utils/index.js'
import {setTokens} from "@/utils/token";
export default {
namespaced: true,
@@ -42,6 +43,11 @@ export default {
const res = await v2Config.getAppConfigs()
if (res) {
commit('setConfigs', utils.deepMerge(DefaultAppConfigs, res))
// 存储token
if (res?.basicConfig?.tokenConfig) {
setTokens(res.basicConfig.tokenConfig)
}
resolve(res)
} else {
dispatch("setDefaultAppSettings");
+1 -1
View File
@@ -10,7 +10,7 @@
* 修改时间:
*/
import HaloTokenConfig from '@/config/token.config.js'
import HaloTokenConfig from '@/config/uhalo.config.js'
import {getAppConfigs} from '@/config/index.js'
const utils = {
+5 -5
View File
@@ -1,8 +1,8 @@
/**
* 设置缓存
* @param {缓存key} key
* @param {需要存储的缓存值} value
* @param {过期时间,默认0表示永久有效} expire
* @param {string} key 缓存key
* @param {Object} value 需要存储的缓存值
* @param {number} expire 过期时间,默认0表示永久有效
*/
export const setCache = (key, value, expire = 0) => {
let obj = {
@@ -15,7 +15,7 @@ export const setCache = (key, value, expire = 0) => {
/**
* 获取缓存
* @param {缓存key} key
* @param {string} key
*/
export const getCache = (key) => {
let val = uni.getStorageSync(key)
@@ -32,7 +32,7 @@ export const getCache = (key) => {
/**
* 删除缓存
* @param {缓存key} key
* @param { string } key 缓存key
*/
export const delCache = (key) => {
uni.removeStorageSync(key)
+52
View File
@@ -0,0 +1,52 @@
import {getCache, setCache} from "./storage";
import utils from "./index"
export const APP_TOKENS_KEY = "APP_TOKENS";
/**
* 获取 tokens
*/
export function getTokens() {
const tokens = getCache(APP_TOKENS_KEY);
return tokens ?? null;
}
/**
* 设置 tokens
*/
export function setTokens(value) {
setCache(APP_TOKENS_KEY, value);
return true;
}
/**
* 设置 tokens
*/
export function updateTokens(value) {
if (!value) return false
const tokens = getTokens(APP_TOKENS_KEY);
if (!tokens) return false
setTokens(utils.deepMerge(tokens, value))
return true
}
/**
* 获取 系统token
*/
export function getPersonalToken() {
const tokens = getTokens(APP_TOKENS_KEY);
console.log("tokens", tokens)
return tokens?.personalToken;
}
/**
* 设置 系统token
*/
export function setPersonalToken(personalToken) {
const tokens = getCache(APP_TOKENS_KEY);
if (!tokens) return false
tokens.personalToken = personalToken
updateTokens(tokens)
return true
}