From 0735174e7387a010ca682753296595c832785082 Mon Sep 17 00:00:00 2001 From: expdsn <18111002318@163.com> Date: Thu, 14 Nov 2024 15:19:14 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=BA=86=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E7=9A=84=E8=87=AA=E5=8A=A8=E5=90=8C=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/user/useUserStore.ts | 96 +++++++++++++++++++++++----------------- 1 file changed, 56 insertions(+), 40 deletions(-) diff --git a/src/user/useUserStore.ts b/src/user/useUserStore.ts index 9141eb6..c33c792 100644 --- a/src/user/useUserStore.ts +++ b/src/user/useUserStore.ts @@ -3,7 +3,7 @@ import useLayoutStore from '@/layout/useLayoutStore' import useRouterStore from '@/useRouterStore' import request from '@/utils/request' import { defineStore } from 'pinia' -import { computed, reactive, ref, toRefs, watch } from 'vue' +import { computed, reactive, ref, watch } from 'vue' interface UserInfo { id: string @@ -22,39 +22,23 @@ const defaultUserInfo: UserInfo = { 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 { if (arr1.length !== arr2.length) { return false; } // 将 arr2 转换为一个以 id 为键的映射 - const arr2Map = new Map(arr2.map(item => [item.id, item])); + // 检查 arr1 中的每个 item 是否在 arr2 中存在,并且值是否相同 - for (const item1 of arr1) { - const item2 = arr2Map.get(item1.id); - if (!item2 || JSON.stringify(item1) !== JSON.stringify(item2)) { - return false; + for (let i = 0; i < arr1.length; i++) { + console.log(arr1[i].id); + console.log(arr2[i].id); + + if (arr1[i].id !== arr2[i].id) { + + return false } } @@ -70,7 +54,25 @@ export default defineStore('user', () => { const layout = useLayoutStore() 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( token, async (val) => { @@ -90,19 +92,32 @@ export default defineStore('user', () => { console.log(val); const data = await request('GET', '/api/backup') - if (!data) return + if (!data) { + useRouterStore().replace('') + + return + } const remoteList = getLinkList(data) const localList = getLinkList(layout.state) - const addList: Block[] = [] + + remoteAddList.value = [] for (const item of remoteList) { - if (!localList.some((el) => el.id !== item.id)) { - addList.push(item) + // if (!localList.some((el) => el.id !== item.id)) { + // // 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: 交给张阳 - remoteAddList.value = addList + // remoteAddList.value = addList remoteData.value = data + useRouterStore().go('global-backup') } }, @@ -112,16 +127,17 @@ export default defineStore('user', () => { Object.assign(profile, { ...defaultUserInfo }) } const comineData = () => { - if (!remoteData.value) return - Object.assign(layout.state.content[layout.state.currentPage].pages, remoteAddList.value) - + if (!remoteAddList.value) return + console.log({ ...remoteAddList.value }); + remoteAddList.value.map(item => { + layout.state.content[layout.state.current].pages[layout.state.currentPage].list.push(item) + }) } const coverageData = () => { if (!remoteData.value) return - console.log({ ...layout.state }); - console.log({ ...remoteData.value }); - - Object.assign(layout.state, remoteData.value) + Object.assign(layout.state.content, remoteData.value.content) + Object.assign(layout.state.dock, remoteData.value.dock) + Object.assign(layout.state.dir, remoteData.value.dir) } return {