From 3550f077313ecaf41f55f3920bdee283ce56e1d7 Mon Sep 17 00:00:00 2001 From: "YUN-PC5\\user" Date: Thu, 19 Oct 2023 16:28:58 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=9A=E7=9F=A5=E5=85=AC=E5=91=8A=E6=A0=B7?= =?UTF-8?q?=E5=BC=8F=E4=BC=98=E5=8C=96=EF=BC=8C=E4=B8=9A=E5=8A=A1=E5=8D=95?= =?UTF-8?q?=E5=8F=B7=E8=A7=84=E5=88=99=E9=A1=B5=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/system/baseCodeRole.ts | 32 +++ src/components/Notice/Index.vue | 153 ++++++----- src/components/Notice/noticeDialog/index.vue | 98 ++++++- src/main.ts | 2 +- src/signalr/analysis.js | 61 +++++ src/signalr/index.js | 87 +++++++ src/views/noticecenter/index.vue | 75 ++++-- src/views/system/codeRule/index.vue | 260 +++++++++++++++++++ 8 files changed, 667 insertions(+), 101 deletions(-) create mode 100644 src/api/system/baseCodeRole.ts create mode 100644 src/signalr/analysis.js create mode 100644 src/signalr/index.js create mode 100644 src/views/system/codeRule/index.vue diff --git a/src/api/system/baseCodeRole.ts b/src/api/system/baseCodeRole.ts new file mode 100644 index 0000000..0db588c --- /dev/null +++ b/src/api/system/baseCodeRole.ts @@ -0,0 +1,32 @@ +import request from '@/utils/request' + +export const addBaseCodeRule = (data: any) => { + return request({ + url: 'base/codeRule/addBaseCodeRule', + method: 'post', + data + }) +} + +export const deleteBaseCodeRule = (code: any) => { + return request({ + url: 'base/codeRule/deleteBaseCodeRule' + '/' + code, + method: 'delete' + }) +} + +export const getBaseCodeRuleList = (params: any) => { + return request({ + url: 'base/codeRule/getBaseCodeRuleList', + method: 'get', + params + }) +} + +export const updateBaseCodeRule = (data: any) => { + return request({ + url: 'base/codeRule/updateBaseCodeRule', + method: 'put', + data + }) +} diff --git a/src/components/Notice/Index.vue b/src/components/Notice/Index.vue index c0e9dc4..bbc2ba4 100644 --- a/src/components/Notice/Index.vue +++ b/src/components/Notice/Index.vue @@ -1,6 +1,6 @@ - + diff --git a/src/main.ts b/src/main.ts index 9aeba93..dbb32f8 100644 --- a/src/main.ts +++ b/src/main.ts @@ -11,7 +11,7 @@ import directive from './directive' // directive // 注册指令 import plugins from './plugins' // plugins import { downFile } from '@/utils/request' -import signalR from '@/utils/signalR' +import signalR from '@/signalr' import vueI18n from './i18n/index' import pinia from '@/store/index' diff --git a/src/signalr/analysis.js b/src/signalr/analysis.js new file mode 100644 index 0000000..ca1844d --- /dev/null +++ b/src/signalr/analysis.js @@ -0,0 +1,61 @@ +import { ElNotification, ElMessageBox } from 'element-plus' +import useSocketStore from '@/store/modules/socket' +import useUserStore from '@/store/modules/user' +import { webNotify } from '@/utils/index' + +export default { + onMessage(connection) { + connection.on(MsgType.M001, (data) => { + useSocketStore().setOnlineUserNum(data) + }) + + connection.on(MsgType.M002, (data) => {}) + // 接受后台手动推送消息 + connection.on(MsgType.M003, (title, data) => { + ElNotification({ + type: 'info', + title: title, + message: data, + dangerouslyUseHTMLString: true, + duration: 0 + }) + webNotify({ title: title, body: data }) + }) + // 接受系统通知/公告 + connection.on(MsgType.M004, (data1, data2) => { + // if (data.code == 200) { + useSocketStore().setUnreadNoticeList(data1) + useSocketStore().setReadNoticeList(data2) + // } + }) + // 接受聊天数据 + connection.on(MsgType.M005, (data) => { + const { fromUser, message } = data + + useSocketStore().setChat(data) + + if (data.userid != useUserStore().userId) { + ElNotification({ + title: fromUser.nickName, + message: message, + type: 'success', + duration: 3000 + }) + } + webNotify({ title: fromUser.nickName, body: message }) + }) + }, + AllReadNotice(connection) { + connection.invoke('AllReadNotice') + }, + ReadNotice(connection, noticeId) { + connection.invoke('ReadNotice', noticeId) + } +} +const MsgType = { + M001: 'onlineNum', + M002: 'connId', + M003: 'receiveNotice', + M004: 'moreNotice', + M005: 'receiveChat' +} diff --git a/src/signalr/index.js b/src/signalr/index.js new file mode 100644 index 0000000..a2d159c --- /dev/null +++ b/src/signalr/index.js @@ -0,0 +1,87 @@ +// 官方文档:https://docs.microsoft.com/zh-cn/aspnet/core/signalr/javascript-client?view=aspnetcore-6.0&viewFallbackFrom=aspnetcore-2.2&tabs=visual-studio +import * as signalR from '@microsoft/signalr' +import { getToken } from '@/utils/auth' +import { ElMessage } from 'element-plus' +import cache from '@/plugins/cache' +import analysis from '@/signalr/analysis' + +export default { + // signalR对象 + SR: {}, + // 失败连接重试次数 + failNum: 4, + init(url) { + var socketUrl = window.location.origin + url + '?clientId=' + cache.local.get('clientId') + const connection = new signalR.HubConnectionBuilder() + .withUrl(socketUrl, { accessTokenFactory: () => getToken() }) + .withAutomaticReconnect() //自动重新连接 + .configureLogging(signalR.LogLevel.Warning) + .build() + this.SR = connection + // 断线重连 + connection.onclose(async (error) => { + console.error('断开连接了' + error) + console.assert(connection.state === signalR.HubConnectionState.Disconnected) + // 建议用户重新刷新浏览器 + await this.start() + }) + + connection.onreconnected((connectionId) => { + ElMessage({ + message: '与服务器通讯已连接成功', + type: 'success', + duration: 2000 + }) + console.log('断线重新连接成功' + connectionId) + }) + + connection.onreconnecting(async () => { + console.log('断线重新连接中... ') + + await this.start() + }) + analysis.onMessage(connection) + // 启动 + // this.start(); + }, + /** + * 调用 this.signalR.start().then(async () => { await this.SR.invoke("method")}) + * @returns + */ + async start() { + try { + console.debug('signalR-1', this.SR.state) + //使用async和await 或 promise的then 和catch 处理来自服务端的异常 + if (this.SR.state === signalR.HubConnectionState.Disconnected) { + await this.SR.start() + } + + console.debug('signalR-2', this.SR.state) + return true + } catch (error) { + console.error(error) + this.failNum-- + // console.log(`失败重试剩余次数${that.failNum}`, error) + if (this.failNum > 0 && this.SR.state.Disconnected) { + setTimeout(async () => { + await this.start() + }, 5000) + } + return false + } + }, + AllReadNotice() { + analysis.AllReadNotice(this.SR) + }, + ReadNotice(noticeId) { + analysis.ReadNotice(this.SR, noticeId) + } + // AllReadAimMsg() { + // const connection = this.SR + // connection.invoke('AllReadAimMsg') + // }, + // ReadAimMsg(msgId) { + // const connection = this.SR + // connection.invoke('ReadAimMsg', msgId) + // } +} diff --git a/src/views/noticecenter/index.vue b/src/views/noticecenter/index.vue index 88be026..0a361a4 100644 --- a/src/views/noticecenter/index.vue +++ b/src/views/noticecenter/index.vue @@ -7,14 +7,23 @@ - + + - + + - - - + + +