home.ts 8.7 KB

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