移动端聊天实现
This commit is contained in:
@@ -23,20 +23,10 @@
|
||||
// todo 下列两行
|
||||
uni.setStorageSync('device', 'H5');
|
||||
uni.setStorageSync('version', versionName);
|
||||
this.$http.request({
|
||||
url: '/common/getVersion',
|
||||
success: (res) => {
|
||||
if(res.data.data.upgrade=='Y'){
|
||||
console.log(
|
||||
`%c 有新版本 `+res.data.data.version,
|
||||
'background:#007aff ;padding: 1px; border-radius: 0 3px 3px 0; color: #fff; font-weight: bold;'
|
||||
)
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// #endif
|
||||
console.log('App Launch')
|
||||
let token= uni.getStorageSync('Authorization');
|
||||
let token= uni.getStorageSync('imUserSign');
|
||||
if (!token) {
|
||||
//不存在则跳转至登录页
|
||||
// #ifdef APP-PLUS
|
||||
@@ -104,29 +94,7 @@
|
||||
})
|
||||
}
|
||||
// #ifdef APP-PLUS
|
||||
//升级检测
|
||||
uni.getSystemInfo({
|
||||
success: (res)=> {
|
||||
uni.setStorageSync('device', res.platform);
|
||||
plus.runtime.getProperty(plus.runtime.appid, (widgetInfo)=> {
|
||||
uni.setStorageSync('version', widgetInfo.version);
|
||||
this.$http.request({
|
||||
url: '/common/getVersion',
|
||||
success: (res) => {
|
||||
if(res.data.data.upgrade=='Y'){
|
||||
appUpgrade.init({
|
||||
titleText: '版本更新'+res.data.data.version,
|
||||
packageUrl:res.data.data.url,
|
||||
content: res.data.data.content,
|
||||
forceUpgrade:res.data.data.forceUpgrade=='Y' ? true : false
|
||||
});
|
||||
appUpgrade.show();
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
uni.onNetworkStatusChange( (res)=> {
|
||||
if(res.isConnected){
|
||||
this.$store.dispatch('get_UserInfo')
|
||||
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -2,6 +2,9 @@ import http from '@/common/request'
|
||||
import browser from '@/common/browser'
|
||||
import store from '../store'
|
||||
import pinyin from '@/common/pinyin.js';
|
||||
|
||||
import * as dayjs from 'dayjs'
|
||||
|
||||
export default {
|
||||
// 获取字典
|
||||
getdict(dict) {
|
||||
@@ -464,6 +467,12 @@ export default {
|
||||
store.dispatch('getChatList');
|
||||
var chatWindowData = store.state.chatDatalist[userId].list
|
||||
var chatListInfo = store.state.chatlist[userId]
|
||||
|
||||
var data2 = this.getKeyObjectStorage(store.state.userInfo.userId + '_' +
|
||||
'chatlistData')
|
||||
// var _num=data2[userId].num||
|
||||
console.log("===========data2:",JSON.stringify(data2[userId]))
|
||||
|
||||
// 找到数组中对象属性值一样的对象并返回
|
||||
function arrfindobject({
|
||||
arr,
|
||||
@@ -508,11 +517,11 @@ export default {
|
||||
nickName: fromInfo.nickName,
|
||||
portrait: fromInfo.portrait,
|
||||
content: msgTypeLabel,
|
||||
time: time,
|
||||
time: dayjs(time).format("YYYY/MM/DD HH:mm:ss"),
|
||||
num: disturb == 'Y' ? 'dot' : (chatListInfo.num ? chatListInfo.num + 1 : 1),
|
||||
windowType: windowType,
|
||||
disturb: disturb, //是否静默消息
|
||||
top: top, //是否置顶
|
||||
disturb: "N", //是否静默消息
|
||||
top: "N", //是否置顶
|
||||
userType: fromInfo.userType
|
||||
}
|
||||
}
|
||||
@@ -526,7 +535,7 @@ export default {
|
||||
time: time,
|
||||
num: disturb == 'Y' ? 'dot' : (chatListInfo.num ? chatListInfo.num + 1 : 1),
|
||||
windowType: windowType,
|
||||
disturb: disturb, //是否静默消息
|
||||
disturb: "N", //是否静默消息
|
||||
top: top, //是否置顶
|
||||
userType: 'GROUP'
|
||||
}
|
||||
@@ -990,6 +999,31 @@ export default {
|
||||
});
|
||||
// #endif
|
||||
},
|
||||
formatMessage(msg){
|
||||
msg.pushType="MSG"
|
||||
const _data=JSON.parse(msg.messageBody)
|
||||
msg.msgContent={
|
||||
msgType:"TEXT",
|
||||
content:_data.content,
|
||||
disturb:false,
|
||||
top:false,
|
||||
}
|
||||
|
||||
msg.windowType="SINGLE"
|
||||
msg.createTime=msg.messageTime;
|
||||
msg.userId=msg.fromId;
|
||||
msg.fromInfo={
|
||||
userId:msg.fromId,
|
||||
userType:1,
|
||||
nickName:msg.fromId,
|
||||
portrait:"https://shudong2022.oss-cn-hangzhou.aliyuncs.com/common/user/logo.png"
|
||||
};
|
||||
msg.groupInfo={};
|
||||
msg.msgId=msg.messageKey;
|
||||
|
||||
return msg
|
||||
|
||||
},
|
||||
debounce(func, wait = 1000, immediate = true) {
|
||||
/**
|
||||
* @desc 函数防抖
|
||||
|
||||
@@ -4,9 +4,10 @@ let retimer = null
|
||||
import store from '../store'
|
||||
|
||||
import * as sdk from "@/common/imSdk/lim-sdk.js";
|
||||
|
||||
import lim from "@/common/imSdk/sdk-bridge.js";
|
||||
|
||||
import publicFc from '@/common/publicFc';
|
||||
|
||||
export default {
|
||||
socketTask: null,
|
||||
// 连接WebSocket
|
||||
@@ -28,42 +29,40 @@ export default {
|
||||
onSocketReConnectSuccessEvent: () => {
|
||||
console.log("重连成功");
|
||||
},
|
||||
onOfflineMessage:(e)=>{
|
||||
onOfflineMessage: (e) => {
|
||||
console.log("====================拉取到离线消息")
|
||||
console.log(e)
|
||||
setTimeout(function() {
|
||||
if (e.length > 0) {
|
||||
e.map((msg) => {
|
||||
console.log(msg)
|
||||
setTimeout(()=>{
|
||||
const _data = publicFc.formatMessage(msg)
|
||||
uni.$emit('onP2PMessage', _data);
|
||||
store.dispatch('onP2PMessage', _data)
|
||||
},500)
|
||||
})
|
||||
}
|
||||
}, 2000);
|
||||
|
||||
},
|
||||
onTestMessage: (e) => {
|
||||
console.log("onTestMessage :" + e);
|
||||
store.dispatch('onP2PMessage',e)
|
||||
console.log("onTestMessage :");
|
||||
store.dispatch('onP2PMessage', e)
|
||||
},
|
||||
onP2PMessage: (e) => {
|
||||
console.log("onP2PMessage :" + e);
|
||||
console.log("onP2PMessag e :::");
|
||||
console.log(JSON.parse(e))
|
||||
e = JSON.parse(e)
|
||||
e.data.pushType="MSG"
|
||||
const _data=JSON.parse(e.data.messageBody)
|
||||
e.data.msgContent={
|
||||
msgType:"TEXT",
|
||||
content:_data.content,
|
||||
disturb:false,
|
||||
top:false,
|
||||
}
|
||||
|
||||
e.data.windowType="SINGLE"
|
||||
e.data.createTime=e.data.messageTime;
|
||||
e.data.userId=e.data.fromId;
|
||||
e.data.fromInfo={
|
||||
userId:e.data.fromId
|
||||
};
|
||||
e.data.groupInfo={};
|
||||
e.data.msgId=e.data.messageId;
|
||||
|
||||
uni.$emit('onP2PMessage', e.data);
|
||||
store.dispatch('onP2PMessage',e.data)
|
||||
|
||||
const _data = publicFc.formatMessage(e.data)
|
||||
|
||||
uni.$emit('onP2PMessage', _data);
|
||||
store.dispatch('onP2PMessage', _data)
|
||||
|
||||
},
|
||||
onLogin: (uid) => {
|
||||
console.log("用户" + uid + "登陆sdk成功");
|
||||
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
@@ -79,7 +78,7 @@ export default {
|
||||
|
||||
console.log(lim)
|
||||
|
||||
// 防止多次建立ws链接,貌似还没有生效,需要检查
|
||||
// 防止多次建立ws链接,貌似还没有生效,需要检查
|
||||
if (this.socketTask != null) {
|
||||
return false
|
||||
}
|
||||
@@ -92,12 +91,12 @@ export default {
|
||||
console.log(this.socketTask)
|
||||
|
||||
console.log('sdk 成功连接的回调, 可以使用 sdk 请求数据了.');
|
||||
sdk.getAllFriend().then(res => {
|
||||
console.warn(res)
|
||||
})
|
||||
sdk.getUserSequence().then(res => {
|
||||
console.warn(res)
|
||||
})
|
||||
// sdk.getAllFriend().then(res => {
|
||||
// console.warn(res)
|
||||
// })
|
||||
// sdk.getUserSequence().then(res => {
|
||||
// console.warn(res)
|
||||
// })
|
||||
|
||||
sdk.getUserConversationList(0).then(res => {
|
||||
console.warn(res)
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
"author": "zmm2113@qq.com",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"dayjs": "^1.11.10",
|
||||
"localforage": "^1.10.0"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,283 +1,343 @@
|
||||
import http from '@/common/request';
|
||||
import publicFc from '@/common/publicFc';
|
||||
|
||||
import * as sdk from "@/common/imSdk/lim-sdk.js";
|
||||
import lim from "@/common/imSdk/sdk-bridge.js";
|
||||
|
||||
const _im = lim.lim.im
|
||||
|
||||
function sortBy(field) {
|
||||
//根据传过来的字段进行排序
|
||||
return (x, y) => {
|
||||
return x[field] - y[field]
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// #ifndef VUE3
|
||||
import Vue from 'vue'
|
||||
import Vuex from 'vuex'
|
||||
Vue.use(Vuex)
|
||||
const store = new Vuex.Store({
|
||||
// #endif
|
||||
// #endif
|
||||
|
||||
|
||||
import * as sdk from "@/common/imSdk/lim-sdk.js";
|
||||
import lim from "@/common/imSdk/sdk-bridge.js";
|
||||
|
||||
const _im=lim.lim.im
|
||||
|
||||
// #ifdef VUE3
|
||||
import { createStore } from 'vuex'
|
||||
const store = createStore({
|
||||
// #endif
|
||||
state: {
|
||||
watermark:'HS-IM',
|
||||
userInfo:'',
|
||||
chatlist:[],
|
||||
chatDatalist:[],
|
||||
chatListNum:0,
|
||||
chatDataState:1,
|
||||
chatDataUserId:'',
|
||||
topicReply:{},
|
||||
topicRed:{},
|
||||
friendApply:{}
|
||||
},
|
||||
mutations: {
|
||||
update_UserInfo(state, data) {//个人信息
|
||||
state.userInfo = JSON.parse(data)
|
||||
},
|
||||
update_ChatList(state, data) {
|
||||
state.chatlist = data
|
||||
},
|
||||
update_chatDatalist(state, data) {
|
||||
state.chatDatalist = data
|
||||
},
|
||||
update_topicReply(state, data) {
|
||||
state.topicReply = data
|
||||
uni.setStorageSync('topicReply', JSON.stringify(data));
|
||||
},
|
||||
update_topicRed(state, data) {
|
||||
state.topicRed = data
|
||||
uni.setStorageSync('topicRed', JSON.stringify(data));
|
||||
},
|
||||
update_friendApply(state, data) {
|
||||
state.friendApply = data
|
||||
uni.setStorageSync('friendApply', JSON.stringify(data));
|
||||
}
|
||||
},
|
||||
actions: {
|
||||
onP2PMessage(context,data){
|
||||
console.log("========================store 收到信息")
|
||||
console.log("========================store 收到信息")
|
||||
console.log("========================store 收到信息")
|
||||
console.error(data)
|
||||
publicFc.getPush(data)
|
||||
|
||||
},
|
||||
sendMsg(context,data){
|
||||
console.log("========================")
|
||||
console.log("========================")
|
||||
console.log("========================")
|
||||
console.error(data)
|
||||
_im.sendP2PMessage(_im.createP2PTextMessage(data.userId,data.data.content))
|
||||
},
|
||||
updateChatDataState(context,data){
|
||||
context.state.chatDataState++
|
||||
context.state.chatDataUserId=data
|
||||
},
|
||||
tabBarpull(context){
|
||||
context.dispatch('get_UserInfo').then(res=>{
|
||||
context.dispatch('getChatList')
|
||||
// 朋友圈新消息
|
||||
var topicReply=publicFc.getKeyObjectStorage('topicReply')
|
||||
context.state.topicReply=topicReply
|
||||
// 新朋友圈
|
||||
var topicRed=publicFc.getKeyObjectStorage('topicRed')
|
||||
context.state.topicRed=topicRed
|
||||
// 新朋友
|
||||
var friendApply=publicFc.getKeyObjectStorage('friendApply')
|
||||
context.state.friendApply=friendApply
|
||||
var chatListNum=context.state.chatListNum ? context.state.chatListNum : 0
|
||||
var topicReplyCount=topicReply.count ? topicReply.count : 0
|
||||
var friendApplyCount=friendApply.count ? friendApply.count : 0
|
||||
var sumCount=chatListNum+topicReplyCount+friendApplyCount
|
||||
// #ifdef APP-PLUS
|
||||
plus.runtime.setBadgeNumber(sumCount);
|
||||
// #ifdef VUE3
|
||||
import {
|
||||
createStore
|
||||
} from 'vuex'
|
||||
const store = createStore({
|
||||
// #endif
|
||||
})
|
||||
},
|
||||
get_UserInfo (context) {
|
||||
// TODO 改用SDK的更新用户数据
|
||||
return new Promise((resolve, reject) => {
|
||||
const userId = uni.getStorageSync("userId")
|
||||
|
||||
lim.lim.im.getSingleUserInfo(userId).then((res)=>{
|
||||
if(res.code==200){
|
||||
context.commit('update_UserInfo',JSON.stringify(res.data))
|
||||
resolve(res.data)
|
||||
}else{
|
||||
reject(res.data)
|
||||
}
|
||||
})
|
||||
})
|
||||
},
|
||||
getchatDatalist(context) {//获取聊天数据列表
|
||||
var data=publicFc.getKeyObjectStorage(context.state.userInfo.userId+'_'+'chatData')
|
||||
context.commit('update_chatDatalist',data)
|
||||
},
|
||||
updateChatById(context,data) {//更新聊天数据
|
||||
var getKey=data.userId
|
||||
var retdata=publicFc.getKeyObjectStorage(context.state.userInfo.userId+'_'+'chatData')
|
||||
retdata[getKey]['list']=data.data
|
||||
uni.setStorageSync(context.state.userInfo.userId+'_'+'chatData', JSON.stringify(retdata));
|
||||
},
|
||||
updateChatObjById(context,data) {//更新聊天数据对象
|
||||
var getKey=data.userId
|
||||
var retdata=publicFc.getKeyObjectStorage(context.state.userInfo.userId+'_'+'chatData')
|
||||
retdata[getKey]=data.data
|
||||
uni.setStorageSync(context.state.userInfo.userId+'_'+'chatData', JSON.stringify(retdata));
|
||||
},
|
||||
getChatList(context) {//获取聊天记录列表
|
||||
var data=publicFc.getKeyObjectStorage(context.state.userInfo.userId+'_'+'chatlistData')
|
||||
var sum=0
|
||||
for(var key in data){
|
||||
sum+=data[key].num
|
||||
}
|
||||
context.state.chatListNum=sum
|
||||
context.commit('update_ChatList',data)
|
||||
},
|
||||
updateChatListInfoById(context,data) {//修改聊天记录
|
||||
console.log("修改聊天记录 2")
|
||||
var getKey=data.userId
|
||||
var retdata=publicFc.getKeyObjectStorage(context.state.userInfo.userId+'_'+'chatlistData')
|
||||
retdata[getKey]=data.data
|
||||
uni.setStorageSync(context.state.userInfo.userId+'_'+'chatlistData', JSON.stringify(retdata));
|
||||
},
|
||||
createChatObj(context,data){
|
||||
console.log("修改聊天记录 3")
|
||||
console.log("context.state.userInfo.userId:",context.state.userInfo.userId)
|
||||
console.log("data.userId:",data.userId)
|
||||
var userId=data.userId
|
||||
var windowType=data.windowType
|
||||
state: {
|
||||
watermark: 'HS-IM',
|
||||
userInfo: '',
|
||||
chatlist: [],
|
||||
chatDatalist: [],
|
||||
chatListNum: 0,
|
||||
chatDataState: 1,
|
||||
chatDataUserId: '',
|
||||
topicReply: {},
|
||||
topicRed: {},
|
||||
friendApply: {}
|
||||
},
|
||||
mutations: {
|
||||
update_UserInfo(state, data) { //个人信息
|
||||
state.userInfo = JSON.parse(data)
|
||||
},
|
||||
update_ChatList(state, data) {
|
||||
state.chatlist = data
|
||||
},
|
||||
update_chatDatalist(state, data) {
|
||||
state.chatDatalist = data
|
||||
},
|
||||
update_topicReply(state, data) {
|
||||
state.topicReply = data
|
||||
uni.setStorageSync('topicReply', JSON.stringify(data));
|
||||
},
|
||||
update_topicRed(state, data) {
|
||||
state.topicRed = data
|
||||
uni.setStorageSync('topicRed', JSON.stringify(data));
|
||||
},
|
||||
update_friendApply(state, data) {
|
||||
state.friendApply = data
|
||||
uni.setStorageSync('friendApply', JSON.stringify(data));
|
||||
}
|
||||
},
|
||||
actions: {
|
||||
onP2PMessage(context, data) {
|
||||
console.log("========================store 收到信息")
|
||||
console.log(data)
|
||||
publicFc.getPush(data)
|
||||
|
||||
//初始化聊天记录
|
||||
var data2=publicFc.getKeyObjectStorage(context.state.userInfo.userId+'_'+'chatlistData')
|
||||
if(!data2[userId]){
|
||||
data2[userId]=new Object()
|
||||
}
|
||||
uni.setStorageSync(context.state.userInfo.userId+'_'+'chatlistData', JSON.stringify(data2));
|
||||
// 初始化聊天数据
|
||||
var data1=publicFc.getKeyObjectStorage(context.state.userInfo.userId+'_'+'chatData')
|
||||
return new Promise((resolve, reject)=>{
|
||||
if(!data1[userId]){
|
||||
console.log('初始化聊天')
|
||||
switch (windowType){
|
||||
case 'SINGLE':
|
||||
|
||||
// lim.lim.im.getSingleUserInfo(userId).then(res=>{
|
||||
// console.warn(res)
|
||||
// })
|
||||
|
||||
_im.getSingleUserInfo(userId).then((res)=>{
|
||||
console.log(res.data)
|
||||
console.log(res.data)
|
||||
console.log(res.data)
|
||||
console.log(res.data)
|
||||
},
|
||||
sendMsg(context, data) {
|
||||
console.log("========================")
|
||||
// console.log("========================")
|
||||
// console.log("========================")
|
||||
// console.error(data)
|
||||
_im.sendP2PMessage(_im.createP2PTextMessage(data.userId, data.data.content))
|
||||
},
|
||||
sendP2PMessageReciveAck(context, data) {
|
||||
_im.sendP2PMessageReciveAck(data)
|
||||
},
|
||||
sendMsgReadedReceipt(context, data) {
|
||||
_im.sendP2PMessageReadedReceipt(data)
|
||||
},
|
||||
updateChatDataState(context, data) {
|
||||
context.state.chatDataState++
|
||||
context.state.chatDataUserId = data
|
||||
},
|
||||
tabBarpull(context) {
|
||||
context.dispatch('get_UserInfo').then(res => {
|
||||
context.dispatch('getChatList')
|
||||
// 朋友圈新消息
|
||||
var topicReply = publicFc.getKeyObjectStorage('topicReply')
|
||||
context.state.topicReply = topicReply
|
||||
// 新朋友圈
|
||||
var topicRed = publicFc.getKeyObjectStorage('topicRed')
|
||||
context.state.topicRed = topicRed
|
||||
// 新朋友
|
||||
var friendApply = publicFc.getKeyObjectStorage('friendApply')
|
||||
context.state.friendApply = friendApply
|
||||
var chatListNum = context.state.chatListNum ? context.state.chatListNum : 0
|
||||
var topicReplyCount = topicReply.count ? topicReply.count : 0
|
||||
var friendApplyCount = friendApply.count ? friendApply.count : 0
|
||||
var sumCount = chatListNum + topicReplyCount + friendApplyCount
|
||||
// #ifdef APP-PLUS
|
||||
plus.runtime.setBadgeNumber(sumCount);
|
||||
// #endif
|
||||
})
|
||||
},
|
||||
get_UserInfo(context) {
|
||||
// TODO 改用SDK的更新用户数据
|
||||
return new Promise((resolve, reject) => {
|
||||
const userId = uni.getStorageSync("userId")
|
||||
|
||||
lim.lim.im.getSingleUserInfo(userId).then((res) => {
|
||||
if (res.code == 200) {
|
||||
var detail=res.data
|
||||
data1[userId]={
|
||||
fromInfo:{
|
||||
nickName: detail.nickName,
|
||||
portrait: detail.photo,
|
||||
userId: detail.userId,
|
||||
userType:detail.userType
|
||||
},
|
||||
groupInfo:{},
|
||||
list:[]
|
||||
}
|
||||
|
||||
uni.setStorageSync(context.state.userInfo.userId+'_'+'chatData', JSON.stringify(data1));
|
||||
// 创建记录
|
||||
var msgList = {
|
||||
userId: detail.userId,
|
||||
personId: context.state.userInfo.userId,
|
||||
nickName: detail.nickName,
|
||||
portrait: detail.photo,
|
||||
content: '',
|
||||
time: publicFc.getNewDate('format',true),
|
||||
num: 0,
|
||||
windowType: 'SINGLE',
|
||||
disturb:'N',//是否静默消息
|
||||
top:'N',//是否置顶
|
||||
userType:'SINGLE'
|
||||
}
|
||||
context.dispatch('updateChatListInfoById', {
|
||||
userId: detail.userId,
|
||||
data: msgList
|
||||
});
|
||||
resolve({
|
||||
msg:'创建成功',
|
||||
data:data1[userId]
|
||||
})
|
||||
}else{
|
||||
reject({
|
||||
msg:'创建失败'
|
||||
});
|
||||
context.commit('update_UserInfo', JSON.stringify(res.data))
|
||||
resolve(res.data)
|
||||
} else {
|
||||
reject(res.data)
|
||||
}
|
||||
}
|
||||
);
|
||||
break;
|
||||
case 'GROUP':
|
||||
http.request({
|
||||
url: '/group/getInfo/'+userId,
|
||||
complete:(res)=>{
|
||||
if (res.data.code == 200) {
|
||||
var detail=res.data.data
|
||||
var portraits=[]
|
||||
for (var i = 0; i < detail.user.length; i++) {
|
||||
portraits.push(detail.user[i].portrait)
|
||||
}
|
||||
data1[userId]={
|
||||
fromInfo:{},
|
||||
groupInfo:{
|
||||
nickName: detail.group.name,
|
||||
portrait: JSON.stringify(portraits),
|
||||
userId: detail.group.groupId,
|
||||
},
|
||||
list:[]
|
||||
}
|
||||
uni.setStorageSync(context.state.userInfo.userId+'_'+'chatData', JSON.stringify(data1));
|
||||
// 创建记录
|
||||
var msgList = {
|
||||
userId: detail.group.groupId,
|
||||
personId: context.state.userInfo.userId,
|
||||
nickName: detail.group.name,
|
||||
portrait: JSON.stringify(portraits),
|
||||
content: '',
|
||||
time: publicFc.getNewDate('format',true),
|
||||
num: 0,
|
||||
windowType: 'GROUP',
|
||||
disturb:'N',//是否静默消息
|
||||
top:'N',//是否置顶
|
||||
userType:'GROUP'
|
||||
}
|
||||
context.dispatch('updateChatListInfoById', {
|
||||
userId: detail.group.groupId,
|
||||
data: msgList
|
||||
});
|
||||
resolve({
|
||||
msg:'创建成功',
|
||||
data:data1[userId]
|
||||
})
|
||||
}else{
|
||||
reject({
|
||||
msg:'创建失败'
|
||||
});
|
||||
})
|
||||
})
|
||||
},
|
||||
getchatDatalist(context) { //获取聊天数据列表
|
||||
var data = publicFc.getKeyObjectStorage(context.state.userInfo.userId + '_' +
|
||||
'chatData')
|
||||
context.commit('update_chatDatalist', data)
|
||||
},
|
||||
updateChatById(context, data) { //更新聊天数据
|
||||
var getKey = data.userId
|
||||
var retdata = publicFc.getKeyObjectStorage(context.state.userInfo.userId + '_' +
|
||||
'chatData')
|
||||
|
||||
//TODO 排序方法可以优化
|
||||
data.data = data.data.sort(sortBy('time'))
|
||||
|
||||
retdata[getKey]['list'] = data.data
|
||||
uni.setStorageSync(context.state.userInfo.userId + '_' + 'chatData', JSON.stringify(
|
||||
retdata));
|
||||
},
|
||||
updateChatObjById(context, data) { //更新聊天数据对象
|
||||
var getKey = data.userId
|
||||
var retdata = publicFc.getKeyObjectStorage(context.state.userInfo.userId + '_' +
|
||||
'chatData')
|
||||
|
||||
//TODO 排序方法可以优化
|
||||
data.data = data.data.sort(sortBy('time'))
|
||||
retdata[getKey] = data.data
|
||||
uni.setStorageSync(context.state.userInfo.userId + '_' + 'chatData', JSON.stringify(
|
||||
retdata));
|
||||
},
|
||||
getChatList(context) { //获取聊天记录列表
|
||||
var data = publicFc.getKeyObjectStorage(context.state.userInfo.userId + '_' +
|
||||
'chatlistData')
|
||||
var sum = 0
|
||||
for (var key in data) {
|
||||
sum += data[key].num
|
||||
}
|
||||
context.state.chatListNum = sum
|
||||
context.commit('update_ChatList', data)
|
||||
},
|
||||
updateChatListInfoById(context, data) { //修改聊天记录
|
||||
console.log("*********************** 修改聊天记录 2 ********************")
|
||||
console.log("*********************** 修改聊天记录 2 ********************")
|
||||
console.log("*********************** 修改聊天记录 2 ********************")
|
||||
console.log(data)
|
||||
var getKey = data.userId
|
||||
var retdata = publicFc.getKeyObjectStorage(context.state.userInfo.userId + '_' +
|
||||
'chatlistData')
|
||||
console.log(retdata[getKey])
|
||||
retdata[getKey] = data.data
|
||||
uni.setStorageSync(context.state.userInfo.userId + '_' + 'chatlistData', JSON.stringify(
|
||||
retdata));
|
||||
},
|
||||
createChatObj(context, data) {
|
||||
console.log("初始化聊天记录")
|
||||
// console.log("context.state.userInfo.userId:", context.state.userInfo.userId)
|
||||
// console.log("data.userId:", data.userId)
|
||||
var userId = data.userId
|
||||
var windowType = data.windowType
|
||||
|
||||
//初始化聊天记录
|
||||
var data2 = publicFc.getKeyObjectStorage(context.state.userInfo.userId + '_' +
|
||||
'chatlistData')
|
||||
|
||||
// console.log("===========data2:",JSON.stringify(data2[userId]))
|
||||
|
||||
var _num=0
|
||||
console.error(data2[userId])
|
||||
if (!data2[userId]) {
|
||||
data2[userId] = new Object()
|
||||
}else{
|
||||
console.warn(data2[userId].num)
|
||||
// _num=data2[userId].num+1
|
||||
}
|
||||
uni.setStorageSync(context.state.userInfo.userId + '_' + 'chatlistData', JSON.stringify(
|
||||
data2));
|
||||
// 初始化聊天数据
|
||||
var data1 = publicFc.getKeyObjectStorage(context.state.userInfo.userId + '_' +
|
||||
'chatData')
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!data1[userId]) {
|
||||
console.log('初始化聊天')
|
||||
switch (windowType) {
|
||||
case 'SINGLE':
|
||||
|
||||
// lim.lim.im.getSingleUserInfo(userId).then(res=>{
|
||||
// console.warn(res)
|
||||
// })
|
||||
|
||||
_im.getSingleUserInfo(userId).then((res) => {
|
||||
// console.log(res.data)
|
||||
|
||||
if (res.code == 200) {
|
||||
var detail = res.data
|
||||
data1[userId] = {
|
||||
fromInfo: {
|
||||
nickName: detail.nickName,
|
||||
portrait: detail.photo,
|
||||
userId: detail.userId,
|
||||
userType: detail.userType
|
||||
},
|
||||
groupInfo: {},
|
||||
list: []
|
||||
}
|
||||
|
||||
uni.setStorageSync(context.state.userInfo
|
||||
.userId + '_' + 'chatData', JSON
|
||||
.stringify(data1));
|
||||
// 创建记录
|
||||
console.log("============= 创建记录 ")
|
||||
|
||||
|
||||
|
||||
var msgList = {
|
||||
userId: detail.userId,
|
||||
personId: context.state.userInfo.userId,
|
||||
nickName: detail.nickName,
|
||||
portrait: detail.photo,
|
||||
content: '',
|
||||
time: publicFc.getNewDate('format',
|
||||
true),
|
||||
num: 0,
|
||||
windowType: 'SINGLE',
|
||||
disturb: 'N', //是否静默消息
|
||||
top: 'N', //是否置顶
|
||||
userType: 'SINGLE'
|
||||
}
|
||||
context.dispatch('updateChatListInfoById', {
|
||||
userId: detail.userId,
|
||||
data: msgList
|
||||
});
|
||||
resolve({
|
||||
msg: '创建成功',
|
||||
data: data1[userId]
|
||||
})
|
||||
} else {
|
||||
reject({
|
||||
msg: '创建失败'
|
||||
});
|
||||
}
|
||||
});
|
||||
break;
|
||||
case 'GROUP':
|
||||
http.request({
|
||||
url: '/group/getInfo/' + userId,
|
||||
complete: (res) => {
|
||||
if (res.data.code == 200) {
|
||||
var detail = res.data.data
|
||||
var portraits = []
|
||||
for (var i = 0; i < detail.user
|
||||
.length; i++) {
|
||||
portraits.push(detail.user[i]
|
||||
.portrait)
|
||||
}
|
||||
data1[userId] = {
|
||||
fromInfo: {},
|
||||
groupInfo: {
|
||||
nickName: detail.group.name,
|
||||
portrait: JSON.stringify(
|
||||
portraits),
|
||||
userId: detail.group
|
||||
.groupId,
|
||||
},
|
||||
list: []
|
||||
}
|
||||
uni.setStorageSync(context.state
|
||||
.userInfo.userId + '_' +
|
||||
'chatData', JSON.stringify(
|
||||
data1));
|
||||
// 创建记录
|
||||
var msgList = {
|
||||
userId: detail.group.groupId,
|
||||
personId: context.state.userInfo
|
||||
.userId,
|
||||
nickName: detail.group.name,
|
||||
portrait: JSON.stringify(
|
||||
portraits),
|
||||
content: '',
|
||||
time: publicFc.getNewDate(
|
||||
'format', true),
|
||||
num: 0,
|
||||
windowType: 'GROUP',
|
||||
disturb: 'N', //是否静默消息
|
||||
top: 'N', //是否置顶
|
||||
userType: 'GROUP'
|
||||
}
|
||||
context.dispatch(
|
||||
'updateChatListInfoById', {
|
||||
userId: detail.group
|
||||
.groupId,
|
||||
data: msgList
|
||||
});
|
||||
resolve({
|
||||
msg: '创建成功',
|
||||
data: data1[userId]
|
||||
})
|
||||
} else {
|
||||
reject({
|
||||
msg: '创建失败'
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
resolve({
|
||||
msg: '已存在',
|
||||
data: data1[userId]
|
||||
})
|
||||
}
|
||||
});
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
},
|
||||
createChat(context, data) {
|
||||
|
||||
}
|
||||
}else{
|
||||
resolve({
|
||||
msg:'已存在',
|
||||
data:data1[userId]
|
||||
})
|
||||
}
|
||||
});
|
||||
},
|
||||
createChat(context,data){
|
||||
|
||||
}
|
||||
}
|
||||
})
|
||||
export default store
|
||||
})
|
||||
export default store
|
||||
@@ -174,7 +174,7 @@
|
||||
onLoad(e) {
|
||||
|
||||
// // 监听接收聊天信息
|
||||
// uni.$on('onP2PMessage', this.onMessage)
|
||||
uni.$on('onP2PMessage', this.onMessage)
|
||||
|
||||
this.talkTo = e;
|
||||
console.log(this.talkTo)
|
||||
@@ -232,8 +232,6 @@
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
},
|
||||
onPageScroll() {
|
||||
this.clickToSubmitSure();
|
||||
@@ -267,7 +265,19 @@
|
||||
},
|
||||
methods: {
|
||||
onMessage(message) {
|
||||
|
||||
//判断当前是单聊还是群聊
|
||||
if (this.talkTo.windowType != 'SINGLE') {
|
||||
return;
|
||||
}
|
||||
//如果是单聊,则判断当前消息发送人是不是当前窗口的聊天对象
|
||||
if(this.talkTo.userId!=message.fromId){
|
||||
return;
|
||||
}
|
||||
console.log("message 聊天页收到回调")
|
||||
console.log(message)
|
||||
//如果当前消息发送人和当前窗口的聊天对象一致,则发送已读回执
|
||||
console.log("########### 此时需要发送已读回执 ###########")
|
||||
this.$store.dispatch('sendP2PMessageReciveAck',message)
|
||||
},
|
||||
addMsg(e) {
|
||||
this.msg += e;
|
||||
|
||||
@@ -158,12 +158,7 @@
|
||||
this.$store.dispatch('tabBarpull');
|
||||
},
|
||||
mounted() {
|
||||
|
||||
|
||||
this.$socketTask.connectSocket()
|
||||
|
||||
|
||||
|
||||
},
|
||||
methods: {
|
||||
longpressItem(e, i, v) {
|
||||
|
||||
Reference in New Issue
Block a user