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

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,
});
}

View File

@@ -13,11 +13,10 @@
:src="item.image_url"
class="img"
mode="widthFix"
@click="previewVideo(item.video_url)"
></video>
<view class="" v-if="item.msg_type == 4">
<view>{{ item.coupon.title }}</view>
<view class="u-m-t-16 bg-f7 coupon u-flex">
<view class="u-m-t-16 bg-f7 coupon u-flex u-col-stretch" style="min-width: 500rpx;">
<template v-if="item.coupon.type == 1">
<view class="left">
<view class="price">
@@ -76,10 +75,10 @@
</view>
</template>
<view class="right u-p-l-28">
<view class="u-font-32 ">{{item.coupon.couponName}}</view>
<view class="u-font-24 color-999 u-m-t-8">有效期{{ returnTime(item.coupon) }} </view>
</view>
<view class="right u-p-l-28 u-flex u-col-center">
<view class="u-font-32">{{ item.coupon.couponName }}</view>
</view>
</view>
</view>
</template>
@@ -141,5 +140,8 @@ function returnTime(coupon){
color: #999;
}
}
.u-col-stretch{
align-items: stretch;
}
</style>

View File

@@ -2,7 +2,7 @@
<view class="min-page bg-f7 u-font-28">
<view class="user-list bg-fff">
<view class="u-flex u-row-between u-col-center">
<text class="color-000">群成员22</text>
<text class="color-000">群成员{{allUser.length}}</text>
<text class="color-red" @click="showRemove = !showRemove">移除</text>
</view>
<view class="list u-m-t-26">
@@ -17,7 +17,7 @@
:src="item.avatar"
round="8rpx"
></up-avatar>
<view class="u-m-t-8 color-000">{{ item.nick_name }}</view>
<view class="u-m-t-8 color-000 u-line-1" style="max-width: 104rpx;">{{ item.nick_name }}</view>
<view
class="remove u-absolute"
v-if="showRemove && item.role != 1"
@@ -32,8 +32,10 @@
</view>
</view>
<view class="u-flex u-row-center color-666 u-m-t-30" v-if="hasMore">
<text class="u-m-r-20">查看更多</text>
<up-icon name="arrow-down" size="24rpx" color="#666"></up-icon>
<view class="u-flex" @click="loadMore">
<text class="u-m-r-20">查看更多</text>
<up-icon name="arrow-down" size="24rpx" color="#666"></up-icon>
</view>
</view>
</view>
@@ -61,10 +63,12 @@
</view>
<view
class="u-flex u-row-between default-padding bg-fff"
@click="go.to('PAGES_CHAT_COUPON_ACTIVITY', {
group_id: options.group_id,
session_id: options.session_id,
})"
@click="
go.to('PAGES_CHAT_COUPON_ACTIVITY', {
group_id: options.group_id,
session_id: options.session_id,
})
"
>
<text>优惠券领取记录</text>
<view class="u-flex color-666">
@@ -122,16 +126,20 @@ function groupMuteChange(e) {
});
}
const showRemove = ref(false);
let allUser = [];
let allUser = ref([]);
const userLists = ref([]);
const hasMore = ref(false);
function getMembers() {
chatApi.groupMembers({ group_id: options.group_id }).then((res) => {
allUser = res.user_list || [];
hasMore.value = allUser.length > 20;
userLists.value = allUser.slice(0, 20);
allUser.value = res.user_list || [];
hasMore.value = allUser.value.length > 20;
userLists.value = allUser.value.slice(0, 20);
});
}
function loadMore() {
userLists.value=allUser.value
}
onShow(() => {
getMembers();
});

View File

