home.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. import PageContainerBehavior from "../../core/behavior/page-container.behavior";
  2. import { DraggableSheetBehavior, getDraggableSheetContext } from "../../core/behavior/draggableSheet.behavior";
  3. import { login } from "../../lib/logic";
  4. const { shared, Easing, timing } = wx.worklet
  5. const offset = shared(0);
  6. // pages/home/home.ts
  7. import { getPatients, healthReportMethod, healthIndexMethod, getSolarTerms, getShortScienceList } from "./request";
  8. import { toCertificationPage } from "./router";
  9. import { useLocation } from "../../lib/use/use-location";
  10. Component({
  11. behaviors: [
  12. PageContainerBehavior,
  13. DraggableSheetBehavior('.draggable-sheet-wrapper'),
  14. ],
  15. lifetimes: {
  16. attached() {
  17. this.initFabAnimated();
  18. }
  19. },
  20. pageLifetimes: {
  21. show() { this.load(); },
  22. hide() {
  23. offset.value = timing(0, { duration: 100, easing: (<any>Easing).linear }, () => { 'worklet' });
  24. }
  25. },
  26. properties: {
  27. patientId: { type: String, value: '' },
  28. next: { type: String, value: '' },
  29. },
  30. data: {
  31. patients: [] as (App.Patient.Model & { isDefault: 'Y' | 'N' })[],
  32. patient: null as App.Patient.Model | null,
  33. healthId: '',
  34. healthReport: { data: null, message: '' },
  35. healthIndex: { data: [], message: '' },
  36. position: {} as AnyObject,
  37. location: {} as AnyObject,
  38. solarTerms: {} as AnyObject,
  39. sheet: false,
  40. scienceList: [] as AnyArray,
  41. _loaded: false,
  42. },
  43. observers: {
  44. 'patient'(model: { patientId: string, sex: 0 | 1 }) {
  45. wx.setStorageSync('patientId', model.patientId);
  46. this._getHealthReport();
  47. this._getAbnormalHealthIndex();
  48. const patientIcon = { 0: 'gender-male', 1: 'gender-female' }[model.sex];
  49. const patientIconColor = { 0: '#0f40f5', 1: '#E560B3' }[model.sex];
  50. this.setData({ patientIcon, patientIconColor })
  51. },
  52. },
  53. methods: {
  54. async load() {
  55. await login();
  56. wx.showLoading({ title: '加载中' });
  57. const { patient } = await getPatients(/*this.data.patientId*/);
  58. if (!patient) await toCertificationPage();
  59. else this.setData({ patient });
  60. wx.hideLoading();
  61. if (!this.data._loaded) {
  62. this.loadScienceList();
  63. useLocation().then((location) => { this.setData({ location }) }).catch(()=>{});
  64. getSolarTerms().then((solarTerms) => { this.setData({ solarTerms }) }).catch(()=>{});
  65. this.setData({ _loaded: true });
  66. }
  67. },
  68. async _getHealthReport() {
  69. wx.showLoading({ title: '加载中' });
  70. this.setData({ 'healthReport.loading': true, })
  71. try {
  72. const data = await healthReportMethod();
  73. this.setData({
  74. 'healthReport.data': data,
  75. 'healthReport.loading': false,
  76. healthId: data?.healthAnalysisReportId,
  77. });
  78. } catch (error) {
  79. this.setData({
  80. 'healthReport.data': [],
  81. 'healthReport.loading': false,
  82. 'healthReport.message': error.errMsg,
  83. healthId: '',
  84. });
  85. }
  86. wx.hideLoading();
  87. },
  88. async _getAbnormalHealthIndex() {
  89. this.setData({ 'healthIndex.loading': true, })
  90. try {
  91. const data = await healthIndexMethod();
  92. this.setData({
  93. 'healthIndex.data': data.map((item: AnyObject) => item.abnormalDesc).filter(Boolean),
  94. 'healthIndex.loading': false,
  95. });
  96. } catch (error) {
  97. this.setData({
  98. 'healthIndex.data': [],
  99. 'healthIndex.loading': false,
  100. 'healthIndex.message': error.errMsg,
  101. });
  102. }
  103. },
  104. onBodyModel(event: WechatMiniprogram.TouchEvent) {
  105. if (event.detail?.position === 'LB') { this.toReportPage(); }
  106. else if (event.detail?.position === 'LT') {
  107. const report = this.data.healthReport.data as unknown as AnyObject;
  108. this.setData({
  109. position: {
  110. LT: ['willillState', 'willillDegree', 'willillSocial', 'willillFunction'].map(key => {
  111. const title = report[`${key}Name`]
  112. const description = report[`${key}Description`]
  113. return title || description ? { title, description } : null
  114. }).filter(Boolean)
  115. },
  116. });
  117. this.showDraggableSheet();
  118. }
  119. else if (event.detail?.position === 'RT') {
  120. const report = this.data.healthReport.data as unknown as AnyObject;
  121. const get = (key: string) => ({ [key]: report[key] })
  122. this.setData({
  123. position: {
  124. RT: {
  125. ...get('constitutionGroupName'),
  126. ...get('constitutionGroupDefinition'),
  127. ...get('faceImg'),
  128. ...get('faceAnalysisResult'),
  129. ...get('tongueAnalysisResult'),
  130. ...get('upImg'),
  131. ...get('downImg'),
  132. }
  133. },
  134. });
  135. this.showDraggableSheet();
  136. }
  137. else if (event.detail?.position === 'RB') {
  138. const report = this.data.healthReport.data as unknown as AnyObject;
  139. const get = (key: string) => ({ [key]: report[key] })
  140. this.setData({
  141. position: {
  142. RB: {
  143. ...get('diagnoseSyndromeSummary'),
  144. ...get('diagnoseSyndromes'),
  145. ...get('factorItemSummary'),
  146. ...get('factorItems'),
  147. }
  148. },
  149. });
  150. this.showDraggableSheet();
  151. }
  152. else if (event.detail?.position === 'CT') {
  153. this.setData({
  154. position: { CT: this.data.healthIndex.data.map((item, index) => `${index + 1}、${item}`) }
  155. });
  156. this.showDraggableSheet();
  157. }
  158. },
  159. initFabAnimated() {
  160. (<any>this).applyAnimatedStyle('.fab-wrapper', () => {
  161. 'worklet'
  162. return { right: `${Math.min(offset.value, 36) - 36}px` };
  163. });
  164. (<any>this).applyAnimatedStyle('.fab-1', () => {
  165. 'worklet'
  166. return { transform: `translateY(${-offset.value}px)` };
  167. });
  168. (<any>this).applyAnimatedStyle('.fab-2', () => {
  169. 'worklet'
  170. return { transform: `translateX(${-offset.value}px) translateY(${-offset.value / 2}px)` };
  171. });
  172. (<any>this).applyAnimatedStyle('.fab-3', () => {
  173. 'worklet'
  174. return { transform: `translateX(${-offset.value}px) translateY(${offset.value / 2}px)` };
  175. });
  176. (<any>this).applyAnimatedStyle('.fab-4', () => {
  177. 'worklet'
  178. return { transform: `translateY(${offset.value}px)` };
  179. });
  180. },
  181. onFabTap() {
  182. const value = Math.abs(offset.value - 72);
  183. offset.value = timing(value, { duration: 500, easing: (<any>Easing).linear }, () => {
  184. 'worklet'
  185. if (offset.value > 0) offset.value = 72
  186. })
  187. },
  188. toChatsPage() {
  189. wx.navigateTo({ url: `/module/chats/pages/index/index` })
  190. },
  191. toHealthPage() {
  192. wx.navigateTo({ url: `/module/health/pages/home/home` })
  193. },
  194. toDietTonicPage() {
  195. wx.navigateTo({ url: `/module/article/pages/diet-list/diet-list?classify=tonic` })
  196. },
  197. toDietTeaPage() {
  198. wx.navigateTo({ url: `/module/article/pages/diet-list/diet-list?classify=tea` })
  199. },
  200. toSciencePage() {
  201. wx.navigateTo({ url: `/module/article/pages/science-list/science-list` })
  202. },
  203. toSchemePage() {
  204. const id = this.data.healthId;
  205. if (id) wx.navigateTo({ url: `/module/health/pages/scheme/scheme?id=${id}` })
  206. else wx.showToast({ title: '暂无调理方案', icon: 'none' });
  207. },
  208. toReportPage() {
  209. const id = this.data.healthId;
  210. if (id) wx.navigateTo({ url: `/module/health/pages/report/report?id=${id}` })
  211. else wx.showToast({ title: '暂无分析报告', icon: 'none' });
  212. },
  213. onDraggableSizeUpdate(e) {
  214. 'worklet'
  215. if (e.pixels < 1) {
  216. wx.worklet.runOnJS(this.hideDraggableSheet.bind(this))()
  217. }
  218. },
  219. showDraggableSheet() {
  220. getDraggableSheetContext.call(this).scrollTo({
  221. size: 0.5,
  222. pixels: 600,
  223. animated: true,
  224. duration: 300,
  225. easingFunction: 'ease'
  226. });
  227. this.setData({ sheet: true });
  228. },
  229. hideDraggableSheet(event?: any) {
  230. if (event) {
  231. getDraggableSheetContext.call(this).scrollTo({
  232. size: 0,
  233. animated: true,
  234. duration: 300,
  235. easingFunction: 'ease'
  236. });
  237. }
  238. this.setData({ position: {}, sheet: false })
  239. },
  240. async loadScienceList() {
  241. try {
  242. const { data } = await getShortScienceList();
  243. this.setData({ scienceList: data })
  244. } catch (error) {
  245. }
  246. }
  247. },
  248. })