This commit is contained in:
plightfield 2024-11-15 13:25:47 +08:00
parent 3a07dae8b3
commit 35be5445b6
2 changed files with 40 additions and 45 deletions

View File

@ -1,6 +1,7 @@
import type { Block, Layout } from '@/layout/layout.types' import type { Block, Layout } from '@/layout/layout.types'
import useLayoutStore from '@/layout/useLayoutStore' import useLayoutStore from '@/layout/useLayoutStore'
import useRouterStore from '@/useRouterStore' import useRouterStore from '@/useRouterStore'
import { sendParent } from '@/utils/parent'
import request from '@/utils/request' import request from '@/utils/request'
import { defineStore } from 'pinia' import { defineStore } from 'pinia'
import { computed, reactive, ref, watch } from 'vue' import { computed, reactive, ref, watch } from 'vue'
@ -22,27 +23,24 @@ const defaultUserInfo: UserInfo = {
openId: '' openId: ''
} }
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 为键的映射
// 检查 arr1 中的每个 item 是否在 arr2 中存在,并且值是否相同 // 检查 arr1 中的每个 item 是否在 arr2 中存在,并且值是否相同
for (let i = 0; i < arr1.length; i++) { for (let i = 0; i < arr1.length; i++) {
console.log(arr1[i].id); console.log(arr1[i].id)
console.log(arr2[i].id); console.log(arr2[i].id)
if (arr1[i].id !== arr2[i].id) { if (arr1[i].id !== arr2[i].id) {
return false return false
} }
} }
return true; return true
} }
export default defineStore('user', () => { export default defineStore('user', () => {
@ -77,59 +75,57 @@ export default defineStore('user', () => {
token, token,
async (val) => { async (val) => {
localStorage.setItem('token', val) localStorage.setItem('token', val)
if (!val) {
if (!val) return sendParent(['logout'])
return
}
sendParent(['login', val])
const res = await request<UserInfo>('GET', '/api/profile') const res = await request<UserInfo>('GET', '/api/profile')
Object.assign(profile, res) Object.assign(profile, res)
}, },
{ immediate: true } { immediate: true }
) )
watch( watch(token, async (val) => {
token, if (!val) return
async (val) => { console.log(val)
if (!val) return
console.log(val);
const data = await request<Layout>('GET', '/api/backup') const data = await request<Layout>('GET', '/api/backup')
if (!data) { if (!data) {
useRouterStore().replace('') useRouterStore().replace('')
return return
}
const remoteList = getLinkList(data)
const localList = getLinkList(layout.state)
remoteAddList.value = []
for (const item of remoteList) {
// 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 })
} }
const remoteList = getLinkList(data) }
const localList = getLinkList(layout.state)
remoteAddList.value = [] if (remoteAddList.value.length > 0 || !areArraysEqualById(remoteList, localList)) {
for (const item of remoteList) { // TODO: 交给张阳
// if (!localList.some((el) => el.id !== item.id)) { // remoteAddList.value = addList
// // addList.push(item) remoteData.value = data
// remoteAddList.value.push(item)
// }
if (localList.findIndex(val => val.id === item.id) === -1) {
remoteAddList.value.push({ ...item }) useRouterStore().go('global-backup')
} }
} })
if (remoteAddList.value.length > 0 || !areArraysEqualById(remoteList, localList)) {
// TODO: 交给张阳
// remoteAddList.value = addList
remoteData.value = data
useRouterStore().go('global-backup')
}
},
)
const logout = () => { const logout = () => {
token.value = '' token.value = ''
Object.assign(profile, { ...defaultUserInfo }) Object.assign(profile, { ...defaultUserInfo })
} }
const comineData = () => { const comineData = () => {
if (!remoteAddList.value) return if (!remoteAddList.value) return
console.log({ ...remoteAddList.value }); console.log({ ...remoteAddList.value })
remoteAddList.value.map(item => { remoteAddList.value.map((item) => {
layout.state.content[layout.state.current].pages[layout.state.currentPage].list.push(item) layout.state.content[layout.state.current].pages[layout.state.currentPage].list.push(item)
}) })
} }
@ -138,7 +134,6 @@ export default defineStore('user', () => {
Object.assign(layout.state.content, remoteData.value.content) Object.assign(layout.state.content, remoteData.value.content)
Object.assign(layout.state.dock, remoteData.value.dock) Object.assign(layout.state.dock, remoteData.value.dock)
Object.assign(layout.state.dir, remoteData.value.dir) Object.assign(layout.state.dir, remoteData.value.dir)
} }
return { return {
token, token,

View File

@ -1,5 +1,5 @@
// 发送消息 // 发送消息
export function sendParent(data: ['openSide']) { export function sendParent(data: ['openSide'] | ['login', string] | ['logout']) {
parent.window.postMessage( parent.window.postMessage(
{ {
type: data[0], type: data[0],