You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
4.5 KiB

3 weeks ago
<template>
<div class="container">
2 weeks ago
<el-table :data="tableData" style="width: 100%" :show-header="false" @row-click="handleRowClick">
3 weeks ago
<el-table-column width="80">
<template slot-scope="scope">
<img :src="getIcon(scope.$index)" alt="icon" style="width: 2.19rem; height: 1.25rem;">
</template>
</el-table-column>
<el-table-column prop="name" width="300">
1 week ago
<template slot-scope="scope">
<span :style="{ color: scope.row.isRead === 2 ? '#ccc' : 'inherit' }">
{{ scope.row.name }}
</span>
</template>
3 weeks ago
</el-table-column>
<el-table-column prop="date" width="100">
</el-table-column>
</el-table>
2 weeks ago
<!-- 详情弹窗 -->
<el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="40%">
<div>
2 weeks ago
<p><strong>时间:</strong> {{ selectedMessage.date || '暂无' }}</p>
2 weeks ago
<p><strong>内容:</strong> {{ selectedMessage.name }}</p>
</div>
<span slot="footer" class="dialog-footer">
<el-button type="primary" @click="handleConfirm"></el-button>
<el-button @click="dialogVisible = false">关闭</el-button>
</span>
</el-dialog>
3 weeks ago
</div>
</template>
<script>
import { getAllMessagestwo } from '@/api/ManageApi/index';
1 week ago
import { markSmartReminderAsRead } from '@/api/ManageApi/index';
3 weeks ago
export default {
name: 'MessageTable',
data() {
return {
2 weeks ago
tableData: [],
dialogVisible: false,
selectedMessage: {
id: '',
name: '',
date: ''
},
dialogTitle: '消息详情'
3 weeks ago
};
},
mounted() {
this.getData();
},
methods: {
async getData() {
const response = await getAllMessagestwo();
if (response && response.code === 200 && response.data) {
this.tableData = this.processData(response.data);
1 week ago
this.sortTableData(); // 添加排序方法
3 weeks ago
}
},
processData(data) {
return data.map(item => ({
1 week ago
id: item.id,
3 weeks ago
name: item.content,
1 week ago
date: this.formatDate(item.createTime),
isRead: item.isRead // 确保包含 isRead 字段
3 weeks ago
}));
},
1 week ago
sortTableData() {
this.tableData.sort((a, b) => {
if (a.isRead === 2 && b.isRead !== 2) {
return 1;
} else if (a.isRead !== 2 && b.isRead === 2) {
return -1;
} else {
return 0;
}
});
},
3 weeks ago
getIcon(index) {
return index === 0 ? require('@/assets/images/new@2x.png') : require('@/assets/images/other.png');
},
formatDate(dateString) {
3 weeks ago
if (!dateString) {
return '';
}
3 weeks ago
const date = new Date(dateString);
3 weeks ago
if (isNaN(date.getTime())) {
2 weeks ago
return '';
3 weeks ago
}
3 weeks ago
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
return `${year}-${month}-${day}`;
2 weeks ago
},
handleRowClick(row) {
this.selectedMessage = row;
this.dialogVisible = true;
},
handleConfirm() {
if (this.selectedMessage.id) {
markSmartReminderAsRead(this.selectedMessage.id)
.then(response => {
if (response && response.code === 200) {
this.$message.success('消息已标记为已读');
this.dialogVisible = false;
this.getData(); // 刷新数据
} else {
this.$message.error('标记为已读失败');
}
})
.catch(error => {
console.error('标记为已读失败:', error);
this.$message.error('标记为已读失败');
});
} else {
this.$message.error('消息 ID 不存在');
this.dialogVisible = false;
}
3 weeks ago
}
}
};
</script>
<style scoped>
.container {
width: 30rem;
height: 14.5rem;
}
</style>