zdg #167

Merged
zhengdegang merged 4 commits from zdg into main 2024-09-02 22:23:09 +08:00
5 changed files with 55 additions and 19 deletions
Showing only changes of commit ea9af1440e - Show all commits

View File

@ -13,7 +13,7 @@ import remote from '@electron/remote/main'
// 第二步: 初始化remote // 第二步: 初始化remote
remote.initialize() remote.initialize()
// 日志配置-初始化(日志直接绑定到console上) // 日志配置-初始化(日志直接绑定到console上)
Logger.initialize() if(!is.dev) Logger.initialize()
// 持久化数据-初始化 // 持久化数据-初始化
Store.initialize() Store.initialize()

View File

@ -3,13 +3,14 @@
*/ */
const isNode = typeof require !== 'undefined' // 是否支持node函数 const isNode = typeof require !== 'undefined' // 是否支持node函数
const { ipcRenderer } = isNode?require('electron'):{} // app使用 const { ipcRenderer } = isNode?require('electron'):{} // app使用
import { sessionStore } from '@/utils/tool'
// const Remote = isNode?require('@electron/remote'):{} // 远程模块 // const Remote = isNode?require('@electron/remote'):{} // 远程模块
export function shareStorePlugin({store}) { export function shareStorePlugin({store}) {
store.$subscribe((mutation, state) => { // 自动同步 store.$subscribe((mutation, state) => { // 自动同步
// mutation 变量包含了变化前后的状态 // mutation 变量包含了变化前后的状态
// mutation.events: key newValue target oldValue oldTarget // mutation.events: key newValue target oldValue oldTarget
// state 是变化后的状态 // state 是变化后的状态
// console.log('store.$subscribe', mutation) // console.log('store.$subscribe', mutation, state, store)
// 在存储变化的时候执行 // 在存储变化的时候执行
// const storeName = store.$id // const storeName = store.$id
const storeName = mutation.storeId const storeName = mutation.storeId
@ -19,13 +20,15 @@ export function shareStorePlugin({store}) {
const { storeId: storeName, payload, events, type } = mutation // direct const { storeId: storeName, payload, events, type } = mutation // direct
// if (!Object.keys(payload).length) return // if (!Object.keys(payload).length) return
if (type != 'direct' || !events || Array.isArray(events) || !events.key) return if (type != 'direct' || !events || Array.isArray(events) || !events.key) return
stateSync(storeName, events.key, events.newValue) // 需要同步 stateSync(storeName, events.key, events.newValue, state) // 需要同步
} }
}) })
// 暴露方法-手动同步 // 暴露方法-手动同步
store.stateSync = (storeName, key, value) => { store.stateSync = (storeName, key, value) => {
if (!storeName && !!key && !!value) stateSync(storeName, key, value) const state = store.$state
else stateSyncAll(store) if (!storeName && !!key && !!value) stateSync(storeName, key, value, state)
else stateSyncAll(store, state)
} }
// 暴露方法-发送当前状态-新窗口 // 暴露方法-发送当前状态-新窗口
store.stateSyncInit = wid => stateSyncInit(wid, store) store.stateSyncInit = wid => stateSyncInit(wid, store)
@ -34,14 +37,16 @@ export function shareStorePlugin({store}) {
} }
// 同步数据-发送给主线程-单独 // 同步数据-发送给主线程-单独
function stateSync(storeName, key, value) { function stateSync(storeName, key, value, state) {
// console.log('state-change', storeName, key, value) // console.log('state-change', storeName, key, value)
try { try {
let jsonStr = '' const { data, keystr } = filterByKey(state, key, value)
if (typeof key === 'string') jsonStr = JSON.stringify({[key]:value}) const jsonStr = JSON.stringify(data) // 从新组装-json数据
else if (typeof value === 'object') jsonStr = JSON.stringify(key) // 更新本地数据-session
sessionStore.set(keystr, value)
// 通知主线程更新 // 通知主线程更新
ipcRenderer?.invoke('pinia-state-change', storeName, jsonStr) ipcRenderer?.invoke('pinia-state-change', storeName, jsonStr)
// console.log('======',keystr, data )
} catch (error) { } catch (error) {
console.log('state-change-error', error) console.log('state-change-error', error)
} }
@ -95,3 +100,27 @@ const circularSafeStringify = (obj) => {
}); });
} }
// 过滤对象
const filterByKey = (obj, key, value) => {
let res = { data:{}, keystr:'' }
for (let k in obj) {
if (obj.hasOwnProperty(k)) {
const isEqual = JSON.stringify(obj[k]) === JSON.stringify(value) // 值是否相同
if (k === key && isEqual) {
// 如果匹配,则添加到新对象中
res.data[k] = obj[k];
res.keystr = k;
} else {
if (obj[k] !== null && typeof obj[k] === 'object') {
// 如果是对象,则递归处理
const {data, keystr} = filterByKey(obj[k], key, value)
res.data[k] = data
res.keystr = keystr ? `${k}.${keystr}`: key
}
}
}
}
return res;
}
// 对象克隆
const objClone = (obj) => JSON.parse(JSON.stringify(obj))

