| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- 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'
- })
- }
- })
|