const userApi = require('../../api/user') const navigation = require('../../utils/navigation') /** * notifications page logic. * Call guide: page data drives the WXML template, and each method is called by user events or lifecycle callbacks. */ Page({ data: { loading: false, messages: [], unreadCount: 0, detailVisible: false, 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({ messages: list, unreadCount: list.filter(item => item.readStatus !== 1).length }) } catch (error) { wx.showToast({ title: error.message || '消息加载失败', 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 } if (item.actionPath) { try { if (item.readStatus !== 1) { await userApi.markMessageRead(item.id) } const opened = await navigation.openActionPath(item.actionPath) await this.loadMessages() if (opened) { return } } catch (error) { wx.showToast({ title: error.message || '打开消息失败', icon: 'none' }) return } } this.setData({ currentMessage: item, 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({ title: error.message || '标记已读失败', 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({ detailVisible: false, 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 } wx.previewImage({ current: imageUrl, urls: [imageUrl] }) }, 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') } })