const { fetchNotifications, markAllNotificationsRead, } = require('../../utils/request'); const { MOBILE_MODULES } = require('../../modules/registry'); const { resolveNotificationModule } = require('../../utils/notificationModuleMap'); /** 通知页不感知首页可见性:以全部注册模块作为候选,最终由工作台按角色校验 */ const ALL_MODULE_IDS = MOBILE_MODULES.map((mod) => mod.id); /** 拆分订单号/SKU(小号)与核心问题(突出换行) */ function splitNotificationContent(raw, relatedOrderCode) { const original = String(raw || '').trim(); let text = original; const metaParts = []; const orderMatch = text.match(/\b(ORD[_A-Z0-9]+)\b/i) || text.match(/(?:订单)\s*[::]?\s*([A-Z0-9_-]+)/i); const orderCode = (orderMatch && orderMatch[1]) || (relatedOrderCode ? String(relatedOrderCode).trim() : ''); if (orderCode) { metaParts.push(orderCode); text = text .replace(/\bORD[_A-Z0-9]+\b/gi, ' ') .replace(/(?:订单)\s*[::]?\s*[A-Z0-9_-]+/gi, ' '); } const skuMatch = text.match(/\b(SKU[A-Z0-9_-]*)\b/i) || text.match(/(?:SKU)\s*[::]?\s*([A-Z0-9_-]+)/i); if (skuMatch && skuMatch[1]) { metaParts.push(skuMatch[1]); text = text .replace(/\bSKU[A-Z0-9_-]*\b/gi, ' ') .replace(/(?:SKU)\s*[::]?\s*[A-Z0-9_-]+/gi, ' '); } text = text .replace(/[//||]+/g, ' ') .replace(/[::]\s*/g, ' ') .replace(/\b商品\b/g, ' ') .replace(/\s{2,}/g, ' ') .replace(/^[\s\-–—·,,、]+|[\s\-–—·,,、]+$/g, '') .trim(); return { displayMeta: metaParts.join(' · '), displayContent: text || original, }; } Page({ data: { loading: true, rows: [], unreadCount: 0, }, onShow() { this.load(); }, async load() { this.setData({ loading: true }); try { const notifications = await fetchNotifications(); this.setData({ rows: (notifications.data || []).map((row) => { const split = splitNotificationContent(row.content, row.related_order_code); return { ...row, displayMeta: split.displayMeta, displayContent: split.displayContent, displayTime: row.created_at ? new Date(row.created_at).toLocaleString() : '', }; }), unreadCount: Number(notifications.unread_count) || 0, }); } catch (error) { wx.showToast({ title: error.message || '加载失败', icon: 'none' }); } finally { this.setData({ loading: false }); } }, async markAllRead() { try { await markAllNotificationsRead(); await this.load(); } catch (error) { wx.showToast({ title: error.message || '操作失败', icon: 'none' }); } }, onNotificationTap(e) { const type = String(e.currentTarget.dataset.type || '').trim(); if (!type) return; // 通用直达:通知类型 → 工作台模块(含角色自动切换),由工作台统一落地 const moduleId = resolveNotificationModule(type, ALL_MODULE_IDS); if (moduleId) { wx.reLaunch({ url: `/pages/home/home?module=${encodeURIComponent(moduleId)}` }); return; } wx.showToast({ title: '该消息暂无对应入口', icon: 'none' }); }, });