zdg #175
|
@ -4,7 +4,9 @@
|
|||
const isNode = typeof require !== 'undefined' // 是否支持node函数
|
||||
const { ipcRenderer } = isNode?require('electron'):{} // app使用
|
||||
import { sessionStore } from '@/utils/store'
|
||||
// import { diff } from 'jsondiffpatch'
|
||||
// const Remote = isNode?require('@electron/remote'):{} // 远程模块
|
||||
|
||||
export function shareStorePlugin({store}) {
|
||||
store.$subscribe((mutation, state) => { // 自动同步
|
||||
// mutation 变量包含了变化前后的状态
|
||||
|
@ -19,8 +21,8 @@ export function shareStorePlugin({store}) {
|
|||
if (names.includes(storeName)) {
|
||||
const { storeId: storeName, payload, events, type } = mutation // direct
|
||||
// if (!Object.keys(payload).length) return
|
||||
if (type != 'direct' || !events || Array.isArray(events) || !events.key) return
|
||||
stateSync(storeName, events.key, events.newValue, state) // 需要同步
|
||||
// if (type != 'direct' || !events || Array.isArray(events) || !events.key) return
|
||||
stateSyncWatch(storeName, state) // 需要同步
|
||||
}
|
||||
})
|
||||
|
||||
|
@ -38,7 +40,7 @@ export function shareStorePlugin({store}) {
|
|||
|
||||
// 同步数据-发送给主线程-单独
|
||||
function stateSync(storeName, key, value, state) {
|
||||
console.log('state-change', storeName, key, value)
|
||||
// console.log('state-change', storeName, key, value)
|
||||
try {
|
||||
const { data, keystr } = filterByKey(state, key, value)
|
||||
const jsonStr = JSON.stringify(data) // 从新组装-json数据
|
||||
|
@ -51,6 +53,28 @@ function stateSync(storeName, key, value, state) {
|
|||
console.log('state-change-error', error)
|
||||
}
|
||||
}
|
||||
// 同步数据-发送给主线程-单独($subscribe-监听使用)
|
||||
function stateSyncWatch(storeName, newState) {
|
||||
const oldState = sessionStore.store // 旧数据
|
||||
const diffData = findDifferences(oldState, newState)
|
||||
// console.log('state-change-diffData', diffData)
|
||||
try {
|
||||
for(const key in diffData) {
|
||||
const value = diffData[key]
|
||||
const newValue = {} // 重新组装pinia需要的数据 {a:{b:1}} 这种
|
||||
const keyArr = key.split('.') || []
|
||||
keyArr.reduce((o,c,i)=>{o[c] = i === keyArr.length-1 ? value : {};return o[c]}, newValue)
|
||||
const jsonStr = JSON.stringify(newValue) // 从新组装-json数据
|
||||
// // 更新本地数据-session
|
||||
sessionStore.set(key, value)
|
||||
// // 通知主线程更新
|
||||
ipcRenderer?.invoke('pinia-state-change', storeName, jsonStr)
|
||||
// console.log('======',key, value, jsonStr )
|
||||
}
|
||||
} catch (error) {
|
||||
console.log('state-change-error', error)
|
||||
}
|
||||
}
|
||||
|
||||
// 同步数据-发送给主线程-全量更新
|
||||
function stateSyncAll(store) {
|
||||
|
@ -141,5 +165,43 @@ const filterByKey = (obj, key, value) => {
|
|||
const getObjValue = (obj, key) => {
|
||||
|
||||
}
|
||||
|
||||
// 找出两个对象之间的差异
|
||||
const findDifferences = (obj1, obj2) => {
|
||||
const differences = {};
|
||||
function compareObjects(o1, o2, path = '') {
|
||||
for (const key in o1) {
|
||||
if (o1.hasOwnProperty(key)) {
|
||||
const newPath = path ? `${path}.${key}` : key;
|
||||
if (o2.hasOwnProperty(key)) {
|
||||
const v1 = toJsonStr(o1[key])
|
||||
const v2 = toJsonStr(o2[key])
|
||||
if (typeof o1[key] === 'object' && typeof o2[key] === 'object' && !Array.isArray(o1[key])) {
|
||||
compareObjects(o1[key], o2[key], newPath);
|
||||
} else if (v1 !== v2) {
|
||||
differences[newPath] = o2[key];
|
||||
}
|
||||
} else {
|
||||
differences[newPath] = o2[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (const key in o2) {
|
||||
if (o2.hasOwnProperty(key) && !o1.hasOwnProperty(key)) {
|
||||
const newPath = path ? `${path}.${key}` : key;
|
||||
differences[newPath] = o2[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
compareObjects(objClone(obj1), objClone(obj2));
|
||||
// 特殊处理
|
||||
|
||||
|
||||
return differences;
|
||||
}
|
||||
// 对象克隆
|
||||
const objClone = (obj) => JSON.parse(JSON.stringify(obj))
|
||||
// 转换为json
|
||||
const toJsonStr = (obj) => JSON.stringify(obj)
|
||||
|
|
|
@ -6,6 +6,8 @@ import { sessionStore } from '@/utils/tool'
|
|||
|
||||
// 默认数据
|
||||
const defData = sessionStore.store || {}
|
||||
// 延时
|
||||
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))
|
||||
|
||||
export const useToolState = defineStore('tool', {
|
||||
state: () => ({
|
||||
|
@ -20,5 +22,12 @@ export const useToolState = defineStore('tool', {
|
|||
...defData // 默认数据-覆盖上面的配置(不要删除, 会导致新窗口-获取状态失败)
|
||||
}),
|
||||
actions: {
|
||||
async resetDef() { // 重置数据-下课
|
||||
this.model = 'select' // 悬浮球-当前模式
|
||||
await sleep(20) // 休眠20ms
|
||||
this.showBoardAll = false // 全屏画板-是否显示
|
||||
await sleep(20) // 休眠20ms
|
||||
this.isToolWin = false // 工具窗口是否打开
|
||||
}
|
||||
}
|
||||
})
|
||||
|
|
|
@ -186,6 +186,7 @@ export function toolWindow({url, isConsole, isWeb=true, option={}}) {
|
|||
sandbox: false,
|
||||
nodeIntegration: true, // nodeApi调用
|
||||
contextIsolation: false, // 沙箱取消
|
||||
devTools: true
|
||||
},
|
||||
...option
|
||||
}
|
||||
|
|
|
@ -145,7 +145,7 @@ import { parseCataByNode, creatPPT, asyncLocalFile } from '@/utils/talkFile'
|
|||
import FileOperBatch from '@/views/prepare/container/file-oper-batch.vue'
|
||||
import SetHomework from '@/components/set-homework/index.vue'
|
||||
import outLink from '@/utils/linkConfig'
|
||||
import { createWindow, ipcMsgSend } from '@/utils/tool'
|
||||
import { createWindow, ipcMsgSend, sessionStore } from '@/utils/tool'
|
||||
import { cloneDeep } from 'lodash'
|
||||
import { delClasswork } from '@/api/teaching/classwork'
|
||||
import { getSelfReserv, startClass } from '@/api/classManage'
|
||||
|
@ -220,6 +220,8 @@ export default {
|
|||
this.initReserv()
|
||||
},
|
||||
mounted() {
|
||||
window.test = sessionStore
|
||||
window.test1 = toolStore
|
||||
this.$watch(
|
||||
() => toolStore.isToolWin,
|
||||
(newD, oldD) => {
|
||||
|
|
|
@ -219,7 +219,8 @@ const sideChange = async o => {
|
|||
// 延迟2秒后关闭窗口,如果马上解散群,会导致群组不存在
|
||||
setTimeout(async() => {
|
||||
// elMsg.close()
|
||||
toolStore.isToolWin = false
|
||||
// toolStore.isToolWin = false
|
||||
toolStore.resetDef() // 重置状态
|
||||
await imChatRef.value?.deleteGroup() // 解散群
|
||||
await imChatRef.value?.logout() // 退出im
|
||||
await classManageApi.endClass(route.query.reservId)
|
||||
|
|
Loading…
Reference in New Issue