report.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. var tcm = require('../../../../utils/tcm.js')
  2. Page({
  3. data: {
  4. loading: true,
  5. status: 'loading',
  6. errorMessage: '',
  7. examId: '',
  8. scaleUserId: '',
  9. exam: null,
  10. scaleUser: null,
  11. latestScaleResult: null,
  12. statList: [],
  13. questionList: [],
  14. sectionList: [],
  15. imageList: [],
  16. reportTime: ''
  17. },
  18. timer: null,
  19. pollCount: 0,
  20. onLoad: function (options) {
  21. this.setData({
  22. examId: options.examId || '',
  23. scaleUserId: options.scaleUserId || ''
  24. })
  25. this.loadReport(false)
  26. },
  27. onUnload: function () {
  28. this.clearTimer()
  29. },
  30. loadReport: function (silent) {
  31. var that = this
  32. if (!silent) {
  33. that.setData({
  34. loading: true,
  35. errorMessage: ''
  36. })
  37. }
  38. var requestPromise = that.data.examId
  39. ? tcm.getExamDetail(that.data.examId)
  40. : tcm.getLatestExam(that.data.scaleUserId)
  41. requestPromise.then(function (data) {
  42. var exam = data && data.exam ? data.exam : null
  43. var aiSections = data && data.aiSections ? data.aiSections : {}
  44. var hasSections = that.buildSectionList(aiSections).length > 0
  45. if (!exam) {
  46. that.setData({
  47. loading: false,
  48. status: 'empty',
  49. exam: null,
  50. sectionList: [],
  51. imageList: []
  52. })
  53. return
  54. }
  55. var status = exam.status || 'pending'
  56. if (status === 'completed' && !hasSections) {
  57. status = 'generating'
  58. }
  59. that.setData({
  60. loading: false,
  61. status: status,
  62. exam: exam,
  63. scaleUser: data.scaleUser || null,
  64. latestScaleResult: data.latestScaleResult || null,
  65. statList: that.buildStatList(data.scaleUser || null, data.latestScaleResult || null),
  66. questionList: that.buildQuestionList(data.questionAnswersLocalizedExpanded || {}),
  67. sectionList: that.buildSectionList(aiSections),
  68. imageList: that.buildImageList(exam.observationImagesOss || exam.observationImages || {}),
  69. errorMessage: aiSections && aiSections.error ? aiSections.error : '',
  70. reportTime: that.formatDateTime(exam.CreatedAt || '')
  71. })
  72. if (status === 'pending' || status === 'generating') {
  73. if (!that.timer) {
  74. that.startPolling()
  75. }
  76. } else {
  77. that.clearTimer()
  78. }
  79. }).catch(function (err) {
  80. that.setData({
  81. loading: false,
  82. status: 'error',
  83. errorMessage: err && err.message ? err.message : '报告加载失败'
  84. })
  85. })
  86. },
  87. buildQuestionList: function (source) {
  88. var list = []
  89. if (Array.isArray(source)) {
  90. return source
  91. }
  92. for (var key in source) {
  93. list.push({
  94. title: key,
  95. value: Array.isArray(source[key]) ? source[key].join(',') : source[key]
  96. })
  97. }
  98. return list
  99. },
  100. buildSectionList: function (aiSections) {
  101. var mapping = [
  102. { key: 'general', title: '一般情况', badge: '壹' },
  103. { key: 'inquiry', title: '问诊分析', badge: '贰' },
  104. { key: 'observation', title: '望诊分析', badge: '叁' },
  105. { key: 'constitution', title: '体质辨识', badge: '肆' },
  106. { key: 'advice', title: '调养建议', badge: '伍' },
  107. { key: 'summary', title: '报告摘要', badge: '陆' }
  108. ]
  109. var list = []
  110. for (var i = 0; i < mapping.length; i++) {
  111. if (aiSections && aiSections[mapping[i].key]) {
  112. list.push({
  113. key: mapping[i].key,
  114. badge: mapping[i].badge,
  115. title: mapping[i].title,
  116. content: aiSections[mapping[i].key]
  117. })
  118. }
  119. }
  120. return list
  121. },
  122. buildStatList: function (scaleUser, latestScaleResult) {
  123. var list = []
  124. if (scaleUser) {
  125. if (scaleUser.sex !== undefined && scaleUser.sex !== null) {
  126. list.push({
  127. label: '性别',
  128. value: Number(scaleUser.sex) === 1 ? '女' : '男'
  129. })
  130. }
  131. if (scaleUser.age) {
  132. list.push({
  133. label: '年龄',
  134. value: scaleUser.age + '岁'
  135. })
  136. }
  137. if (scaleUser.height) {
  138. list.push({
  139. label: '身高',
  140. value: scaleUser.height + 'cm'
  141. })
  142. }
  143. }
  144. if (latestScaleResult) {
  145. var weight = latestScaleResult.weight_kg || latestScaleResult.weight
  146. if (weight) {
  147. list.push({
  148. label: '体重',
  149. value: this.formatValue(weight) + 'kg'
  150. })
  151. }
  152. if (latestScaleResult.bodyScore) {
  153. list.push({
  154. label: '评分',
  155. value: this.formatValue(latestScaleResult.bodyScore) + '分'
  156. })
  157. }
  158. }
  159. return list.slice(0, 5)
  160. },
  161. formatDateTime: function (value) {
  162. if (!value) {
  163. return ''
  164. }
  165. return String(value).replace('T', ' ').slice(0, 16)
  166. },
  167. formatValue: function (value) {
  168. if (value === undefined || value === null || value === '') {
  169. return '--'
  170. }
  171. var num = Number(value)
  172. if (isNaN(num)) {
  173. return String(value)
  174. }
  175. if (Math.floor(num) === num) {
  176. return String(num)
  177. }
  178. return num.toFixed(1).replace(/\.0$/, '')
  179. },
  180. buildImageList: function (images) {
  181. var mapping = {
  182. tongue: '舌面',
  183. tongueBottom: '舌下',
  184. face: '面部',
  185. body: '全身'
  186. }
  187. var orderedKeys = ['tongue', 'tongueBottom', 'face', 'body']
  188. if (typeof images === 'string' && images) {
  189. try {
  190. images = JSON.parse(images)
  191. } catch (err) {
  192. images = {}
  193. }
  194. }
  195. var list = []
  196. for (var i = 0; i < orderedKeys.length; i++) {
  197. var key = orderedKeys[i]
  198. var urls = images ? images[key] : null
  199. if (Array.isArray(urls) && urls.length) {
  200. list.push({
  201. title: mapping[key],
  202. url: urls[0]
  203. })
  204. } else if (typeof urls === 'string' && urls) {
  205. list.push({
  206. title: mapping[key],
  207. url: urls
  208. })
  209. }
  210. }
  211. return list
  212. },
  213. startPolling: function () {
  214. var that = this
  215. that.clearTimer()
  216. that.pollCount = 0
  217. that.timer = setInterval(function () {
  218. that.pollCount++
  219. if (that.pollCount > 60) {
  220. that.clearTimer()
  221. that.setData({
  222. status: 'error',
  223. errorMessage: '报告生成超时,请稍后重新查看'
  224. })
  225. return
  226. }
  227. that.loadReport(true)
  228. }, 3000)
  229. },
  230. clearTimer: function () {
  231. if (this.timer) {
  232. clearInterval(this.timer)
  233. this.timer = null
  234. }
  235. },
  236. previewReportImage: function (e) {
  237. var current = e.currentTarget.dataset.src
  238. var urls = this.data.imageList.map(function (item) {
  239. return item.url
  240. })
  241. wx.previewImage({
  242. current: current,
  243. urls: urls
  244. })
  245. },
  246. retryReport: function () {
  247. this.clearTimer()
  248. this.loadReport(false)
  249. },
  250. goDiagnosis: function () {
  251. wx.redirectTo({
  252. url: '/packageWellness/pages/tcm/diagnosis/diagnosis'
  253. })
  254. }
  255. })