From 50080a8935c38318e412278033947c2fcdabd273 Mon Sep 17 00:00:00 2001 From: shuax Date: Sun, 28 Aug 2022 15:01:18 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E9=80=9A=E9=81=93=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E4=B8=BAwebsocket?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/data.js | 88 --------------- src/components/main/main.vue | 200 +++++++++++++++++++++++------------ src/libs/api.request.js | 7 -- src/libs/axios.js | 78 -------------- src/view/hotkey.vue | 6 +- 5 files changed, 138 insertions(+), 241 deletions(-) delete mode 100644 src/api/data.js delete mode 100644 src/libs/api.request.js delete mode 100644 src/libs/axios.js diff --git a/src/api/data.js b/src/api/data.js deleted file mode 100644 index dd2a084..0000000 --- a/src/api/data.js +++ /dev/null @@ -1,88 +0,0 @@ -import axios from '@/libs/api.request' - -export const LoadSettings = () => { - return axios.request({ - url: '/load_settings', - data: {}, - method: 'post', - headers: { - 'Content-Type': 'text/plain' - } - }) -} - -export const SaveSettings = data => { - return axios.request({ - url: '/save_settings', - data: data, - method: 'post', - headers: { - 'Content-Type': 'text/plain' - } - }) -} - -export const ResetSettings = () => { - return axios.request({ - url: '/reset_settings', - data: {}, - method: 'post', - headers: { - 'Content-Type': 'text/plain' - } - }) -} - -export const Ping = () => { - return axios.request({ - url: '/ping', - data: {}, - method: 'post', - headers: { - 'Content-Type': 'text/plain' - } - }) -} - -// export const getTableData = () => { -// return axios.request({ -// url: 'get_table_data', -// method: 'get' -// }) -// } - -// export const getDragList = () => { -// return axios.request({ -// url: 'get_drag_list', -// method: 'get' -// }) -// } - -// export const errorReq = () => { -// return axios.request({ -// url: 'error_url', -// method: 'post' -// }) -// } - -// export const saveErrorLogger = info => { -// return axios.request({ -// url: 'save_error_logger', -// data: info, -// method: 'post' -// }) -// } - -// export const uploadImg = formData => { -// return axios.request({ -// url: 'image/upload', -// data: formData -// }) -// } - -// export const getOrgData = () => { -// return axios.request({ -// url: 'get_org_data', -// method: 'get' -// }) -// } diff --git a/src/components/main/main.vue b/src/components/main/main.vue index 350d5d2..9cc4cc8 100644 --- a/src/components/main/main.vue +++ b/src/components/main/main.vue @@ -15,10 +15,10 @@
- +
- +
@@ -70,13 +70,14 @@ import { mapMutations, mapGetters } from 'vuex' // import routers from '@/router/routers' // import minLogo from '@/assets/images/logo-min.jpg' // import maxLogo from '@/assets/images/logo.jpg' -import { LoadSettings, SaveSettings, ResetSettings, Ping } from '@/api/data' +// import { LoadSettings, SaveSettings, ResetSettings, Ping } from '@/api/data' // import { js as beautify_js } from 'js-beautify' // import jsBeautify from 'js-beautify/js/lib/beautify' import beautify from 'js-beautify' // import JsonEdit from '@/view/components/json.vue' import config from '@/config' -import { setTitle } from '@/libs/util' +import { setTitle, getParams } from '@/libs/util' + import './main.less' const { version } = config export default { @@ -176,18 +177,20 @@ export default { }, reset () { this.reset_loading = true + this.$Loading.start() - ResetSettings().then(response => { - this.init = false - this.modified = false - this.setSettings(response.data) - this.$Message.success(this.$t('reset_ok')) - }).catch(error => { - this.$Message.error(error.message) - }).then(() => { - this.reset_loading = false - this.reset_warning = false - }) + this.websocket.send(JSON.stringify({ 'type': 'reset_settings' })) + // ResetSettings().then(response => { + // this.init = false + // this.modified = false + // this.setSettings(response.data) + // this.$Message.success(this.$t('reset_ok')) + // }).catch(error => { + // this.$Message.error(error.message) + // }).then(() => { + // this.reset_loading = false + // this.reset_warning = false + // }) }, refresh () { location.reload() @@ -207,37 +210,39 @@ export default { indent_with_tabs: true, eol: '\r\n' }) - SaveSettings(cfg).then(response => { - this.init = false - this.modified = false - this.setSettings(response.data) - this.$Message.success(this.$t('save_ok')) - this.$Loading.finish() - }).catch(error => { - this.$Message.error(error.message) - this.$Loading.error() - }).then(() => { - this.save_loading = false - }) - }, - heartbeat () { - // console.log('heartbeat') - Ping().then(response => { - this.timer = setTimeout(this.heartbeat, 1000) - }).catch(() => { - // clearInterval(this.timer) - // this.loading = 'Network connection is down' - // this.$Message.error({content:'Network connection is down', duration: 999999}) - - this.$Spin.show({ - render: (h) => { - return h('div', '设置连接通道错误,可能是MouseInc主程序已经退出') - } - }) - }).then(() => { - }) + this.websocket.send(JSON.stringify({ 'type': 'save_settings', 'config': cfg })) + // SaveSettings(cfg).then(response => { + // this.init = false + // this.modified = false + // this.setSettings(response.data) + // this.$Message.success(this.$t('save_ok')) + // this.$Loading.finish() + // }).catch(error => { + // this.$Message.error(error.message) + // this.$Loading.error() + // }).then(() => { + // this.save_loading = false + // }) } + // heartbeat () { + // // console.log('heartbeat') + // Ping().then(response => { + // this.timer = setTimeout(this.heartbeat, 1000) + // }).catch(() => { + // // clearInterval(this.timer) + // // this.loading = 'Network connection is down' + // // this.$Message.error({content:'Network connection is down', duration: 999999}) + + // this.$Spin.show({ + // render: (h) => { + // return h('div', '设置连接通道错误,可能是MouseInc主程序已经退出') + // } + // }) + // }).then(() => { + + // }) + // } // handleCloseTag (res, type, route) { // if (type !== 'others') { // if (type === 'all') { @@ -310,30 +315,91 @@ export default { ]) } }) - LoadSettings().then(response => { - this.$Spin.hide() - this.init = false - this.modified = false - this.setSettings(response.data) - this.timer = setTimeout(this.heartbeat, 1000) - version[0] = response.data.version - if (response.data.admin === 1) version[0] += ' A' - else version[0] += ' B' - setTitle(this.$router.currentRoute, this.$router.app) - // if (response.data.version !== '2.11.8') { - // this.$Message.error({ - // content: '您使用的MouseInc非最新版本,建议更新', - // background: true, - // duration: 86400 - // }) - // } - }).catch(error => { - // this.$Spin.hide(); - // console.log(error) - this.loading = error.message - // this.$Message.error(error.message) - }) + let port = getParams(location.search || '?')['port'] + port = port || 80 + this.websocket = new WebSocket('ws://127.0.0.1:' + port + '/ws') + this.websocket.onopen = (evt) => { + this.websocket.send(JSON.stringify({ 'type': 'load_settings' })) + } + this.websocket.onclose = (evt) => { + this.$Spin.show({ + render: (h) => { + return h('div', '设置连接通道错误,可能是MouseInc主程序已经退出') + } + }) + } + this.websocket.onmessage = (evt) => { + var message = JSON.parse(evt.data) + if (message['type'] === 'load_settings') { + this.$Spin.hide() + + this.init = false + this.modified = false + this.setSettings(message['data']) + + version[0] = message['data'].version + if (message['data'].admin === 1) version[0] += ' A' + else version[0] += ' B' + setTitle(this.$router.currentRoute, this.$router.app) + if (message['data'].version !== '2.13.0') { + this.$Message.error({ + content: '您使用的MouseInc非最新版本,建议更新', + background: true, + duration: 86400 + }) + } + } else if (message['type'] === 'save_settings') { + this.$Message.success(this.$t('save_ok')) + this.$Loading.finish() + this.save_loading = false + + this.init = false + this.modified = false + this.setSettings(message['data']) + } else if (message['type'] === 'reset_settings') { + this.$Message.success(this.$t('reset_ok')) + this.$Loading.finish() + this.reset_loading = false + this.reset_warning = false + + this.init = false + this.modified = false + this.setSettings(message['data']) + } + } + this.websocket.onerror = (evt) => { + this.$Spin.show({ + render: (h) => { + return h('div', '设置连接通道错误,可能是MouseInc主程序已经退出') + } + }) + // console.log('onError(evt)') + } + // LoadSettings().then(response => { + // this.$Spin.hide() + // this.init = false + // this.modified = false + // this.setSettings(response.data) + + // this.timer = setTimeout(this.heartbeat, 1000) + // version[0] = response.data.version + // if (response.data.admin === 1) version[0] += ' A' + // else version[0] += ' B' + // setTitle(this.$router.currentRoute, this.$router.app) + // // if (response.data.version !== '2.11.8') { + // // this.$Message.error({ + // // content: '您使用的MouseInc非最新版本,建议更新', + // // background: true, + // // duration: 86400 + // // }) + // // } + // }).catch(error => { + // // this.$Spin.hide(); + // // console.log(error) + // this.loading = error.message + // // this.$Message.error(error.message) + // }) // 如果当前打开页面不在标签栏中,跳到homeName页 // if (!this.tagNavList.find(item => item.name === this.$route.name)) { // this.$router.push({ diff --git a/src/libs/api.request.js b/src/libs/api.request.js deleted file mode 100644 index da2b507..0000000 --- a/src/libs/api.request.js +++ /dev/null @@ -1,7 +0,0 @@ -import HttpRequest from '@/libs/axios' -// import config from '@/config' -import { getParams } from '@/libs/util' -let port = getParams(location.search || '?')['port'] -port = port || 80 -const axios = new HttpRequest('http://127.0.0.1:' + port) -export default axios diff --git a/src/libs/axios.js b/src/libs/axios.js deleted file mode 100644 index 6a47523..0000000 --- a/src/libs/axios.js +++ /dev/null @@ -1,78 +0,0 @@ -import axios from 'axios' -// import store from '@/store' -// import { Spin } from 'iview' -// const addErrorLog = errorInfo => { -// const { statusText, status, request: { responseURL } } = errorInfo -// let info = { -// type: 'ajax', -// code: status, -// mes: statusText, -// url: responseURL -// } -// if (!responseURL.includes('save_error_logger')) store.dispatch('addErrorLog', info) -// } - -class HttpRequest { - constructor (baseUrl = baseURL) { - this.baseUrl = baseUrl - this.queue = {} - } - getInsideConfig () { - const config = { - baseURL: this.baseUrl, - headers: { - // - } - } - return config - } - destroy (url) { - delete this.queue[url] - if (!Object.keys(this.queue).length) { - // Spin.hide() - } - } - interceptors (instance, url) { - // 请求拦截 - instance.interceptors.request.use(config => { - // 添加全局的loading... - if (!Object.keys(this.queue).length) { - // Spin.show() // 不建议开启,因为界面不友好 - } - this.queue[url] = true - return config - }, error => { - return Promise.reject(error) - }) - // 响应拦截 - instance.interceptors.response.use(res => { - this.destroy(url) - const { data, status } = res - return { data, status } - }, error => { - this.destroy(url) - // let errorInfo = error.response - // console.log(url) - // console.log("error") - console.error(error) - // console.log(errorInfo) - // if (!errorInfo) { - // const { request: { statusText, status }, config } = JSON.parse(JSON.stringify(error)) - // errorInfo = { - // statusText, - // status, - // request: { responseURL: config.url } - // } - // } - // addErrorLog(errorInfo) - return Promise.reject(error) - }) - } - request (options) { - const instance = axios.create() - options = Object.assign(this.getInsideConfig(), options) - this.interceptors(instance, options.url) - return instance(options) - } -} -export default HttpRequest diff --git a/src/view/hotkey.vue b/src/view/hotkey.vue index de7467c..c545b75 100644 --- a/src/view/hotkey.vue +++ b/src/view/hotkey.vue @@ -1,7 +1,7 @@