index.js
3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import Vue from 'vue'
import Vuex from 'vuex'
import tim from '../commen/tim/tim'
Vue.use(Vuex)
const store=new Vuex.Store({
state:{
landParam:{
page:'',
per_page:15,
province:'',
city:'',
cityTxt:'定位中',
keyword:'',
star:'',
starName:'星级',
longitude:'',
latitude:''
},
noReadNum:0,
attrVal:[],//detailAttr里的valindex [0,0]
attrTxt:'请选择商品规格尺寸',
refreshStory:false,
isLogin:false,
userinfo:uni.getStorageSync('userinfo')?JSON.parse(uni.getStorageSync('userinfo')):{},
isSDKReady: false ,// TIM SDK 是否 ready
conversationActive:{}, //聊天进行中的会话
toUserId:'', //聊天对象id
conversationList:[], //会话列表
currentMessageList:[], //消息列表
},
mutations:{
setRefreshStory(state,data){
state.refreshStory = data
},
defaultRefreshStory(state){
state.refreshStory = false
},
setLandParam(state,data){
state.landParam = data
},
defaultLandParam(){
state.landParam = {
province:'',
city:'',
keyword:'',
star:'',
longitude:'',
latitude:''
}
},
setNoReadNum(state,data){
state.noReadNum=data.noReadNum
},
setAttr(state,data){
state.attrVal=data.attrVal
state.attrTxt=data.attrTxt
},
defaultAttr(state){
state.attrVal=[]
state.attrTxt='请选择商品规格尺寸'
},
login(state,data){
state.userinfo=data;
uni.setStorageSync('userinfo',JSON.stringify(data))
},
logout(state){
state.userinfo=[
];
console.log(state.userinfo)
uni.setStorageSync('userinfo','')
uni.setStorageSync('token','')
},
setHead(state,data){
state.userinfo.image=data;
uni.setStorageSync('userinfo',JSON.stringify(state))
},
setNickname(state,data){
state.userinfo.nickname=data;
uni.setStorageSync('userinfo',JSON.stringify(state))
},
//更新登录状态
toggleIsLogin(state, isLogin) {
state.isLogin = typeof isLogin === 'undefined' ? !state.isLogin : isLogin
},
//更新TIMSDK状态
toggleIsSDKReady(state, isSDKReady) {
state.isSDKReady = typeof isSDKReady === 'undefined' ? !state.isSDKReady : isSDKReady
},
//退出登录重置状态
reset(state) {
state.isLogin = false
state.isSDKReady = false
},
//选择好友聊天--创建会话/拼接会话id
createConversationActive(state,toUserId){
state.conversationActive.conversationID = 'C2C'+toUserId
state.toUserId = toUserId
state.currentMessageList = []
},
//选择已有会话聊天--更新选中会话详情
updateConversationActive(state,conversationItem){
state.conversationActive = Object.assign({}, conversationItem.conversation);
state.toUserId = conversationItem.user.userId
state.currentMessageList = []
},
//更新会话列表
updateConversationList(state,newConversationList){
state.conversationList = newConversationList
},
/**
* 将消息插入当前会话列表
* 调用时机:收/发消息事件触发时
* @param {Object} state
* @param {Message[]|Message} data
* @returns
*/
pushCurrentMessageList(state, data) {
// 还没当前会话,则跳过
if (Array.isArray(data)) {
// 筛选出当前会话的消息
const result = data.filter(item => item.conversationID === state.conversationActive.conversationID)
state.currentMessageList = [...state.currentMessageList, ...result]
} else if (data.conversationID === state.conversationActive.conversationID) {
state.currentMessageList = [...state.currentMessageList, data]
}
console.log('1111')
console.log( state.currentMessageList)
},
/**
* 滑到顶部请求更多的历史消息
* */
unshiftCurrentMessageList(state,data){
console.log(data)
if(data && state.currentMessageList.length > 1){
state.currentMessageList = [...data,...state.currentMessageList]
}
}
}
})
export default store;