更换socket

master
2023-01-12 14:35:24 +08:00
parent bbe315fb45
commit 83b2295818
2 changed files with 848 additions and 733 deletions

View File

@ -10,8 +10,7 @@
<!-- 消息 -->
<view class="flex-column-start" v-if="msgList.length" v-for="(item, index) in msgList" :key="index">
<view class="flex-row-start column-time">
<view v-show="compareTime(index, item.createTime)" class="flex-row-start date-text"
>
<view v-show="compareTime(index, item.createTime)" class="flex-row-start date-text">
</view>
</view>
<!-- 用户消息 头像可选加入-->
@ -20,11 +19,14 @@
<view>
<view class="user-name">{{ user.nickName }}</view>
<view class="margin-left padding-chat bg-user-orang" style="border-radius: 35rpx; ">
<text style="word-break: break-all;" v-if="item.messageType === 'MESSAGE'">{{ item.text }}</text>
<!-- <view v-if="item.messageType == 'GOODS'">
<view class="goodsCard u-flex u-row-between u-p-b-0" style="width:100%;margin: 0 0; ">
<text style="word-break: break-all;"
v-if="item.messageType === 'MESSAGE'">{{ item.text }}</text>
<view v-if="item.messageType == 'GOODS'">
<view class="goodsCard u-flex u-row-between u-p-b-0"
style="width:100%;margin: 0 0; ">
<view class="imagebox" @click="jumpGoodDelic">
<image class="image" :src="JSON.parse(item.text)['thumbnail']" mode="widthFix"></image>
<image class="image" :src="JSON.parse(item.text)['thumbnail']"
mode="widthFix"></image>
</view>
<view class="goodsdesc" @click="jumpGoodDelic">
<view class="goodsdesc-name">
@ -39,49 +41,49 @@
</view>
</view>
</view>
</view> -->
<!-- <view v-if="item.messageType === 'ORDER'">
</view>
<view v-if="item.messageType == 'ORDER'">
<view class="orderSn">
<text>订单号{{ JSON.parse(item.text)['sn'] }}</text>
<view class="oederList">
<img style="height: 120rpx; width: 120rpx; margin-top: 15rpx;"
:src="JSON.parse(item.text)['groupImages']" mode="widthFix" />
<view class="groupNameOrTime">
<text @click="linkTosOrders(JSON.parse(item.text)['sn'])">{{ JSON.parse(item.text)['groupName']
<text @click="linkTosOrders(item.text)">{{ JSON.parse(item.text)['groupName']
}}</text>
<view class="orderTime"> <text>{{ JSON.parse(item.text)['paymentTime'] }}</text></view>
<view class="orderTime">
<text>{{ JSON.parse(item.text)['paymentTime'] }}</text>
</view>
</view>
</view>
</view>
</view>
</view> -->
</view>
</view>
</view>
<view>
<u-avatar :src="user.face" :text="user.face ? '' : user.name" bg-color="#DDDDDD"></u-avatar>
<!-- <u-image class="chat-img margin-left" style="height: 100rpx;width: 100rpx;" shape="circle"
:src="user.face || 'https://avatars.dicebear.com/api/initials/' + user.nickName + '.svg?fontSize=38'"
mode="aspectFill"></u-image> -->
</view>
</view>
<!-- 接收人消息 -->
<view v-else class="flex-row-start margin-left margin-top one-show">
<view class="chat-img flex-row-center">
<u-avatar :src="toUser.face" :text="toUser.face ? '' : toUser.name" bg-color="#DDDDDD"></u-avatar>
<!-- <u-image style="height: 100rpx;width: 100rpx;" shape="circle"
:src="toUser.face || 'https://avatars.dicebear.com/api/initials/' + toUser.name + '.svg?fontSize=38'"
mode="aspectFit"></u-image> -->
<u-avatar :src="toUser.face" :text="toUser.face ? '' : toUser.name" bg-color="#DDDDDD">
</u-avatar>
</view>
<view class="flex" style="width: 500rpx;">
<view>
<view class="other-name">{{ toUser.name }}</view>
<view class="margin-left padding-chat flex-column-start bg-to-color" style="border-radius: 35rpx;">
<text style="word-break: break-all;" v-if="item.messageType === 'MESSAGE'">{{ item.text }}</text>
<view class="margin-left padding-chat flex-column-start bg-to-color"
style="border-radius: 35rpx;">
<text style="word-break: break-all;"
v-if="item.messageType === 'MESSAGE'">{{ item.text }}</text>
<view v-if="item.messageType === 'GOODS'">
<!-- <view class="goodsCard u-flex u-row-between u-p-b-0" style="width:100%;margin: 0 0; ">
<view class="goodsCard u-flex u-row-between u-p-b-0"
style="width:100%;margin: 0 0; ">
<view class="imagebox" @click="jumpGoodDelic">
<image class="image" :src="JSON.parse(item.text)['thumbnail']" mode="widthFix"></image>
<image class="image" :src="JSON.parse(item.text)['thumbnail']"
mode="widthFix"></image>
</view>
<view class="goodsdesc" @click="jumpGoodDelic">
<view class="goodsdesc-name">
@ -95,22 +97,24 @@
}}</text>
</view>
</view>
</view> -->
</view>
<!-- <view v-if="item.messageType === 'ORDER'">
</view>
<view v-if="item.messageType === 'ORDER'">
<view class="orderSn">
<text>订单号{{ JSON.parse(item.text)['sn'] }}</text>
<view class="oederList">
<img style="height: 120rpx; width: 120rpx; margin-top: 15rpx;"
:src="JSON.parse(item.text)['groupImages']" mode="widthFix" />
<view class="groupNameOrTime">
<text @click="linkTosOrders(JSON.parse(item.text)['sn'])">{{ JSON.parse(item.text)['groupName']
<text @click="linkTosOrders(item.text)">{{ JSON.parse(item.text)['groupName']
}}</text>
<view class="orderTime"> <text>{{ JSON.parse(item.text)['paymentTime'] }}</text></view>
<view class="orderTime">
<text>{{ JSON.parse(item.text)['paymentTime'] }}</text>
</view>
</view>
</view>
</view>
</view>
</view> -->
</view>
</view>
@ -131,7 +135,8 @@
goodListData.goodsName
}}</text>
</view>
<view class="goodsdesc-rice" style="margin-top:10rpx; color: orange;"><text style="font-size:20rpx;">¥{{
<view class="goodsdesc-rice" style="margin-top:10rpx; color: orange;"><text
style="font-size:20rpx;">¥{{
goodListData.price
}}</text>
</view>
@ -151,9 +156,9 @@
<view class="flex" style="width: 500rpx;">
<view class="margin-left padding-chat flex-column-start"
style="border-radius: 35rpx;background-color: #f9f9f9;">
<view class="cuIcon-loading turn-load" style="font-size: 35rpx;color: #3e9982;">
<!-- <view class="cuIcon-loading turn-load" style="font-size: 35rpx;color: #3e9982;">
</view>
</view> -->
</view>
</view>
</view>
@ -165,8 +170,9 @@
<view class="flex-column-center" style="position: fixed;bottom: -180px;" :animation="animationData">
<view class="bottom-dh-char flex-row-around" style="font-size: 55rpx;">
<!-- vue无法使用软键盘"发送" -->
<input v-model="msg" class="dh-input" type="text" style="background-color: #f0f0f0;" @confirm="sendMessage"
confirm-type="search" placeholder-class="my-neirong-sm" placeholder="用一句简短的话描述您的问题" />
<input v-model="msg" class="dh-input" type="text" style="background-color: #f0f0f0;"
@confirm="sendMessage" confirm-type="search" placeholder-class="my-neirong-sm"
placeholder="用一句简短的话描述您的问题" />
<view @click="sendMessage" class="cu-tag bg-cyan round">
发送
</view>
@ -194,20 +200,36 @@ var l
var wh
//
var mgUpHeight
import { getTalkMessage, getTalkByUser, jumpObtain } from "@/api/im.js";
import {
getTalkMessage,
getTalkByUser,
jumpObtain
} from "@/api/im.js";
import SocketService from "@/utils/socket_service.js";
import storage from "@/utils/storage.js";
import { beautifyTime } from "@/utils/filters.js"
import {
beautifyTime
} from "@/utils/filters.js"
import config from '@/config/config.js'
export default {
// imGoodId
onUnload() {
storage.setImGoodsLink('')
// #ifdef H5
uni.setStorageSync("imGoodId", '');
// #endif
if (this.socketOpen == true) {
uni.closeSocket();
}
},
onLoad(options) {
this.sokcet();
// goodsid
this.showHideModel = options.goodsid
// localimGoodId
this.localImGoodsId = storage.getImGoodsLink()
// #ifdef H5
this.localImGoodsId = uni.getStorageSync("imGoodId");
// #endif
this.resolve = options
//
if (this.resolve.goodsid) {
@ -249,7 +271,8 @@ export default {
}
this.ws.connect();
// this.ws.connect();
this.sokcet()
},
onPullDownRefresh() {
this.params.pageNumber = this.params.pageNumber + 1
@ -261,6 +284,7 @@ export default {
data() {
return {
socketOpen: false, //
storage,
fixed: 'fixed',
bottom: '50px',
@ -290,38 +314,54 @@ export default {
goodListData: {}
}
},
watch: {
'ws.callBackMapping': {
handler: function (val) {
val = JSON.parse(val)
if (val.messageResultType == 'MESSAGE') {
this.msgList.push(val.result)
}
this.newMessageNum++;
//
let msg = val
msg.operation_type = 'READ'
this.ws.send(JSON.stringify(msg))
}
}
},
// watch: {
// 'ws.callBackMapping': {
// handler: function (val) {
// val = JSON.parse(val)
// if (val.messageResultType == 'MESSAGE') {
// this.msgList.push(val.result)
// }
// this.newMessageNum++;
// //
// let msg = val
// msg.operation_type = 'READ'
// this.ws.send(JSON.stringify(msg))
// }
// }
// },
methods: {
beautifyTime,
//
linkTosOrders (val) {
console.log(val);
uni.navigateTo({
url: '/pages/order/orderDetail?sn=' + val,
sendMessage() {
if (this.msg == "") {
return 0;
}
if (this.socketOpen == false) {
return
}
let msg = {
operation_type: "MESSAGE",
to: this.toUser.userId,
from: this.user.id,
message_type: "MESSAGE",
context: this.msg,
talk_id: this.params.talkId,
}
let data = JSON.stringify(msg);
try {
uni.sendSocketMessage({
data: data
});
this.msgList.push({
"text": this.msg,
"my": true,
"messageType": 'MESSAGE'
})
let type = 'down';
this.msgGo(type)
this.msg = ""
} catch (e) {
uni.closeSocket();
}
},
//
jumpGoodDelic () {
uni.navigateTo({
url: `/pages/product/goods?id=${this.resolve.skuid}&goodsId=${this.resolve.goodsid}`,
});
},
//
sendGoodsMessage() {
let msg = {
operation_type: "MESSAGE",
@ -331,19 +371,88 @@ export default {
context: this.goodListData,
talk_id: this.params.talkId,
}
this.ws.send(JSON.stringify(msg))
this.msgList.push({ "text": JSON.stringify(this.goodListData), "my": true, "messageType": 'GOODS' })
let data = JSON.stringify(msg);
uni.sendSocketMessage({
data: data
});
this.msgList.push({
"text": JSON.stringify(this.goodListData),
"my": true,
"messageType": 'GOODS'
})
this.showHide = false
storage.setImGoodsLink(this.params.talkId)
//
// #ifdef H5
uni.setStorageSync("imGoodId", 1111111);
// #endif
this.$nextTick(() => {
uni.pageScrollTo({
scrollTop: 2000000,
duration: 0
duration: 300
});
})
},
sokcet() {
var _this = this;
uni.closeSocket();
this.socketOpen = false;
try {
//WebSocket
var url = config.baseWsUrl + '/' + storage.getAccessToken();
//
uni.connectSocket({
url: url,
});
// WebSocket
uni.onSocketOpen(function(res) {
_this.socketOpen = true;
});
//
uni.onSocketError(function(err) {
if (err && err.code !== 1000) {
setTimeout(function() {
_this.socketOpen = true;
uni.connectSocket({
url: url,
});
}, 5 * 1000)
}
});
//
uni.onSocketClose(function(err) {
if (err && err.code !== 1000) {
setTimeout(function() {
_this.socketOpen = true;
uni.connectSocket({
url: url,
});
}, 5 * 1000)
}
});
//
uni.onSocketMessage(function(res) {
uni.hideLoading()
})
} catch (e) {
}
},
beautifyTime,
//
linkTosOrders(val) {
let order = JSON.parse(val)
uni.navigateTo({
url: '/pages/order/orderDetail?sn=' + order.sn,
});
},
//
jumpGoodDelic() {
uni.navigateTo({
url: `/pages/product/goods?id=${this.resolve.skuid}&goodsId=${this.resolve.goodsid}`,
});
},
//
cancelModel() {
this.showHide = false
@ -423,30 +532,35 @@ export default {
// id,index
},
sendMessage () {
//
if (this.msg == "") {
return 0;
}
// msg,my(,穿)
let msg = {
operation_type: "MESSAGE",
to: this.toUser.userId,
from: this.user.id,
message_type: "MESSAGE",
context: this.msg,
talk_id: this.params.talkId,
}
this.ws.send(JSON.stringify(msg))
this.msgList.push({ "text": this.msg, "my": true, "messageType": 'MESSAGE' })
//
let type = 'down';
this.msgGo(type)
//
// this.msgKf(this.msg)
//
this.msg = ""
},
// sendMessage() {
// //
// if (this.msg == "") {
// return 0;
// }
// // msg,my(,穿)
// let msg = {
// operation_type: "MESSAGE",
// to: this.toUser.userId,
// from: this.user.id,
// message_type: "MESSAGE",
// context: this.msg,
// talk_id: this.params.talkId,
// }
// this.ws.send(JSON.stringify(msg))
// this.msgList.push({
// "text": this.msg,
// "my": true,
// "messageType": 'MESSAGE'
// })
// console.log(this.msgList, 'this.msgListthis.msgListthis.msgListthis.msgListthis.msgList')
// //
// let type = 'down';
// this.msgGo(type)
// //
// // this.msgKf(this.msg)
// //
// this.msg = ""
// },
// msgKf(x) {
// // loading
// // this.msgLoad = true
@ -503,7 +617,6 @@ export default {
})
}
})
this.msgGo(type)
},
touchMoreMessage(e) {
@ -534,8 +647,10 @@ export default {
if (this.msgList[index].is_revoke == 1) {
return false;
}
if (datetime) {
datetime = datetime.replace(/-/g, "/");
}
let time = Math.floor(Date.parse(datetime) / 1000);
let currTime = Math.floor(new Date().getTime() / 1000);
@ -546,7 +661,7 @@ export default {
return true;
}
let nextDate
if (this.msgList[index + 1]) {
if (this.msgList[index + 1] && this.msgList[index + 1].createTime) {
nextDate = this.msgList[index + 1].createTime.replace(/-/g, "/");
if (nextDate - datetime < 300) return false;
}
@ -585,9 +700,9 @@ export default {
if (new RegExp("(" + k + ")").test(_format))
_format = _format.replace(
RegExp.$1,
RegExp.$1.length === 1
? o[k]
: ("00" + o[k]).substr(("" + o[k]).length)
RegExp.$1.length === 1 ?
o[k] :
("00" + o[k]).substr(("" + o[k]).length)
);
return _format;
},

View File

@ -601,10 +601,10 @@ export default {
linkMsgDetail () {
// lili
this.$options.filters.talkIm(this.storeDetail.storeId)
// uni.navigateTo({
// url: `/pages/mine/im/index?userId=${this.goodsDetail.storeId}&goodsid=${this.routerVal.goodsId}&skuid=${this.routerVal.id}`
// });
// this.$options.filters.talkIm(this.storeDetail.storeId)
uni.navigateTo({
url: `/pages/mine/im/index?userId=${this.goodsDetail.storeId}&goodsid=${this.routerVal.goodsId}&skuid=${this.routerVal.id}`
});
// udesk
// if (this.storeDetail.merchantEuid) {