diff --git a/common/api/im.js b/common/api/im.js index 1d31ccb..48358ac 100644 --- a/common/api/im.js +++ b/common/api/im.js @@ -6,13 +6,13 @@ // * @Description: file content // */ // import { MsbSkt } from "../utils/webSkt"; -// import { createUUID } from '@/common/utils'; +// import { CreateUUID } from '@/common/utils'; // /** // * 系统消息心跳 // */ // export const ApiSktSysHeart = () => MsbSkt.send({ -// traceId: createUUID(), +// traceId: CreateUUID(), // traceType: '0', // content: { text: "ping" } // }); @@ -22,7 +22,7 @@ // * @param {*} content // */ // export const ApiSktSysGetSession = (content) => MsbSkt.send({ -// traceId: createUUID(), +// traceId: CreateUUID(), // traceType: '1', // content // }); @@ -32,7 +32,7 @@ // * @param {*} content // */ // export const ApiSktSysGetHistory = (content) => MsbSkt.send({ -// traceId: createUUID(), +// traceId: CreateUUID(), // traceType: '2', // content // }); @@ -42,7 +42,7 @@ // * @param {*} content // */ // export const ApiSktSysGetHistory = (content) => MsbSkt.send({ -// traceId: createUUID(), +// traceId: CreateUUID(), // traceType: '6', // content // }); \ No newline at end of file diff --git a/common/plugins/msbIm.js b/common/plugins/msbIm.js new file mode 100644 index 0000000..85788d8 --- /dev/null +++ b/common/plugins/msbIm.js @@ -0,0 +1,238 @@ +/* + * @Author: ch + * @Date: 2022-05-18 14:54:47 + * @LastEditors: ch + * @LastEditTime: 2022-05-20 11:25:57 + * @Description: file content + */ +const connect = Symbol('connect'), + send = Symbol('send'); + +export default class MsbIm { + option = { + ioKey : 'traceId', + reconnect: true, + } + socket = null; + queue = {}; + interceptors = { + dataChangeBefore: null, + dataChangeAfter: null, + onMessage: null, + onResponse: null + + }; + sessionData = []; + curSession = {}; + + constructor(option) { + this.option = { + ...this.option, + ...option + } + } + /** + * 创建连接返回一个Promise 创建成功并成功打开连接算连接成功 + * @param {*} option + */ + [connect](option) { + return new Promise((resolve, reject) => { + this.socket = uni.connectSocket({ + ...option, + fail(e){ + reject(e); + } + }); + this.socket.onOpen(() => { + this.socket.onMessage((res) => { + const result = JSON.parse(res.data); + // 每次消息推送都会推送至onMessage Hook中 + this.interceptors.onMessage && this.interceptors.onMessage(result); + + // 查看是否有返回信息处理Hook,有的话优先取处理后的结果返回 + const responseData = this.interceptors.onResponse ? this.interceptors.onResponse(result) : result; + const isError = responseData.constructor === Promise; + // 如果再消息堆里有此消息回调,则执行回调,并删除 + const cbk = this.queue[result[this.option.ioKey]]; + if (cbk) { + cbk(isError ? {error:responseData} : {result:responseData}); + delete this.queue[result[this.option.ioKey]]; + } + }) + resolve(this.socket); + }); + this.socket.onClose(() => { + if (this.option.reconnect) { + this.connect(); + } + }); + }); + } + /** + * 向服务端发送消息||请求,返回一个Promise对象,收到ioKey对应的消息会算一个同步完成 + * @param {*} data + */ + [send](data) { + return new Promise((resolve, reject) => { + this.queue[data[this.option.ioKey]] = ({ result, error }) => { + if (result) { + resolve(result); + } else { + reject(error); + } + }; + this.socket.send({ + data : JSON.stringify(data), + fail(e) { + if (this.interceptors.onResponse) { + reject(this.interceptors.onResponse(e)) + } + reject(e); + } + }); + }) + } + /** + * 扩展钩子 + * @param {*} hookName + * @param {*} cb + */ + use(hookName, cb){ + this.hook[hookName] = cb; + } + init (config) { + return new Promise((resolve, reject) => { + const heart = () => { + setTimeout(async () => { + await this[send]({ + traceId: CreateUUID(), + traceType: '0', + content: { text: "ping" } + }); + heart(); + },5000) + } + this[connect]({ + ...config, + // url : `wss://you-gateway.mashibing.com/ws?client=${store.state.token}&type=1` + // url : `wss://k8s-horse-gateway.mashibing.cn/ws?client=${store.state.token}&type=1`, // url是websocket连接ip + + }).then(() => { + heart(); + resolve(this); + }); + }) + + } + /** + * 设置 + */ + setSessionData(data) { + + this.interceptors.dataChangeBefore && this.interceptors.dataChangeBefore(data, this.sessionData); + this.sessionData = data; + this.interceptors.dataChangeAfter && this.interceptors.dataChangeAfter(this.sessionData); + }, + /** + * 设置当前聊天窗口 + * Data为Session数据 + * @param {*} data + */ + setCurSession(data) { + this.curSession = data; + } + async getSessionList (params) { + let {content} = await this[send]({ + traceId: CreateUUID(), + ...params + }); + content.sessionVOS.forEach(item => { + if (item.lastMessage) { + item.lastMessage.createTimeStamp = FormatDate(item.lastMessage.createTimeStamp, 'mm-dd hh:ii') + item.lastMessage.payload = JSON.parse(item.lastMessage.payload || {}); + } + let historyData = this.sessionData; + let hisIndex = historyData.findIndex(i => i.id === item.id); + if(hisIndex >= 0){ + historyData[hisIndex].lastMessage = item.lastMessage; + historyData[hisIndex].unreadCount++; + + this.interceptors.dataChangeBefore && this.interceptors.dataChangeBefore(historyData, this.sessionData); + this.sessionData = historyData; + this.interceptors.dataChangeAfter && this.interceptors.dataChangeAfter(this.sessionData); + }else{ + item.messageList = []; + const newData = [...historyData, item] + this.interceptors.dataChangeBefore && this.interceptors.dataChangeBefore(newData, this.sessionData); + this.sessionData = newData; + this.interceptors.dataChangeAfter && this.interceptors.dataChangeAfter(this.sessionData); + } + }); + } + async getHistoryMsg (params){ + let { content } = await this[send]({ + traceId: CreateUUID(), + traceType : 23, + ...params + }) + if(!content.length){ + return false; + } + // ctx.reverse(); + let newData = this.sessionData; + const hisIdx = newData.findIndex(i => i.id === content[0].sessionId); + content.forEach(item => { + item.createTimeStamp = FormatDate(item.createTimeStamp, 'mm-dd hh:ii'); + item.payload = JSON.parse(item.payload) + }) + newData[hisIdx].messageList = newData[hisIdx].messageList.concat(content); + this.interceptors.dataChangeBefore && this.interceptors.dataChangeBefore(newData, this.sessionData); + this.sessionData = newData; + this.interceptors.dataChangeAfter && this.interceptors.dataChangeAfter(this.sessionData); + return Promise.resolve(content); + + } + async setRead (params){ + await this[send]({ + traceId : CreateUUID(), + traceType : "6", + ...params + }); + // 计算页头消息数 + this.curSession.unreadCount = 0; + } + async sendMsg (params){ + const { content } = await this[send]({ + traceId: CreateUUID(), + traceType: 20, + ...params + }); + return Promise.resolve(content) + } + async createSession (params){ + const { content } = await this[send]({ + traceId : CreateUUID(), + traceType : 21, + ...params + }); + let historyData = this.sessionData; + let curSession = historyData.find(i => i.id === content.id); + if (!curSession) { + curSession = { + ...content, + unreadCount: 0, + messageList : [] + } + const newData = [...historyData, curSession]; + this.interceptors.dataChangeBefore && this.interceptors.dataChangeBefore(newData, this.sessionData); + this.sessionData = newData; + this.interceptors.dataChangeAfter && this.interceptors.dataChangeAfter(this.sessionData); + } + this.curSession = curSession; + // store.commit('SET_SESSION_DATA',this.sessionData); + // store.commit('SET_SESSION_MSG_ID',curSession.id); + return Promise.resolve(content); + } + + +} \ No newline at end of file diff --git a/common/plugins/webSkt.js b/common/plugins/webSkt.js index bcd0d6d..4b2f310 100644 --- a/common/plugins/webSkt.js +++ b/common/plugins/webSkt.js @@ -2,7 +2,7 @@ * @Author: ch * @Date: 2022-05-18 14:54:47 * @LastEditors: ch - * @LastEditTime: 2022-05-19 15:42:19 + * @LastEditTime: 2022-05-19 17:35:21 * @Description: file content */ @@ -17,8 +17,7 @@ export class MsbWebSktNew{ this.queue = {}; this.hook = {}; - this.data = []; - this.count = 0; + this.sessionData = []; this.curSession = {}; } /** diff --git a/common/utils/im.js b/common/utils/im.js new file mode 100644 index 0000000..bd28695 --- /dev/null +++ b/common/utils/im.js @@ -0,0 +1,31 @@ +/* + * @Author: ch + * @Date: 2022-05-20 11:00:07 + * @LastEditors: ch + * @LastEditTime: 2022-05-20 11:36:51 + * @Description: file content + */ + +import MsbIm from '@/common/plugins/msbIm' ; +import $store from '@/common/store'; + +const Im = new MsbIm({ + reconnect: true, +}); +const ImInit = () => { + return Im.init({ + url: `wss://k8s-horse-gateway.mashibing.cn/ws?client=${$store.state.token}&type=1` + }) +}; +Im.interceptors.dataChangeAfter = () => { + $store.commit('SET_SESSION_DATA', Im.sessionData); + let msgCount = 0; + Im.sessionData.forEach(i => { + msgCount += i.unreadCount; + }) + $store.commit('SET_SESSION_MSG_COUNT', msgCount); +} +export { + Im, + ImInit +} diff --git a/common/utils/utils.js b/common/utils/utils.js index 38770d2..7ef40fb 100644 --- a/common/utils/utils.js +++ b/common/utils/utils.js @@ -2,7 +2,7 @@ * @Author: ch * @Date: 2022-03-17 19:15:10 * @LastEditors: ch - * @LastEditTime: 2022-05-07 17:18:50 + * @LastEditTime: 2022-05-20 10:02:24 * @Description: 一些无法归类的公共方法容器 */ @@ -79,7 +79,7 @@ const IsDouble = (str) => { * @param {number|string|Date} d 时间参数能被new Date识别的数字,字符串,日期 * @param {string} fmt 时间格式参数 字符串类型 默认'yyyy/mm/dd' */ -const formatDate = (date, fmt = 'yyyy/mm/dd' ) =>{ +const FormatDate = (date, fmt = 'yyyy/mm/dd' ) =>{ // 处理不识别的时间表示字符串,如2020年01月01日00时00分00秒 if(date.constructor === String){ date = date.replace(/\D+/ig,'/'); @@ -118,7 +118,7 @@ const formatDate = (date, fmt = 'yyyy/mm/dd' ) =>{ return fmt; } -const createUUID = (len,radix) => { +const CreateUUID = (len,radix) => { const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split(''); let uuid = [], i=0; @@ -140,7 +140,7 @@ const createUUID = (len,radix) => { return uuid.join(''); } -const toSearchJson = (search)=>{ +const ToSearchJson = (search)=>{ search = search.replace(/\?/g,'&'); let searchArr = search.split('&'), obj = {}; @@ -170,7 +170,7 @@ export { // 判断是否double或float IsDouble, // 时间格式化 - formatDate, - createUUID, - toSearchJson + FormatDate, + CreateUUID, + ToSearchJson } \ No newline at end of file diff --git a/common/utils/webSkt.js b/common/utils/webSkt.js index f0f43d9..89f54fc 100644 --- a/common/utils/webSkt.js +++ b/common/utils/webSkt.js @@ -2,12 +2,12 @@ * @Author: ch * @Date: 2022-05-18 15:27:02 * @LastEditors: ch - * @LastEditTime: 2022-05-19 15:50:12 + * @LastEditTime: 2022-05-19 16:42:44 * @Description: file content */ import { MsbWebSktNew } from "../plugins/webSkt"; -import { createUUID, formatDate } from '@/common/utils'; +import { CreateUUID, FormatDate } from '@/common/utils'; import store from "../store"; const MsbSkt = new MsbWebSktNew(); @@ -25,8 +25,8 @@ MsbSkt.use('onMessage', (data) => { } ctx = data.content; ctx.payload = JSON.parse(ctx.payload || {}); - ctx.createTimeStamp = formatDate(ctx.createTimeStamp || new Date(), 'mm-dd hh:ii') - let historyData = MsbSkt.data; + ctx.createTimeStamp = FormatDate(ctx.createTimeStamp || new Date(), 'mm-dd hh:ii') + let historyData = MsbSkt.sessionData; const hisIndex = historyData.findIndex(i => i.id === ctx.sessionId); // 不在当前会话框则全局消息加1 // if(ctx.sessionId !== store.state.sessionMsgId){ @@ -40,10 +40,10 @@ MsbSkt.use('onMessage', (data) => { // if(ctx.sessionId !== store.state.sessionMsgId){ // curHisData.unreadCount++; // } - MsbSkt.data = historyData; + MsbSkt.sessionData = historyData; }else{ // 会话列表不存在,则创建一个会话 - MsbSkt.data = [...historyData, { + MsbSkt.sessionData = [...historyData, { fromAvatar : ctx.fromAvatar, fromId : ctx.fromId, fromNickname : ctx.fromNickname, @@ -53,7 +53,7 @@ MsbSkt.use('onMessage', (data) => { unreadCount : 1 }] } - store.commit('SET_SESSION_DATA',MsbSkt.data); + store.commit('SET_SESSION_DATA',MsbSkt.sessionData); }); /** @@ -64,7 +64,7 @@ export const MsbSktInit = () => { const heart = () => { setTimeout(async () => { await MsbSkt.send({ - traceId: createUUID(), + traceId: CreateUUID(), traceType: '0', content: { text: "ping" } }); @@ -88,23 +88,23 @@ export const MsbSktInit = () => { */ export const MsbSktGetSessionList = async (params) => { let {content} = await MsbSkt.send({ - traceId: createUUID(), + traceId: CreateUUID(), ...params }); content.sessionVOS.forEach(item => { if (item.lastMessage) { - item.lastMessage.createTimeStamp = formatDate(item.lastMessage.createTimeStamp, 'mm-dd hh:ii') + item.lastMessage.createTimeStamp = FormatDate(item.lastMessage.createTimeStamp, 'mm-dd hh:ii') item.lastMessage.payload = JSON.parse(item.lastMessage.payload || {}); } - let historyData = MsbSkt.data; + let historyData = MsbSkt.sessionData; let hisIndex = historyData.findIndex(i => i.id === item.id); if(hisIndex >= 0){ historyData[hisIndex].lastMessage = item.lastMessage; historyData[hisIndex].unreadCount++; - MsbSkt.data = historyData + MsbSkt.sessionData = historyData }else{ item.messageList = []; - MsbSkt.data = [...historyData, item]; + MsbSkt.sessionData = [...historyData, item]; } }); } @@ -113,7 +113,7 @@ export const MsbSktGetSessionList = async (params) => { */ export const MsbSktGetHistoryMsg = async (params) => { let { content } = await MsbSkt.send({ - traceId: createUUID(), + traceId: CreateUUID(), traceType : 23, ...params }) @@ -121,14 +121,14 @@ export const MsbSktGetHistoryMsg = async (params) => { return false; } // ctx.reverse(); - let newData = MsbSkt.data; + let newData = MsbSkt.sessionData; const hisIdx = newData.findIndex(i => i.id === content[0].sessionId); content.forEach(item => { - item.createTimeStamp = formatDate(item.createTimeStamp, 'mm-dd hh:ii'); + item.createTimeStamp = FormatDate(item.createTimeStamp, 'mm-dd hh:ii'); item.payload = JSON.parse(item.payload) }) newData[hisIdx].messageList = newData[hisIdx].messageList.concat(content); - MsbSkt.data = newData; + MsbSkt.sessionData = newData; store.commit('SET_SESSION_DATA', newData); return Promise.resolve(content); @@ -138,7 +138,7 @@ export const MsbSktGetHistoryMsg = async (params) => { */ export const MsbSktSetRead = async (params) => { await MsbSkt.send({ - traceId : createUUID(), + traceId : CreateUUID(), traceType : "6", ...params }); @@ -151,7 +151,7 @@ export const MsbSktSetRead = async (params) => { */ export const MsbSktSendMsg = async (params) => { const { content } = await MsbSkt.send({ - traceId: createUUID(), + traceId: CreateUUID(), traceType: 20, ...params }); @@ -162,11 +162,11 @@ export const MsbSktSendMsg = async (params) => { */ export const MsbSktCreateSession = async (params) => { const { content } = await MsbSkt.send({ - traceId : createUUID(), + traceId : CreateUUID(), traceType : 21, ...params }); - let historyData = MsbSkt.data; + let historyData = MsbSkt.sessionData; let curSession = historyData.find(i => i.id === content.id); if (!curSession) { curSession = { @@ -174,10 +174,10 @@ export const MsbSktCreateSession = async (params) => { unreadCount: 0, messageList : [] } - MsbSkt.data = [...historyData, curSession]; + MsbSkt.sessionData = [...historyData, curSession]; } MsbSkt.curSession = curSession; - store.commit('SET_SESSION_DATA',MsbSkt.data); + store.commit('SET_SESSION_DATA',MsbSkt.sessionData); store.commit('SET_SESSION_MSG_ID',curSession.id); return Promise.resolve(content); } diff --git a/common/utils/websocket.js b/common/utils/websocket.js index a1024b9..53dad5f 100644 --- a/common/utils/websocket.js +++ b/common/utils/websocket.js @@ -6,14 +6,14 @@ * @Description: file content */ import store from "../store"; -import {createUUID, formatDate} from '@/common/utils'; +import {CreateUUID, FormatDate} from '@/common/utils'; let sessionListFlag = false; const sessionList = (ctx) => { sessionListFlag = true; // 来新消息先查会话列表是否一存在,存在则加消息数。不存在则向会话列表加一个会话框 ctx.sessionVOS.forEach(item => { if (item.lastMessage) { - item.lastMessage.createTimeStamp = formatDate(item.lastMessage.createTimeStamp, 'mm-dd hh:ii') + item.lastMessage.createTimeStamp = FormatDate(item.lastMessage.createTimeStamp, 'mm-dd hh:ii') item.lastMessage.payload = JSON.parse(item.lastMessage.payload || {}); } let historyData = store.state.sessionData; @@ -38,7 +38,7 @@ const historyMsg = (ctx) => { let newData = store.state.sessionData; const hisIdx = newData.findIndex(i => i.id === ctx[0].sessionId); ctx.forEach(item => { - item.createTimeStamp = formatDate(item.createTimeStamp, 'mm-dd hh:ii'); + item.createTimeStamp = FormatDate(item.createTimeStamp, 'mm-dd hh:ii'); item.payload = JSON.parse(item.payload) }) // newData[hisIdx].messageList = ctx.concat(newData[hisIdx].messageList); @@ -55,7 +55,7 @@ const sessionMsg = (ctx)=>{ return } ctx.payload = JSON.parse(ctx.payload || {}); - ctx.createTimeStamp = formatDate(ctx.createTimeStamp || new Date(), 'mm-dd hh:ii') + ctx.createTimeStamp = FormatDate(ctx.createTimeStamp || new Date(), 'mm-dd hh:ii') let historyData = store.state.sessionData; const hisIndex = historyData.findIndex(i => i.id === ctx.sessionId); // 不在当前会话框则全局消息加1 diff --git a/main.js b/main.js index b4fb30e..d2fe292 100644 --- a/main.js +++ b/main.js @@ -13,9 +13,9 @@ import uView from 'uview-ui'; import store from '@/common/store'; import Confirm from '@/components/mount/index'; import route from 'uview-ui/libs/util/route'; -import {toSearchJson} from '@/common/utils'; +import {ToSearchJson} from '@/common/utils'; import {ApiGetOpenId, ApiGetAuthUrl} from '@/common/api/wx'; -import {MsbWebSkt, MsbWebSktInit, createUUID} from '@/common/utils'; +import {MsbWebSkt, MsbWebSktInit, CreateUUID} from '@/common/utils'; import { MsbSktInit, MsbSktGetSessionList} from './common/utils/webSkt'; import { ApiSktSysGetSession, ApiSktSysHeart } from './common/api/im'; @@ -34,7 +34,7 @@ if (store.state.token) { const ua = navigator ? navigator.userAgent.toLowerCase() : ''; if(ua.includes('micromessenger')) { if(!store.state.openId){ - let query = toSearchJson(window.location.search) + let query = ToSearchJson(window.location.search) if(query.code){ ApiGetOpenId({ code : query.code diff --git a/pages/account/message/chat/components/ChatList.vue b/pages/account/message/chat/components/ChatList.vue deleted file mode 100644 index d64b424..0000000 --- a/pages/account/message/chat/components/ChatList.vue +++ /dev/null @@ -1,926 +0,0 @@ - - - - - - diff --git a/pages/account/message/chat/components/Footer.vue b/pages/account/message/chat/components/Footer.vue index 62ca3bc..32f5cd4 100644 --- a/pages/account/message/chat/components/Footer.vue +++ b/pages/account/message/chat/components/Footer.vue @@ -45,7 +45,7 @@ - - diff --git a/pages/account/message/chat/index.vue b/pages/account/message/chat/index.vue index 3168cf2..3a7386c 100644 --- a/pages/account/message/chat/index.vue +++ b/pages/account/message/chat/index.vue @@ -2,7 +2,7 @@ * @Author: ch * @Date: 2022-03-26 14:32:03 * @LastEditors: ch - * @LastEditTime: 2022-05-19 14:05:29 + * @LastEditTime: 2022-05-20 10:02:52 * @Description: file content -->