This commit is contained in:
plightfield 2024-11-12 18:30:13 +08:00
parent 3d7eec25b2
commit b3f1ed6f34
1 changed files with 40 additions and 6 deletions

View File

@ -1,3 +1,5 @@
import type { Block, Layout } from '@/layout/layout.types'
import useLayoutStore from '@/layout/useLayoutStore'
import request from '@/utils/request'
import { defineStore } from 'pinia'
import { computed, reactive, ref, watch } from 'vue'
@ -18,19 +20,53 @@ const defaultUserInfo: UserInfo = {
avatar: '',
openId: ''
}
function getLinkList(data: Layout) {
const list = [] as Block[]
for (const mode of data.content) {
for (const page of mode.pages) {
for (const item of page.list) {
if (!item.link || item.link.startsWith('id:')) break
list.push(item)
}
}
}
for (const item of data.dock) {
if (item) list.push(item)
}
for (const dirItem of Object.values(data.dir)) {
for (const item of dirItem.list) {
list.push(item)
}
}
return list
}
export default defineStore('user', () => {
const token = ref(localStorage.getItem('token') || '')
watch(token, (val) => {
localStorage.setItem('token', val)
})
const profile = reactive({ ...defaultUserInfo })
const layout = useLayoutStore()
watch(
token,
(val) => {
async (val) => {
if (!val) return
request<UserInfo>('GET', '/api/profile').then((res) => {
Object.assign(profile, res)
})
const res = await request<UserInfo>('GET', '/api/profile')
Object.assign(profile, res)
const data = await request<Layout>('GET', '/api/backup')
const remoteList = getLinkList(data)
const localList = getLinkList(layout.state)
const addList: Block[] = []
for (const item of remoteList) {
if (!localList.some((el) => el.id !== item.id)) {
addList.push(item)
}
}
if (addList.length > 0) {
// TODO: 交给张阳
}
},
{ immediate: true }
)
@ -38,9 +74,7 @@ export default defineStore('user', () => {
const logout = () => {
token.value = ''
Object.assign(profile, { ...defaultUserInfo })
// profile.avatar = ''
}
// 自动备份
return {
token,
profile,