@@ -1,63 +1,175 @@
<template>
<view class="min-page bg-f7 color-333 u-font-28">
<up-sticky>
<view class="top u-flex u-row-between">
<view></view>
<view class="u-flex" @click="clearAllmsg">
<view class="top u-flex u-row-between u-col-center">
<view style="width: 420rpx">
<up-search
v-model="query.key"
placeholder="搜索群名称"
:showAction="false"
@clear="throttleSearch"
@change="throttleSearch"
></up-search>
</view>
<view class="u-flex u-col-center" @click="clearAllmsg">
<text class="color-666 u-m-r-12">清空未读</text>
<image src="/pageChat/static/clear.png" class="clear"></image>
</view>
</view>
</up-sticky>
<view class="list">
<view
class="item u-flex"
v-for="(item, index) in list"
:key="item.id"
@click="toDetail(item)"
>
<view class="u-flex avatar">
<up-avatar
size="118rpx"
:src="item.avatar"
shape="square"
round="8rpx"
></up-avatar>
<view class="bandage" v-if="item.unread_count > 0">{{
item.unread_count >= 99 ? "99" : item.unread_count
}}</view>
</view>
<view class="u-flex-1 u-flex u-row-between u-p-l-14">
<view style="max-width: 364rpx">
<view class="color-000 u-line-1">{{ item.name }}</view>
<view class="u-m-t-28 u-line-1 u-font-24 color-999"
>用户昵称这里是消息内容这里,,,,</view
>
<up-swipe-action>
<up-swipe-action-item
:options="options1"
v-for="(item, index) in list"
v-model:show="item.showOptions"
@click="optionsClick($event, item, index)"
:key="item.id"
>
<view class="item u-flex" @click="toDetail(item)">
<view class="u-flex avatar">
<up-avatar
size="118rpx"
:src="item.avatar"
shape="square"
round="8rpx"
></up-avatar>
<view class="bandage" v-if="item.unread_count > 0">{{
item.unread_count >= 99 ? "99" : item.unread_count
}}</view>
</view>
<view class="u-flex-1 u-flex u-row-between u-p-l-14">
<view style="max-width: 364rpx">
<view class="color-000 u-line-1">{{ item.name }}</view>
<view class="u-m-t-28 u-line-1 u-font-24 color-999">{{
item.msg
}}</view>
</view>
<view class="color-333 u-font-24">{{ item.send_time }}</view>
</view>
</view>
<view class="color-333 u-font-24">{{ item.send_time }}</view>
</view>
</view>
</up-swipe-action-item>
</up-swipe-action>
</view>
</view>
</template>
<script setup>
import go from "@/commons/utils/go.js";
import * as chatApi from "@/http/php/chat";
import { ref ,reactive} from "vue";
import { ref, reactive,inject } from "vue";
import { onShow } from "@dcloudio/uni-app";
import { useChatStore } from "@/store/chat";
const chatStore = useChatStore();
chatStore.onReceiveMsg = (msg) => {
console.log("onReceiveMsg", msg);
if (msg.operate_type == "receive_msg" && msg.chat_type == 2) {
const index = allList.value.findIndex((v) => v.group_id == msg.group_id);
if (index != -1) {
allList.value[index].unread_count += 1;
allList.value[index].msg = returnMsg(msg);
allList.value[index].send_time = msg.send_time;
}
const index1 = list.value.findIndex((v) => v.group_id == msg.group_id);
if (index1 != -1) {
list.value[index1].unread_count += 1;
list.value[index1].msg = returnMsg(msg);
list.value[index1].send_time = msg.send_time;
}
}
};
function returnMsg(msg) {
if (msg.msg_type == 1) {
return msg.nick+""+msg.content;
}
if (msg.msg_type == 2) {
return msg.nick+""+"[图片]";
}
if (msg.msg_type == 5) {
return msg.nick+""+"[视频]";
}
if (msg.msg_type == 4) {
return msg.nick+""+"[优惠券]";
}
}
chatStore.connectSocket();
// 使用 reactive 创建响应式对象
const options1 = reactive([
{
text: "删除",
style: {
backgroundColor: "#f56c6c",
color: "#fff",
},
},
]);
function optionsClick(e, item, index) {
if (e.index == 0) {
//删除
chatApi
.sessionlistdel({
session_id: item.session_id,
})
.then((res) => {
if (res) {
uni.showToast({
title: "删除成功",
icon: "none",
duration: 1000,
});
list.value.splice(index, 1);
setTimeout(() => {
getList();
}, 1000);
}
});
}
}
const list = ref([]);
let allList = ref([]);
const query = reactive({
key: "",
});
function throttle(fn, delay) {
let timer = null;
return function (...args) {
if (timer) {
clearTimeout(timer);
}
timer = setTimeout(() => {
fn.apply(this, args);
}, delay);
};
}
//使用节流函数
const throttleSearch = throttle(search, 500);
function search() {
list.value = allList.value
.filter((v) => v.name.includes(query.key.trim()))
.map((v) => {
return {
...v,
showOptions: false,
};
});
}
async function getList() {
const res = await chatApi.messageSessionList({});
list.value = (res.list || []).filter((v) => !v.is_del);
allList.value = (res.list || []).filter((v) => !v.is_del);
search();
}
async function clearAllmsg() {
@@ -66,7 +178,9 @@ async function clearAllmsg() {
content: "确定要清空所有未读消息吗?",
success: async (res) => {
if (res.confirm) {
const res = await chatApi.messageMarkReadAll({session_ids: list.value.map(v=>v.session_id).join(',')});
const res = await chatApi.messageMarkReadAll({
session_ids: list.value.map((v) => v.session_id).join(","),
});
if (res) {
uni.showToast({
title: "清空成功",
@@ -83,9 +197,17 @@ async function clearAllmsg() {
}
function toDetail(item) {
go.to("PAGES_CHAT_CHAT", {
group_id: item.group_id,
session_id: item.session_id,
if (!item.is_th) {
return uni.showToast({
title: "你已不在该群内",
icon: "none",
duration: 1500,
});
}
uni.navigateTo({
url:
"/pageChat/chat" +
`?group_id=${item.group_id}&session_id=${item.session_id}`,
});
}
@@ -115,7 +237,9 @@ onShow(() => {
padding: 32rpx 28rpx;
background-color: #fff;
border-radius: 16rpx;
margin-bottom: 16rpx;
display: flex;
flex-direction: column;
gap: 16rpx;
.avatar {
position: relative;
.bandage {