273 lines
5.7 KiB
JavaScript
273 lines
5.7 KiB
JavaScript
import storage from '@/utils/storage'
|
|
import {
|
|
ACCESS_TOKEN,
|
|
USER_ID
|
|
} from '@/store/mutation-types'
|
|
import io from '@hyoga/uni-socket.io';
|
|
import {
|
|
isEmpty
|
|
} from '@/utils/util'
|
|
import Vue from 'vue'
|
|
import {
|
|
socketUrl
|
|
} from "@/config.js"
|
|
import {
|
|
getConversationList,
|
|
sendMessage,
|
|
getFriemdMessages,
|
|
postMarkRead,
|
|
removeConversation
|
|
} from '@/api/chat.js'
|
|
import {
|
|
getUserDetail
|
|
} from '@/api/love-user-profile.js'
|
|
import {getArticleCommentUnreadCount} from '@/api/article-comment.js'
|
|
import {pageLike} from '@/api/user-like.js'
|
|
import {pageLook} from '@/api/user-look.js'
|
|
const theme = {
|
|
state: {
|
|
conversations: {},
|
|
friends: [],
|
|
unReadCommentNumber: 0,
|
|
unReadLikeNumber: 0,
|
|
unReadLikeMeNumber: 0,
|
|
unReadLookNumber: 0,
|
|
},
|
|
|
|
mutations: {
|
|
ADD_FRIEND(state, userInfo) {
|
|
if (!state.conversations[userInfo.userId]) {
|
|
Vue.set(state.conversations, userInfo.userId, {
|
|
friendInfo: userInfo,
|
|
messages: [],
|
|
unRead: 0,
|
|
updateTime: new Date()
|
|
});
|
|
}
|
|
},
|
|
REMOVE_FRIEND(state, userId) {
|
|
if (state.conversations[userId]) {
|
|
Vue.delete(state.conversations,userId)
|
|
}
|
|
},
|
|
ADD_MESSAGE(state, {
|
|
userId,
|
|
message,
|
|
friendInfo
|
|
}) {
|
|
|
|
if (!state.conversations[userId]) {
|
|
|
|
// todo getFriendInfo
|
|
Vue.set(state.conversations, userId, {
|
|
messages: [],
|
|
unRead: 0,
|
|
friendInfo,
|
|
updateTime: new Date()
|
|
});
|
|
}
|
|
state.conversations[userId].messages.push(message);
|
|
// console.log(message.formUserId, storage.get(USER_ID));
|
|
if (message.formUserId != storage.get(USER_ID)) {
|
|
state.conversations[userId].unRead++
|
|
}
|
|
state.conversations[userId].updateTime = new Date()
|
|
console.log(message.type);
|
|
if (message.type == 'image') {
|
|
state.conversations[userId].content = '[图片]'
|
|
} else if (message.type == 'card') {
|
|
state.conversations[userId].content = '[卡片]'
|
|
} else {
|
|
state.conversations[userId].content = message.content
|
|
}
|
|
|
|
uni.$emit('message' + userId, message)
|
|
},
|
|
|
|
SET_CONVERSATION(state, data) {
|
|
state.conversations = data
|
|
},
|
|
|
|
SET_MESSAGES(state, {
|
|
friendId,
|
|
messages
|
|
}) {
|
|
state.conversations[friendId].messages = messages
|
|
},
|
|
MARK_READ(state, friendId) {
|
|
state.conversations[friendId].unRead = 0
|
|
},
|
|
SET_UNREAD_COMMENT_NUMBER(state, number) {
|
|
if(number < 0) {
|
|
return
|
|
}
|
|
state.unReadCommentNumber = number
|
|
},
|
|
SET_UNREAD_LIKE_NUMBER(state, number) {
|
|
if(number < 0) {
|
|
return
|
|
}
|
|
state.unReadLikeNumber = number
|
|
},
|
|
SET_UNREAD_LIKE_ME_NUMBER(state, number) {
|
|
if(number < 0) {
|
|
return
|
|
}
|
|
state.unReadLikeMeNumber = number
|
|
},
|
|
SET_UNREAD_LOOK_NUMBER(state, number) {
|
|
if(number < 0) {
|
|
return
|
|
}
|
|
state.unReadLookNumber = number
|
|
}
|
|
},
|
|
|
|
actions: {
|
|
async RemoveFriend({
|
|
state,
|
|
commit
|
|
}, friendId){
|
|
removeConversation(state.conversations[friendId].id)
|
|
commit('REMOVE_FRIEND', friendId);
|
|
|
|
},
|
|
|
|
async SendMessage({
|
|
commit
|
|
}, data) {
|
|
await sendMessage(data)
|
|
data.createTime = new Date()
|
|
commit('ADD_MESSAGE', {
|
|
userId: data.toUserId,
|
|
message: data
|
|
})
|
|
},
|
|
async LoadFriendMessage({
|
|
commit
|
|
}, friendId) {
|
|
const res = await getFriemdMessages(friendId)
|
|
commit('SET_MESSAGES', {
|
|
friendId: friendId,
|
|
messages: res.data
|
|
})
|
|
|
|
},
|
|
ConnectSocket({
|
|
state,
|
|
commit
|
|
}, data) {
|
|
console.log("ConnectSocket");
|
|
const socket = io(socketUrl, {
|
|
//这里是连接时所携带的数据
|
|
query: {
|
|
token: storage.get(ACCESS_TOKEN),
|
|
userId: storage.get(USER_ID)
|
|
},
|
|
//连接方式
|
|
transports: ['websocket', 'polling'],
|
|
//过期时间
|
|
timeout: 5000,
|
|
});
|
|
|
|
socket.on('connect', () => {
|
|
const {
|
|
id
|
|
} = socket
|
|
console.log("socketio 已连接 clientId" + id);
|
|
uni.socket = socket
|
|
// 获取聊天记录
|
|
getConversationList().then(res => {
|
|
const data = {}
|
|
res.data.forEach(item => {
|
|
item.messages = []
|
|
if(item.friendInfo){
|
|
data[item.friendInfo.userId] = item
|
|
}
|
|
})
|
|
console.log('SET_CONVERSATION', data);
|
|
commit('SET_CONVERSATION', data)
|
|
}).catch(err=>{
|
|
console.log(err)
|
|
})
|
|
// 获取恋爱圈未读消息
|
|
getArticleCommentUnreadCount().then(res=>{
|
|
commit('SET_UNREAD_COMMENT_NUMBER', res.data)
|
|
})
|
|
// 喜欢我和我喜欢
|
|
pageLike({
|
|
sceneType: 'UN_READ_USER_LIKE'
|
|
}).then(res=>{
|
|
commit('SET_UNREAD_LIKE_NUMBER', res.data.count)
|
|
})
|
|
pageLike({
|
|
sceneType: 'UN_READ_USER_LIKE_ME'
|
|
}).then(res=>{
|
|
commit('SET_UNREAD_LIKE_ME_NUMBER', res.data.count)
|
|
})
|
|
pageLook({
|
|
sceneType: 'UN_READ_USER_LOOK'
|
|
}).then(res=>{
|
|
commit('SET_UNREAD_LOOK_NUMBER', res.data.count)
|
|
})
|
|
})
|
|
|
|
socket.on('message', (data) => {
|
|
if (!state.conversations[data.formUserId]) {
|
|
getUserDetail(data.formUserId).then(res => {
|
|
commit('ADD_MESSAGE', {
|
|
userId: data.formUserId,
|
|
message: data,
|
|
friendInfo: res.data.userInfo
|
|
})
|
|
})
|
|
} else {
|
|
commit('ADD_MESSAGE', {
|
|
userId: data.formUserId,
|
|
message: data
|
|
})
|
|
}
|
|
})
|
|
|
|
socket.on('pyq', (data) => {
|
|
commit('SET_UNREAD_COMMENT_NUMBER', data)
|
|
})
|
|
|
|
socket.on('likeme', (data) => {
|
|
console.log("likeme",state.unReadLikeMeNumber , data);
|
|
commit('SET_UNREAD_LIKE_ME_NUMBER', state.unReadLikeMeNumber + data)
|
|
})
|
|
|
|
socket.on('look', (data) => {
|
|
console.log("look",state.unReadLookNumber , data);
|
|
commit('SET_UNREAD_LOOK_NUMBER', state.unReadLookNumber + data)
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
},
|
|
MarkRead({
|
|
state,
|
|
commit
|
|
}, friendId) {
|
|
postMarkRead({
|
|
id: state.conversations[friendId].id
|
|
})
|
|
commit('MARK_READ', friendId)
|
|
},
|
|
|
|
CloseSocket({
|
|
state,
|
|
commit
|
|
}, data) {
|
|
if (uni.socket) {
|
|
uni.socket.close()
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
export default theme |