UNPKG

logic-helper

Version:

This template should help get you started developing with Vue 3 in Vite. The template uses Vue 3 `<script setup>` SFCs, check out the [script setup docs](https://v3.vuejs.org/api/sfc-script-setup.html#sfc-script-setup) to learn more.

117 lines (114 loc) 3.29 kB
import { getWsEventBus } from '@/utils/util.js' import { markRaw } from 'vue'; import deepEqual from 'deep-equal'; import { diff } from 'deep-object-diff'; import { ElMessage, ElMessageBox } from 'element-plus' /** * 用于同步两端数据 */ export class Stack { constructor() { this.request = getWsEventBus('topo/src/ws/index'); this.initialBar = new Promise((resolve, reject) => { this.request.on('open', (event) => { this.init(); resolve() }); this.request.on('close', (event) => { console.log('断开'); stock.post('init/logic'); resolve() }); }) } init() { return stock.post('init/logic').then(conf=>{ this.logicConfig = conf; console.log('连接'); }) } async post(action, data) { await this.initialBar; let res = await this.request.send({ action, data }, { to: 'topo-server' }); return res.response } async get(action, data) { await this.initialBar; return this.post(action, data); } } const stock = new Stack(); const checkDataCache = async () => { let server = await stock.get('get/logic'); let local = localStorage.getItem(stock.logicConfig.name); try{ local = JSON.parse(local) }catch(e){ local = '' } if(local&&!deepEqual(local, server)){ console.error('本地缓存数据与服务器数据不一致,使用本地数据?') console.error('本地缓存:服务端数据:', diff(local,server)); console.error('服务器:本地数据:', diff(server,local)); server = await new Promise((resolve, reject) => { ElMessageBox.confirm( '本地缓存数据与服务器数据不一致,查看“控制台”对比数据', '使用哪端数据?', { confirmButtonText: '本地', cancelButtonText: '服务器', type: 'warning', } ) .then(() => { stock.post('set/logic', local).then(()=>{ resolve(local) }).catch(()=>{ reject() }) }) .catch(() => { localStorage.setItem(stock.logicConfig.name, JSON.stringify(server)); resolve(server) }) }) } return server; } class Buffer { async setLogic(value) { localStorage.setItem(stock.logicConfig.name, JSON.stringify(value)); return await stock.post('set/logic', value) } async getLogic() { try { return await checkDataCache(); } catch (error) { console.error(error, '::::$$$$$') } } getLogicConfig() { return stock.logicConfig; } getRootDir() { return stock.get('get/project'); } writeFile(obj) { return stock.post('write/file', obj); } getRelPath(obj) { return stock.post('get/relPath', obj); } } export default markRaw(new Buffer());