feature/im-0602-ch
ch 3 years ago
parent 317cf6225d
commit e891706f3d

@ -2,7 +2,7 @@
* @Author: ch * @Author: ch
* @Date: 2022-05-05 14:40:00 * @Date: 2022-05-05 14:40:00
* @LastEditors: ch * @LastEditors: ch
* @LastEditTime: 2022-05-26 19:04:26 * @LastEditTime: 2022-06-02 15:09:46
* @Description: 根据git分支生成对应环境的环境变量 * @Description: 根据git分支生成对应环境的环境变量
* 开发时如果环境变量换了可以不用重启服务直接运行node env.config.js即可 * 开发时如果环境变量换了可以不用重启服务直接运行node env.config.js即可
*/ */
@ -13,7 +13,8 @@ const envConfig = {
dev : { dev : {
baseUrl: 'https://k8s-horse-gateway.mashibing.cn', baseUrl: 'https://k8s-horse-gateway.mashibing.cn',
staticUrl : 'https://k8s-shop-app.mashibing.cn', staticUrl : 'https://k8s-shop-app.mashibing.cn',
imUrl : 'wss://k8s-horse-gateway.mashibing.cn' imUrl : 'ws://192.168.10.94:8090'
// imUrl : 'wss://k8s-horse-gateway.mashibing.cn'
}, },
test : { test : {
baseUrl: 'https://k8s-horse-gateway.mashibing.cn', baseUrl: 'https://k8s-horse-gateway.mashibing.cn',

@ -1,48 +1,14 @@
// /* /*
// * @Author: ch * @Author: ch
// * @Date: 2022-05-18 15:21:10 * @Date: 2022-05-27 17:44:36
// * @LastEditors: ch * @LastEditors: ch
// * @LastEditTime: 2022-05-18 17:24:37 * @LastEditTime: 2022-06-02 14:45:51
// * @Description: file content * @Description: file content
// */ */
// import { MsbSkt } from "../utils/webSkt"; import {ToAsyncAwait, MsbRequestTk} from '@/common/utils';
// import { CreateUUID } from '@/common/utils'; const BASE_URL = '/mall/im';
// /** /**
// * 系统消息心跳 * 获取soket登录秘钥
// */ */
// export const ApiSktSysHeart = () => MsbSkt.send({ export const ApiGetSoketTicket = () => ToAsyncAwait(MsbRequestTk.get(`${BASE_URL}/ticket`));
// traceId: CreateUUID(),
// traceType: '0',
// content: { text: "ping" }
// });
// /**
// * 获取系统通知会话
// * @param {*} content
// */
// export const ApiSktSysGetSession = (content) => MsbSkt.send({
// traceId: CreateUUID(),
// traceType: '1',
// content
// });
// /**
// * 获取系统消息历史消息
// * @param {*} content
// */
// export const ApiSktSysGetHistory = (content) => MsbSkt.send({
// traceId: CreateUUID(),
// traceType: '2',
// content
// });
// /**
// * 系统消息已读
// * @param {*} content
// */
// export const ApiSktSysGetHistory = (content) => MsbSkt.send({
// traceId: CreateUUID(),
// traceType: '6',
// content
// });

@ -2,16 +2,40 @@
* @Author: ch * @Author: ch
* @Date: 2022-05-18 14:54:47 * @Date: 2022-05-18 14:54:47
* @LastEditors: ch * @LastEditors: ch
* @LastEditTime: 2022-05-24 22:51:47 * @LastEditTime: 2022-06-02 18:10:40
* @Description: file content * @Description: file content
*/ */
import { CreateUUID, FormatDate, ToAsyncAwait } from "@/common/utils"; import { CreateUUID, FormatDate, ToAsyncAwait } from "@/common/utils";
import './potoReq';
import './protoRsp'
const connect = Symbol('connect'), const connect = Symbol('connect'),
send = Symbol('send'), send = Symbol('send'),
onResponse = Symbol('onResponse'), onResponse = Symbol('onResponse'),
onMessage = Symbol('onMessage'), onMessage = Symbol('onMessage'),
updateData = Symbol('updateData') updateData = Symbol('updateData')
; ;
const fromatPotoReq = (traceId, traceType, content) => {
let messageModel = new proto.ReqModel();
messageModel.setTraceid(traceId);
messageModel.setTracetype(traceType);
content && messageModel.setContent(JSON.stringify(content));
return messageModel.serializeBinary();
},
fromatPotoRsp = (data)=>{
const res = proto.RspModel.deserializeBinary(new Uint8Array(data));
let ctx = res.getContent();
ctx = ctx ? JSON.parse(ctx) : {};
if (ctx.payload) {
ctx.payload = JSON.parse(ctx.payload)
}
return {
content: ctx,
traceId: res.getTraceid(),
traceType: res.getTracetype(),
code: res.getCode(),
message : res.getMessage()
}
}
export default class MsbIm { export default class MsbIm {
option = { option = {
ioKey : 'traceId', ioKey : 'traceId',
@ -19,6 +43,7 @@ export default class MsbIm {
logout : false logout : false
} }
socket = null; socket = null;
isOpen = false;
queue = {}; queue = {};
interceptors = { interceptors = {
dataChangeBefore: null, dataChangeBefore: null,
@ -51,10 +76,11 @@ export default class MsbIm {
}); });
this.socket.onOpen(() => { this.socket.onOpen(() => {
this.socket.onMessage(async (res) => { this.socket.onMessage(async (res) => {
const result = JSON.parse(res.data); const result = fromatPotoRsp(res.data);
if (result.content?.payload) {
result.content.payload = JSON.parse(result.content.payload); // if (result.content?.payload) {
} // result.content.payload = JSON.parse(result.content.payload);
// }
// 401主动退出 // 401主动退出
if (result.code === 401) { if (result.code === 401) {
this.logout(); this.logout();
@ -94,8 +120,9 @@ export default class MsbIm {
reject(error); reject(error);
} }
}; };
this.socket.send({ this.socket.send({
data : JSON.stringify(data), data : fromatPotoReq(data.traceId, data.traceType, data.content),
fail(e) { fail(e) {
reject({error : e}); reject({error : e});
} }
@ -146,11 +173,11 @@ export default class MsbIm {
setTimeout(async () => { setTimeout(async () => {
await this[send]({ await this[send]({
traceId: CreateUUID(), traceId: CreateUUID(),
traceType: '0', traceType: 0,
content: { text: "ping" } content: { text: "ping" }
}).catch((e)=>{}); }).catch((e)=>{});
heart(); heart();
},5000) },1000)
} }
this[connect]({ this[connect]({
...config, ...config,
@ -187,16 +214,19 @@ export default class MsbIm {
* @param {*} params * @param {*} params
*/ */
async getSessionList(params) { async getSessionList(params) {
const par = {
let {error, result} = await ToAsyncAwait(this[send]({
traceId: CreateUUID(), traceId: CreateUUID(),
traceType: 1, traceType: 1,
...params ...params
})); };
let {error, result} = await ToAsyncAwait(this[send](par));
if (error) { if (error) {
return Promise.reject(error); return Promise.reject(error);
} }
const { content } = result; const { content } = result;
console.log('获取会话列表', par, result);
content.sessionVOS.forEach(item => { content.sessionVOS.forEach(item => {
if (item.lastMessage) { if (item.lastMessage) {
item.lastMessage.payload = JSON.parse(item.lastMessage.payload || {}); item.lastMessage.payload = JSON.parse(item.lastMessage.payload || {});
@ -219,23 +249,30 @@ export default class MsbIm {
* 获取会话的历史消息记录 * 获取会话的历史消息记录
* @param {*} params * @param {*} params
*/ */
async getHistoryMsg(params) { async getHistoryMsg() {
const {error, result} = await ToAsyncAwait(this[send]({ const curSessionIdx = this.sessionData.findIndex(i => i.id === this.curSessionId);
const curSession = this.sessionData[curSessionIdx];
const msgList = curSession.messageList || [];
const par = {
traceId: CreateUUID(), traceId: CreateUUID(),
traceType: 23, traceType: 23,
...params content: {
})); sessionId: this.curSessionId,
topMessageId: msgList.length ? msgList[0].id : null
}
};
const {error, result} = await ToAsyncAwait(this[send](par));
if (error) { if (error) {
return Promise.reject(error); return Promise.reject(error);
} }
const { content } = result; const { content } = result;
console.log('获取会话历史消息',par, result);
if (content.length) { if (content.length) {
let newData = this.sessionData; let newData = this.sessionData;
const hisIdx = newData.findIndex(i => i.id === content[0].sessionId);
content.forEach(item => { content.forEach(item => {
item.payload = JSON.parse(item.payload) item.payload = JSON.parse(item.payload)
}) })
newData[hisIdx].messageList = newData[hisIdx].messageList.concat(content); newData[curSessionIdx].messageList = content.concat(newData[curSessionIdx].messageList);
this.setSessionData(newData); this.setSessionData(newData);
} }
return Promise.resolve(result); return Promise.resolve(result);
@ -246,11 +283,15 @@ export default class MsbIm {
* @param {*} params * @param {*} params
*/ */
async setRead(params) { async setRead(params) {
await this[send]({ const par = {
traceId : CreateUUID(), traceId : CreateUUID(),
traceType : "6", traceType : "6",
...params ...params
}); }
const {error, result } = await this[send](par);
console.log('会话已读', par, result);
let newData = this.sessionData.map(item => { let newData = this.sessionData.map(item => {
if (item.id == params.content.sessionId) { if (item.id == params.content.sessionId) {
item.unreadCount = 0; item.unreadCount = 0;
@ -285,6 +326,8 @@ export default class MsbIm {
// 超过时间未返回视为发送失败 // 超过时间未返回视为发送失败
this.timerStatus(msgCtx); this.timerStatus(msgCtx);
const { error, result } = await ToAsyncAwait(this[send](par)); const { error, result } = await ToAsyncAwait(this[send](par));
console.log('发送消息', par, result);
// 接到通知,标记消息是否发送成功 // 接到通知,标记消息是否发送成功
for (let i = curSession.messageList.length; i--;) { for (let i = curSession.messageList.length; i--;) {
const item = curSession.messageList[i]; const item = curSession.messageList[i];

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

@ -2,13 +2,14 @@
* @Author: ch * @Author: ch
* @Date: 2022-05-20 11:00:07 * @Date: 2022-05-20 11:00:07
* @LastEditors: ch * @LastEditors: ch
* @LastEditTime: 2022-05-23 14:54:50 * @LastEditTime: 2022-06-02 17:18:33
* @Description: file content * @Description: file content
*/ */
import MsbIm from '@/common/plugins/msbIm' ; import MsbIm from '@/common/plugins/msbIm' ;
import { ToAsyncAwait } from '@/common/utils'; import { ToAsyncAwait, FormatJsonSearch } from '@/common/utils';
import { ApiGetCurrentUser } from '@/common/api/account'; import { ApiGetCurrentUser } from '@/common/api/account';
import { ApiGetSoketTicket } from '@/common/api/im';
import $store from '@/common/store'; import $store from '@/common/store';
import ENV from '@/common/config/env'; import ENV from '@/common/config/env';
@ -16,13 +17,27 @@ const Im = new MsbIm({
reconnect: true, reconnect: true,
}); });
const ImInit = async () => { const ImInit = async () => {
const { error } = await ApiGetCurrentUser(); const { error, result } = await ApiGetCurrentUser();
if (error) { if (error) {
return false; return false;
} }
// const { error: er, result: res } = await ApiGetSoketTicket();
// if (er) {
// return false;
// }
const par = FormatJsonSearch({
// client: res.client,
// ticket: res.ticket,
client: 'yan_xuan',
ticket: '6w/8bplSGxqhAbo3vTGaWQ==',
// 1普通用户 2客服链接
connect: 1,
user: result.id,
nickname: result.nickname,
avatar : result.avatar
})
await ToAsyncAwait(Im.init({ await ToAsyncAwait(Im.init({
// url: `wss://k8s-horse-gateway.mashibing.cn/ws?client=${$store.state.token}&type=1` url: `${ENV.imUrl}/ws${par}`
url: `${ENV.imUrl}/ws?client=${$store.state.token}&type=1`
})) }))
}; };
@ -32,7 +47,6 @@ Im.interceptors.dataChangeAfter = () => {
Im.sessionData.forEach(i => { Im.sessionData.forEach(i => {
msgCount += i.unreadCount; msgCount += i.unreadCount;
}) })
console.log(Im.sessionData)
$store.commit('SET_IM_MSG_COUNT', msgCount); $store.commit('SET_IM_MSG_COUNT', msgCount);
} }

@ -2,7 +2,7 @@
* @Author: ch * @Author: ch
* @Date: 2022-03-17 19:15:10 * @Date: 2022-03-17 19:15:10
* @LastEditors: ch * @LastEditors: ch
* @LastEditTime: 2022-05-31 17:36:08 * @LastEditTime: 2022-06-02 14:55:45
* @Description: 一些无法归类的公共方法容器 * @Description: 一些无法归类的公共方法容器
*/ */
@ -11,7 +11,8 @@ import {
isPhone as IsPhone, isPhone as IsPhone,
formatDate as FormatDate, formatDate as FormatDate,
creatUuid as CreateUUID, creatUuid as CreateUUID,
formatSearchJson as FormatSearchJson formatSearchJson as FormatSearchJson,
formatJsonSearch as FormatJsonSearch
} from "js-util-all"; } from "js-util-all";
import ENV from '@/common/config/env'; import ENV from '@/common/config/env';
@ -79,6 +80,7 @@ export {
// 时间格式化 // 时间格式化
FormatDate, FormatDate,
FormatSearchJson, FormatSearchJson,
FormatJsonSearch,
CreateUUID, CreateUUID,
// 防抖函数 // 防抖函数
Debounce, Debounce,

@ -2,7 +2,7 @@
* @Author: ch * @Author: ch
* @Date: 2021-07-26 23:22:16 * @Date: 2021-07-26 23:22:16
* @LastEditors: ch * @LastEditors: ch
* @LastEditTime: 2022-05-23 20:55:47 * @LastEditTime: 2022-06-02 16:11:56
* @Description: file content * @Description: file content
*/ */
import Vue from 'vue'; import Vue from 'vue';
@ -20,9 +20,7 @@ if (store.state.token) {
// 初始化IM // 初始化IM
ImInit().then(() => { ImInit().then(() => {
// 获取到会话列表 // 获取到会话列表
Im.getSessionList({ Im.getSessionList();
content: { sysId : 1 }
});
}); });
} }

@ -2,7 +2,7 @@
* @Author: ch * @Author: ch
* @Date: 2022-03-26 14:32:03 * @Date: 2022-03-26 14:32:03
* @LastEditors: ch * @LastEditors: ch
* @LastEditTime: 2022-05-30 17:49:28 * @LastEditTime: 2022-06-02 17:33:13
* @Description: file content * @Description: file content
--> -->
<template> <template>
@ -109,7 +109,9 @@ export default {
this.sessionId = this.$Route.query.sessionId; this.sessionId = this.$Route.query.sessionId;
if(this.sessionId){ if(this.sessionId){
if(!this.msgData?.length){
this.getHistoryMsg(); this.getHistoryMsg();
}
this.readMsg(); this.readMsg();
}else{ }else{
this.createSessionMain(); this.createSessionMain();
@ -144,7 +146,7 @@ export default {
uni.$u.toast(error.message); uni.$u.toast(error.message);
return false; return false;
} }
this.sessionId = result.content.id this.sessionId = result.content.id;
this.getHistoryMsg(); this.getHistoryMsg();
this.readMsg(); this.readMsg();
@ -154,13 +156,7 @@ export default {
*/ */
async getHistoryMsg(){ async getHistoryMsg(){
this.loading = true; this.loading = true;
const lastMsg = this.msgData?.length ? this.msgData[0] : {}; const {error, result} = await ToAsyncAwait(Im.getHistoryMsg());
const {error, result} = await ToAsyncAwait(Im.getHistoryMsg({
content : {
sessionId : this.sessionId,
topMessageId : lastMsg.id || null
}
}));
if(error){ if(error){
uni.$u.toast(error.errMsg || error.message); uni.$u.toast(error.errMsg || error.message);
return false return false

@ -2,7 +2,7 @@
* @Author: ch * @Author: ch
* @Date: 2022-03-22 16:13:00 * @Date: 2022-03-22 16:13:00
* @LastEditors: ch * @LastEditors: ch
* @LastEditTime: 2022-05-27 15:32:18 * @LastEditTime: 2022-06-02 18:26:55
* @Description: file content * @Description: file content
--> -->
<template> <template>
@ -62,11 +62,13 @@ export default {
methods:{ methods:{
FormatDate, FormatDate,
openMsg(item){ openMsg(item){
if(item.type === 3){ if(item.type === 4){
this.$Router.push(`/messageChat?sessionId=${item.id}`); if(JSON.parse(item.payload).type === 'system'){
}else{
this.$Router.push(`/messageSystem?sessionId=${item.id}`); this.$Router.push(`/messageSystem?sessionId=${item.id}`);
return false;
}
} }
this.$Router.push(`/messageChat?sessionId=${item.id}`);
} }
} }

@ -2,7 +2,7 @@
* @Author: ch * @Author: ch
* @Date: 2022-03-26 14:32:03 * @Date: 2022-03-26 14:32:03
* @LastEditors: ch * @LastEditors: ch
* @LastEditTime: 2022-05-27 17:02:59 * @LastEditTime: 2022-06-02 18:07:09
* @Description: file content * @Description: file content
--> -->
<template> <template>
@ -21,7 +21,6 @@
<text class="msg-item--desc-link" v-if="i.shipType === 1" @click="openLink(i)" :key="i.shipContent">[]</text> <text class="msg-item--desc-link" v-if="i.shipType === 1" @click="openLink(i)" :key="i.shipContent">[]</text>
<text v-else :key="i.shipContent">{{i.shipContent}}</text> <text v-else :key="i.shipContent">{{i.shipContent}}</text>
</template> </template>
</template> </template>
</view> </view>
</view> </view>
@ -78,13 +77,7 @@ export default {
}, },
async getHistoryMsg(){ async getHistoryMsg(){
this.loading = true; this.loading = true;
const lastMsg = this.msgData?.length ? this.msgData[this.msgData.length - 1] : {}; await ToAsyncAwait(Im.getHistoryMsg());
await ToAsyncAwait(Im.getHistoryMsg({
content : {
sessionId : this.$route.query.sessionId,
topMessageId : lastMsg.id || null
}
}));
}, },
/** /**
* 把当前会话消息置为已读 * 把当前会话消息置为已读

@ -2,7 +2,7 @@
* @Author: ch * @Author: ch
* @Date: 2022-03-23 17:27:21 * @Date: 2022-03-23 17:27:21
* @LastEditors: ch * @LastEditors: ch
* @LastEditTime: 2022-05-30 17:44:24 * @LastEditTime: 2022-06-02 14:39:30
* @Description: file content * @Description: file content
--> -->
<template> <template>
@ -169,10 +169,7 @@ page {
width: 50rpx; width: 50rpx;
height: 50rpx; height: 50rpx;
left: 40rpx; left: 40rpx;
/* #ifdef H5 */
top: 40rpx; top: 40rpx;
/* #endif */
/* #ifndef H5 */ /* #ifndef H5 */
top: 128rpx; top: 128rpx;
/* #endif */ /* #endif */

Loading…
Cancel
Save