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