forked from mxx1111/mdlook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmpAccounts.ts
More file actions
111 lines (102 loc) · 3.3 KB
/
Copy pathmpAccounts.ts
File metadata and controls
111 lines (102 loc) · 3.3 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import { v4 as uuidv4 } from 'uuid'
import { addPrefix } from '@/utils'
import { store } from '@/utils/storage'
export interface MpAccount {
/** UUID — internal store identifier */
id: string
/** WeChat public account base64 ID, e.g. "MzIxNjA5ODQ0OQ==" */
mpId: string
name: string
logo: string
desc: string
/**
* 1: 公众号
* 2: 服务号
*/
serviceType: `1` | `2`
/**
* 0: 无标识
* 1: 个人认证
* 2: 企业认证
*/
verify: `0` | `1` | `2`
}
export const useMpAccountsStore = defineStore(`mpAccounts`, () => {
const accounts = store.reactive<MpAccount[]>(addPrefix(`mp-accounts`), [])
const currentAccountId = store.reactive<string>(addPrefix(`mp-current-account-id`), ``)
// ── 迁移旧数据(MD__mp-profile → accounts 数组)──────────────────────────
if (accounts.value.length === 0) {
try {
const oldRaw = localStorage.getItem(addPrefix(`mp-profile`))
if (oldRaw) {
const old = JSON.parse(oldRaw)
if (old && (old.name || old.id)) {
const migrated: MpAccount = {
id: uuidv4(),
mpId: old.id ?? ``,
name: old.name ?? ``,
logo: old.logo ?? ``,
desc: old.desc ?? ``,
serviceType: old.serviceType ?? `1`,
verify: old.verify ?? `0`,
}
accounts.value.push(migrated)
currentAccountId.value = migrated.id
}
}
}
catch {
// ignore parse errors
}
}
// ── 计算属性 ─────────────────────────────────────────────────────────────
const currentAccount = computed<MpAccount | null>(() => {
return (
accounts.value.find(a => a.id === currentAccountId.value)
?? accounts.value[0]
?? null
)
})
// ── 操作 ─────────────────────────────────────────────────────────────────
function addAccount(data?: Partial<Omit<MpAccount, 'id'>>): MpAccount {
const newAccount: MpAccount = {
id: uuidv4(),
mpId: data?.mpId ?? ``,
name: data?.name ?? `新账号`,
logo: data?.logo ?? ``,
desc: data?.desc ?? ``,
serviceType: data?.serviceType ?? `1`,
verify: data?.verify ?? `0`,
}
accounts.value.push(newAccount)
currentAccountId.value = newAccount.id
return newAccount
}
function updateAccount(id: string, data: Partial<Omit<MpAccount, 'id'>>) {
const index = accounts.value.findIndex(a => a.id === id)
if (index !== -1) {
accounts.value[index] = { ...accounts.value[index], ...data }
}
}
function deleteAccount(id: string) {
const index = accounts.value.findIndex(a => a.id === id)
if (index === -1)
return
accounts.value.splice(index, 1)
if (currentAccountId.value === id) {
currentAccountId.value = accounts.value[0]?.id ?? ``
}
}
function setCurrentAccount(id: string) {
currentAccountId.value = id
}
return {
accounts,
currentAccountId,
currentAccount,
addAccount,
updateAccount,
deleteAccount,
setCurrentAccount,
}
})