/** * Purpose: userApi supports index.js by holding or executing the user api page or API role. * Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow. */ const userApi = require('../../api/user') /** * Purpose: navigation supports index.js by holding or executing the navigation page or API role. * Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow. */ const navigation = require('../../utils/navigation') const { previewImagesSafely } = require('../../utils/file') /** * notifications page logic. * Call guide: page data drives the WXML template, and each method is called by user events or lifecycle callbacks. */ Page({ /** * Purpose: data supports index.js by holding or executing the data page or API role. * Usage: import, call, read, or update this page-data from the owning page/module while preserving the surrounding loading and permission flow. */ data: { /** * Purpose: loading supports index.js by holding or executing the loading page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ loading: false, /** * Purpose: messages supports index.js by holding or executing the messages page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ messages: [], /** * Purpose: unreadCount supports index.js by holding or executing the unread count page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ unreadCount: 0, /** * Purpose: detailVisible supports index.js by holding or executing the detail visible page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ detailVisible: false, /** * Purpose: currentMessage supports index.js by holding or executing the current message page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ currentMessage: null }, /** * `onShow` page method. Use this from WXML event bindings or lifecycle hooks. * Call guide: read or update page data with `this.setData(...)` inside the method when state changes. */ onShow() { this.loadMessages() }, async loadMessages() { if (!navigation.ensureLogin('查看消息通知')) { return } this.setData({ loading: true }) try { const messages = await userApi.listMyMessages() const list = Array.isArray(messages) ? messages : [] this.setData({ /** * Purpose: messages supports index.js by holding or executing the messages page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ messages: list, /** * Purpose: unreadCount supports index.js by holding or executing the unread count page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ unreadCount: list.filter(item => item.readStatus !== 1).length }) } catch (error) { wx.showToast({ /** * Purpose: title supports index.js by holding or executing the title page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ title: error.message || '消息加载失败', /** * Purpose: icon supports index.js by holding or executing the icon page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ icon: 'none' }) } finally { this.setData({ loading: false }) } }, async handleOpenMessage(event) { const id = event.currentTarget.dataset.id const item = this.data.messages.find(message => String(message.id) === String(id)) /** * `if` page method. Use this from WXML event bindings or lifecycle hooks. * Call guide: read or update page data with `this.setData(...)` inside the method when state changes. */ if (!item) { return } const actionTarget = navigation.buildMessageActionPath(item) if (actionTarget) { try { if (item.readStatus !== 1) { await userApi.markMessageRead(item.id) } const opened = await navigation.openActionPath(actionTarget) await this.loadMessages() if (opened) { return } } catch (error) { wx.showToast({ /** * Purpose: title supports index.js by holding or executing the title page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ title: error.message || '打开消息失败', /** * Purpose: icon supports index.js by holding or executing the icon page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ icon: 'none' }) return } } this.setData({ /** * Purpose: currentMessage supports index.js by holding or executing the current message page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ currentMessage: item, /** * Purpose: detailVisible supports index.js by holding or executing the detail visible page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ detailVisible: true }) /** * `if` page method. Use this from WXML event bindings or lifecycle hooks. * Call guide: read or update page data with `this.setData(...)` inside the method when state changes. */ if (item.readStatus !== 1) { try { await userApi.markMessageRead(item.id) await this.loadMessages() } catch (error) { wx.showToast({ /** * Purpose: title supports index.js by holding or executing the title page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ title: error.message || '标记已读失败', /** * Purpose: icon supports index.js by holding or executing the icon page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ icon: 'none' }) } } }, /** * `handleCloseDetail` page method. Use this from WXML event bindings or lifecycle hooks. * Call guide: read or update page data with `this.setData(...)` inside the method when state changes. */ handleCloseDetail() { this.setData({ /** * Purpose: detailVisible supports index.js by holding or executing the detail visible page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ detailVisible: false, /** * Purpose: currentMessage supports index.js by holding or executing the current message page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ currentMessage: null }) }, /** * `handlePreviewImage` page method. Use this from WXML event bindings or lifecycle hooks. * Call guide: read or update page data with `this.setData(...)` inside the method when state changes. */ handlePreviewImage() { const imageUrl = this.data.currentMessage && this.data.currentMessage.imageUrl /** * `if` page method. Use this from WXML event bindings or lifecycle hooks. * Call guide: read or update page data with `this.setData(...)` inside the method when state changes. */ if (!imageUrl) { return } previewImagesSafely(imageUrl, [imageUrl]) }, /** * Purpose: noop supports index.js by holding or executing the noop page or API role. * Usage: import, call, read, or update this method from the owning page/module while preserving the surrounding loading and permission flow. */ noop() {}, /** * `handleBack` page method. Use this from WXML event bindings or lifecycle hooks. * Call guide: read or update page data with `this.setData(...)` inside the method when state changes. */ handleBack() { navigation.goBack('/pages/profile/index') } })