|
| 1 | +<template> |
| 2 | + <div style="display: none"> |
| 3 | + <!-- 这是一个无界面的功能组件,用于封装事件发送 --> |
| 4 | + </div> |
| 5 | +</template> |
| 6 | + |
| 7 | +<script> |
| 8 | +import { sendEvent } from "@/utils/socketClient"; |
| 9 | +
|
| 10 | +export default { |
| 11 | + name: "EventSender", |
| 12 | + emits: ["sent", "error"], |
| 13 | + methods: { |
| 14 | + /** |
| 15 | + * 发送事件 |
| 16 | + * @param {string} eventName - 事件名称 |
| 17 | + * @param {Object} content - 事件内容 |
| 18 | + * @returns {Promise} 发送结果 |
| 19 | + */ |
| 20 | + async sendEvent(eventName, content = {}) { |
| 21 | + try { |
| 22 | + sendEvent(eventName, content); |
| 23 | +
|
| 24 | + this.$emit("sent", { |
| 25 | + eventName, |
| 26 | + content, |
| 27 | + timestamp: new Date().toISOString(), |
| 28 | + success: true, |
| 29 | + }); |
| 30 | +
|
| 31 | + // 返回可能的 eventId 和 notificationId,方便调用者关联回执 |
| 32 | + return { |
| 33 | + success: true, |
| 34 | + eventId: content?.eventId || null, |
| 35 | + notificationId: content?.notificationId || null |
| 36 | + }; |
| 37 | + } catch (error) { |
| 38 | + console.error("发送事件失败:", error); |
| 39 | +
|
| 40 | + this.$emit("error", { |
| 41 | + eventName, |
| 42 | + content, |
| 43 | + error: error.message, |
| 44 | + timestamp: new Date().toISOString(), |
| 45 | + success: false, |
| 46 | + }); |
| 47 | +
|
| 48 | + return { success: false, error: error.message }; |
| 49 | + } |
| 50 | + }, |
| 51 | +
|
| 52 | + /** |
| 53 | + * 发送通知事件(简化接口) |
| 54 | + * @param {string} message - 通知内容 |
| 55 | + * @param {boolean} isUrgent - 是否加急 |
| 56 | + * @param {Array} targetDevices - 目标设备 |
| 57 | + * @param {Object} senderInfo - 发送者信息 |
| 58 | + * @param {string} notificationId - 32位通知ID(可选) |
| 59 | + */ |
| 60 | + async sendNotification( |
| 61 | + message, |
| 62 | + isUrgent = false, |
| 63 | + targetDevices = [], |
| 64 | + senderInfo = {}, |
| 65 | + notificationId = null |
| 66 | + ) { |
| 67 | + // 生成一个客户端事件 ID,便于在接收回执时进行映射 |
| 68 | + const eventId = `evt-${Date.now()}-${Math.random() |
| 69 | + .toString(36) |
| 70 | + .slice(2, 8)}`; |
| 71 | + return this.sendEvent("notification", { |
| 72 | + eventId, |
| 73 | + notificationId, |
| 74 | + message, |
| 75 | + isUrgent, |
| 76 | + targetDevices, |
| 77 | + senderInfo, |
| 78 | + }); |
| 79 | + }, |
| 80 | +
|
| 81 | + /** |
| 82 | + * 发送回执事件 |
| 83 | + * @param {string} originalEventId - 原事件ID |
| 84 | + * @param {string} status - 回执状态 (displayed|read) |
| 85 | + * @param {Object} deviceInfo - 设备信息 |
| 86 | + * @param {string} notificationId - 原通知ID(可选) |
| 87 | + */ |
| 88 | + async sendReceipt(originalEventId, status, deviceInfo = {}, notificationId = null) { |
| 89 | + const eventId = `rcpt-${Date.now()}-${Math.random() |
| 90 | + .toString(36) |
| 91 | + .slice(2, 6)}`; |
| 92 | + return this.sendEvent("notification-receipt", { |
| 93 | + eventId, |
| 94 | + originalEventId, |
| 95 | + notificationId, |
| 96 | + status, |
| 97 | + deviceInfo, |
| 98 | + }); |
| 99 | + }, |
| 100 | +
|
| 101 | + /** |
| 102 | + * 单独发送"已显示"回执事件 |
| 103 | + * @param {Object} deviceInfo 设备信息 |
| 104 | + * @param {string} notificationId 原通知ID |
| 105 | + */ |
| 106 | + async sendDisplayedReceipt(deviceInfo = {}, notificationId = null) { |
| 107 | + const eventId = `disp-${Date.now()}-${Math.random() |
| 108 | + .toString(36) |
| 109 | + .slice(2, 6)}`; |
| 110 | + return this.sendEvent("notification-displayed", { |
| 111 | + eventId, |
| 112 | + notificationId, |
| 113 | + deviceInfo, |
| 114 | + }); |
| 115 | + }, |
| 116 | + /** |
| 117 | + * 单独发送"已读"回执事件 |
| 118 | + * @param {Object} deviceInfo 设备信息 |
| 119 | + * @param {string} notificationId 原通知ID |
| 120 | + */ |
| 121 | + async sendReadReceipt(deviceInfo = {}, notificationId = null) { |
| 122 | + const eventId = `read-${Date.now()}-${Math.random() |
| 123 | + .toString(36) |
| 124 | + .slice(2, 6)}`; |
| 125 | + return this.sendEvent("notification-read", { |
| 126 | + eventId, |
| 127 | + notificationId, |
| 128 | + deviceInfo, |
| 129 | + }); |
| 130 | + }, |
| 131 | + }, |
| 132 | +}; |
| 133 | +</script> |
0 commit comments