完成了登录的自动同步

This commit is contained in:
expdsn 2024-11-14 15:19:14 +08:00
parent 9a74b31eac
commit 0735174e73
1 changed files with 56 additions and 40 deletions

View File

@ -3,7 +3,7 @@ import useLayoutStore from '@/layout/useLayoutStore'
import useRouterStore from '@/useRouterStore' import useRouterStore from '@/useRouterStore'
import request from '@/utils/request' import request from '@/utils/request'
import { defineStore } from 'pinia' import { defineStore } from 'pinia'
import { computed, reactive, ref, toRefs, watch } from 'vue' import { computed, reactive, ref, watch } from 'vue'
interface UserInfo { interface UserInfo {
id: string id: string
@ -22,39 +22,23 @@ const defaultUserInfo: UserInfo = {
openId: '' 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
}
function areArraysEqualById(arr1: Block[], arr2: Block[]): boolean { function areArraysEqualById(arr1: Block[], arr2: Block[]): boolean {
if (arr1.length !== arr2.length) { if (arr1.length !== arr2.length) {
return false; return false;
} }
// 将 arr2 转换为一个以 id 为键的映射 // 将 arr2 转换为一个以 id 为键的映射
const arr2Map = new Map(arr2.map(item => [item.id, item]));
// 检查 arr1 中的每个 item 是否在 arr2 中存在,并且值是否相同 // 检查 arr1 中的每个 item 是否在 arr2 中存在,并且值是否相同
for (const item1 of arr1) { for (let i = 0; i < arr1.length; i++) {
const item2 = arr2Map.get(item1.id); console.log(arr1[i].id);
if (!item2 || JSON.stringify(item1) !== JSON.stringify(item2)) { console.log(arr2[i].id);
return false;
if (arr1[i].id !== arr2[i].id) {
return false
} }
} }
@ -70,7 +54,25 @@ export default defineStore('user', () => {
const layout = useLayoutStore() const layout = useLayoutStore()
const isLogin = computed(() => !!token.value && !!profile.id) const isLogin = computed(() => !!token.value && !!profile.id)
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) {
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
}
watch( watch(
token, token,
async (val) => { async (val) => {
@ -90,19 +92,32 @@ export default defineStore('user', () => {
console.log(val); console.log(val);
const data = await request<Layout>('GET', '/api/backup') const data = await request<Layout>('GET', '/api/backup')
if (!data) return if (!data) {
useRouterStore().replace('')
return
}
const remoteList = getLinkList(data) const remoteList = getLinkList(data)
const localList = getLinkList(layout.state) const localList = getLinkList(layout.state)
const addList: Block[] = []
remoteAddList.value = []
for (const item of remoteList) { for (const item of remoteList) {
if (!localList.some((el) => el.id !== item.id)) { // if (!localList.some((el) => el.id !== item.id)) {
addList.push(item) // // addList.push(item)
// remoteAddList.value.push(item)
// }
if (localList.findIndex(val => val.id === item.id) === -1) {
remoteAddList.value.push({ ...item })
} }
} }
if (addList.length > 0 || areArraysEqualById(remoteList, localList) || (remoteList.length !== localList.length)) {
if (remoteAddList.value.length > 0 || !areArraysEqualById(remoteList, localList)) {
// TODO: 交给张阳 // TODO: 交给张阳
remoteAddList.value = addList // remoteAddList.value = addList
remoteData.value = data remoteData.value = data
useRouterStore().go('global-backup') useRouterStore().go('global-backup')
} }
}, },
@ -112,16 +127,17 @@ export default defineStore('user', () => {
Object.assign(profile, { ...defaultUserInfo }) Object.assign(profile, { ...defaultUserInfo })
} }
const comineData = () => { const comineData = () => {
if (!remoteData.value) return if (!remoteAddList.value) return
Object.assign(layout.state.content[layout.state.currentPage].pages, remoteAddList.value) console.log({ ...remoteAddList.value });
remoteAddList.value.map(item => {
layout.state.content[layout.state.current].pages[layout.state.currentPage].list.push(item)
})
} }
const coverageData = () => { const coverageData = () => {
if (!remoteData.value) return if (!remoteData.value) return
console.log({ ...layout.state }); Object.assign(layout.state.content, remoteData.value.content)
console.log({ ...remoteData.value }); Object.assign(layout.state.dock, remoteData.value.dock)
Object.assign(layout.state.dir, remoteData.value.dir)
Object.assign(layout.state, remoteData.value)
} }
return { return {