聊天问题修复优化

This commit is contained in:
2025-12-05 19:19:54 +08:00
parent b534344b25
commit 28041ddb09
6 changed files with 1322 additions and 382 deletions

View File

@@ -40,12 +40,14 @@
>
<view class="user u-flex u-row-right">
<view class="u-p-r-18">
<view class="color-000 u-line-1 text-right">{{ item.nick_name }}</view>
<view class="color-000 u-line-1 text-right">{{
item.nick_name
}}</view>
<view
class="u-m-t-14 msg u-p-l-30"
:class="['type' + item.msg_type]"
>
<chatItem :item="item"></chatItem>
<chatItem :item="item" @previewImage="previewImage"></chatItem>
</view>
</view>
<up-avatar
@@ -60,21 +62,26 @@
<template v-else>
<view class="shop u-flex">
<up-avatar
:src="shopInfo.logo"
:src="item.avatar"
size="122rpx"
shape="square"
bg-color="#fff"
></up-avatar>
<view class="u-p-l-18">
<view class="u-flex">
<view class="tag">商家</view>
<view class="color-000">{{ shopInfo.shopName }}</view>
<template v-if="item.is_shop == 1">
<view class="tag">商家</view>
<view class="color-000">{{ item.nick_name }}</view>
</template>
<template v-else>
<view class="color-000">{{ item.nick }}</view>
</template>
</view>
<view
class="u-m-t-14 msg"
:class="['type' + item.msg_type]"
>
<chatItem :item="item" @getCoupon="getCoupon"></chatItem>
<chatItem :item="item" @getCoupon="getCoupon" @previewImage="previewImage"></chatItem>
</view>
</view>
</view>
@@ -92,7 +99,11 @@
<!-- <view :style="bottomSlotHeight"></view> -->
<view class="bottom" :class="[showMoreBtn ? '' : 'safe-bottom']">
<view class="u-flex" style="padding: 14rpx 28rpx" v-if="groupInfo&&!groupInfo.is_mute">
<view
class="u-flex"
style="padding: 14rpx 28rpx"
v-if="groupInfo && !groupInfo.is_mute"
>
<!-- <input
type="text"
class="u-flex-1 u-m-r-52 iput"
@@ -112,12 +123,14 @@
<up-icon
name="plus-circle"
color="#666"
size="40rpx"
size="60rpx"
@click="showMoreBtnToggle"
v-else
></up-icon>
</view>
<view class="color-666 u-font-28 text-center u-m-t-28" v-else>商家已禁言</view>
<view class="color-666 u-font-28 text-center u-m-t-28" v-else
>商家已禁言</view
>
<view class="more-btn" v-if="showMoreBtn">
<view
v-for="(item, index) in moreBtns"
@@ -144,7 +157,7 @@
<view class="header-wrap">
<text class="t">退出群{{ "{" + groupInfo.name + "}" }}</text>
<view class="close" @click="popupShow = false">
<u-icon name="close" size="16" color="#666"></u-icon>
<u-icon name="close" size="20" color="#666"></u-icon>
</view>
</view>
<view class="u-p-40 text-center u-font-28 color-333 border-bottom"
@@ -168,10 +181,16 @@
</view>
</view>
</u-popup>
<!-- 预览图 -->
<xbSwiperPreview :visable="showPrveImg" :imgs="prveImgsList" @update:visable="showPrveImg = $event"></xbSwiperPreview>
</template>
<script setup>
import {
onReady,
onShow,
onReachBottom,
onLoad,
onPageScroll,
@@ -183,12 +202,18 @@ import {
nextTick,
reactive,
watch,
onUnmounted,
computed,
} from "vue";
import { useChatStore } from "@/stores/chat";
import * as chatApi from "@/http/php/chat";
import { uploadFile } from "@/common/api/upload.js";
import * as javaChatApi from "@/common/api/market/chat";
import xbSwiperPreview from '@/components/xb-swiper-preview/index.vue';
const showPrveImg = ref(false);
const prveImgsList = ref([]);
function exitGroup() {
chatApi
@@ -214,7 +239,8 @@ function getCoupon(item) {
javaChatApi
.couponGrant({
id: item.coupon.activity_id,
shopUserId: shopInfo.id,
// shopUserId: shopUserInfo.id,
shopId: groupInfo.value.shop_id,
userId: uni.cache.get("userInfo").id,
})
.then((res) => {
@@ -224,13 +250,12 @@ function getCoupon(item) {
icon: "none",
duration: 2000,
});
refresh()
}
});
}
function refresh() {
query.page=1;
function refresh() {
query.page = 1;
getMsgList();
}
const go = {
@@ -282,12 +307,7 @@ const modalData = reactive({
});
const chatStore = useChatStore();
chatStore.onReceiveMsg = (msg) => {
nextTick(() => {
scrollView.intoView = "msg-0";
});
};
chatStore.connectSocket();
const msg = ref("");
const shopInfo = uni.cache.get("shopInfo");
@@ -323,67 +343,222 @@ function moreBtnsClick(item, index) {
sendVideo();
}
}
function videoErrorCallback(e) {
console.error("视频播放失败", e);
}
function sendImg() {
uni.chooseImage({
count: 3, //默认9
sizeType: ["original", "compressed"], //可以指定是原图还是压缩图,默认二者都有
sourceType: ["album", "camera "],
success: async function (res) {
uni.showLoading({
title: "发送中",
});
console.log(res);
for (let i = 0; i < res.tempFiles.length; i++) {
const fileRes = await uploadFile(res.tempFiles[i]);
if (fileRes) {
sendMsg({
image_url: fileRes,
msg_type: 2,
});
} else {
// 通用相机权限校验函数参考openLocationAuth逻辑
async function checkCameraAuth() {
return new Promise((resolve, reject) => {
try {
// 1. 检查当前相机权限状态
uni.getSetting({
success: (settingRes) => {
const authSetting = settingRes.authSetting || {};
if (authSetting["scope.camera"]) {
// 2. 已授权:直接返回成功
resolve(true);
} else if (authSetting["scope.camera"] === undefined) {
// 3. 未请求过授权:发起授权请求
uni.authorize({
scope: "scope.camera",
success: () => {
// 授权成功
resolve(true);
},
fail: (authErr) => {
// 授权失败(用户拒绝)
console.error("相机授权失败:", authErr);
reject(false);
}
});
} else {
// 4. 已拒绝授权:提示用户去设置页开启
uni.showModal({
title: "开启相机权限",
content: "请允许“零点八零”使用您的相机,方便您拍摄图片/视频",
confirmText: "去设置",
cancelText: "取消",
success: (modalRes) => {
if (modalRes.confirm) {
// 跳转微信小程序授权设置页
uni.openSetting({
success: (openRes) => {
if (openRes.authSetting["scope.camera"]) {
// 用户在设置页开启授权
resolve(true);
} else {
// 用户未开启授权
reject(false);
}
},
fail: (openErr) => {
console.error("打开设置页失败:", openErr);
reject(false);
}
});
} else {
// 用户取消设置
reject(false);
}
},
fail: (modalErr) => {
console.error("弹窗失败:", modalErr);
reject(false);
}
});
}
},
fail: (settingErr) => {
console.error("获取权限设置失败:", settingErr);
reject(false);
}
}
},
});
} catch (err) {
console.error("相机权限校验异常:", err);
reject(false);
}
});
}
function sendVideo() {
uni.chooseVideo({
count: 1, //默认9
sizeType: ["original", "compressed"], //可以指定是原图还是压缩图,默认二者都有
sourceType: ["album", "camera "],
success: async function (res) {
uni.showLoading({
title: "发送中",
// 图片选择与发送(优化后)
async function sendImg() {
try {
// 1. 调用相机权限校验
const isAuth = await checkCameraAuth();
if (!isAuth) {
// 权限校验失败,终止流程
return;
}
// 2. 权限校验通过调用图片选择API
const res = await new Promise((resolve, reject) => {
uni.chooseImage({
count: 3,
sizeType: ["original", "compressed"],
sourceType: ["album", "camera"],
success: resolve,
fail: (err) => {
// 捕获选择失败(如用户取消选择)
if (err.errMsg && err.errMsg.includes("cancel")) {
// 用户主动取消,静默处理
reject(new Error("user cancel"));
} else {
// 其他错误(如系统异常)
reject(err);
}
}
});
console.log(res);
const fileRes = await uploadFile({ path: res.tempFilePath });
uni.hideLoading();
});
uni.showLoading({ title: "发送中" });
console.log("选择图片成功", res);
// 3. 批量上传图片
for (let i = 0; i < res.tempFiles.length; i++) {
const fileRes = await uploadFile(res.tempFiles[i]);
if (fileRes) {
sendMsg({
image_url: fileRes,
msg_type: 5,
});
} else {
uni.showToast({
title: "发送失败",
icon: "none",
msg_type: 2,
});
}
},
});
}
} catch (err) {
console.error("图片选择/发送失败", err);
// 仅处理非用户取消的错误
if (err.message !== "user cancel") {
uni.showToast({
title: "图片发送失败",
icon: "none",
duration: 2000
});
}
} finally {
uni.hideLoading();
}
}
// 视频选择与发送(优化后)
async function sendVideo() {
try {
// 1. 调用相机权限校验(与图片共用)
const isAuth = await checkCameraAuth();
if (!isAuth) {
// 权限校验失败,终止流程
return;
}
// 2. 权限校验通过调用视频选择API
const res = await new Promise((resolve, reject) => {
uni.chooseVideo({
count: 1,
sizeType: ["original", "compressed"],
sourceType: ["album", "camera"],
success: resolve,
fail: (err) => {
// 捕获选择失败(如用户取消选择)
if (err.errMsg && err.errMsg.includes("cancel")) {
// 用户主动取消,静默处理
reject(new Error("user cancel"));
} else {
// 其他错误(如系统异常)
reject(err);
}
}
});
});
uni.showLoading({ title: "发送中" });
console.log("选择视频成功", res);
// 3. 上传视频
const fileRes = await uploadFile({ path: res.tempFilePath });
if (fileRes) {
sendMsg({
image_url: fileRes,
msg_type: 5,
});
} else {
uni.showToast({
title: "视频发送失败",
icon: "none",
duration: 2000
});
}
} catch (err) {
console.error("视频选择/发送失败", err);
// 仅处理非用户取消的错误
if (err.message !== "user cancel") {
uni.showToast({
title: "视频发送失败",
icon: "none",
duration: 2000
});
}
} finally {
uni.hideLoading();
}
}
const groupInfo = ref({});
const handleReceiveMsg = (msg) => {
nextTick(() => {
scrollView.intoView = "msg-0";
});
};
// 注册消息回调
chatStore.registerReceiveMsgCallback(handleReceiveMsg);
async function init() {
const res = await chatApi.groupInfo({
group_id: options.group_id,
});
console.log(res);
groupInfo.value = res || {};
if (res) {
chatStore.shop_id = groupInfo.value.shop_id;
chatStore.connectSocket();
chatStore.init();
}
getMsgList();
}
const query = reactive({
@@ -395,7 +570,6 @@ async function getMsgList() {
if (isEnd.value) {
scrollView.refresherTriggered = false;
isLoading.value = false;
uni.showToast({ title: "没有更多了", icon: "none" });
return;
}
@@ -443,8 +617,9 @@ onLoad((opt) => {
Object.assign(options, opt);
init();
chatApi.messageMarkReadAll({
session_ids: options.session_id,
session_ids: options.group_id,
});
chatStore.group_id = options.group_id;
chatApi.groupMembers({ group_id: options.group_id }).then((res) => {
console.log(res);
membersRes.value = res;
@@ -474,7 +649,7 @@ function sendMsg(msg) {
content: msg.value,
image_url: "",
order_id: "",
session_id: "",
session_id: groupInfo.value.session_id || options.session_id,
...msg,
});
}
@@ -519,9 +694,9 @@ function confirmCoupon() {
}
function previewImage(url) {
uni.previewImage({
urls: [url],
});
prveImgsList.value = [url];
showPrveImg.value = true;
}
//是否到底了
@@ -556,6 +731,18 @@ const pageHeight = computed(() => {
}
return "";
});
onShow(() => {
// 页面显示时,检查连接状态
if (!chatStore.isConnect || !chatStore.socketTask) {
console.log("聊天页显示检查Socket连接");
chatStore.forceReconnect();
}
});
onUnmounted(() => {
// 组件卸载时,移除消息回调
chatStore.removeReceiveMsgCallback(handleReceiveMsg);
});
</script>
<style lang="scss" scoped>
.top {