Files
guoxue-mp/pages/msg/index.vue
T
2026-03-25 15:59:24 +08:00

286 lines
6.8 KiB
Vue

<template>
<view class="msg-page" :style="{ paddingTop: pagePadding }">
<view class="background-blur"></view>
<view class="msg-header">
<text class="header-title">消息</text>
<u-icon name="plus-circle" @click="$jump('/pages/user/setting')" size="60rpx"/>
</view>
<view class="msg-list">
<view
v-for="item in messageList"
:key="item.id"
class="msg-item"
@click="openMessage(item)"
@longpress="handleLongPress(item)"
>
<image class="avatar" :src="item.role.avatar" mode="aspectFit"></image>
<view class="msg-info">
<view class="msg-name u-line-1">{{ item.title ? `${item.title}·` : '' }}{{ item.role.roleName }}</view>
<view class="msg-preview u-line-2">{{ item.lastMessage.reply }}</view>
</view>
<text class="msg-time">{{ item.createTime.replace('T', ' ') }}</text>
</view>
</view>
<Login ref="Login" @done="getMessageList"/>
</view>
</template>
<script>
import {chatListReq, deleteChatListReq, updateChatListReq} from "@/api/chat";
import {getUserInfo} from "@/util/user";
import Login from "@/components/Login.vue";
export default {
components: {Login},
data() {
return {
pagePadding: '120rpx',
messageList: [
// {
// avatar: '/static/icon/图片 2 Copy 1@1x.png',
// name: '五师兄',
// preview: '你好',
// time: '2025-11-20 12:00:00',
// }
]
}
},
async onShow() {
const statusBarHeight = uni.getSystemInfoSync().statusBarHeight || 20
this.pagePadding = `${statusBarHeight + 44}px`
if (!getUserInfo().token) {
return this.$refs.Login.open()
}
await this.getMessageList()
},
methods: {
handleBack() {
uni.navigateBack().catch(() => {
uni.switchTab({url: '/pages/index/index'})
})
},
async getMessageList() {
const res = await chatListReq()
this.messageList = res.data
if (!this.messageList.length) {
uni.navigateTo({url: '/pages/chat/select'})
}
},
openMessage(item) {
// if (!this.messageList.length) {
// uni.navigateTo({ url: '/pages/chat/select' })
// return
// }
let url = `/pages/chat/index?chatId=${item.id}&conversationId=${item.conversationId}`
url += `&roleId=${item.roleId}`
uni.navigateTo({url})
},
handleLongPress(item) {
uni.showActionSheet({
itemList: ['修改标题', '删除'],
success: ({tapIndex}) => {
if (tapIndex === 0) {
this.promptRename(item)
} else if (tapIndex === 1) {
this.confirmDelete(item)
}
}
})
},
promptRename(item) {
const currentName = (item && item.title) || ''
uni.showModal({
title: '修改标题',
editable: true,
placeholderText: '请输入新的标题',
content: currentName,
success: (res) => {
if (!res.confirm) return
if (typeof res.content === 'undefined') {
uni.showToast({title: '当前环境暂不支持此操作', icon: 'none'})
return
}
const newTitle = res.content.trim()
if (!newTitle) {
uni.showToast({title: '标题不能为空', icon: 'none'})
return
}
if (newTitle === currentName) {
uni.showToast({title: '标题无改动', icon: 'none'})
return
}
this.updateChatTitle(item, newTitle)
}
})
},
async updateChatTitle(item, title) {
try {
uni.showLoading({title: '保存中...', mask: true})
await updateChatListReq({
id: item.id,
title,
})
this.messageList = this.messageList.map(msg => {
if (msg.id === item.id) {
return {
...msg,
title,
role: msg.role
}
}
return msg
})
uni.showToast({title: '修改成功', icon: 'success'})
} catch (err) {
const msg = (err && err.msg) || '修改失败'
uni.showToast({title: msg, icon: 'none'})
} finally {
uni.hideLoading()
}
},
confirmDelete(item) {
uni.showModal({
title: '删除会话',
content: '删除后不可恢复,是否继续?',
confirmText: '删除',
confirmColor: '#fa3534',
success: (res) => {
if (res.confirm) {
this.deleteChat(item)
}
}
})
},
async deleteChat(item) {
try {
uni.showLoading({title: '删除中...', mask: true})
await deleteChatListReq(item.id)
this.messageList = this.messageList.filter(msg => msg.id !== item.id)
uni.showToast({title: '已删除', icon: 'success'})
if (!this.messageList.length) {
uni.navigateTo({url: '/pages/chat/select'})
}
} catch (err) {
const msg = (err && err.msg) || '删除失败'
uni.showToast({title: msg, icon: 'none'})
} finally {
uni.hideLoading()
}
}
}
}
</script>
<style scoped lang="scss">
.msg-page {
position: relative;
min-height: 100vh;
padding: 60rpx 30rpx 60rpx;
box-sizing: border-box;
background: linear-gradient(180deg, #e4f4ff 0%, #f3edff 50%, #fbf7ff 100%);
}
.background-blur {
position: absolute;
inset: 0;
background: rgba(255, 255, 255, 0.6);
filter: blur(40rpx);
z-index: 0;
}
.msg-header {
position: relative;
z-index: 1;
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 40rpx;
}
.header-left {
width: 80rpx;
display: flex;
align-items: center;
}
.header-title {
font-size: 36rpx;
font-weight: 600;
color: #111;
}
.header-right {
width: 140rpx;
display: flex;
justify-content: flex-end;
}
.right-pill {
min-width: 120rpx;
height: 60rpx;
border-radius: 40rpx;
background: rgba(255, 255, 255, 0.9);
display: flex;
align-items: center;
justify-content: center;
gap: 14rpx;
box-shadow: 0 8rpx 16rpx rgba(116, 136, 255, 0.18);
}
.pill-icon-gap {
margin-left: 6rpx;
}
.msg-list {
position: relative;
z-index: 1;
}
.msg-item {
display: flex;
align-items: center;
padding: 30rpx;
//border-bottom: 1px solid rgba(226, 232, 240, 0.8);
margin: 20rpx 0;
background: rgba(255, 255, 255, 0.8);
border-radius: 30rpx;
box-shadow: 0 16rpx 30rpx rgba(146, 160, 255, 0.2);
}
.msg-item:last-child {
border-bottom: none;
}
.avatar {
width: 90rpx;
height: 90rpx;
border-radius: 18rpx;
margin-right: 24rpx;
}
.msg-info {
flex: 1;
display: flex;
flex-direction: column;
gap: 10rpx;
}
.msg-name {
font-size: 32rpx;
color: #111;
font-weight: 600;
}
.msg-preview {
font-size: 26rpx;
color: #8f8fb0;
}
.msg-time {
font-size: 24rpx;
color: #a0aec0;
}
</style>