var tcm = require('../../../../utils/tcm.js') var compliance = require('../../../../utils/compliance.js') Page({ data: { loading: true, status: 'loading', errorMessage: '', examId: '', scaleUserId: '', exam: null, scaleUser: null, latestScaleResult: null, statList: [], questionList: [], sectionList: [], imageList: [], reportTime: '' }, timer: null, pollCount: 0, onLoad: function (options) { this.setData({ examId: options.examId || '', scaleUserId: options.scaleUserId || '' }) this.loadReport(false) }, onUnload: function () { this.clearTimer() }, loadReport: function (silent) { var that = this if (!silent) { that.setData({ loading: true, errorMessage: '' }) } var requestPromise = that.data.examId ? tcm.getExamDetail(that.data.examId) : tcm.getLatestExam(that.data.scaleUserId) requestPromise.then(function (data) { var exam = data && data.exam ? data.exam : null var aiSections = data && data.aiSections ? data.aiSections : {} var hasSections = that.buildSectionList(aiSections).length > 0 if (!exam) { that.setData({ loading: false, status: 'empty', exam: null, sectionList: [], imageList: [] }) return } var status = exam.status || 'pending' if (status === 'completed' && !hasSections) { status = 'generating' } that.setData({ loading: false, status: status, exam: exam, scaleUser: data.scaleUser || null, latestScaleResult: data.latestScaleResult || null, statList: that.buildStatList(data.scaleUser || null, data.latestScaleResult || null), questionList: that.buildQuestionList(data.questionAnswersLocalizedExpanded || {}), sectionList: that.buildSectionList(aiSections), imageList: that.buildImageList(exam.observationImagesOss || exam.observationImages || {}), errorMessage: aiSections && aiSections.error ? compliance.sanitizeText(aiSections.error) : '', reportTime: that.formatDateTime(exam.CreatedAt || '') }) if (status === 'pending' || status === 'generating') { if (!that.timer) { that.startPolling() } } else { that.clearTimer() } }).catch(function (err) { that.setData({ loading: false, status: 'error', errorMessage: err && err.message ? compliance.sanitizeText(err.message) : '报告加载失败' }) }) }, buildQuestionList: function (source) { var list = [] if (Array.isArray(source)) { return compliance.sanitizeValue(source) } for (var key in source) { list.push({ title: compliance.sanitizeText(key), value: compliance.sanitizeText(Array.isArray(source[key]) ? source[key].join(',') : source[key]) }) } return list }, buildSectionList: function (aiSections) { var mapping = [ { key: 'general', title: '一般情况', badge: '壹' }, { key: 'inquiry', title: '健康问答分析', badge: '贰' }, { key: 'observation', title: '图片分析', badge: '叁' }, { key: 'constitution', title: '体质分析', badge: '肆' }, { key: 'advice', title: '调养建议', badge: '伍' }, { key: 'summary', title: '报告摘要', badge: '陆' } ] var list = [] for (var i = 0; i < mapping.length; i++) { if (aiSections && aiSections[mapping[i].key]) { list.push({ key: mapping[i].key, badge: mapping[i].badge, title: mapping[i].title, content: compliance.sanitizeText(aiSections[mapping[i].key]) }) } } return list }, buildStatList: function (scaleUser, latestScaleResult) { var list = [] if (scaleUser) { if (scaleUser.sex !== undefined && scaleUser.sex !== null) { list.push({ label: '性别', value: Number(scaleUser.sex) === 1 ? '女' : '男' }) } if (scaleUser.age) { list.push({ label: '年龄', value: scaleUser.age + '岁' }) } if (scaleUser.height) { list.push({ label: '身高', value: scaleUser.height + 'cm' }) } } if (latestScaleResult) { var weight = latestScaleResult.weight_kg || latestScaleResult.weight if (weight) { list.push({ label: '体重', value: this.formatValue(weight) + 'kg' }) } if (latestScaleResult.bodyScore) { list.push({ label: '评分', value: this.formatValue(latestScaleResult.bodyScore) + '分' }) } } return list.slice(0, 5) }, formatDateTime: function (value) { if (!value) { return '' } return String(value).replace('T', ' ').slice(0, 16) }, formatValue: function (value) { if (value === undefined || value === null || value === '') { return '--' } var num = Number(value) if (isNaN(num)) { return String(value) } if (Math.floor(num) === num) { return String(num) } return num.toFixed(1).replace(/\.0$/, '') }, buildImageList: function (images) { var mapping = { tongue: '舌面', tongueBottom: '舌下', face: '面部', body: '全身' } var orderedKeys = ['tongue', 'tongueBottom', 'face', 'body'] if (typeof images === 'string' && images) { try { images = JSON.parse(images) } catch (err) { images = {} } } var list = [] for (var i = 0; i < orderedKeys.length; i++) { var key = orderedKeys[i] var urls = images ? images[key] : null if (Array.isArray(urls) && urls.length) { list.push({ title: mapping[key], url: urls[0] }) } else if (typeof urls === 'string' && urls) { list.push({ title: mapping[key], url: urls }) } } return list }, startPolling: function () { var that = this that.clearTimer() that.pollCount = 0 that.timer = setInterval(function () { that.pollCount++ if (that.pollCount > 60) { that.clearTimer() that.setData({ status: 'error', errorMessage: '报告生成超时,请稍后重新查看' }) return } that.loadReport(true) }, 3000) }, clearTimer: function () { if (this.timer) { clearInterval(this.timer) this.timer = null } }, previewReportImage: function (e) { var current = e.currentTarget.dataset.src var urls = this.data.imageList.map(function (item) { return item.url }) wx.previewImage({ current: current, urls: urls }) }, retryReport: function () { this.clearTimer() this.loadReport(false) }, goDiagnosis: function () { wx.redirectTo({ url: '/packageWellness/pages/tcm/diagnosis/diagnosis' }) } })