进入uniapp
This commit is contained in:
469
im-uniapp/store/index.js
Normal file
469
im-uniapp/store/index.js
Normal file
@@ -0,0 +1,469 @@
|
||||
import http from '@/common/request';
|
||||
import publicFc from '@/common/publicFc';
|
||||
import customHttp from "@/common/customHttp.js"
|
||||
import * as sdk from "@/common/imSdk/lim-sdk.js";
|
||||
import lim from "@/common/imSdk/sdk-bridge.js";
|
||||
import localForage from 'localforage';
|
||||
|
||||
import dayjs from "dayjs"
|
||||
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: {},
|
||||
addressList: [],
|
||||
},
|
||||
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));
|
||||
},
|
||||
update_addressList(state, data) {
|
||||
state.addressList = data;
|
||||
localForage.setItem("addressList", 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 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
|
||||
|
||||
// #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)
|
||||
}
|
||||
})
|
||||
})
|
||||
},
|
||||
|
||||
updateChartHistoryFromServer(context, data) {
|
||||
const _userId = uni.getStorageSync("userId")
|
||||
var retdata = publicFc.getKeyObjectStorage(_userId + '_' + 'chatData')
|
||||
|
||||
var currentUserMsgList = retdata[data.userId].list
|
||||
// if
|
||||
|
||||
_im.getSingleUserInfo(data.userId).then((res) => {
|
||||
var detail = res.data
|
||||
let _msgList = {
|
||||
fromInfo: {
|
||||
nickName: detail.nickName,
|
||||
portrait: detail.photo,
|
||||
userId: detail.userId,
|
||||
userType: detail.userType
|
||||
},
|
||||
groupInfo: {},
|
||||
list: []
|
||||
}
|
||||
var _serverMsgList = []
|
||||
_im.getChatMessageHistoryList(data.userId, 1, 100).then((res) => {
|
||||
res.data.map((item)=>{
|
||||
item.messageBody=JSON.parse(item.messageBody)
|
||||
var obj={
|
||||
content:item.messageBody.content,
|
||||
messageKey:item.messageKey,
|
||||
msgType:item.messageBody.msgType,
|
||||
nickName:_userId==item.fromId?"我":detail.nickName,
|
||||
personId:item.fromId,
|
||||
sendtype:"waiting",
|
||||
time:item.createTime,
|
||||
type:_userId==item.fromId?2:1,
|
||||
userId:item.fromId,
|
||||
windowType:"SINGLE"
|
||||
}
|
||||
/*
|
||||
if(obj.msgType=="CALL_video"){
|
||||
obj.content=JSON.parse(item.messageBody.content)
|
||||
obj.content=JSON.parse(obj.content)
|
||||
}
|
||||
*/
|
||||
_serverMsgList.push(obj)
|
||||
})
|
||||
|
||||
_serverMsgList = _serverMsgList.sort(sortBy('time'))
|
||||
|
||||
_msgList.list=_serverMsgList;
|
||||
|
||||
retdata[data.userId] = _msgList
|
||||
uni.setStorageSync(_userId + '_' + 'chatData', JSON.stringify(
|
||||
retdata));
|
||||
|
||||
|
||||
const _obj=_serverMsgList[_serverMsgList.length-1]
|
||||
|
||||
var _content=JSON.parse(_obj.content)
|
||||
|
||||
if(_obj.msgType=="CALL_video"){
|
||||
_obj.content="【视频通话】"
|
||||
}
|
||||
|
||||
if(_obj.msgType=="CALL_voice"){
|
||||
_obj.content="【语音通话】"
|
||||
}
|
||||
|
||||
context.dispatch('updateChatListInfoById',{
|
||||
userId:data.userId,
|
||||
data:{
|
||||
content:_obj.content,
|
||||
userId: data.userId,
|
||||
personId: _userId,
|
||||
nickName: detail.nickName,
|
||||
portrait: detail.photo,
|
||||
time: dayjs(_obj.createTime).format("YYYY/MM/DD HH:mm:ss"),
|
||||
num: 0,
|
||||
windowType: 'SINGLE',
|
||||
disturb: 'N', //是否静默消息
|
||||
top: 'N', //是否置顶
|
||||
userType: 'SINGLE'
|
||||
}
|
||||
})
|
||||
|
||||
})
|
||||
})
|
||||
},
|
||||
|
||||
getchatDatalist(context) { //获取聊天数据列表
|
||||
const _userId = uni.getStorageSync("userId")
|
||||
var data = publicFc.getKeyObjectStorage(_userId + '_' +
|
||||
'chatData')
|
||||
context.commit('update_chatDatalist', data)
|
||||
},
|
||||
updateChatById(context, data) { //更新聊天数据
|
||||
const _userId = uni.getStorageSync("userId")
|
||||
var getKey = data.userId
|
||||
var retdata = publicFc.getKeyObjectStorage(_userId + '_' +
|
||||
'chatData')
|
||||
|
||||
//TODO 排序方法可以优化
|
||||
data.data = data.data.sort(sortBy('time'))
|
||||
|
||||
retdata[getKey]['list'] = data.data
|
||||
uni.setStorageSync(_userId + '_' + 'chatData', JSON.stringify(
|
||||
retdata));
|
||||
},
|
||||
updateChatObjById(context, data) { //更新聊天数据对象
|
||||
const _userId = uni.getStorageSync("userId")
|
||||
var getKey = data.userId
|
||||
var retdata = publicFc.getKeyObjectStorage(_userId + '_' +
|
||||
'chatData')
|
||||
|
||||
//TODO 排序方法可以优化
|
||||
data.data = data.data.sort(sortBy('time'))
|
||||
retdata[getKey] = data.data
|
||||
uni.setStorageSync(_userId + '_' + 'chatData', JSON.stringify(
|
||||
retdata));
|
||||
},
|
||||
getChatList(context) { //获取聊天记录列表
|
||||
const _userId = uni.getStorageSync("userId")
|
||||
var data = publicFc.getKeyObjectStorage(_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) {
|
||||
const userId = uni.getStorageSync("userId")
|
||||
var getKey = data.userId
|
||||
var retdata = publicFc.getKeyObjectStorage(userId + '_' +
|
||||
'chatlistData')
|
||||
retdata[getKey] = data.data
|
||||
uni.setStorageSync(userId + '_' + 'chatlistData', JSON.stringify(
|
||||
retdata));
|
||||
},
|
||||
createChatObj(context, data) {
|
||||
const _userId = uni.getStorageSync("userId")
|
||||
// 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(_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(_userId + '_' + 'chatlistData', JSON.stringify(
|
||||
data2));
|
||||
// 初始化聊天数据
|
||||
var data1 = publicFc.getKeyObjectStorage(_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(_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) {
|
||||
|
||||
},
|
||||
getAddressList(context) {
|
||||
//获取通讯录,存储通讯录数据到本地
|
||||
customHttp.customHttp.get('/api/im/app/addressList', {
|
||||
params: {},
|
||||
/* 会加在url上 */
|
||||
header: {},
|
||||
/* 会与全局header合并,如有同名属性,局部覆盖全局 */
|
||||
dataType: 'json',
|
||||
// 注:如果局部custom与全局custom有同名属性,则后面的属性会覆盖前面的属性,相当于Object.assign(全局,局部)
|
||||
custom: {
|
||||
target: "znzq"
|
||||
}, // 可以加一些自定义参数,在拦截器等地方使用。比如这里我加了一个auth,可在拦截器里拿到,如果true就传token
|
||||
}).then(res => {
|
||||
console.log("=========获取到数据")
|
||||
console.log(res)
|
||||
if (res.statusCode == 200) {
|
||||
if (res.data.code == 200) {
|
||||
var records = res.data.result.records
|
||||
context.state.addressList = records
|
||||
}
|
||||
}
|
||||
}).catch(err => {
|
||||
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
export default store
|
||||
Reference in New Issue
Block a user