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 // #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: { createRoom(context,room){ _im.createRoom(room).then((res) => { }) }, joinRoom(context,room){ _im.joinRoom(room).then((res) => { }) }, getRoomInfo(context,room){ }, onP2PMessage(context, data) { // console.log("========================store 收到信息") publicFc.getPush(data) }, sendMsg(context, data) { // console.log("======================== store:sendMsg") // console.log(data) // console.log("========================") // console.log("========================") // console.error(data) _im.sendP2PMessage(_im.createP2PTextMessage(data.userId, data.data)) }, 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) { 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') //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 if (!data2[userId]) { data2[userId] = new Object() }else{ // _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]) { 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] }) } }); }, createChat(context, data) { } } }) export default store