View File

@ -2,6 +2,10 @@
* 工具类-窗口-状态管理 * 工具类-窗口-状态管理
*/ */
import { defineStore } from 'pinia' import { defineStore } from 'pinia'
import { sessionStore } from '@/utils/tool'
// 默认数据
const defData = sessionStore.store || {}
export const useToolState = defineStore('tool', { export const useToolState = defineStore('tool', {
state: () => ({ state: () => ({
@ -12,7 +16,8 @@ export const useToolState = defineStore('tool', {
curSubjectNode: { curSubjectNode: {
data: {}, // 当前教材节点 (包含当前教材 单元) data: {}, // 当前教材节点 (包含当前教材 单元)
querySearch: {} // 查询资源所需参数 querySearch: {} // 查询资源所需参数
} },
...defData // 默认数据-覆盖上面的配置(不要删除, 会导致新窗口-获取状态失败)
}), }),
actions: { actions: {
} }

View File

@ -11,13 +11,13 @@ const path = isNode?require('path'):{}
const Remote = isNode?require('@electron/remote'):{} const Remote = isNode?require('@electron/remote'):{}
const { ipcRenderer } = isNode?require('electron'):window.electron || {} const { ipcRenderer } = isNode?require('electron'):window.electron || {}
const API = isNode?window.api:{} // preload-api const API = isNode?window.api:{} // preload-api
import { useToolState } from '@/store/modules/tool' // 获取store状态 // import { useToolState } from '@/store/modules/tool' // 获取store状态
const Store = isNode?require('electron-store'):null // 持久化存储 const Store = isNode?require('electron-store'):null // 持久化存储
// 常用变量 // 常用变量
const BaseUrl = isNode?process.env['ELECTRON_RENDERER_URL']+'/#':'' const BaseUrl = isNode?process.env['ELECTRON_RENDERER_URL']+'/#':''
const isDev = isNode?process.env.NODE_ENV !== 'production':'' const isDev = isNode?process.env.NODE_ENV !== 'production':''
const toolState = useToolState() // 获取store状态 // const toolState = useToolState() // 获取store状态
// 暴露Remote中的属性 // 暴露Remote中的属性
export const ipcMain = Remote?.ipcMain || {} export const ipcMain = Remote?.ipcMain || {}
@ -137,7 +137,7 @@ export const createWindow = async (type, data) => {
winPdf.restore(); winPdf.restore();
} else{ } else{
winPdf.focus(); winPdf.focus();
toolState.isPdfWin=true // toolState.isPdfWin=true
} }
return return
@ -204,9 +204,9 @@ export function toolWindow({url, isConsole, isWeb=true, option={}}) {
mainWin.once('closed', () => { win.destroy()}) mainWin.once('closed', () => { win.destroy()})
// 内部监听器 // 内部监听器
win.webContents.on('did-finish-load', () => { win.webContents.on('did-finish-load', () => {
setTimeout(() => { // setTimeout(() => {
toolState.stateSyncInit(win.id) // 同步状态 // toolState.stateSyncInit(win.id) // 同步状态
}, 200); // }, 200);
}) })
// 内部监听器-是否打印 // 内部监听器-是否打印
if (!!isConsole) { if (!!isConsole) {

View File

@ -144,12 +144,13 @@ import { parseCataByNode, creatPPT, asyncLocalFile } from '@/utils/talkFile'
import FileOperBatch from '@/views/prepare/container/file-oper-batch.vue' import FileOperBatch from '@/views/prepare/container/file-oper-batch.vue'
import SetHomework from '@/components/set-homework/index.vue' import SetHomework from '@/components/set-homework/index.vue'
import outLink from '@/utils/linkConfig' import outLink from '@/utils/linkConfig'
import { createWindow } from '@/utils/tool' import { createWindow, sessionStore } from '@/utils/tool'
import { cloneDeep } from 'lodash' import { cloneDeep } from 'lodash'
import { delClasswork } from '@/api/teaching/classwork' import { delClasswork } from '@/api/teaching/classwork'
import { getSelfReserv, startClass } from '@/api/classManage' import { getSelfReserv, startClass } from '@/api/classManage'
import { useGetHomework } from '@/hooks/useGetHomework' import { useGetHomework } from '@/hooks/useGetHomework'
const toolStore = useToolState() const toolStore = useToolState()
const fs = require('fs') const fs = require('fs')
const { ipcRenderer } = window.electron || {} const { ipcRenderer } = window.electron || {}
@ -423,7 +424,8 @@ export default {
this.uploadData.levelThirdId = cata[2] this.uploadData.levelThirdId = cata[2]
this.uploadData.textbookId = data.textBook.curBookId this.uploadData.textbookId = data.textBook.curBookId
toolStore.curSubjectNode.data = data toolStore.curSubjectNode.data = data
toolStore.curSubjectNode.querySearch = this.uploadData //
this.$nextTick(() =>{ toolStore.curSubjectNode.querySearch = this.uploadData })
this.initHomeWork() this.initHomeWork()
await this.asyncAllFile() await this.asyncAllFile()
}, },