|
|
@@ -224,7 +224,6 @@ export default {
|
|
|
onLoad(options) {
|
|
|
const eventChannel = this.getOpenerEventChannel();
|
|
|
eventChannel.on('acceptDataFromOpenerPage', (data) => {
|
|
|
- this.pageParams = data;
|
|
|
// console.log("---------- pageParams: ", data);
|
|
|
this.initializePage(data);
|
|
|
})
|
|
|
@@ -275,12 +274,6 @@ export default {
|
|
|
init() {},
|
|
|
|
|
|
initializePage(params) {
|
|
|
- if (params.goodsId) {
|
|
|
- this.currentGoodsInfo = {
|
|
|
- ...params
|
|
|
- };
|
|
|
- }
|
|
|
-
|
|
|
if (params.chatPerson) {
|
|
|
// 设置聊天人名称
|
|
|
uni.setNavigationBarTitle({
|
|
|
@@ -288,26 +281,30 @@ export default {
|
|
|
})
|
|
|
}
|
|
|
|
|
|
+ const paramsUserId = this._parseAndValidateId(params.userId, "登录用户数据异常,请稍后再试", false);
|
|
|
+ // if (paramsUserId === null) return;
|
|
|
+
|
|
|
if (this.loginInfo.id) {
|
|
|
- this.currentUser.userId = parseInt(this.loginInfo.id);
|
|
|
- if(this.currentUser.userId == 0) {
|
|
|
- console.error('userId 出错 ---------- this.currentUser.userId: ', this.currentUser.userId);
|
|
|
+ this.currentUser.userId = this._parseAndValidateId(this.loginInfo.id, "登录用户数据异常,请稍后再试");
|
|
|
+ if (this.currentUser.userId === null) return;
|
|
|
+
|
|
|
+ if (paramsUserId != null && this.currentUser.userId !== paramsUserId) {
|
|
|
+ console.error("登录用户数据异常 ---- :", this.currentUser.userId, paramsUserId)
|
|
|
this.$msg("登录用户数据异常,请稍后再试");
|
|
|
return;
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
if (params.customId) {
|
|
|
- this.currentUser.customId = parseInt(params.customId);
|
|
|
- if(this.currentUser.customId == 0) {
|
|
|
- console.error('customId 出错 ---------- this.currentUser.customId: ', this.currentUser.customId);
|
|
|
- }
|
|
|
+ this.currentUser.customId = this._parseAndValidateId(params.customId, "客户数据异常,请稍后再试");
|
|
|
+ if (this.currentUser.customId === null) return;
|
|
|
}
|
|
|
+
|
|
|
if (params.shopId) {
|
|
|
- this.currentUser.shopId = parseInt(params.shopId);
|
|
|
- if(this.currentUser.shopId == 0) {
|
|
|
- console.error('shopId 出错 ---------- this.currentUser.shopId: ', this.currentUser.shopId);
|
|
|
- }
|
|
|
+ this.currentUser.shopId = this._parseAndValidateId(params.shopId, "门店数据异常,请稍后再试");
|
|
|
+ if (this.currentUser.shopId === null) return;
|
|
|
}
|
|
|
+
|
|
|
if (params.name) {
|
|
|
this.currentUser.username = params.name;
|
|
|
}
|
|
|
@@ -324,11 +321,35 @@ export default {
|
|
|
}
|
|
|
// console.log("---------- this.room: ", this.room);
|
|
|
|
|
|
+ if (params.goodsId) {
|
|
|
+ this.currentGoodsInfo = {
|
|
|
+ ...params
|
|
|
+ };
|
|
|
+ }
|
|
|
// 加载聊天记录
|
|
|
this.loadChatHistory();
|
|
|
// 连接WebSocket
|
|
|
this.connectWebSocket();
|
|
|
},
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 解析并验证ID
|
|
|
+ * @param {any} id - 需要解析和验证的ID
|
|
|
+ * @param {string} errorMsg - 验证失败时的错误信息
|
|
|
+ * @returns {number|null} - 验证通过则返回ID,否则返回null
|
|
|
+ */
|
|
|
+ _parseAndValidateId(id, errorMsg, showMsg=true) {
|
|
|
+ const parsedId = parseInt(id);
|
|
|
+ if (isNaN(parsedId) || parsedId <= 0) {
|
|
|
+ console.error(errorMsg + ' ---------- id: ', id);
|
|
|
+ if (showMsg) {
|
|
|
+ this.$msg(errorMsg);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return parsedId;
|
|
|
+ },
|
|
|
+
|
|
|
goBack() {
|
|
|
// 获取事件通道
|
|
|
const eventChannel = this.getOpenerEventChannel()
|
|
|
@@ -354,7 +375,7 @@ export default {
|
|
|
if (res.code == 1 && res.data) {
|
|
|
this.allMessages = res.data.map((item, index) => {
|
|
|
// 判断是否是我(本客户)发送的消息
|
|
|
- const isMine = item.customId ? parseInt(item.customId) == this.currentUser.customId : false;
|
|
|
+ const isMine = item && item.customId ? parseInt(item.customId) == this.currentUser.customId : false;
|
|
|
//console.log("---isMine---: ", isMine);
|
|
|
|
|
|
const newItem = {
|
|
|
@@ -482,6 +503,8 @@ export default {
|
|
|
this.isConnected = true;
|
|
|
this.reconnectCount = 0;
|
|
|
this.isManualDisconnect = false; // 连接成功时重置标志位
|
|
|
+ // 启动心跳
|
|
|
+ this.startHeartbeat();
|
|
|
});
|
|
|
|
|
|
// 监听WebSocket消息
|
|
|
@@ -520,6 +543,37 @@ export default {
|
|
|
}
|
|
|
},
|
|
|
|
|
|
+ /**
|
|
|
+ * 启动心跳
|
|
|
+ */
|
|
|
+ startHeartbeat() {
|
|
|
+ if (this.heartbeatTimer) {
|
|
|
+ clearInterval(this.heartbeatTimer);
|
|
|
+ }
|
|
|
+ this.heartbeatTimer = setInterval(() => {
|
|
|
+ if (this.isConnected) {
|
|
|
+ this.socket.send({
|
|
|
+ data: '*hb*',
|
|
|
+ success: () => {
|
|
|
+ // console.log('发送ping');
|
|
|
+ },
|
|
|
+ fail: (error) => {
|
|
|
+ console.error('发送ping失败:', error);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }, 30000); // 每30秒发送一次
|
|
|
+ },
|
|
|
+ /**
|
|
|
+ * 停止心跳
|
|
|
+ */
|
|
|
+ stopHeartbeat() {
|
|
|
+ if (this.heartbeatTimer) {
|
|
|
+ clearInterval(this.heartbeatTimer);
|
|
|
+ this.heartbeatTimer = null;
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
// 构建WebSocket子协议参数(模拟POST body传输)
|
|
|
buildWebSocketProtocols() {
|
|
|
try {
|
|
|
@@ -754,6 +808,8 @@ export default {
|
|
|
this.socket = null;
|
|
|
this.isConnected = false;
|
|
|
}
|
|
|
+ // 停止心跳
|
|
|
+ this.stopHeartbeat();
|
|
|
},
|
|
|
|
|
|
// 重连WebSocket
|
|
|
@@ -806,8 +862,6 @@ export default {
|
|
|
// }
|
|
|
|
|
|
const messageType = messageData.messageType || "text";
|
|
|
- console.log('messageType -- ', messageType);
|
|
|
-
|
|
|
// 创建消息对象
|
|
|
const newMessage = {
|
|
|
id: "msg_receive_" + Math.floor(Date.now() / 1000) + "_" + Math.floor(Math.random() * 100000) + 1,
|
|
|
@@ -900,7 +954,7 @@ export default {
|
|
|
this.socket.send({
|
|
|
data: json,
|
|
|
success: () => {
|
|
|
- console.log("消息发送成功");
|
|
|
+ // console.log("消息发送成功");
|
|
|
},
|
|
|
fail: (error) => {
|
|
|
console.error("消息发送失败:", error);
|
|
|
@@ -922,13 +976,13 @@ export default {
|
|
|
|
|
|
// 处理连接错误
|
|
|
handleConnectionError() {
|
|
|
- uni.showToast({
|
|
|
- title: "连接失败",
|
|
|
- icon: "none",
|
|
|
- duration: 2000,
|
|
|
- });
|
|
|
- // 清除所有连接
|
|
|
- this.disconnectWebSocket();
|
|
|
+ // uni.showToast({
|
|
|
+ // title: "连接失败",
|
|
|
+ // icon: "none",
|
|
|
+ // duration: 2000,
|
|
|
+ // });
|
|
|
+
|
|
|
+ console.error('连接失败--', this.reconnectCount);
|
|
|
},
|
|
|
|
|
|
// 滚动到底部
|
|
|
@@ -1094,7 +1148,7 @@ export default {
|
|
|
const userId = parseInt(keyArr[1]);
|
|
|
const goodsId = parseInt(keyArr[2]);
|
|
|
if (isNaN(shopId) || isNaN(userId) || isNaN(goodsId)) {
|
|
|
- //console.log("isNaN(shopId) || isNaN(userId) || isNaN(goodsId)", shopId, userId, goodsId)
|
|
|
+ console.error("isNaN(shopId) || isNaN(userId) || isNaN(goodsId)", shopId, userId, goodsId)
|
|
|
return parsed;
|
|
|
}
|
|
|
|
|
|
@@ -1124,7 +1178,7 @@ export default {
|
|
|
}
|
|
|
}
|
|
|
}).catch(err => {
|
|
|
- console.log("getQuotedPrice error: ", key, err)
|
|
|
+ console.error("getQuotedPrice error: ", key, err)
|
|
|
})
|
|
|
}
|
|
|
return parsed;
|
|
|
@@ -1154,23 +1208,40 @@ export default {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- this.$util.pageTo({
|
|
|
- url: "/pages/goods/detail",
|
|
|
- query: { // id=2020&categoryId=1402&hdId=229&account=36548&shopId=36548&name=花仙子&avatar=https://img.theflorist.cn/hhb_small.png
|
|
|
- id: goodsId,
|
|
|
- account: goodsInfo.account,
|
|
|
- hdId: goodsInfo.hdId,
|
|
|
- categoryId: categoryId,
|
|
|
- shopId: goodsInfo.shopId,
|
|
|
- name: goodsInfo.name,
|
|
|
- avatar: goodsInfo.avatar,
|
|
|
- },
|
|
|
+ // this.$util.pageTo({
|
|
|
+ // url: "/pages/goods/detail",
|
|
|
+ // query: { // id=2020&categoryId=1402&hdId=229&account=36548&shopId=36548&name=花仙子&avatar=https://img.theflorist.cn/hhb_small.png
|
|
|
+ // id: goodsId,
|
|
|
+ // account: goodsInfo.account,
|
|
|
+ // hdId: goodsInfo.hdId,
|
|
|
+ // categoryId: categoryId,
|
|
|
+ // shopId: goodsInfo.shopId,
|
|
|
+ // name: goodsInfo.name,
|
|
|
+ // avatar: goodsInfo.avatar,
|
|
|
+ // },
|
|
|
+ // });
|
|
|
+
|
|
|
+ const params = {
|
|
|
+ id: goodsId,
|
|
|
+ account: goodsInfo.account,
|
|
|
+ hdId: goodsInfo.hdId,
|
|
|
+ categoryId: categoryId,
|
|
|
+ shopId: goodsInfo.shopId,
|
|
|
+ name: goodsInfo.name,
|
|
|
+ avatar: goodsInfo.avatar,
|
|
|
+ };
|
|
|
+
|
|
|
+ uni.navigateTo({
|
|
|
+ url: '/pages/goods/detail',
|
|
|
+ success: function(res) {
|
|
|
+ res.eventChannel.emit('acceptDataFromChatPage', params)
|
|
|
+ }
|
|
|
});
|
|
|
},
|
|
|
// 去购买
|
|
|
toBuy(goodsInfo) {
|
|
|
this.disconnectWebSocket();// 离开本页面,必须断开WebSocket连接
|
|
|
- console.log("---------- goodsInfo: ", goodsInfo);
|
|
|
+ // console.log("---------- goodsInfo: ", goodsInfo);
|
|
|
const goodsId = parseInt(goodsInfo.goodsId);
|
|
|
const categoryId = parseInt(goodsInfo.categoryId);
|
|
|
const hdId = parseInt(goodsInfo.hdId);
|
|
|
@@ -1178,25 +1249,45 @@ export default {
|
|
|
const price = parseFloat(goodsInfo.goodsPrice).toFixed(2);
|
|
|
const priceType = goodsInfo.goodsPriceType;
|
|
|
if (isNaN(goodsId) || isNaN(categoryId) || isNaN(hdId) || isNaN(account) || isNaN(price)) {
|
|
|
- console.log("商品数据异常", goodsId, categoryId, hdId, account, price)
|
|
|
+ console.error("商品数据异常", goodsId, categoryId, hdId, account, price)
|
|
|
this.$msg("商品数据异常");
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- this.$util.pageTo({
|
|
|
- url: "/pages/goods/detail",
|
|
|
- query: {
|
|
|
- id: goodsId,
|
|
|
- account: goodsInfo.account,
|
|
|
- hdId: goodsInfo.hdId,
|
|
|
- categoryId: categoryId,
|
|
|
- shopId: goodsInfo.shopId,
|
|
|
- name: goodsInfo.name,
|
|
|
- avatar: goodsInfo.avatar,
|
|
|
- price: price,
|
|
|
- priceType: priceType,
|
|
|
- key: goodsInfo.key,
|
|
|
- },
|
|
|
+ // this.$util.pageTo({
|
|
|
+ // url: "/pages/goods/detail",
|
|
|
+ // query: {
|
|
|
+ // id: goodsId,
|
|
|
+ // account: goodsInfo.account,
|
|
|
+ // hdId: goodsInfo.hdId,
|
|
|
+ // categoryId: categoryId,
|
|
|
+ // shopId: goodsInfo.shopId,
|
|
|
+ // name: goodsInfo.name,
|
|
|
+ // avatar: goodsInfo.avatar,
|
|
|
+ // price: price,
|
|
|
+ // priceType: priceType,
|
|
|
+ // key: goodsInfo.key,
|
|
|
+ // },
|
|
|
+ // });
|
|
|
+
|
|
|
+ const params = {
|
|
|
+ id: goodsId,
|
|
|
+ account: goodsInfo.account,
|
|
|
+ hdId: goodsInfo.hdId,
|
|
|
+ categoryId: categoryId,
|
|
|
+ shopId: goodsInfo.shopId,
|
|
|
+ name: goodsInfo.name,
|
|
|
+ avatar: goodsInfo.avatar,
|
|
|
+ price: price,
|
|
|
+ priceType: priceType,
|
|
|
+ key: goodsInfo.key,
|
|
|
+ };
|
|
|
+
|
|
|
+ uni.navigateTo({
|
|
|
+ url: '/pages/goods/detail',
|
|
|
+ success: function(res) {
|
|
|
+ res.eventChannel.emit('acceptDataFromChatPage', params)
|
|
|
+ }
|
|
|
});
|
|
|
},
|
|
|
},
|