You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

235 lines
5.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import request from '@/components/hzjc/utils/request.js'
let chat = {}
// 1--列表页 2--详情页 3--其他页
chat.curPage = 3
// 当前在线聊天室ID
chat.curRoomId = ''
// 当前页面object
chat.that = {}
/**
* 获取和好友的聊天列表
* @date 2022-09-15
*/
chat.listChatRoom = function() {
return new Promise((resolve, reject) => {
request.getData('chat/api/FriendsChat/listChatRoom').then(res => {
resolve(res)
}).catch(res => {
reject(res)
})
})
}
/**
* 绑定客户端到组和USER_ID
* @param {int} roomId 房间ID
* @param {string} clientId 客户端ID
* @date 2022-09-15
*/
chat.bindUser = function(roomId, clientId) {
return new Promise((resolve, reject) => {
request.getData('chat/api/FriendsChat/bindUser', {
room_id: roomId,
client_id: clientId
}).then(res => {
resolve(res)
}).catch(res => {
reject(res)
})
})
}
/**
* 获取聊天室详情
* @param {int} roomId 房间ID
* @date 2022-09-15
*/
chat.getChatRoomDetail = function(roomId) {
return new Promise((resolve, reject) => {
request.getData('chat/api/FriendsChat/getChatRoomDetail', {
room_id: roomId
}).then(res => {
resolve(res)
}).catch(res => {
reject(res)
})
})
}
/**
* 更改用户在线状态以及标记消息已读
* @param {int} roomId 房间ID
* @param {int} isOnline 是否在线 0--离线 1--在线
* @date 2022-09-15
*/
chat.updateUserOnlineStatus = function(roomId, isOnline) {
return new Promise((resolve, reject) => {
request.getData('chat/api/FriendsChat/updateUserOnlineStatus', {
room_id: roomId,
is_online: isOnline
}).then(res => {
resolve(res)
}).catch(res => {
reject(res)
})
})
}
/**
* 获取和某个好友的聊天记录
* @param {int} roomId 房间ID
* @param {int} curFirstMessageId 当前最上面的信息ID
* @param {int} firstPage 1--加载第一页,自动清空列表 0--加载上一页,不清空列表
* @param {object} _$this
* @date 2022-09-15
*/
chat.listChatRoomMessage = function(roomId, curFirstMessageId, firstPage, _$this) {
return new Promise((resolve, reject) => {
request.getUpList('chat/api/FriendsChat/listChatRoomMessage', {
room_id: roomId,
cur_first_message_id: curFirstMessageId
}, {
that: _$this,
first_page: firstPage
}).then(res => {
resolve(res)
}).catch(res => {
reject(res)
})
})
}
/**
* 发送消息
* @param {int} roomId 房间ID
* @param {int} messageType 消息类型
* @param {string} messageType 消息内容
* @param {int|string} recordSeconds messageType=2时录音时长
* @param {object} attach 附加数据
* @date 2022-09-15
*/
chat.insertChatMessage = function(roomId, messageType, messageContent, attach = {}, product = {}) {
let data = {
room_id: roomId,
type: messageType,
content: messageContent
}
// 语音类型
if (messageType == 2) {
data.record_seconds = attach.recordSeconds
}
// 位置类型
if (messageType == 4) {
data.name = attach.name
data.address = attach.address
data.lat = attach.lat
data.lng = attach.lng
}
// 商品类型
if (messageType == 5) {
data.product_id = product.id;
data.product_name = product.name;
data.product_cover_img = product.cover_img;
}
return new Promise((resolve, reject) => {
request.getData('chat/api/FriendsChat/insertChatMessage', data).then(res => {
resolve(res)
}).catch(res => {
reject(res)
})
})
}
/**
* 监听消息
* @date 2022-09-21
*/
chat.onMessage = function() {
// 监听WebSocket接受到服务器的消息事件
uni.onSocketMessage((res) => {
let data = JSON.parse(res.data)
console.log('onSocketMessage', data)
switch (data.type) {
// websocket连接成功
case "init":
uni.setStorageSync('clientId', data.to_client)
// 绑定用户和所有直播间
request.getData('chat/api/FriendsChat/bindUser', {
client_id: data.to_client
}).then(res => {
})
break;
// 心跳
case "heart_beat":
break;
// 收到聊天消息
case "chat_friend_message":
switch (this.curPage) {
// 列表页
case 1:
let roomList = this.that.roomList
let curData = {}
let isInRoomList = false
roomList.forEach((e, index) => {
// 消息在聊天列表中,仅更新且移到第一位
if (e.room_id == data.to_group) {
isInRoomList = true
curData = e
curData.unread_number = e.unread_number + 1
curData.last_message_type = data.data.type
curData.show_message_text = data.data.show_message_text
curData.show_time = data.data.list_time_show
roomList.splice(index, 1)
}
})
if (!isInRoomList) {
curData.room_id = data.data.room_id
curData.to_user_id = data.data.to_user_id
curData.to_user_nick_name = data.data.to_user_nick_name
curData.to_user_head_img = data.data.to_user_head_img
curData.unread_number = 1
curData.last_message_type = data.data.type
curData.show_message_text = data.data.show_message_text
curData.show_time = data.data.list_time_show
}
roomList.unshift(curData)
this.that.roomList = roomList
break;
// 详情页
case 2:
if (data.to_group == this.curRoomId) {
delete data.data.show_message_text
delete data.data.list_time_show
delete data.data.to_user_nick_name
delete data.data.to_user_head_img
this.that.up_list.push(data.data)
// 滚动到最新消息
setTimeout(() => {
this.that.scrollToId = 'scroll' + data.data.id
}, 100)
}
break;
// 其他页
case 3:
break;
// 未知
default:
break;
}
break;
default:
break;
}
})
}
export default chat