聊天功能优化,部分问题修复

This commit is contained in:
2025-12-05 09:37:30 +08:00
parent 6590a3514b
commit 885ef57c93
9 changed files with 554 additions and 159 deletions

View File

@@ -59,6 +59,7 @@
size="122rpx"
shape="square"
bg-color="#fff"
:src="shopInfo.logo"
></up-avatar>
</view>
</template>
@@ -234,9 +235,7 @@ const modalData = reactive({
couponId: "",
},
});
const websocketUtil = inject("websocketUtil");
websocketUtil.closeSocket();
websocketUtil.offMessage();
const chatStore = useChatStore();
chatStore.onReceiveMsg = (msg) => {
@@ -282,56 +281,122 @@ function moreBtnsClick(item, index) {
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: "发送中",
// 图片选择与发送优化
async function sendImg() {
try {
// 1. 调用图片选择API添加fail回调
const res = await new Promise((resolve, reject) => {
uni.chooseImage({
count: 3,
sizeType: ["original", "compressed"],
sourceType: ["album", "camera"],
success: resolve,
fail: reject // 捕获选择失败(含权限拒绝)
});
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 {
}
}
uni.hideLoading();
},
});
}
});
function sendVideo() {
uni.chooseVideo({
count: 1, //默认9
sizeType: ["original", "compressed"], //可以指定是原图还是压缩图,默认二者都有
sourceType: ["album", "camera "],
success: async function (res) {
uni.showLoading({
title: "发送中",
});
console.log(res);
const fileRes = await uploadFile({ path: res.tempFilePath });
uni.hideLoading();
uni.showLoading({ title: "发送中" });
console.log("选择图片成功", res);
// 2. 批量上传图片(保持原有逻辑)
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);
// 3. 处理权限拒绝场景
handlePermissionError(err, "图片");
} finally {
// 4. 确保加载弹窗关闭(无论成功/失败)
uni.hideLoading();
}
}
// 视频选择与发送优化
async function sendVideo() {
try {
// 1. 调用视频选择API添加fail回调
const res = await new Promise((resolve, reject) => {
uni.chooseVideo({
count: 1,
sizeType: ["original", "compressed"],
sourceType: ["album", "camera"],
success: resolve,
fail: reject // 捕获选择失败(含权限拒绝)
});
});
uni.showLoading({ title: "发送中" });
console.log("选择视频成功", res);
// 2. 上传视频(保持原有逻辑)
const fileRes = await uploadFile({ path: res.tempFilePath });
if (fileRes) {
sendMsg({
image_url: fileRes,
msg_type: 5,
});
} else {
uni.showToast({
title: "视频发送失败",
icon: "none",
});
}
} catch (err) {
console.error("视频选择/发送失败", err);
// 3. 处理权限拒绝场景
handlePermissionError(err, "视频");
} finally {
// 4. 确保加载弹窗关闭(无论成功/失败)
uni.hideLoading();
}
}
// 通用权限错误处理函数
function handlePermissionError(err, mediaType) {
const errMsg = err.errMsg || "";
// 识别权限拒绝关键词(兼容不同平台)
const isAuthDenied = [
"auth deny",
"permission denied",
"auth denied",
"用户拒绝"
].some(keyword => errMsg.includes(keyword));
if (isAuthDenied) {
// 弹窗提示用户,并引导至设置页
uni.showModal({
title: "权限提示",
content: `需要${mediaType}权限才能使用该功能,请前往设置开启`,
confirmText: "去设置",
cancelText: "取消",
success: (modalRes) => {
if (modalRes.confirm) {
// 跳转到小程序设置页
uni.openSetting({
success: (settingRes) => {
console.log("设置页返回结果", settingRes);
// 可根据需要添加权限开启后的回调逻辑
}
});
}
}
});
} else {
// 其他错误(如取消选择),仅轻提示
if (!errMsg.includes("cancel")) {
uni.showToast({
title: `${mediaType}选择失败`,
icon: "none"
});
}
}
}
const groupInfo = ref({});
@@ -432,7 +497,7 @@ function sendMsg(msg) {
content: msg.value,
image_url: "",
order_id: "",
session_id: "",
session_id: groupInfo.value.session_id||options.session_id,
...msg,
});
}