搜索结果列表页面开发
This commit is contained in:
143
src/stores/user.ts
Normal file
143
src/stores/user.ts
Normal file
@@ -0,0 +1,143 @@
|
||||
import { acceptHMRUpdate, defineStore } from 'pinia';
|
||||
import { reactive, ref } from 'vue';
|
||||
import { getUserToken, getUserInfo } from '@/api/user';
|
||||
|
||||
export interface AccountInfo {
|
||||
domain_id?: string;
|
||||
email?: string;
|
||||
id?: string;
|
||||
arts_id?: string;
|
||||
mobile?: string;
|
||||
avatar?: string;
|
||||
nickname?: string;
|
||||
username?: string;
|
||||
access_token?: string;
|
||||
refresh_token?: string;
|
||||
xauth_token?: string;
|
||||
isFollow?: boolean;
|
||||
atomgit_username?: string;
|
||||
[x: string]: any;
|
||||
}
|
||||
|
||||
export const useAccountStore = defineStore('accountInfo', () => {
|
||||
const cacheInfo = localStorage.getItem('userInfo');
|
||||
const isLogin = ref(Boolean(localStorage.getItem('access_token')));
|
||||
const userInfo = cacheInfo ? JSON.parse(cacheInfo) : {};
|
||||
const accountInfo = reactive<AccountInfo>({
|
||||
access_token: localStorage.getItem('access_token') || '',
|
||||
refresh_token: localStorage.getItem('refresh_token') || '',
|
||||
xauth_token: localStorage.getItem('xauth_token') || '',
|
||||
...userInfo
|
||||
});
|
||||
|
||||
const refresh = () => {
|
||||
/** 重定向跳转时强刷使用 */
|
||||
const info = localStorage.getItem('userInfo');
|
||||
isLogin.value = Boolean(localStorage.getItem('access_token'));
|
||||
const userData = info ? JSON.parse(info) : {};
|
||||
for (const key in userData) {
|
||||
accountInfo[key] = userData[key];
|
||||
}
|
||||
accountInfo.access_token = localStorage.getItem('access_token') || '';
|
||||
accountInfo.refresh_token = localStorage.getItem('refresh_token') || '';
|
||||
accountInfo.xauth_token = localStorage.getItem('xauth_token') || '';
|
||||
};
|
||||
|
||||
const saveStatus = (status: boolean) => {
|
||||
isLogin.value = status;
|
||||
};
|
||||
|
||||
const saveAccountInfo = (info?: AccountInfo) => {
|
||||
if (info) {
|
||||
for (const key in info) {
|
||||
accountInfo[key] = info[key];
|
||||
}
|
||||
} else {
|
||||
for (const key in accountInfo) {
|
||||
accountInfo[key] = '';
|
||||
}
|
||||
}
|
||||
};
|
||||
// 可能有从博客跳转过来的,通过接口判断是否登陆
|
||||
const checkIsLogin = async () => {
|
||||
const { data, error } = await getUserToken();
|
||||
if (data?.data) {
|
||||
const { access_token, refresh_token } = data.data;
|
||||
localStorage.setItem('access_token', access_token);
|
||||
localStorage.setItem('refresh_token', refresh_token);
|
||||
saveStatus(true);
|
||||
const userRes = await getUserInfo();
|
||||
if (userRes.data?.data) {
|
||||
localStorage.setItem('userInfo', JSON.stringify(userRes.data?.data));
|
||||
saveAccountInfo({
|
||||
access_token: localStorage.getItem('access_token') || '',
|
||||
refresh_token: localStorage.getItem('refresh_token') || '',
|
||||
...userRes.data?.data
|
||||
});
|
||||
}
|
||||
return true;
|
||||
}
|
||||
localStorage.removeItem('access_token');
|
||||
localStorage.removeItem('refresh_token');
|
||||
localStorage.removeItem('userInfo');
|
||||
saveAccountInfo({});
|
||||
return false;
|
||||
};
|
||||
const saveAtomgitUserName = (username: string) => {
|
||||
accountInfo.atomgit_username = username;
|
||||
};
|
||||
|
||||
return {
|
||||
isLogin,
|
||||
accountInfo,
|
||||
checkIsLogin,
|
||||
saveStatus,
|
||||
refresh,
|
||||
saveAccountInfo,
|
||||
saveAtomgitUserName
|
||||
};
|
||||
});
|
||||
|
||||
export const otherAccountStore = defineStore('otherInfo', () => {
|
||||
const isLogin = ref(Boolean(localStorage.getItem('access_token')));
|
||||
const userInfo = {};
|
||||
const accountInfo = reactive<AccountInfo>({
|
||||
...userInfo
|
||||
});
|
||||
|
||||
const saveAccountInfo = (info?: AccountInfo) => {
|
||||
if (info) {
|
||||
for (const key in info) {
|
||||
accountInfo[key] = info[key];
|
||||
}
|
||||
} else {
|
||||
for (const key in accountInfo) {
|
||||
accountInfo[key] = '';
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const saveFollowed = (isFollowed?: boolean) => {
|
||||
accountInfo.isFollow = isFollowed;
|
||||
};
|
||||
|
||||
return {
|
||||
isLogin,
|
||||
accountInfo,
|
||||
saveAccountInfo,
|
||||
saveFollowed
|
||||
};
|
||||
});
|
||||
|
||||
export const entranceData = defineStore('entrance', () => {
|
||||
const repoList = ref([]);
|
||||
const orgList = ref([]);
|
||||
return {
|
||||
repoList,
|
||||
orgList
|
||||
};
|
||||
});
|
||||
|
||||
if ((import.meta as any).hot) {
|
||||
(import.meta as any).hot.accept(acceptHMRUpdate(useAccountStore, (import.meta as any).hot));
|
||||
}
|
||||
Reference in New Issue
Block a user