Update token handling and user state management
This commit is contained in:
@ -2,7 +2,26 @@
|
||||
export const useToken = () =>
|
||||
useState<string>('token', () => {
|
||||
const token = useCookie<string | undefined>('token');
|
||||
return token.value ? 'Bearer ' + token.value : '';
|
||||
return token.value ? token.value : '';
|
||||
});
|
||||
|
||||
type UserInfo = {
|
||||
nickname: string,
|
||||
avatar: string,
|
||||
mobile: string,
|
||||
id: number | undefined,
|
||||
vipLevel: number | undefined,
|
||||
sex: number | undefined,
|
||||
}
|
||||
export const useUserInfo = () => useState<UserInfo>('userInfo', () => {
|
||||
return {
|
||||
nickname: '',
|
||||
avatar: '',
|
||||
mobile: '',
|
||||
id: undefined,
|
||||
vipLevel: undefined,
|
||||
sex: undefined,
|
||||
}
|
||||
});
|
||||
|
||||
/** 热门数据 */
|
||||
|
||||
@ -6,7 +6,8 @@ const useServerRequest = async <T>(
|
||||
url: string,
|
||||
opts?: UseFetchOptions<T, unknown>
|
||||
) => {
|
||||
const token = useCookie<string | undefined>("token");
|
||||
const token = useToken();
|
||||
const user = useUserInfo();
|
||||
const runtimeConfig = useRuntimeConfig();
|
||||
|
||||
const defaultOptions: UseFetchOptions<unknown> = {
|
||||
@ -14,9 +15,15 @@ const useServerRequest = async <T>(
|
||||
onRequest({ options }) {
|
||||
options.headers = options.headers || "application/json";
|
||||
if (token.value) {
|
||||
console.log('user----', user.value);
|
||||
|
||||
console.log('token----', "Bearer " + token.value)
|
||||
// @ts-ignore
|
||||
options.headers["authorization"] = "Bearer " + token.value;
|
||||
}
|
||||
|
||||
console.log(url +'-----' +'options----', options);
|
||||
|
||||
},
|
||||
onResponse({ response }) {
|
||||
if (+response.status === 200 && +response._data.code !== 0) {
|
||||
|
||||
Reference in New Issue
Block a user