MessageList.vue
9.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<template>
<div>
<a-card title="消息管理" :bordered="false">
<div class="list-toolbar">
<div class="list-toolbar-left">
<a-select
v-if="isAdmin"
v-model:value="selectedCityId"
placeholder="请选择租户"
style="width: 180px"
@change="handleCityChange"
>
<a-select-option v-for="item in cityList" :key="item.id" :value="item.id">
{{ item.name }}
</a-select-option>
</a-select>
<div v-else class="managed-city-pill">当前租户:{{ currentCityName }}</div>
<a-input
v-model:value="searchForm.riderId"
placeholder="骑手ID"
style="width: 150px"
allow-clear
/>
<a-select
v-model:value="searchForm.type"
placeholder="消息类型"
style="width: 120px"
allow-clear
>
<a-select-option :value="1">订单消息</a-select-option>
<a-select-option :value="2">系统通知</a-select-option>
</a-select>
<a-button type="primary" :disabled="!canQuery" @click="handleSearch">查询</a-button>
</div>
<div class="list-toolbar-right">
<a-button type="primary" :disabled="!canQuery" @click="openSend">发送消息</a-button>
<a-button :disabled="!canQuery" @click="openBroadcast">群发消息</a-button>
</div>
</div>
<a-alert
v-if="isAdmin"
type="info"
show-icon
message="平台管理员需要先选择租户,消息列表、单发和群发都会限定在所选租户内。"
style="margin-bottom: 16px"
/>
<a-table
:dataSource="list"
:columns="columns"
:loading="loading"
:pagination="pagination"
@change="handleTableChange"
rowKey="id"
>
<template #bodyCell="{ column, record }">
<template v-if="column.key === 'type'">
<a-tag :color="record.type === 1 ? 'blue' : 'orange'">
{{ record.type === 1 ? '订单' : '系统' }}
</a-tag>
</template>
<template v-if="column.key === 'isRead'">
<a-tag :color="record.isRead === 1 ? 'green' : 'default'">
{{ record.isRead === 1 ? '已读' : '未读' }}
</a-tag>
</template>
<template v-if="column.key === 'createTime'">
{{ formatTime(record.createTime) }}
</template>
</template>
</a-table>
</a-card>
<!-- 发送消息模态框 -->
<a-modal
v-model:open="sendVisible"
title="发送消息"
@ok="handleSend"
:confirmLoading="sending"
>
<a-alert
:message="`将发送给当前租户(${currentCityName})下的指定骑手`"
type="info"
show-icon
style="margin-bottom: 16px"
/>
<a-form :model="sendForm" layout="vertical">
<a-form-item label="骑手ID" required>
<a-input-number v-model:value="sendForm.riderId" style="width: 100%" placeholder="请输入骑手ID" />
</a-form-item>
<a-form-item label="消息类型" required>
<a-radio-group v-model:value="sendForm.type">
<a-radio :value="1">订单消息</a-radio>
<a-radio :value="2">系统通知</a-radio>
</a-radio-group>
</a-form-item>
<a-form-item label="标题" required>
<a-input v-model:value="sendForm.title" placeholder="请输入消息标题" />
</a-form-item>
<a-form-item label="内容" required>
<a-textarea v-model:value="sendForm.content" :rows="4" placeholder="请输入消息内容" />
</a-form-item>
</a-form>
</a-modal>
<!-- 群发消息模态框 -->
<a-modal
v-model:open="broadcastVisible"
title="群发消息"
@ok="handleBroadcast"
:confirmLoading="broadcasting"
>
<a-alert
:message="`将发送给当前租户(${currentCityName})所有正常状态的骑手`"
type="warning"
show-icon
style="margin-bottom: 16px"
/>
<a-form :model="broadcastForm" layout="vertical">
<a-form-item label="消息类型" required>
<a-radio-group v-model:value="broadcastForm.type">
<a-radio :value="1">订单消息</a-radio>
<a-radio :value="2">系统通知</a-radio>
</a-radio-group>
</a-form-item>
<a-form-item label="标题" required>
<a-input v-model:value="broadcastForm.title" placeholder="请输入消息标题" />
</a-form-item>
<a-form-item label="内容" required>
<a-textarea v-model:value="broadcastForm.content" :rows="4" placeholder="请输入消息内容" />
</a-form-item>
</a-form>
</a-modal>
</div>
</template>
<script setup lang="ts">
import { onMounted, reactive, ref, computed } from 'vue'
import { message } from 'ant-design-vue'
import { messageApi } from '@/api'
import { useRoleCityList } from '@/composables/useRoleCityList'
import type { TablePaginationConfig } from 'ant-design-vue'
const { isAdmin, managedCityId, managedCityName, cityList, loadCities, getCityName } = useRoleCityList()
const loading = ref(false)
const sending = ref(false)
const broadcasting = ref(false)
const list = ref<any[]>([])
const sendVisible = ref(false)
const broadcastVisible = ref(false)
const selectedCityId = ref<number | undefined>()
const searchForm = reactive({
riderId: undefined as number | undefined,
type: undefined as number | undefined,
})
const sendForm = reactive({
riderId: undefined as number | undefined,
type: 2,
title: '',
content: '',
})
const broadcastForm = reactive({
type: 2,
title: '',
content: '',
})
const currentPage = ref(1)
const pageSize = ref(20)
const total = ref(0)
const canQuery = computed(() => !!selectedCityId.value)
const currentCityName = computed(() => selectedCityId.value ? getCityName(selectedCityId.value) : managedCityName.value || '-')
const pagination = computed<TablePaginationConfig>(() => ({
current: currentPage.value,
pageSize: pageSize.value,
total: total.value,
showSizeChanger: false,
showTotal: (count) => `共 ${count} 条`,
}))
const columns = [
{ title: 'ID', dataIndex: 'id', width: 80 },
{ title: '骑手ID', dataIndex: 'riderId', width: 100 },
{ title: '类型', key: 'type', width: 100 },
{ title: '标题', dataIndex: 'title', width: 150 },
{ title: '内容', dataIndex: 'content', ellipsis: true },
{ title: '状态', key: 'isRead', width: 80 },
{ title: '创建时间', key: 'createTime', width: 180 },
]
async function loadList() {
if (!selectedCityId.value) {
list.value = []
total.value = 0
return
}
loading.value = true
try {
const res: any = await messageApi.list({
cityId: selectedCityId.value,
riderId: searchForm.riderId,
type: searchForm.type,
page: currentPage.value,
})
const data = res.data || {}
list.value = Array.isArray(data.list) ? data.list : []
total.value = Number(data.total || 0)
currentPage.value = Number(data.page || currentPage.value)
pageSize.value = Number(data.pageSize || 20)
} finally {
loading.value = false
}
}
function handleSearch() {
currentPage.value = 1
loadList()
}
function handleCityChange() {
currentPage.value = 1
loadList()
}
function handleTableChange(pag: TablePaginationConfig) {
const nextPage = pag.current ?? 1
if (nextPage === currentPage.value) return
currentPage.value = nextPage
loadList()
}
function ensureCitySelected() {
if (!selectedCityId.value) {
message.error('请先选择租户')
return false
}
return true
}
function openSend() {
if (!ensureCitySelected()) return
Object.assign(sendForm, { riderId: undefined, type: 2, title: '', content: '' })
sendVisible.value = true
}
function openBroadcast() {
if (!ensureCitySelected()) return
Object.assign(broadcastForm, { type: 2, title: '', content: '' })
broadcastVisible.value = true
}
async function handleSend() {
if (!ensureCitySelected()) return
if (!sendForm.riderId || !sendForm.title || !sendForm.content) {
message.error('请填写完整信息')
return
}
sending.value = true
try {
await messageApi.send({ ...sendForm, cityId: selectedCityId.value })
message.success('发送成功')
sendVisible.value = false
loadList()
} finally {
sending.value = false
}
}
async function handleBroadcast() {
if (!ensureCitySelected()) return
if (!broadcastForm.title || !broadcastForm.content) {
message.error('请填写完整信息')
return
}
broadcasting.value = true
try {
await messageApi.broadcast({ ...broadcastForm, cityId: selectedCityId.value })
message.success('群发成功')
broadcastVisible.value = false
loadList()
} finally {
broadcasting.value = false
}
}
function formatTime(timestamp: number) {
if (!timestamp) return '-'
const date = new Date(timestamp)
return date.toLocaleString('zh-CN', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
})
}
onMounted(async () => {
await loadCities()
if (!isAdmin.value) {
selectedCityId.value = managedCityId.value
await loadList()
}
})
</script>
<style scoped>
.list-toolbar {
display: flex;
justify-content: space-between;
gap: 12px;
margin-bottom: 16px;
}
.list-toolbar-left {
display: flex;
flex-wrap: wrap;
gap: 8px;
}
.list-toolbar-right {
display: flex;
gap: 8px;
}
.managed-city-pill {
display: inline-flex;
align-items: center;
min-height: 32px;
padding: 0 12px;
border: 1px solid var(--line);
border-radius: 10px;
background: var(--panel-strong);
color: var(--text-main);
font-size: 13px;
}
</style>