| 1 | import { create } from 'zustand'; |
| 2 | import { combine } from 'zustand/middleware'; |
| 3 | import lodash from 'lodash'; |
| 4 | import { AccountInfo } from '@/views/account/comment'; |
| 5 | import { |
| 6 | AccountGroup, |
| 7 | acpAccountLoginCheckMulti, |
| 8 | icpGetAccountGroup, |
| 9 | icpGetAccountList, |
| 10 | } from '@/icp/account'; |
| 11 | import { onAccountLoginFinish } from '@/icp/receiveMsg'; |
| 12 | import { sleep } from '@@/utils'; |
| 13 | |
| 14 | export interface AccountGroupItem extends AccountGroup { |
| 15 | children: AccountInfo[]; |
| 16 | } |
| 17 | |
| 18 | export interface IAccountStore { |
| 19 | accountList: AccountInfo[]; |
| 20 | accountMap: Map<number, AccountInfo>; |
| 21 | unBindFn?: () => void; |
| 22 | accountGroupList: AccountGroupItem[]; |
| 23 | accountGroupMap: Map<number, AccountGroupItem>; |
| 24 | } |
| 25 | |
| 26 | const store: IAccountStore = { |
| 27 | // 不分组的账户数据 |
| 28 | accountList: [], |
| 29 | // 分组的账户数据 |
| 30 | accountGroupList: [], |
| 31 | accountGroupMap: new Map([]), |
| 32 | accountMap: new Map([]), |
| 33 | unBindFn: undefined, |
| 34 | }; |
| 35 | |
| 36 | const getStore = () => { |
| 37 | return lodash.cloneDeep(store); |
| 38 | }; |
| 39 | |
| 40 | // 视频发布所有组件的共享状态和方法 |
| 41 | export const useAccountStore = create( |
| 42 | combine( |
| 43 | { |
| 44 | ...getStore(), |
| 45 | }, |
| 46 | (set, get, storeApi) => { |
| 47 | const methods = { |
| 48 | clear() { |
| 49 | set({ |
| 50 | ...getStore(), |
| 51 | }); |
| 52 | if (get().unBindFn) get().unBindFn!(); |
| 53 | }, |
| 54 | |
| 55 | setAccountGroupList(accountGroupList: AccountGroupItem[]) { |
| 56 | set({ accountGroupList }); |
| 57 | }, |
| 58 | |
| 59 | async getAccountList() { |
| 60 | const accountMap = new Map<number, AccountInfo>([]); |
| 61 | const result = await icpGetAccountList(); |
| 62 | if (!result) return; |
| 63 | |
| 64 | for (const item of result) { |
| 65 | accountMap.set(item.id, item); |
| 66 | } |
| 67 | |
| 68 | set({ |
| 69 | accountList: result, |
| 70 | accountMap, |
| 71 | }); |
| 72 | await methods.getAccountGroup(); |
| 73 | }, |
| 74 | |
| 75 | // 获取用户组的数据并且将用户放到对应组下 |
| 76 | async getAccountGroup() { |
| 77 | const groupList = await icpGetAccountGroup(); |
| 78 | if (groupList.length === 0) return; |
| 79 | const accountGroupList: AccountGroupItem[] = []; |
| 80 | // key=组ID,val=账户ID |
| 81 | const accountGroupMap = new Map<number, AccountGroupItem>(); |
| 82 | groupList.map((v) => { |
| 83 | const accountGroupItem = { |
| 84 | ...v, |
| 85 | children: [], |
| 86 | }; |
| 87 | accountGroupList.push(accountGroupItem); |
| 88 | accountGroupMap.set(v.id, accountGroupItem); |
| 89 | }); |
| 90 | get().accountList.map((v) => { |
| 91 | accountGroupMap.get(v.groupId!)!.children?.push(v); |
| 92 | }); |
| 93 | |
| 94 | accountGroupList.sort((a, b) => { |
| 95 | return a.rank - b.rank; |
| 96 | }); |
| 97 | |
| 98 | set({ |
| 99 | accountGroupList, |
| 100 | accountGroupMap, |
| 101 | }); |
| 102 | }, |
| 103 | |
| 104 | async init() { |
| 105 | const unBindFn = onAccountLoginFinish(() => { |
| 106 | methods.getAccountList(); |
| 107 | }); |
| 108 | |
| 109 | set({ |
| 110 | unBindFn, |
| 111 | }); |
| 112 | |
| 113 | await methods.getAccountList(); |
| 114 | methods.timeingCheckAccount(); |
| 115 | }, |
| 116 | // 轮询检测账户有效性 |
| 117 | async timeingCheckAccount() { |
| 118 | return new Promise(async () => { |
| 119 | while (true) { |
| 120 | const { accountList } = get(); |
| 121 | |
| 122 | await acpAccountLoginCheckMulti( |
| 123 | accountList.map((v) => ({ |
| 124 | pType: v.type, |
| 125 | uid: v.uid, |
| 126 | })), |
| 127 | ); |
| 128 | |
| 129 | // 等待24小时执行,如果用户没有关闭应用 |
| 130 | await sleep(3600000 * 24); |
| 131 | } |
| 132 | }); |
| 133 | }, |
| 134 | }; |
| 135 | return methods; |
| 136 | }, |
| 137 | ), |
| 138 | ); |
| 139 |