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.

1129 lines
33 KiB

4 months ago
<template>
<view class="page">
<view class="page-header" :style="{ paddingTop: geStatusBarHeight()+ 8 + 'px'}">
<view class="text-center">消息</view>
<uni-icons type="staff-filled" size="30" color="#ffffff" @click="$u.throttle(gotoContacts(), 2000)"></uni-icons>
</view>
<view class="wrapcon">
1 month ago
4 months ago
<!-- 列表 -->
<view v-if="grouplist.length==0 && loadStatus=='nomore'" class="nodata">~</view>
<view class="listcell">
2 weeks ago
<u-swipe-action ref="swipeRef" :options="options" v-for="(info, index) in grouplist" :key="info.id" style="min-width: 600rpx;"
@click="$u.throttle(actionClick(info,index), 2000)">
<view class="lcon" @click="gotoGroup(info,index)" :key="index" @longpress="handleLongPress(info,index)">
<view class="limg">
<image class="img" :src="info.img" mode="aspectFill"></image>
</view>
<view class="lright">
<view class="lrow">
<view class="pname">{{info.name}}</view>
<view class="ptime"> {{ changeTime(info.datetime)}}</view>
4 months ago
</view>
2 weeks ago
<view class="lrow">
<view class="pnr">
{{ info.type==0?info.content:(info.type==1?"图片":(info.type==2?"文件":(info.type==3?"语音":(info.type==4?"视频":
(info.type==5?"订单咨询":(info.type==6?"商品咨询":info.content))))))}}
1 month ago
</view>
2 weeks ago
<view class="lnum" v-if="info.sl>0">
{{ info.sl}}
1 month ago
</view>
4 months ago
</view>
2 weeks ago
</view>
</view>
1 month ago
</u-swipe-action>
4 months ago
</view>
1 month ago
4 months ago
<u-loadmore v-if="grouplist.length==0 && loadStatus=='loading'" :status="loadStatus" :marginTop="20"></u-loadmore>
</view>
<!-- 提示信息弹窗 -->
<uni-popup ref="message" type="message">
<uni-popup-message :type="msgType" :message="messageText" :duration="2000"></uni-popup-message>
</uni-popup>
</view>
</template>
<script>
2 days ago
import { getcartNum,getmessageNum,myCache } from '../../utils/utils.js';
4 months ago
import dateTime from '@/common/dateTime.js';
export default {
data() {
return {
1 month ago
openId:"",
phone:"",
userid:"",
userName:"",
userheadimg:"",
4 months ago
messageText:'', //错误提示框
msgType :'error',
loadStatus:'loadmore',
1 month ago
options: [
{
text: '删除',
style: {
backgroundColor: '#ed2a28'
}
}
2 weeks ago
], // u-swipe-action样式
4 months ago
imgurl:uni.$http.baseUrl,
1 month ago
grouplist:[],
socketmsg:[],//接收信息
2 days ago
// isConnected: false, // WebSocket是否打开
// heartbeatInterval: null, // 心跳包20秒连接一次
// heartbeatTimeout: 20000, // 心跳间隔时间例如每20秒发送一次
4 months ago
}
},
onLoad(options) {
},
onShow(){
2 days ago
1 month ago
this.openId = myCache('openId');
var user = myCache('user');
this.userid = user.userid? user.userid:'';
this.userName=user.nickName?user.nickName:"";
this.userheadimg=user.avatar?user.avatar:require("@/static/image/girl.png");
this.phone = user.userphone;
2 days ago
4 weeks ago
// 从缓存聊天群里获取聊天群列表
4 months ago
this.loadData();
2 days ago
var that=this;
setTimeout(() => {
if(!that.isConnected){
// 重新连接socket
that.reconnect();
}
}, 500);
that.setmsgnum();
setTimeout(() => {
that.setmsgnum();
}, 2000);
4 months ago
},
onPullDownRefresh() {
console.log('onPullDownRefresh');
setTimeout(()=>{
uni.stopPullDownRefresh()
},500);
},
methods: {
1 month ago
// 定时心跳包
startHeartbeat() {
2 days ago
var that=this;
1 month ago
console.log("startHeartbeat")
this.heartbeatInterval = setInterval(() => {
2 days ago
if(this.isConnected){
uni.sendSocketMessage({
data: JSON.stringify({
'cmd': 1,//心跳
'data': {
'accessToken':uni.getStorageSync("token")
},
}), // 发送心跳包数据,
success(re) {
console.log(re);
console.log('消息发送成功!')
},
fail(err) {
console.log(err);
console.log('消息发送失败!');
that.reconnect();
}
});
}
1 month ago
}, this.heartbeatTimeout);
},
2 days ago
// 开始重连
reconnect () {
if (!this.isConnected) {
clearTimeout(this.heartbeatInterval);
this.heartbeatInterval = setTimeout(() => {
console.log('开始重连...');
this.socketinit();
},this.heartbeatTimeout);
}
},
1 month ago
// ws接收消息
socketinit(){
var that = this;
2 days ago
uni.connectSocket({
2 weeks ago
url: "wss://www.sanduolantoyoga.com/yoga-imserver/",
header: {
// 'content-type': 'application/json',
Authorization: uni.getStorageSync("token"),
},
success: (res) => {
console.log(res, 'socket连接成功success');
},
fail: (res) => {
console.log(res, 'socket连接失败')
},
complete: (res) => {
console.log(res,'socket连接成功complete');
}
});
uni.onSocketOpen(resopen => {
console.log('WebSocket连接已打开');
2 days ago
this.isConnected = true;
this.$forceUpdate();
2 weeks ago
uni.sendSocketMessage({
data: JSON.stringify({
"cmd": 0,//心跳
"data": {
"accessToken":uni.getStorageSync("token"),
},
}), // 发送心跳包数据 // 发送的消息内容
success(re) {
console.log(re);
console.log('消息发送成功!')
1 month ago
},
2 weeks ago
fail(err) {
console.log(err);
console.log('消息发送失败!')
1 month ago
}
});
2 weeks ago
// 定时发送心跳包
this.startHeartbeat();
// 拉取离线消息
this.grouplist.forEach((cell,i)=>{
if(cell.minId){
// 通过某个会话中已读消息的最大id 获取离线消息
this.pullMessage(i,cell);
}
});
// 已经删除的有最大id的继续拉取是否有消息
var data= myCache("privateMsgMaxId")?myCache("privateMsgMaxId"):[{id:0,minId:0}];
data.forEach((cell)=>{
console.log("privateMsgMaxId")
var ifcz=false;
this.grouplist.forEach((item)=>{
if(cell.id==item.id){
// 已经存在列表了,不用拉取离线信息
ifcz=true;
1 month ago
}
});
2 weeks ago
if(!ifcz){
// 拉取离线信息
this.getList(cell);
}
1 month ago
});
2 weeks ago
var data= myCache("groupMsgMaxId")?myCache("groupMsgMaxId"):[{id:0,minId:0}];
data.forEach((cell)=>{
var ifcz=false;
this.grouplist.forEach((item)=>{
if(cell.id==item.id){
// 已经存在列表了,不用拉取离线信息
ifcz=true;
}
});
if(!ifcz){
// 拉取离线信息
this.getGroupList(cell);
}
});
});
2 days ago
// 连接关闭后重新连接
uni.onSocketClose(res => {
console.log('WebSocket连接已关闭',res);
this.isConnected = false;
this.$forceUpdate();
// 重新连接socket
this.reconnect();
});
// 连接失败后重新连接
uni.onSocketError(err => {
console.error('WebSocket连接打开失败请检查', err);
this.isConnected = false;
this.$forceUpdate();
// 重新连接socket
this.reconnect();
});
// 接收信息
2 weeks ago
uni.onSocketMessage(res => {
console.log('收到WebSocket服务器消息');
if(res.data){
var rs=JSON.parse(res.data);
2 days ago
console.log("onSocketMessage",rs);
2 weeks ago
if(rs.cmd==3){
// 私聊
if(rs.data){
var data=rs.data;
2 days ago
if(data.recvId&&data.recvId==that.userid){
2 weeks ago
// 是这个用户的接收的私聊信息
// 加入聊天记录
// 消息类型 0:文字 1:图片 2:文件 3:语音 4:视频 10, "撤回" 11, "已读 "12, "消息已读回执 " 30,"加载中标记"
if(data.type==0||data.type==1||data.type==2||data.type==3||data.type==4||data.type==5||data.type==6){
2 days ago
if(data.type==0){
data.content=decodeURIComponent(data.content);
}
var id="privatechat-" + data.recvId +"-" + data.sendId;
var index=this.ifadd(id);
var chatinfo=null;
if(index>-1){
that.grouplist[index].sl=(that.grouplist[index].sl?that.grouplist[index].sl+1:1);
that.grouplist[index].content=data.content;
that.grouplist[index].datetime=data.sendTime;
that.grouplist[index].type=data.type;
that.grouplist[index].minId=data.id;
that.grouplist[index].ifload=1;
that.$forceUpdate();
chatinfo= JSON.parse(JSON.stringify(that.grouplist[index]));
}
else{
2 weeks ago
// 未保存缓存
// 接收到socket后更新聊天列表记录
2 days ago
chatinfo={
id: id,
2 weeks ago
content: data.content,
minId: data.id,
sl:1,
datetime: data.sendTime,
type: data.type,
name: "",
userid: data.recvId,
fromuser: data.sendId,
img: "",
2 days ago
sort:"privatechat",
ifload:0
2 weeks ago
};
2 days ago
this.grouplist.push(chatinfo);
this.$forceUpdate();
1 month ago
}
2 days ago
myCache("chatlist-"+this.userid,this.grouplist);
this.getPrivateInfo(chatinfo);
// 未读消息数量获取
this.setmsgnum();
1 month ago
}
2 weeks ago
else{
// 10, "撤回" 11, "已读 " 12, "消息已读回执 " 30,"加载中标记"
}
1 month ago
}
}
2 weeks ago
}
else if(rs.cmd==4){
// 群聊
if(rs.data){
var data=rs.data;
if(data.atUserIds&&data.atUserIds.includes(that.userid)){
// 是这个用户的接收的群聊信息
// 加入聊天记录
// 消息类型 0:文字 1:图片 2:文件 3:语音 4:视频 10, "撤回" 11, "已读 "12, "消息已读回执 " 30,"加载中标记"
if(data.type==0||data.type==1||data.type==2||data.type==3||data.type==4||data.type==5||data.type==6){
2 days ago
if(data.type==0){
data.content=decodeURIComponent(data.content);
}
var id="groupchat-" + data.groupId;
var index=this.ifadd(id);
var chatinfo=null;
if(index>-1){
that.grouplist[index].sl=(that.grouplist[index].sl?that.grouplist[index].sl+1:1);
that.grouplist[index].content=data.content;
that.grouplist[index].datetime=data.sendTime;
that.grouplist[index].fromuser=data.sendId;
that.grouplist[index].type=data.type;
that.grouplist[index].minId=data.id;
that.grouplist[index].ifload=1;
that.$forceUpdate();
chatinfo= JSON.parse(JSON.stringify(that.grouplist[index]));
}
else{
2 weeks ago
// 未保存缓存
// 接收到socket后更新聊天列表记录
2 days ago
chatinfo={
id: id,
groupId: data.groupId,
2 weeks ago
content: data.content,
minId: data.id,
sl:1,
datetime: data.sendTime,
type: data.type,
name: "",
userid: this.userid,
fromuser: data.sendId,
img: "",
sendId: data.sendId,
sendNickName: data.sendNickName,
2 days ago
sort:"groupchat",
ifload:0
2 weeks ago
};
2 days ago
this.grouplist.push(chatinfo);
this.$forceUpdate();
1 month ago
}
2 days ago
myCache("chatlist-"+this.userid,this.grouplist);
// 未读消息数量获取
this.setmsgnum();
this.getGroupInfo(chatinfo);
1 month ago
}
2 weeks ago
else{
// 10, "撤回" 11, "已读 " 12, "消息已读回执 " 30,"加载中标记"
}
1 month ago
}
}
}
2 weeks ago
}
});
1 month ago
},
2 days ago
// 消息
setmsgnum(){
var messageNum=getmessageNum();
if(messageNum>0){
// 底部菜单消息角标
uni.setTabBarBadge({
//显示未读消息条数
index: 3,
text: messageNum+''
});
}
else{
uni.removeTabBarBadge({
index: 3
})
}
},
handleLongPress(info,index) {
console.log(index);
this.$refs.swipeRef[index].open();
},
// 删除
actionClick(info,index) {
console.log(info,index);
var that=this;
uni.showModal({
title: '提示',
content: "确定要删除此聊天记录吗?",
cancelText: '取消',
confirmText: '确定',
success: ress => {
if (ress.confirm) {
that.$refs.swipeRef[index].close();
myCache(info.id,"");
that.grouplist.splice(index, 1);
that.$forceUpdate();
myCache("chatlist-"+this.userid,that.grouplist);
// 未读消息数量获取
this.setmsgnum();
}
}
});
1 month ago
},
2 weeks ago
// 消息是否存在列表中
ifadd(id){
2 days ago
var ret=-1;
2 weeks ago
this.grouplist.forEach((cell,ii)=>{
if(cell.id==id){
2 days ago
ret=ii;
2 weeks ago
}
});
return ret;
},
4 months ago
gotoContacts(){
uni.navigateTo({
url: `/pages/message/contact`
});
},
changeTime(date) {
return dateTime.dateTime(date);
},
1 month ago
gotoGroup(chat,index){
// 当前消息未读数据消除
this.grouplist[index]["sl"]=0;
this.$forceUpdate();
4 weeks ago
myCache("chatlist-"+this.userid,this.grouplist);
2 days ago
// 未读消息数量获取
this.setmsgnum();
4 months ago
// 去聊天室
1 month ago
if(chat.sort=="privatechat"){
// 私聊
var info={
chatId: chat.id,
chatName: chat.name,
chatAvatar: chat.img,
chatTime: chat.datetime,
friendId: chat.fromuser,
minId: chat.minId,
sort:"privatechat",
from:"message" // yh 用户咨询进入聊天框message 从消息进入聊天框
}
4 months ago
var data=encodeURIComponent(JSON.stringify(info));
uni.navigateTo({
1 month ago
url: `/pages/chat/chat?data=${data}`
4 months ago
});
}
1 month ago
else if(chat.sort=="groupchat"){
// 群聊
var info={
chatId: chat.id,
groupId: chat.groupId,
chatName: chat.name,
chatAvatar: chat.img,
chatTime: chat.datetime,
friendId: chat.fromuser,
4 weeks ago
teacherId: chat.instructor?chat.instructor:chat.teacherId,
1 month ago
minId: chat.minId,
sort:"groupchat", // privatechat 私聊 groupchat 群聊
from:"message", // yh 用户咨询进入聊天框message 从消息进入聊天框
notice: chat.notice,
remarkNickName: chat.remarkNickName,
showNickName: chat.showNickName,
showGroupName: chat.showGroupName,
remarkGroupName: chat.remarkGroupName,
reason: chat.reason,
customerService: chat.customerService,
instructor: chat.instructor,
2 days ago
ownerId: chat.ownerId,
1 month ago
productId: chat.productId,
productName: chat.productName
}
4 months ago
var data=encodeURIComponent(JSON.stringify(info));
1 month ago
console.log(data);
4 months ago
uni.navigateTo({
1 month ago
url: `/pages/chat/groupchat?data=${data}`
4 months ago
});
}
},
loadData(){
1 month ago
// 信息列表加载
this.loadStatus="loading";
setTimeout(() => {
this.getgroupsmembers();
}, 300);
4 months ago
},
1 month ago
// 获取群聊列表
4 months ago
async getgroupsmembers(){
1 month ago
// 获取聊天群列表
2 weeks ago
this.grouplist=myCache("chatlist-"+this.userid)?myCache("chatlist-"+this.userid):[];
1 month ago
this.loadStatus="nomore";
this.$forceUpdate();
2 weeks ago
// 按照时间排序
1 month ago
var that=this;
setTimeout(() => {
that.reorder();
4 weeks ago
myCache("chatlist-"+this.userid,that.grouplist);
2 days ago
// 未读消息数量获取
this.setmsgnum();
1 month ago
}, 300);
// socket接收实时消息
this.socketinit();
},
// 重新排序
reorder(){
this.grouplist.sort((a, b) => b.datetime - a.datetime);
this.$forceUpdate();
},
2 weeks ago
// 拉取离线信息
async pullMessage(i,item) {
1 month ago
if(item.sort=="groupchat"){
this.getgroupsnums(i,item);
}
else{
this.getgroupsnums(i,item);
}
},
2 weeks ago
// 拉取离线消息
async getGroupList(item) {
// 判断当前 minId 是不是列表中已经存在了,是不再拉取,不是继续拉取
var that=this;
const {data: res} = await uni.$http.get('/api/message/group/pullOfflineMessage',{minId: item.minId});
if(res.data&&res.data.length>0){
res.data.forEach((cell,index)=>{
// 获取消息后对消息进行缓存处理
var chatlastinfo={
id: "groupchat-" + cell.groupId,
groupId:cell.groupId,
content: cell.content,
minId: cell.id,
sl: index+1,
datetime: cell.sendTime,
type: cell.type,
name: "",
userid: this.userid,
fromuser: cell.sendId,
img: "",
sendId: cell.sendId,
sendNickName: cell.sendNickName,
2 days ago
sort:"groupchat",
ifload:1
2 weeks ago
};
this.getGroupInfo(chatlastinfo,index);
});
}
},
// 拉取离线消息
async getList(item) {
// 判断当前 minId 是不是列表中已经存在了,是不再拉取,不是继续拉取
var that=this;
const {data: res} = await uni.$http.get('/api/message/private/pullOfflineMessage',{minId: item.minId});
if(res.data&&res.data.length>0){
res.data.forEach((cell,index)=>{
// 获取消息后对消息进行缓存处理
var chatlastinfo={
id: "privatechat-" + cell.recvId +"-" + cell.sendId,
content: cell.content,
minId: cell.id,
sl:index+1,
datetime: cell.sendTime,
type: cell.type,
name: "",
userid: cell.recvId,
fromuser: cell.sendId,
img: "",
2 days ago
sort:"privatechat",
ifload:1
2 weeks ago
};
2 days ago
this.getPrivateInfo(chatlastinfo);
2 weeks ago
});
}
},
ifprivateMsgMaxId(item){
var ret=true;
// 判断当前 minId 是不是列表中已经存在了,是不再拉取,不是继续拉取
var data= myCache("privateMsgMaxId")?myCache("privateMsgMaxId"):[];
data.forEach((cell)=>{
if(cell.id==item.id&&parseInt(item.minId)>=parseInt(cell.minId)){
ret=false;
}
});
return ret;
},
ifgroupMsgMaxId(item){
console.log("ifgroupMsgMaxId")
var ret=true;
// 判断当前 minId 是不是列表中已经存在了,是不再拉取,不是继续拉取
var data= myCache("groupMsgMaxId")?myCache("groupMsgMaxId"):[];
data.forEach((cell)=>{
if(cell.id==item.id&&parseInt(item.minId)>=parseInt(cell.minId)){
ret=false;
}
});
return ret;
},
1 month ago
// 获取群聊中离线信息
async getgroupsnums(i,item){
if(item.sort=="groupchat"){
2 weeks ago
// 判断当前 minId 是不是列表中已经存在了,是不再拉取,不是继续拉取
// if(this.ifgroupMsgMaxId(item)){
// 群聊
const {data: res} = await uni.$http.get("/api/message/group/pullOfflineMessage",{minId:item.minId});
if(res.data&&res.data.length>0){
// 获取消息后对消息进行缓存处理
var data=res.data;
// 获取离线消息数量返回
this.grouplist[i]["sl"]=res.data.length;
var last=res.data[res.data.length-1];
if(last.type==0){
last.content=decodeURIComponent(last.content);
}
if(last.type==0||last.type==1||last.type==2||last.type==3||last.type==4||data.type==5||data.type==6){
this.grouplist[i].content=last.content;
this.grouplist[i].type=last.type;
this.grouplist[i].datetime=last.datetime;
this.$forceUpdate();
// 重新排序
this.reorder();
myCache("chatlist-"+this.userid,this.grouplist);
2 days ago
// 未读消息数量获取
this.setmsgnum();
2 weeks ago
}
1 month ago
}
2 weeks ago
// }
1 month ago
}
else{
2 weeks ago
// 判断当前 minId 是不是列表中已经存在了,是不再拉取,不是继续拉取
// if(this.ifprivateMsgMaxId(item)){
// 私聊
const {data: res} = await uni.$http.get("/api/message/private/pullOfflineMessage",{minId:item.minId});
if(res.data&&res.data.length>0){
// 获取消息后对消息进行缓存处理
var data=res.data;
// 获取离线消息数量返回
this.grouplist[i]["sl"]=res.data.length;
var last=res.data[res.data.length-1];
if(last.type==0){
last.content=decodeURIComponent(last.content);
}
if(last.type==0||last.type==1||last.type==2||last.type==3||last.type==4||data.type==5||data.type==6){
this.grouplist[i].content=last.content;
this.grouplist[i].type=last.type;
this.grouplist[i].datetime=last.datetime;
this.$forceUpdate();
// 重新排序
this.reorder();
myCache("chatlist-"+this.userid,this.grouplist);
2 days ago
// 未读消息数量获取
this.setmsgnum();
2 weeks ago
}
1 month ago
}
2 weeks ago
// }
1 month ago
}
},
2 days ago
// 获取私聊信息
async getPrivateInfo(info) {
if(info.ifload==0){
info[info.fromuser]=(info.fromuser!=this.userid? await this.getFriend(info.fromuser):{});
info.name=info[info.fromuser]?info[info.fromuser].name:"匿名";
info.img=info[info.fromuser]?info[info.fromuser].img:require("@/static/image/girl.png");
this.updateChatList(info);
}
else{
this.updateChatList(info);
}
},
1 month ago
// 获取群信息
2 weeks ago
async getGroupInfo(info) {
2 days ago
if(info.ifload==0){
const {data: res} = await uni.$http.get('/api/group/find/'+info.groupId);
if(res.data){
var data = res.data;
info.name=data.name;
info.img=data.headImage||'/static/image/qltx.png';
info.notice=data.notice;
info.remarkNickName=data.remarkNickName;
info.showNickName=data.showNickName;
info.showGroupName=data.showGroupName;
info.remarkGroupName=data.remarkGroupName;
info.customerService=data.customerService;
info.instructor=data.instructor;
info.ownerId=data.ownerId;
info.productId=data.productId;
info.productName=data.productName;
info[data.ownerId]=(data.ownerId!=this.userid? await this.getFriend(data.ownerId):{});
info[data.customerService]=(data.customerService!= this.userid? await this.getFriend(data.customerService):{});
info[data.instructor]=(data.instructor!=this.userid? await this.getFriend(data.instructor):{});
this.updateChatGroupList(info);
}
}
else{
2 weeks ago
this.updateChatGroupList(info);
1 month ago
}
},
2 days ago
async getFriend(id){
var name="匿名",img=require("@/static/image/girl.png");
const {data: res} = await uni.$http.get("/api/friend/find/"+id);
1 month ago
if(res.data){
2 days ago
name=res.data.nickName?res.data.nickName:"匿名";
img=res.data.headImage?res.data.headImage:require("@/static/image/girl.png");
}
return {
name:name,
img:img
};
},
async updateChatGroupList(info){
// 获取发送人的信息
var name= "匿名",img= require("@/static/image/girl.png");
var idx=this.ifadd(info.id);
if(idx<0){
name=info[info.sendId]?info[info.sendId].name:"匿名";
img=info[info.sendId]?info[info.sendId].img:require("@/static/image/girl.png");
// 新增一条
this.grouplist.push(info);
this.$forceUpdate();
1 month ago
}
else{
2 days ago
name=this.grouplist[idx][info.sendId].name?this.grouplist[idx][info.sendId].name:"匿名";
img=this.grouplist[idx][info.sendId].img?this.grouplist[idx][info.sendId].img:require("@/static/image/girl.png");
this.grouplist[idx].content=info.content;
this.grouplist[idx].datetime=info.datetime;
this.grouplist[idx].fromuser=info.fromuser;
this.grouplist[idx].type=info.type;
this.$forceUpdate();
}
// 重新排序 保存缓存
this.reorder();
myCache("chatlist-"+this.userid,this.grouplist);
// 未读消息数量获取
this.setmsgnum();
// 聊天框消息缓存
var msgchat=myCache(info.id);
if(!msgchat){
// 第一条聊天记录保存缓存
let firstmsg = [{
"fromname": name, // 发送人
"fromuser": info.fromuser, // 发送人
"headimg": img, // 发送人
"toname": this.userName, // 接收人
"touser": this.userid, // 接收人姓名
"content": info.content,
"time": info.type==3?(info.time?info.time:5):0,
"ifaudio":info.type==3? true:false,
"fromtime": info.datetime,
"type": info.type==0?'txt':(info.type==1?'image':info.type==3?'audio':(info.type==4?'video':'product')),
"id": info.minId,
"minId": info.minId,
"sendId": info.sendId,
"send": info.type==6?await this.getProduct(info.content):(info.type==5?await this.getOrder(info.content):null)
}];
myCache(info.id,firstmsg);
}
else{
// 是否已经加入到聊天记录中
if(this.ifchatadd(info.id,info.minId)){
1 month ago
// 聊天框信息 保存缓存
let firstmsg = {
2 days ago
"fromname": name, // 发送人
1 month ago
"fromuser": info.fromuser, // 发送人
"headimg": img, // 发送人
"toname": this.userName, // 接收人
"touser": this.userid, // 接收人姓名
"content": info.content,
2 days ago
"time": info.type==3?(info.time?info.time:5):0,
1 month ago
"ifaudio":info.type==3? true:false,
"fromtime": info.datetime,
"type": info.type==0?'txt':(info.type==1?'image':info.type==3?'audio':(info.type==4?'video':'product')),
2 days ago
"id": info.minId,
1 month ago
"minId": info.minId,
"sendId": info.sendId,
"send": info.type==6?await this.getProduct(info.content):(info.type==5?await this.getOrder(info.content):null)
};
msgchat.push(firstmsg);
myCache(info.id,msgchat);
}
2 days ago
1 month ago
}
},
2 weeks ago
async updateChatList(info){
2 days ago
var name= "匿名",img= require("@/static/image/girl.png");
// 此消息是否已经存在列表中
var idx=this.ifadd(info.id);
if(idx<0){
// 获取发送人的信息
name=info[info.fromuser]?info[info.fromuser].name:"匿名";
img=info[info.fromuser]?info[info.fromuser].img:require("@/static/image/girl.png");
info.img=img;
info.name=name;
this.grouplist.push(info);
this.$forceUpdate();
1 month ago
}
else{
2 days ago
name=this.grouplist[idx].name?this.grouplist[idx].name:"匿名";
img=this.grouplist[idx].img?this.grouplist[idx].img:require("@/static/image/girl.png");
this.grouplist[idx].content=info.content;
this.grouplist[idx].datetime=info.datetime;
this.grouplist[idx].fromuser=info.fromuser;
this.grouplist[idx].type=info.type;
this.$forceUpdate();
}
// 重新排序 保存缓存
this.reorder();
myCache("chatlist-"+this.userid,this.grouplist);
// 未读消息数量获取
this.setmsgnum();
// 聊天框消息缓存
var msgchat=myCache(info.id);
if(!msgchat){
// 第一条聊天记录保存缓存
let firstmsg = [{
"fromname": name, // 发送人
"fromuser": info.fromuser, // 发送人
"headimg": img, // 发送人
"toname": this.userName, // 接收人
"touser": this.userid, // 接收人姓名
"content": info.content,
"time": info.type==3?(info.time?info.time:5):0,
"ifaudio":info.type==3? true:false,
"fromtime": info.datetime,
"type": info.type==0?'txt':(info.type==1?'image':info.type==3?'audio':(info.type==4?'video':'product')),
"id": info.minId,
"minId": info.minId,
"recvId": info.recvId,
"sendId": info.sendId,
"send": info.type==6?await this.getProduct(info.content):(info.type==5?await this.getOrder(info.content):null)
}];
myCache(info.id,firstmsg);
}
else{
if(this.ifchatadd(info.id,info.minId)){
1 month ago
// 聊天框信息 保存缓存
let firstmsg = {
2 days ago
"fromname": name, // 发送人
1 month ago
"fromuser": info.fromuser, // 发送人
2 days ago
"headimg": img, // 发送人
1 month ago
"toname": this.userName, // 接收人
"touser": this.userid, // 接收人姓名
"content": info.content,
2 days ago
"time": info.type==3?(info.time?info.time:5):0,
1 month ago
"ifaudio":info.type==3? true:false,
"fromtime": info.datetime,
"type": info.type==0?'txt':(info.type==1?'image':info.type==3?'audio':(info.type==4?'video':'product')),
2 days ago
"id": info.minId,
1 month ago
"minId": info.minId,
"recvId": info.recvId,
"sendId": info.sendId,
"send": info.type==6?await this.getProduct(info.content):(info.type==5?await this.getOrder(info.content):null)
};
msgchat.push(firstmsg);
myCache(info.id,msgchat);
}
4 months ago
}
2 days ago
},
// 消息是否存在列表中
ifchatadd(id,msgid){
var ret=true;
var msgchat=myCache(id);
msgchat.forEach((cell)=>{
if(cell.id==msgid){
ret=false;
}
});
return ret;
4 months ago
},
1 month ago
async getProduct(id){
console.log("getProduct",id)
var rets=null;
const {data: res} = await uni.$http.post("/api/message/private/getProductForIm",{"productId":id});
if(res.data){
var data=res.data;
rets={
type:data.isCourse==0?1:2,// 1 商品2 课程3订单
id:data.id,
name:data.name,
pic:data.pic,
price:data.price,
unit: data.unit,
brandName:data.brandName,
isCourse:data.isCourse
}
console.log(rets)
return rets;
4 months ago
}
else{
1 month ago
return rets;
}
},
async getOrder(id){
var rets=null;
const {data: res} = await uni.$http.post("/api/message/private/getOrderForIm",{"orderItemId":id});
if(res.data){
var data=res.data;
rets={
type:3,// 1 商品2 课程3订单
id:data.id,
orderId:data.orderId,
name:data.productName,
pic:data.pic?data.pic:'/static/image/nopic.png',
price:data.totalAmount,
quantity:data.quantity,
spData: data.spData? JSON.parse(data.spData):null
}
return rets;
}
else{
return rets;
4 months ago
}
},
// 获取状态栏高度
geStatusBarHeight(){
return uni.getSystemInfoSync()['statusBarHeight'];
},
// 获取导航栏高度
getNavBarHeight(){
return 45+uni.getSystemInfoSync()['statusBarHeight'];
},
}
}
</script>
<style lang="scss" scoped>
.page{
padding: 0;
position: relative;
background-image: url('@/static/image/bg.jpg');
background-attachment: fixed;
background-size: cover;
background-position: center center;
min-height: calc(100vh - var(--window-top) - var(--window-bottom));
}
.page-header {
position: fixed;
width: 100%;
z-index: 999;
padding: 40rpx 30rpx 30rpx 30rpx;
display: flex;
color: #fff;
font-size: 36rpx;
3 months ago
background-color: #00a89b;
4 months ago
.text-center {
width: 100%;
text-align: center;
}
.edit {
position: absolute;
top: 50%;
transform: translateY(-50%);
right: 30rpx;
font-size: 32rpx;
}
}
.wrapcon{
padding: 0;
width: 100%;
position: relative;
margin: 180rpx 0 0 0;
1 month ago
min-height: calc(100vh - 180rpx);
4 months ago
/* #ifdef H5 */
margin: calc( 110rpx + var(--window-top)) 0 0 0;
1 month ago
min-height: calc(100vh - var(--window-top) - var(--window-bottom) - 180rpx);
4 months ago
/* #endif */
overflow-y: auto;
}
.listcell{
background: #fff;
margin: 0;
1 month ago
width: 100%;
2 days ago
min-width: 750rpx;
4 months ago
.lcon{
border-bottom: 1rpx solid #eee;
1 month ago
padding-top: 20rpx;
padding-bottom: 20rpx;
4 months ago
opacity: 1;
display: flex;
flex-direction: row;
align-items: center;
1 month ago
width: 100%;
2 days ago
min-width: 750rpx;
4 months ago
.limg{
1 month ago
width: 100rpx;
height: 100rpx;
margin-right: 20rpx;
margin-left: 20rpx;
4 months ago
display: flex;
flex-direction: row;
flex-wrap: wrap;
align-items: center;
justify-content: center;
border-radius: 50%;
.img{
1 month ago
width: 100rpx;
height: 100rpx;
4 months ago
}
}
.lright{
display: flex;
flex: 1;
flex-direction: column;
}
.lrow{
display: flex;
flex-direction: row;
margin-bottom: 20rpx;
margin-right: 20rpx;
}
.pname{
display: flex;
flex: 1;
line-height: 50rpx;
font-size: 32rpx;
font-family: PingFang SC, PingFang SC;
font-weight: 600;
color: #000000;
}
.ptime{
line-height: 50rpx;
font-size: 24rpx;
font-family: PingFang SC, PingFang SC;
font-weight: 500;
color: #595959;
}
.pnr{
display: flex;
flex: 1;
font-size: 24rpx;
font-family: PingFang SC, PingFang SC;
font-weight: 400;
color: #595959;
2 weeks ago
-webkit-box-orient: vertical;
overflow: hidden;
text-overflow: ellipsis;
/* 假设你的行高是20px并且你想要两行显示 */
line-height: 40rpx; /* 每行的行高 */
max-height: 40rpx;
4 months ago
}
.lnum{
background-color: #de0000;
padding: 0 10rpx;
min-width: 32rpx;
height: 32rpx;
line-height: 32rpx;
color: #fff;
font-size: 24rpx;
border-radius: 28rpx;
}
}
}
.tips{
margin-top: 20rpx;
margin-bottom: 20rpx;
width: 100%;
line-height: 44rpx;
font-size: 26rpx;
font-family: PingFang SC, PingFang SC;
font-weight: 400;
color: #595959;
text-align: center;
}
.submitcon{
margin-top: 200rpx;
display: flex;
align-items: center;
justify-content: center;
}
.bbtn{
margin: 20rpx 30rpx 54rpx;
font-size: 32rpx;
font-family: PingFang SC-Medium, PingFang SC;
font-weight: 500;
color: #FCFCFD;
width: 560rpx;
height: 96rpx;
line-height: 96rpx;
background: #367FFA;
box-shadow: 0rpx 12rpx 64rpx 2rpx rgba(67,110,255,0.4);
border-radius: 254rpx 254rpx 254rpx 254rpx;
opacity: 1;
&::after{
border:none;
}
}
.nodata{
background-image: url('../../static/image/nomsg.png');
background-repeat: no-repeat;
background-position: center center;
3 months ago
background-size: 100%;
4 months ago
height: 376rpx;
width: 256rpx;
3 months ago
color: #00a89b;
4 months ago
display: flex;
align-items: center;
justify-content: flex-end;
flex-direction: column;
padding-bottom: 40rpx;
margin: 100rpx auto;
}
</style>