/* ═════ 历史报告: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(require('../../utils/page-header').page({ onShareAppMessage: function () { return require('../../utils/share').message(); }, data: { bgUrl: api.assetUrl('img/ui-20261009/bg-history-h5.jpg'), items: [], loading: false, error: '', signedIn: false, guest: false, accountLabel: '尚未登录', countText: '正在读取报告…', // H5 #history-count 初值 identityNote: '', toastText: '', toastOn: false, identityChecking: true, identityError: '' }, onShow: function () { this.visible = true; if (!api.user()) { this.setData({ items: [] }); my.redirectTo({ url: '/pages/login/login' }); return; } this.ownerId = api.user().id; if (api.user().kind === 'taobao_guest') { this.showGuest(); return; } var that = this, owner = this.ownerId, seq = this.identitySeq = (this.identitySeq || 0) + 1; this.identityPending = true; this.setData({ identityChecking: true, identityError: '' }); return (typeof api.restore === 'function' ? api.restore() : Promise.resolve(api.user())).then(function (user) { if (!that.visible || seq !== that.identitySeq) return; if (!user || user.id !== owner) { that.setData({ items: [], countText: '', identityNote: '' }); my.redirectTo({ url: '/pages/login/login' }); return; } that.identityPending = false; that.setData({ identityChecking: false, signedIn: true, guest: false, accountLabel: '淘宝已登录' }); that.load(); }).catch(function (err) { if (!that.visible || seq !== that.identitySeq) return; if (err.status === 401 || !api.user() || api.user().id !== owner) { that.setData({ items: [], countText: '', identityNote: '' }); my.redirectTo({ url: '/pages/login/login' }); return; } that.setData({ identityChecking: false, identityError: '网络异常,暂时无法确认当前账号,请重试。' }); }); }, onHide: function () { this.visible = false; this.closed = true; this.loadSeq = (this.loadSeq || 0) + 1; this.identitySeq = (this.identitySeq || 0) + 1; if (api.user() && api.user().kind !== 'taobao_guest') { this.identityPending = true; this.setData({ identityChecking: true, identityError: '' }); } }, onUnload: function () { this.onHide(); clearTimeout(this.toastTimer); }, showGuest: function () { this.identityPending = false; this.setData({ guest: true, signedIn: false, accountLabel: '游客体验', items: [], loading: false, error: '', identityChecking: false, identityError: '', countText: '游客报告不计入历史记录', identityNote: '游客报告仅供本次查看,不保存照片、档案或历史报告。' }); }, onRetryIdentity: function () { return this.onShow(); }, onLogin: function () { my.navigateTo({ url: '/pages/login/login?switch=1' }); }, // 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 () { if (this.identityPending) return; var that = this; if (!api.user()) { my.redirectTo({ url: '/pages/login/login' }); return; } if (api.user().kind === 'taobao_guest') { this.showGuest(); return; } var owner = api.user().id, seq = this.loadSeq = (this.loadSeq || 0) + 1; this.closed = false; this.setData({ guest: false, loading: true, error: '', items: [] }); function current() { return !that.closed && seq === that.loadSeq && api.user() && api.user().id === owner; } api.request('reports').then(function (data) { if (!current()) 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 (current()) that.setData({ error: err.message, countText: '暂时无法读取' }); else if (!api.user() && err.status === 401 && that.visible) my.redirectTo({ url: '/pages/login/login' }); }).then(function () { if (current()) that.setData({ loading: false }); }); }, onOpen: function (e) { if (this.identityPending) return; if (this.data.loading) return; if (!api.user() || api.user().kind === 'taobao_guest') return; var id = e.currentTarget.dataset.id; if (!id) return; var that = this; var owner = api.user().id, seq = this.loadSeq = (this.loadSeq || 0) + 1; this.closed = false; this.setData({ loading: true }); api.request('reports', { id: id }).then(function (data) { if (that.closed || seq !== that.loadSeq || !api.user() || api.user().id !== owner) 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) { if (that.closed || seq !== that.loadSeq) return; if (err.status === 401) { that.setData({ items: [] }); my.redirectTo({ url: '/pages/login/login' }); } else that.toast(err.message); }) .then(function () { if (!that.closed && seq === that.loadSeq) 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/login/login' }); }).catch(function (err) { that.toast(err.message); }); } }, typeof my === 'undefined' ? {} : my));