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 useLayoutStore from '@/layout/useLayoutStore'
import useRouterStore from '@/useRouterStore'
import { sendParent } from '@/utils/parent'
import request from '@/utils/request'
import { defineStore } from 'pinia'
import { computed, reactive, ref, watch } from 'vue'
@ -22,27 +23,24 @@ const defaultUserInfo: UserInfo = {
openId: ''
}
function areArraysEqualById(arr1: Block[], arr2: Block[]): boolean {
if (arr1.length !== arr2.length) {
return false;
return false
}
// 将 arr2 转换为一个以 id 为键的映射
// 检查 arr1 中的每个 item 是否在 arr2 中存在,并且值是否相同
for (let i = 0; i < arr1.length; i++) {
console.log(arr1[i].id);
console.log(arr2[i].id);
console.log(arr1[i].id)
console.log(arr2[i].id)
if (arr1[i].id !== arr2[i].id) {
return false
}
}
return true;
return true
}
export default defineStore('user', () => {
@ -77,19 +75,20 @@ export default defineStore('user', () => {
token,
async (val) => {
localStorage.setItem('token', val)
if (!val) return
if (!val) {
sendParent(['logout'])
return
}
sendParent(['login', val])
const res = await request<UserInfo>('GET', '/api/profile')
Object.assign(profile, res)
},
{ immediate: true }
)
watch(
token,
async (val) => {
watch(token, async (val) => {
if (!val) return
console.log(val);
console.log(val)
const data = await request<Layout>('GET', '/api/backup')
if (!data) {
@ -106,13 +105,11 @@ export default defineStore('user', () => {
// // addList.push(item)
// remoteAddList.value.push(item)
// }
if (localList.findIndex(val => val.id === item.id) === -1) {
if (localList.findIndex((val) => val.id === item.id) === -1) {
remoteAddList.value.push({ ...item })
}
}
if (remoteAddList.value.length > 0 || !areArraysEqualById(remoteList, localList)) {
// TODO: 交给张阳
// remoteAddList.value = addList
@ -120,16 +117,15 @@ export default defineStore('user', () => {
useRouterStore().go('global-backup')
}
},
)
})
const logout = () => {
token.value = ''
Object.assign(profile, { ...defaultUserInfo })
}
const comineData = () => {
if (!remoteAddList.value) return
console.log({ ...remoteAddList.value });
remoteAddList.value.map(item => {
console.log({ ...remoteAddList.value })
remoteAddList.value.map((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.dock, remoteData.value.dock)
Object.assign(layout.state.dir, remoteData.value.dir)
}
return {
token,

View File

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