diff --git a/src/components/UrgentTestDialog.vue b/src/components/UrgentTestDialog.vue index 09dd223c..af2e733e 100644 --- a/src/components/UrgentTestDialog.vue +++ b/src/components/UrgentTestDialog.vue @@ -28,11 +28,36 @@ - - + + + + + + - - - @@ -124,7 +146,6 @@ - @@ -158,7 +179,6 @@ md="6" lg="4" > -
+
发送者:{{ message.senderName }}
发送时间:{{ formatTime(message.timestamp) }}
-
事件ID:{{ message.id }}
-
通知ID:{{ message.notificationId }}
-
- {{ device.deviceName }} -
+
{{ device.deviceType }} @@ -215,7 +232,6 @@
-
- -如果数秒后任然显示这个提示,则可能没有任何设备在线接收通知。 - -
+ color="info-lighten-4" + variant="outlined" + class="mb-1" + size="small" + title="无设备在线" + > + + 如果数秒后任然显示这个提示,则可能没有任何设备在线接收通知。 + +
@@ -265,7 +281,6 @@ - @@ -303,7 +318,6 @@ - 确认删除 @@ -343,7 +357,8 @@ export default { notificationForm: { isUrgent: false, message: '', - isPersistent: false + isPersistent: false, + senderName: '' // 发送者姓名 }, sentMessages: [], receiptCleanup: [], @@ -358,6 +373,10 @@ export default { savingEdit: false, deleteConfirmDialog: false, itemToDelete: null, + + // 教师名单相关 + teacherList: [], + loadingTeachers: false } }, computed: { @@ -373,6 +392,13 @@ export default { mounted() { this.setupEventListeners() this.loadPersistentNotifications() + this.loadTeacherList() // 加载教师列表 + + // 从本地存储加载上次使用的名字作为默认值 + const savedName = localStorage.getItem('classworks_sender_name') + if (savedName) { + this.notificationForm.senderName = savedName + } }, beforeUnmount() { this.cleanup() @@ -388,22 +414,76 @@ export default { return result }, + // 加载教师名单 + async loadTeacherList() { + this.loadingTeachers = true + try { + const data = await dataProvider.loadData('classworks-teacher-list') + if (Array.isArray(data)) { + // 过滤掉非字符串或空字符串,并去重 + this.teacherList = [...new Set(data.filter(item => typeof item === 'string' && item.trim()))] + } + } catch (e) { + console.warn('加载教师名单失败或名单为空', e) + } finally { + this.loadingTeachers = false + } + }, + + // 更新教师名单(如果输入了新名字) + async updateTeacherList(newName) { + if (!newName || typeof newName !== 'string') return + const name = newName.trim() + if (!name) return + + // 如果名字已存在,无需更新 + if (this.teacherList.includes(name)) return + + // 添加到本地列表 + this.teacherList.push(name) + this.teacherList.sort((a, b) => a.localeCompare(b, 'zh-Hans-CN')) // 简单的拼音排序 + + // 异步保存到云端 + try { + await dataProvider.saveData('classworks-teacher-list', this.teacherList) + console.log('教师名单已更新:', name) + } catch (e) { + console.error('保存教师名单失败:', e) + } + }, + async sendNotification() { if (!this.notificationForm.message.trim()) return this.sending = true try { + // 准备发送者信息 + const senderName = this.notificationForm.senderName?.trim() || '教师终端' + + // 1. 保存到本地存储,方便下次自动填充 + localStorage.setItem('classworks_sender_name', senderName) + + // 2. 尝试更新云端教师名单 + await this.updateTeacherList(senderName) + // 生成32位随机通知ID const notificationId = this.generateNotificationId() const messageContent = this.notificationForm.message const isUrgent = this.notificationForm.isUrgent const isPersistent = this.notificationForm.isPersistent + // 构建发送者信息对象 + const senderInfo = { + deviceName: senderName, + deviceType: 'teacher', + isReadOnly: false + } + const result = await this.$refs.eventSender.sendNotification( messageContent, isUrgent, [], - { deviceName: '测试设备', deviceType: 'system', isReadOnly: false }, + senderInfo, // 传递发送者信息 notificationId ) @@ -413,6 +493,7 @@ export default { id: eventId, notificationId: notificationId, message: messageContent, + senderName: senderName, // 记录发送者名字用于显示 isUrgent: isUrgent, timestamp: new Date().toISOString(), receipts: { @@ -430,14 +511,14 @@ export default { if (existingData && Array.isArray(existingData)) { list = existingData } else if (existingData && existingData.success !== false && Array.isArray(existingData.data)) { - // list = existingData.data - list = existingData.data + list = existingData.data } const newNotification = { id: notificationId, message: messageContent, isUrgent: isUrgent, + senderName: senderName, // 保存发送者 timestamp: new Date().toISOString() } @@ -452,9 +533,15 @@ export default { } console.log('通知已发送,事件ID:', eventId, '通知ID:', notificationId) + + // 重置表单,但保留名字 + const currentName = this.notificationForm.senderName this.resetForm() + this.notificationForm.senderName = currentName + } catch (error) { console.error('发送通知失败:', error) + this.$message?.error('发送失败: ' + error.message) } finally { this.sending = false } @@ -464,7 +551,8 @@ export default { this.notificationForm = { isUrgent: false, message: '', - isPersistent: false + isPersistent: false, + senderName: '' } }, @@ -594,15 +682,17 @@ export default { // 如果需要重新发送 if (this.editForm.resend) { - const notificationId = this.editForm.id - const messageContent = this.editForm.message - const isUrgent = this.editForm.isUrgent + const notificationId = this.editForm.id + const messageContent = this.editForm.message + const isUrgent = this.editForm.isUrgent + // 使用当前表单的发送者名字或默认名字 + const senderName = this.notificationForm.senderName || '教师终端' - const result = await this.$refs.eventSender.sendNotification( + const result = await this.$refs.eventSender.sendNotification( messageContent, isUrgent, [], - { deviceName: '测试设备', deviceType: 'system', isReadOnly: false }, + { deviceName: senderName, deviceType: 'teacher', isReadOnly: false }, notificationId ) @@ -613,6 +703,7 @@ export default { id: eventId, notificationId: notificationId, message: messageContent, + senderName: senderName, isUrgent: isUrgent, timestamp: new Date().toISOString(), receipts: { @@ -659,8 +750,6 @@ export default { // 从常驻通知列表中删除 this.persistentNotifications = this.persistentNotifications.filter(notif => notif.id !== notificationId) - // TODO: 调用接口删除通知(如果有的话) - console.log('通知已删除,通知ID:', notificationId) } catch (error) { console.error('删除通知失败:', error)