diff --git a/.hbuilderx/launch.json b/.hbuilderx/launch.json new file mode 100644 index 0000000..81f13f4 --- /dev/null +++ b/.hbuilderx/launch.json @@ -0,0 +1,16 @@ +{ // launch.json 配置了启动调试时相关设置,configurations下节点名称可为 app-plus/h5/mp-weixin/mp-baidu/mp-alipay/mp-qq/mp-toutiao/mp-360/ + // launchtype项可配置值为local或remote, local代表前端连本地云函数,remote代表前端连云端云函数 + "version": "0.0", + "configurations": [{ + "default" : + { + "launchtype" : "local" + }, + "mp-weixin" : + { + "launchtype" : "local" + }, + "type" : "uniCloud" + } + ] +} diff --git a/common/store/index.js b/common/store/index.js index 1858125..56a7f63 100644 --- a/common/store/index.js +++ b/common/store/index.js @@ -2,7 +2,7 @@ * @Author: ch * @Date: 2022-03-22 18:28:52 * @LastEditors: ch - * @LastEditTime: 2022-04-15 14:14:20 + * @LastEditTime: 2022-04-25 19:24:25 * @Description: file content */ import Vue from 'vue' @@ -17,12 +17,14 @@ const // 地址列表 ADDRESS = 'ads'; - export default new Vuex.Store({ state : { token : uni.getStorageSync(TOKEN) || '', userInfo : JSON.parse(uni.getStorageSync(USER_INFO) || '{}'), - address : JSON.parse(uni.getStorageSync(ADDRESS) || '[]') + address : JSON.parse(uni.getStorageSync(ADDRESS) || '[]'), + sessionData : [], + sessionMsgData : {}, + sessionMsgCount : 0 }, mutations:{ SET_TOKEN (state, token = ''){ @@ -36,6 +38,15 @@ export default new Vuex.Store({ SET_ADDRESS (state, address = []){ state.address = address; uni.setStorageSync(ADDRESS, JSON.stringify(address)); + }, + SET_SESSION_DATA (state, data){ + state.sessionData = data ; + }, + SET_SESSION_MSG_COUNT (state, data){ + state.sessionMsgCount = data; } + }, + actions : { + } }) \ No newline at end of file diff --git a/common/utils/index.js b/common/utils/index.js index b8592dd..a280b6d 100644 --- a/common/utils/index.js +++ b/common/utils/index.js @@ -2,13 +2,15 @@ * @Author: ch * @Date: 2022-03-22 16:52:28 * @LastEditors: ch - * @LastEditTime: 2022-03-22 17:26:44 + * @LastEditTime: 2022-04-25 15:01:39 * @Description: 所有工具类统一在这输出 */ import * as util from './utils'; import * as requset from './requset'; +import * as websocket from './requset'; export * from './utils'; export * from './requset'; +export * from './websocket'; -export default { ...util, requset} \ No newline at end of file +export default { ...util, ...requset, ...websocket} \ No newline at end of file diff --git a/common/utils/requset.js b/common/utils/requset.js index 5b0888d..3e0421f 100644 --- a/common/utils/requset.js +++ b/common/utils/requset.js @@ -2,19 +2,19 @@ * @Author: ch * @Date: 2022-03-17 17:42:32 * @LastEditors: ch - * @LastEditTime: 2022-04-22 16:37:06 + * @LastEditTime: 2022-04-26 17:23:44 * @Description: 项目接口请求统一处理器,返回一个需要token和不需要token的请求封装方法 */ import MsbUniRequest from '@/common/plugins/msbUniRequest'; import $store from '@/common/store'; -const ENV = 'test'; +const ENV = 'prod'; const BASE_URL = { - 'test' : 'https://k8s-horse-gateway.mashibing.cn', + 'test' : 'https://like-app.mashibing.com/test', // 'test' : '', 'release' : '', - 'prod' : '' + 'prod' : 'https://like-app.mashibing.com/' }; /** * 接口返回成功结果统一处理 diff --git a/common/utils/utils.js b/common/utils/utils.js index 88ddfe2..a824765 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-04-24 19:00:20 + * @LastEditTime: 2022-04-26 15:49:50 * @Description: 一些无法归类的公共方法容器 */ @@ -16,7 +16,7 @@ * */ const ToAsyncAwait = (promise, isFromatResult = true) => { - if(!isFromatResult){ + if(!isFromatResult){ return promise; }else{ return promise.then((res) => ({error:null,result:res})).catch((err) => ({error:err,result:null})); @@ -117,6 +117,28 @@ const formatDate = (date, fmt = 'yyyy/mm/dd' ) =>{ return fmt; } + +const createUUID = (len,radix) => { + const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split(''); + let uuid = [], + i=0; + radix = radix || chars.length; + if(len){ + for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random()*radix]; + }else{ + let r = 0; + uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-'; + uuid[14] = '4'; + for (i = 0; i < 36; i++) { + if (!uuid[i]) { + r = 0 | Math.random()*16; + uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r]; + } + } + } + + return uuid.join(''); +} // 工具类的文件需要把文件提供的工具类统一放最下方做一个统一输出 export { // async await 标识结果处理 @@ -132,5 +154,6 @@ export { // 判断是否double或float IsDouble, // 时间格式化 - formatDate + formatDate, + createUUID } \ No newline at end of file diff --git a/common/utils/websocket.js b/common/utils/websocket.js new file mode 100644 index 0000000..9aa069d --- /dev/null +++ b/common/utils/websocket.js @@ -0,0 +1,106 @@ +/* + * @Author: ch + * @Date: 2022-04-25 14:39:19 + * @LastEditors: ch + * @LastEditTime: 2022-04-26 16:28:01 + * @Description: file content + */ +import store from "../store"; +import {createUUID, formatDate} from '@/common/utils'; +export const MsbWebSkt = uni.connectSocket({ + url : `ws://192.168.10.92:8090/ws?client=${store.state.token}`, // url是websocket连接ip + success: () => { + console.log('websocket连接成功!') + }, + fail: e => { + console.log('连接失败' + e) + } +}); +MsbWebSkt.onClose(()=>{ + console.log('WebSocket关闭...!') +}) +let sessionListFlag = false; +MsbWebSkt.onMessage((res)=>{ + const data = JSON.parse(res.data || {}); + if(data.code === 200){ + const ctx = data.content; + switch(data.traceType){ + // 会话列表 + case 1 : + sessionListFlag = true; + // 来新消息先查会话列表是否一存在,存在则加消息数。不存在则向会话列表加一个会话框 + ctx.sessionVOS.forEach(item => { + item.lastMessage.createTimeStamp = formatDate(item.lastMessage.createTimeStamp, 'mm-dd hh:ii') + item.lastMessage.payload = JSON.parse(item.lastMessage.payload || {}); + let historyData = store.state.sessionData; + let hisIndex = historyData.findIndex(i => i.id === item.id); + if(hisIndex >= 0){ + historyData[hisIndex].lastMessage = item.lastMessage; + historyData[hisIndex].unreadCount++; + store.commit('SET_SESSION_DATA', historyData); + }else{ + item.messageList = []; + store.commit('SET_SESSION_DATA',[...historyData, item]); + } + }); + // 消息总数 + store.commit('SET_SESSION_MSG_COUNT', ctx.totalUnreadCount || 10); + break; + // 历史消息 + case 2 : + 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.payload = JSON.parse(item.payload) + }) + newData[hisIdx].messageList = ctx.concat(newData[hisIdx].messageList); + store.commit('SET_SESSION_DATA', newData); + console.log(newData); + + break; + // 会话消息 + case 7 : + // 没接到会话列表时接到的消息不做处理 + if(!sessionListFlag){ + break; + } + // ctx.forEach(item => { + ctx.payload = JSON.parse(ctx.payload || {}); + let historyData = store.state.sessionData; + const hisIndex = historyData.findIndex(i => i.id === ctx.sessionId); + store.commit('SET_SESSION_MSG_COUNT', store.state.sessionMsgCount + 1); + if(hisIndex >= 0){ + // 存在会话往现有会话增加一条消息 + const curHisData = historyData[hisIndex]; + curHisData.messageList = [ ...(curHisData.messageList || []), ctx] + curHisData.unreadCount++; + store.commit('SET_SESSION_DATA', historyData); + + }else{ + // 会话列表不存在,则创建一个会话 + store.commit('SET_SESSION_DATA',[...historyData, { + fromAvatar : ctx.fromAvatar, + fromId : ctx.fromId, + fromNickname : ctx.fromNickname, + id : ctx.id, + lastMessage : ctx, + messageList : [ctx], + unreadCount : 1 + }]); + } + + // }); + break; + default : + break; + + } + } +}) + +MsbWebSkt.onError(()=>{ + console.log('WebSocket连接错误') +}) + + diff --git a/h5/index.html b/h5/index.html index a24ff63..4a49d12 100644 --- a/h5/index.html +++ b/h5/index.html @@ -1,2 +1,2 @@