/* ═════ 历史报告:api.request('reports') 拉列表 + 点条目进报告页 对照 H5 index.html #s-history(L32-42) 与 app.js renderHistoryList()/openHistory()(L1944-2001) 跳转前把 payload 放进 globalData,由 pages/report 渲染。 ═════ */ var api = require('../../utils/api'); function pad2(n) { return (n < 10 ? '0' : '') + n; } // H5 用 toLocaleDateString('sv-SE'),即 YYYY-MM-DD 零填充 function formatDate(value) { var d = new Date(value); return d.getFullYear() + '-' + pad2(d.getMonth() + 1) + '-' + pad2(d.getDate()); } Page({ data: { bgUrl: api.assetUrl('img/ui-20261009/bg-history-h5.jpg'), items: [], loading: false, error: '', signedIn: false, accountLabel: '尚未登录', countText: '正在读取报告…', // H5 #history-count 初值 identityNote: '', toastText: '', toastOn: false }, onShow: function () { if (!api.user()) { this.setData({ items: [] }); my.redirectTo({ url: '/pages/splash/splash' }); return; } this.setData({ signedIn: true, accountLabel: '淘宝已登录' }); this.load(); }, onUnload: function () { this.closed = true; }, // H5 .toast toast: function (msg) { var that = this; this.setData({ toastText: msg, toastOn: true }); clearTimeout(this.toastTimer); this.toastTimer = setTimeout(function () { that.setData({ toastOn: false }); }, Math.max(1800, String(msg).length * 130)); }, load: function () { var that = this; this.closed = false; this.setData({ loading: true, error: '' }); api.request('reports').then(function (data) { if (that.closed) return; that.setData({ countText: '您最近已进行过' + data.total + '次面部问题检测', identityNote: '保留最近 5 份报告,同一淘宝账号可跨手机查看。', items: data.items.map(function (item) { return { id: item.id, date: formatDate(item.created_at) }; }) }); }).catch(function (err) { if (!that.closed) that.setData({ error: err.message, countText: '暂时无法读取' }); }).then(function () { if (!that.closed) that.setData({ loading: false }); }); }, onOpen: function (e) { if (this.data.loading) return; var id = e.currentTarget.dataset.id; if (!id) return; var that = this; this.closed = false; this.setData({ loading: true }); api.request('reports', { id: id }).then(function (data) { if (that.closed) return; getApp().globalData.photos = data.report.photos; getApp().globalData.payload = Object.assign({}, data.report.payload, { vm: data.report.vm, presentation: data.report.presentation }); my.navigateTo({ url: '/pages/report/report' }); }).catch(function (err) { that.toast(err.message); }) .then(function () { if (!that.closed) that.setData({ loading: false }); }); }, // H5 data-action="logout-wechat"(历史页右上角) onLogout: function () { var that = this; api.logout().then(function () { that.setData({ signedIn: false, accountLabel: '尚未登录', items: [], countText: '', identityNote: '' }); my.redirectTo({ url: '/pages/splash/splash' }); }).catch(function (err) { that.toast(err.message); }); } });