Files
code-bang/frontend/miniapp/pages/message/detail/index.js
T

83 lines
2.1 KiB
JavaScript

// pages/message/detail/index.js
import { fetchMessageDetail, messageSetRead } from '../../../services/message/fetchMessage';
// tabBar 页面(与 app.json 的 tabBar.list 保持一致):
// navigateTo 无法跳转 tab 页,必须用 switchTab;而 switchTab 不支持带参数
const TAB_PAGES = [
'pages/home/home',
'pages/goods/list/index',
'pages/message/list/index',
'pages/usercenter/index',
];
Page({
data: {
id: 0,
detail: null,
jumpPage: '',
loading: true,
},
onLoad(options) {
const id = options?.id || 0;
this.setData({ id });
// 页面标题用消息标题,onLoad 阶段先给默认值
wx.setNavigationBarTitle({
title: '消息详情'
});
this.loadDetail();
},
loadDetail() {
fetchMessageDetail(this.data.id).then((res) => {
const detail = res.data || null;
const jumpPage = String(detail?.extra_json?.page || '');
this.setData({
detail,
jumpPage,
loading: false
});
if (detail?.title) {
wx.setNavigationBarTitle({
title: detail.title
});
}
// 进入即标记已读
if (detail && !detail.is_read) {
messageSetRead({ id: this.data.id }).then(() => {
this.setData({ 'detail.is_read': 1 });
}).catch(() => {});
}
}).catch(() => {
this.setData({ loading: false });
});
},
// 前往消息关联的业务页面
onJumpTap() {
const jumpPage = String(this.data.jumpPage || '').replace(/^\//, '');
if (!jumpPage) {
return;
}
const pathname = jumpPage.split('?')[0];
//tabBar 页只能 switchTab(不支持带参数),其余走 navigateTo
if (TAB_PAGES.indexOf(pathname) !== -1) {
wx.switchTab({
url: '/' + pathname,
fail: () => {
wx.showToast({ title: '页面跳转失败', icon: 'none' });
}
});
return;
}
wx.navigateTo({
url: '/' + jumpPage,
fail: () => {
wx.showToast({ title: '页面跳转失败', icon: 'none' });
}
});
},
onShareAppMessage() { }
});