index.ts
1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { defineStore } from 'pinia';
import { clearToken } from '@/utils/auth';
import useAuthApi from '@/http/auth';
import { AuthorizedState } from './type';
// eslint-disable-next-line import/no-cycle
import { useAppStore } from '@/store';
import { useCertifyApi } from '@/http/user';
import { useApplyApi } from '@/http/activity';
const useAuthorizedStore = defineStore('authorized', {
state: (): AuthorizedState => ({
id: undefined,
nick_name: undefined,
avatar: undefined,
permissions: [],
auditUserCount: 0,
auditActivityCount: 0,
activityApplyFailCount: 0,
}),
getters: {
authorizedInfo(state: AuthorizedState): AuthorizedState {
return { ...state };
},
getKey(state: AuthorizedState): number {
return state.id || 0;
},
},
actions: {
setInfo(partial: Partial<AuthorizedState>) {
// @ts-ignore
this.$patch(partial);
},
syncAuditUser() {
useCertifyApi.get({ status: 0, page: 1, pageSize: 1 }).then(({ meta }) => this.setInfo({ auditUserCount: meta.total }));
},
syncAuditActivity() {
useApplyApi
.get({ auditStatus: 0, songType: 1, page: 1, pageSize: 1 })
.then(({ meta }) => this.setInfo({ auditActivityCount: meta.total }));
useApplyApi
.get({ auditStatus: 2, songType: 1, createdForm: 1, page: 1, pageSize: 1 })
.then(({ meta }) => this.setInfo({ activityApplyFailCount: meta.total }));
},
async syncInfo() {
const { user, permissions } = await useAuthApi.info();
this.setInfo({ ...user, permissions });
useAppStore().queryPermissionList();
this.syncAuditUser();
this.syncAuditActivity();
},
logout() {
clearToken();
this.$reset();
window.location.reload();
},
async syncToken() {
// TODO
// const { data } = await refreshToken();
// setToken(data.access_token);
},
},
});
export default useAuthorizedStore;