Browse Source

perf: 重构模块

pull/180/head
xiaojunnuo 4 years ago
parent
commit
5c723e99a5
  1. 2
      packages/core/src/shell/scripts/set-system-proxy/index.js
  2. 38
      packages/gui/src/background.js
  3. 35
      packages/gui/src/bridge/api/backend.js
  4. 19
      packages/gui/src/bridge/backend.js
  5. 0
      packages/gui/src/bridge/error/front.js
  6. 21
      packages/gui/src/bridge/file-selector/backend.js
  7. 23
      packages/gui/src/bridge/file-selector/front.js
  8. 21
      packages/gui/src/bridge/front.js
  9. 55
      packages/gui/src/bridge/tongji/backend.js
  10. 62
      packages/gui/src/bridge/tongji/front.js
  11. 23
      packages/gui/src/bridge/update/backend.js
  12. 0
      packages/gui/src/bridge/update/front.js
  13. 10
      packages/gui/src/main.js
  14. 103
      packages/gui/src/tongji.js
  15. 6
      packages/gui/src/view/index.js
  16. 9
      packages/gui/src/view/modules/index.js
  17. 16
      packages/gui/src/view/pages/server.vue

2
packages/core/src/shell/scripts/set-system-proxy/index.js

@ -37,7 +37,7 @@ async function _winUnsetProxy (exec) {
// eslint-disable-next-line no-constant-condition
if (true) {
const proxyPath = getProxyExePath()
await execFile(proxyPath, ['off'])
await execFile(proxyPath, ['set', '1'])
return
}
const regKey = new Registry({

38
packages/gui/src/background.js

@ -1,11 +1,10 @@
'use strict'
/* global __static */
import path from 'path'
import { app, protocol, BrowserWindow, Menu, Tray, ipcMain } from 'electron'
import { app, protocol, BrowserWindow, Menu, Tray, ipcMain, dialog } from 'electron'
import { createProtocol } from 'vue-cli-plugin-electron-builder/lib'
import bridge from './bridge/index'
import updateHandle from './bridge/update-handle'
import { ebtMain } from './tongji'
import backend from './bridge/backend'
import DevSidecar from '@docmirror/dev-sidecar'
import log from './utils/util.log'
// eslint-disable-next-line no-unused-vars
const isMac = process.platform === 'darwin'
@ -113,7 +112,7 @@ function createWindow () {
}
async function beforeQuit () {
return bridge.devSidecar.api.shutdown()
return DevSidecar.api.shutdown()
}
async function quit (app, callback) {
if (tray) {
@ -136,12 +135,6 @@ if (!isFirstInstance) {
} else {
app.on('before-quit', async (event) => {
log.info('before-quit')
// event.preventDefault()
// // if (tray) {
// // tray.displayBalloon({ title: '正在关闭,请稍候...', content: '正在关闭中,请稍候。。。' })
// // }
// await bridge.devSidecar.api.shutdown()
// app.exit()
})
app.on('second-instance', (event, commandLine, workingDirectory) => {
log.info('new app started', commandLine)
@ -180,24 +173,13 @@ if (!isFirstInstance) {
// log.error('Vue Devtools failed to install:', e.toString())
// }
}
createWindow()
bridge.init(win)
let updateUrl = 'https://dev-sidecar.docmirror.cn/update/'
if (process.env.NODE_ENV === 'development') {
Object.defineProperty(app, 'isPackaged', {
get () {
return true
}
})
updateUrl = 'https://dev-sidecar.docmirror.cn/update/'
// updateUrl = 'http://localhost/dev-sidecar/'
try {
createWindow()
const context = { win, app, beforeQuit, ipcMain, dialog,log }
backend.install(context) // 模块安装
} catch (err) {
log.info('err', err)
}
// 自动更新
updateHandle(app, win, beforeQuit, updateUrl)
// 百度分析
ebtMain(ipcMain, isDevelopment)
try {
// 最小化到托盘

35
packages/gui/src/bridge/index.js → packages/gui/src/bridge/api/backend.js

@ -1,13 +1,13 @@
import lodash from 'lodash'
import DevSidecar from '@docmirror/dev-sidecar'
import { ipcMain, Menu } from 'electron'
import { ipcMain } from 'electron'
import fs from 'fs'
import JSON5 from 'json5'
import path from 'path'
const pk = require('../../package.json')
const pk = require('../../../package.json')
const mitmproxyPath = path.join(__dirname, 'mitmproxy.js')
process.env.DS_SYSPROXY_PATH = path.join(__dirname, '../extra/sysproxy.exe')
const log = require('../utils/util.log')
const log = require('../../utils/util.log')
const getDefaultConfigBasePath = function () {
return DevSidecar.api.config.get().server.setting.userBasePath
}
@ -171,25 +171,29 @@ function doMerge (defObj, newObj) {
return newObj2
}
function invoke (api, param) {
let target = lodash.get(localApi, api)
if (target == null) {
target = lodash.get(DevSidecar.api, api)
}
if (target == null) {
log.info('找不到此接口方法:', api)
}
const ret = target(param)
// log.info('api:', api, 'ret:', ret)
return ret
}
export default {
init (win) {
install ({ win }) {
// 接收view的方法调用
ipcMain.handle('apiInvoke', async (event, args) => {
const api = args[0]
let target = lodash.get(localApi, api)
if (target == null) {
target = lodash.get(DevSidecar.api, api)
}
if (target == null) {
log.info('找不到此接口方法:', api)
}
let param
if (args.length >= 2) {
param = args[1]
}
const ret = target(param)
// log.info('api:', api, 'ret:', ret)
return ret
return invoke(api, param)
})
// 注册从core里来的事件,并转发给view
DevSidecar.api.event.register('status', (event) => {
@ -206,5 +210,6 @@ export default {
// 启动所有
localApi.startup()
},
devSidecar: DevSidecar
devSidecar: DevSidecar,
invoke
}

19
packages/gui/src/bridge/backend.js

@ -0,0 +1,19 @@
import api from './api/backend'
import tongji from './tongji/backend'
import update from './update/backend'
import fileSelector from './file-selector/backend'
const modules = {
api, // 核心接口模块
fileSelector, // 文件选择模块
tongji, // 统计模块
update // 自动更新
}
export default {
install (context) {
for (const module in modules) {
modules[module].install(context)
}
},
...modules
}

0
packages/gui/src/view/modules/error/index.js → packages/gui/src/bridge/error/front.js

21
packages/gui/src/bridge/file-selector/backend.js

@ -0,0 +1,21 @@
export default {
install (context) {
const { ipcMain, dialog, log } = context
ipcMain.on('file-selector', function (event, message) {
if (message.key === 'open') {
dialog.showOpenDialog({
properties: ['openFile'],
...message
}).then(result => {
if (result.canceled) {
event.sender.send('file-selector', { key: 'canceled' })
} else {
event.sender.send('file-selector', { key: 'selected', value: result.filePaths })
}
}).catch(err => {
log.error('选择文件失败', err)
})
}
})
}
}

23
packages/gui/src/bridge/file-selector/front.js

@ -0,0 +1,23 @@
function install (app, api) {
api.fileSelector = {
open (value, options) {
return new Promise((resolve, reject) => {
api.ipc.send('file-selector', { key: 'open', value: value, ...options })
api.ipc.on('file-selector', (event, message) => {
console.log('selector', message)
if (message.key === 'selected') {
resolve(message.value)
} else {
reject(new Error('没有选择文件'))
}
api.ipc.on('file-selector', () => {})
})
})
}
}
}
export default {
install
}

21
packages/gui/src/bridge/front.js

@ -0,0 +1,21 @@
// import api from './api/front'
import error from './error/front'
import tongji from './tongji/front'
import update from './update/front'
import fileSelector from './file-selector/front'
const modules = {
// api, // 核心接口模块
error,
fileSelector, // 文件选择模块
tongji, // 统计模块
update // 自动更新
}
export default {
install (app, api, router) {
for (const module in modules) {
modules[module].install(app, api, router)
}
},
...modules
}

55
packages/gui/src/bridge/tongji/backend.js

@ -0,0 +1,55 @@
/**
* first step
* @param {*} ipcMain
*/
const ebtMain = (ipcMain) => {
const isDevelopment = process.env.NODE_ENV !== 'production'
const request = require('request')
/* istanbul ignore else */
if (!(ipcMain && ipcMain.on)) {
throw new TypeError('require ipcMain')
}
// step 2
ipcMain.on('electron-baidu-tongji-message', (event, arg) => {
// electron 生产模式下是直接请求文件系统,没有 http 地址
// 前台拿不到 hm.js 的内容
request({
url: `https://hm.baidu.com/hm.js?${arg}`,
method: 'GET',
headers: {
Referer: 'https://hm.baidu.com/'
}
},
(err, response, body) => {
if (err) {
console.error('百度统计请求出错', err)
return
}
const rource = '(h.c.b.su=h.c.b.u||document.location.href),h.c.b.u=f.protocol+"//"+document.location.host+'
/* istanbul ignore else */
if (body && body.indexOf(rource) >= 0) {
// step 3
let text = body
/* istanbul ignore else */
if (!isDevelopment) {
// 百度统计可能改规则了,不统计 file:// 开始的请求
// 这里强制替换为 https
const target = '(h.c.b.su=h.c.b.u||"https://"+c.dm[0]+a[1]),h.c.b.u="https://"+c.dm[0]+'
const target2 = '"https://"+c.dm[0]+window.location.pathname+window.location.hash'
text = body.replace(rource, target).replace(/window.location.href/g, target2)
}
console.log('baidu tonji: ret')
event.sender.send('electron-baidu-tongji-reply', { text, isDevelopment })
}
})
})
}
export default {
install (context) {
ebtMain(context.ipcMain)
}
}

62
packages/gui/src/bridge/tongji/front.js

@ -0,0 +1,62 @@
/**
* second step
* @param {*} ipcRenderer
* @param {*} siteId
* @param {*} router
*/
const ebtRenderer = (ipcRenderer, siteId, router) => {
/* istanbul ignore else */
if (!(ipcRenderer && ipcRenderer.on && ipcRenderer.send)) {
throw new TypeError('require ipcRenderer')
}
/* istanbul ignore else */
if (!(siteId && typeof siteId === 'string')) {
throw new TypeError('require siteId')
}
// step 4
ipcRenderer.on('electron-baidu-tongji-reply', (_, { text, isDevelopment }) => {
console.log('electron-baidu-tongji-reply')
/* istanbul ignore else */
if (isDevelopment) { document.body.classList.add('electron-baidu-tongji_dev') }
window._hmt = window._hmt || []
const hm = document.createElement('script')
hm.text = text
const head = document.getElementsByTagName('head')[0]
head.appendChild(hm)
// Vue单页应用时,监听router的每次变化
// 把虚拟的url地址赋给百度统计的API接口
/* istanbul ignore else */
if (router && router.beforeEach) {
router.beforeEach((to, _, next) => {
/* istanbul ignore else */
if (to.path) {
window._hmt.push(['_trackPageview', '/#' + to.fullPath])
console.log('baidu trace', to.fullPath)
}
next()
})
}
})
// step 1
ipcRenderer.send('electron-baidu-tongji-message', siteId)
}
export default {
install (app, api, router) {
const BAIDU_SITE_ID = 'f2d170ce560aef0005b689f28697f852'
// 百度统计
const { ipcRenderer } = require('electron')
ebtRenderer(ipcRenderer, BAIDU_SITE_ID, router)
},
ebtRenderer
}

23
packages/gui/src/bridge/update-handle.js → packages/gui/src/bridge/update/backend.js

@ -1,6 +1,5 @@
import { ipcMain, dialog } from 'electron'
import { ipcMain } from 'electron'
import { autoUpdater } from 'electron-updater'
import log from '../utils/util.log'
import path from 'path'
// win是所有窗口的引用
// const path = require('path') // 引入path模块
@ -8,7 +7,7 @@ import path from 'path'
// eslint-disable-next-line no-unused-vars
const isMac = process.platform === 'darwin'
// 检测更新,在你想要检查更新的时候执行,renderer事件触发后的操作自行编写
function updateHandle (app, win, beforeQuit, updateUrl) {
function updateHandle (app, win, beforeQuit, updateUrl,log) {
// // 更新前,删除本地安装包 ↓
// const updaterCacheDirName = 'dev-sidecar-updater'
// const updatePendingPath = path.join(autoUpdater.app.baseCachePath, updaterCacheDirName, 'pending')
@ -95,4 +94,20 @@ function updateHandle (app, win, beforeQuit, updateUrl) {
log.info('auto update inited')
return autoUpdater
}
export default updateHandle
export default {
install (context) {
const { app, win, beforeQuit,log } = context
let updateUrl = 'https://dev-sidecar.docmirror.cn/update/'
if (process.env.NODE_ENV === 'development') {
Object.defineProperty(app, 'isPackaged', {
get () {
return true
}
})
updateUrl = 'https://dev-sidecar.docmirror.cn/update/'
// updateUrl = 'http://localhost/dev-sidecar/'
}
updateHandle(app, win, beforeQuit, updateUrl,log)
}
}

0
packages/gui/src/view/modules/update/index.js → packages/gui/src/bridge/update/front.js

10
packages/gui/src/main.js

@ -7,9 +7,6 @@ import VueRouter from 'vue-router'
import routes from './view/router'
import DsContainer from './view/components/container'
import './view/style/index.scss'
import { ipcRenderer } from 'electron'
const tongji = require('./tongji')
Vue.config.productionTip = false
Vue.use(antd)
@ -28,12 +25,7 @@ view.initApi().then(async (api) => {
router,
render: h => h(App)
}).$mount('#app')
const BAIDU_SITE_ID = 'f2d170ce560aef0005b689f28697f852'
// 百度统计
tongji.ebtRenderer(ipcRenderer, BAIDU_SITE_ID, router)
view.initModules(app)
view.initModules(app, router)
})
// fix vue-router NavigationDuplicated

103
packages/gui/src/tongji.js

@ -1,103 +0,0 @@
const request = require('request')
/**
* first step
* @param {*} ipcMain
*/
const ebtMain = (ipcMain, isDevelopment) => {
/* istanbul ignore else */
if (!(ipcMain && ipcMain.on)) {
throw new TypeError('require ipcMain')
}
// step 2
ipcMain.on('electron-baidu-tongji-message', (event, arg) => {
// electron 生产模式下是直接请求文件系统,没有 http 地址
// 前台拿不到 hm.js 的内容
request({
url: `https://hm.baidu.com/hm.js?${arg}`,
method: 'GET',
headers: {
Referer: 'https://hm.baidu.com/'
}
},
(err, response, body) => {
if (err) {
console.error('百度统计请求出错', err)
return
}
const rource = '(h.c.b.su=h.c.b.u||document.location.href),h.c.b.u=f.protocol+"//"+document.location.host+'
/* istanbul ignore else */
if (body && body.indexOf(rource) >= 0) {
// step 3
let text = body
/* istanbul ignore else */
if (!isDevelopment) {
// 百度统计可能改规则了,不统计 file:// 开始的请求
// 这里强制替换为 https
const target = '(h.c.b.su=h.c.b.u||"https://"+c.dm[0]+a[1]),h.c.b.u="https://"+c.dm[0]+'
const target2 = '"https://"+c.dm[0]+window.location.pathname+window.location.hash'
text = body.replace(rource, target).replace(/window.location.href/g, target2)
}
console.log('baidu tonji: ret')
event.sender.send('electron-baidu-tongji-reply', { text, isDevelopment })
}
})
})
}
/**
* second step
* @param {*} ipcRenderer
* @param {*} siteId
* @param {*} router
*/
const ebtRenderer = (ipcRenderer, siteId, router) => {
/* istanbul ignore else */
if (!(ipcRenderer && ipcRenderer.on && ipcRenderer.send)) {
throw new TypeError('require ipcRenderer')
}
/* istanbul ignore else */
if (!(siteId && typeof siteId === 'string')) {
throw new TypeError('require siteId')
}
// step 4
ipcRenderer.on('electron-baidu-tongji-reply', (_, { text, isDevelopment }) => {
console.log('electron-baidu-tongji-reply')
/* istanbul ignore else */
if (isDevelopment) { document.body.classList.add('electron-baidu-tongji_dev') }
window._hmt = window._hmt || []
const hm = document.createElement('script')
hm.text = text
const head = document.getElementsByTagName('head')[0]
head.appendChild(hm)
// Vue单页应用时,监听router的每次变化
// 把虚拟的url地址赋给百度统计的API接口
/* istanbul ignore else */
if (router && router.beforeEach) {
router.beforeEach((to, _, next) => {
/* istanbul ignore else */
if (to.path) {
window._hmt.push(['_trackPageview', '/#' + to.fullPath])
console.log('baidu trace', to.fullPath)
}
next()
})
}
})
// step 1
ipcRenderer.send('electron-baidu-tongji-message', siteId)
}
module.exports = { ebtMain, ebtRenderer }

6
packages/gui/src/view/index.js

@ -1,5 +1,5 @@
import api, { apiInit } from './api'
import modules from './modules'
import modules from '../bridge/front'
import status from './status'
export default {
initApi: apiInit,
@ -9,7 +9,7 @@ export default {
Vue.prototype.$global = { setting }
await status.install(api)
},
initModules (app) {
modules.install(app, api)
initModules (app, router) {
modules.install(app, api, router)
}
}

9
packages/gui/src/view/modules/index.js

@ -1,9 +0,0 @@
import update from './update'
import error from './error'
export default {
install (app, api) {
error.install(app, api)
update.install(app, api)
}
}

16
packages/gui/src/view/pages/server.vue

@ -38,8 +38,8 @@
<div>开启此项之后被代理应用关闭SSL校验也问题不大了</div>
</a-form-item>
<a-form-item label="根证书:" :label-col="labelCol" :wrapper-col="wrapperCol">
<a-input addon-before="Cert" v-model="config.server.setting.rootCaFile.certPath" ></a-input>
<a-input addon-before="Key" v-model="config.server.setting.rootCaFile.keyPath" ></a-input>
<a-input-search addon-before="Cert" enter-button="选择" @search="onCrtSelect" v-model="config.server.setting.rootCaFile.certPath" />
<a-input-search addon-before="Key" enter-button="选择" @search="onKeySelect" v-model="config.server.setting.rootCaFile.keyPath" />
</a-form-item>
<a-form-item label="启用脚本" :label-col="labelCol" :wrapper-col="wrapperCol">
<a-tooltip title="关闭后,github的clone加速链接复制也将关闭">
@ -112,6 +112,18 @@ export default {
mounted () {
},
methods: {
async onCrtSelect () {
const value = await this.$api.fileSelector.open()
if (value != null && value.length > 0) {
this.config.server.setting.rootCaFile.certPath = value[0]
}
},
async onKeySelect () {
const value = await this.$api.fileSelector.open()
if (value != null && value.length > 0) {
this.config.server.setting.rootCaFile.keyPath = value[0]
}
},
onJsonChange (json) {
},
ready () {

Loading…
Cancel
Save