home.ts 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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(forceLogin = false) {
  60. try {
  61. await login(forceLogin);
  62. wx.showLoading({ title: '加载中' });
  63. const { patient } = await getPatients(/*this.data.patientId*/);
  64. if (!patient) await toCertificationPage();
  65. else {
  66. this.setData({ patient });
  67. this.observerPatient(patient);
  68. };
  69. wx.hideLoading();
  70. } catch (error) {
  71. await wx.showModal({
  72. title: `加载失败`,
  73. content: `${error?.errMsg ?? error?.message ?? ''}`,
  74. showCancel: false,
  75. confirmText: `重新加载`
  76. }).catch(() => { });
  77. this.load(true);
  78. return;
  79. }
  80. if (!this.data._loaded) {
  81. this.loadScienceList();
  82. useLocation().then((location) => { this.setData({ location }) }).catch(() => { });
  83. getSolarTerms().then((solarTerms) => { this.setData({ solarTerms }) }).catch(() => { });
  84. this.setData({ _loaded: true });
  85. }
  86. },
  87. async _getHealthReport() {
  88. wx.showLoading({ title: '加载中' });
  89. this.setData({ 'healthReport.loading': true, })
  90. try {
  91. const data = await healthReportMethod();
  92. this.setData({
  93. 'healthReport.data': data,
  94. 'healthReport.loading': false,
  95. healthId: data?.healthAnalysisReportId,
  96. });
  97. } catch (error) {
  98. this.setData({
  99. 'healthReport.data': [],
  100. 'healthReport.loading': false,
  101. 'healthReport.message': error.errMsg,
  102. healthId: '',
  103. });
  104. }
  105. wx.hideLoading();
  106. },
  107. async _getAbnormalHealthIndex() {
  108. this.setData({ 'healthIndex.loading': true, })
  109. try {
  110. const data = await healthIndexMethod();
  111. this.setData({
  112. 'healthIndex.data': data.map((item: AnyObject) => item.abnormalDesc).filter(Boolean),
  113. 'healthIndex.loading': false,
  114. });
  115. } catch (error) {
  116. this.setData({
  117. 'healthIndex.data': [],
  118. 'healthIndex.loading': false,
  119. 'healthIndex.message': error.errMsg,
  120. });
  121. }
  122. },
  123. onBodyModel(event: WechatMiniprogram.TouchEvent) {
  124. if (event.detail?.position === 'LB') { this.toReportPage(); }
  125. else if (event.detail?.position === 'LT') {
  126. const report = this.data.healthReport.data as unknown as AnyObject;
  127. this.setData({
  128. position: {
  129. LT: ['willillState', 'willillDegree', 'willillSocial', 'willillFunction'].map(key => {
  130. const title = report[`${key}Name`]
  131. const description = report[`${key}Description`]
  132. return title || description ? { title, description } : null
  133. }).filter(Boolean)
  134. },
  135. });
  136. this.showDraggableSheet();
  137. }
  138. else if (event.detail?.position === 'RT') {
  139. const report = this.data.healthReport.data as unknown as AnyObject;
  140. const get = (key: string) => ({ [key]: report[key] })
  141. this.setData({
  142. position: {
  143. RT: {
  144. ...get('constitutionGroupName'),
  145. ...get('constitutionGroupDefinition'),
  146. ...get('faceImg'),
  147. ...get('faceAnalysisResult'),
  148. ...get('tongueAnalysisResult'),
  149. ...get('upImg'),
  150. ...get('downImg'),
  151. }
  152. },
  153. });
  154. this.showDraggableSheet();
  155. }
  156. else if (event.detail?.position === 'RB') {
  157. const report = this.data.healthReport.data as unknown as AnyObject;
  158. const get = (key: string) => ({ [key]: report[key] })
  159. this.setData({
  160. position: {
  161. RB: {
  162. ...get('diagnoseSyndromeSummary'),
  163. ...get('diagnoseSyndromes'),
  164. ...get('factorItemSummary'),
  165. ...get('factorItems'),
  166. }
  167. },
  168. });
  169. this.showDraggableSheet();
  170. }
  171. else if (event.detail?.position === 'CT') {
  172. this.setData({
  173. position: { CT: this.data.healthIndex.data.map((item, index) => `${index + 1}、${item}`) }
  174. });
  175. this.showDraggableSheet();
  176. }
  177. },
  178. initFabAnimated() {
  179. (<any>this).applyAnimatedStyle('.fab-wrapper', () => {
  180. 'worklet'
  181. return { right: `${Math.min(offset.value, 36) - 36}px` };
  182. });
  183. (<any>this).applyAnimatedStyle('.fab-1', () => {
  184. 'worklet'
  185. return { transform: `translateY(${-offset.value}px)` };
  186. });
  187. (<any>this).applyAnimatedStyle('.fab-2', () => {
  188. 'worklet'
  189. return { transform: `translateX(${-offset.value}px) translateY(${-offset.value / 2}px)` };
  190. });
  191. (<any>this).applyAnimatedStyle('.fab-3', () => {
  192. 'worklet'
  193. return { transform: `translateX(${-offset.value}px) translateY(${offset.value / 2}px)` };
  194. });
  195. (<any>this).applyAnimatedStyle('.fab-4', () => {
  196. 'worklet'
  197. return { transform: `translateY(${offset.value}px)` };
  198. });
  199. },
  200. onFabTap() {
  201. const value = Math.abs(offset.value - 72);
  202. offset.value = timing(value, { duration: 500, easing: (<any>Easing).linear }, () => {
  203. 'worklet'
  204. if (offset.value > 0) offset.value = 72
  205. })
  206. },
  207. async toChatsPage() {
  208. if (!this.data.patient?.patientId) {
  209. try {
  210. await this.load();
  211. } catch (error) {
  212. wx.showModal({ title: '出错了', content: error?.errMsg ?? error?.message ?? '错误,请重试', showCancel: false })
  213. return;
  214. }
  215. }
  216. wx.navigateTo({ url: `/module/chats/pages/index/index` })
  217. },
  218. toHealthPage() {
  219. wx.navigateTo({ url: `/module/health/pages/home/home` })
  220. },
  221. toDietTonicPage() {
  222. wx.navigateTo({ url: `/module/article/pages/diet-list/diet-list?classify=tonic` })
  223. },
  224. toDietTeaPage() {
  225. wx.navigateTo({ url: `/module/article/pages/diet-list/diet-list?classify=tea` })
  226. },
  227. toSciencePage() {
  228. wx.navigateTo({ url: `/module/article/pages/science-list/science-list` })
  229. },
  230. toSchemePage() {
  231. const id = this.data.healthId;
  232. if (id) wx.navigateTo({ url: `/module/health/pages/scheme/scheme?id=${id}` })
  233. else wx.showToast({ title: '暂无调理方案', icon: 'none' });
  234. },
  235. toReportPage() {
  236. const id = this.data.healthId;
  237. if (id) wx.navigateTo({ url: `/module/health/pages/report/report?id=${id}` })
  238. else wx.showToast({ title: '暂无分析报告', icon: 'none' });
  239. },
  240. onDraggableSizeUpdate(e) {
  241. 'worklet'
  242. if (e.pixels < 1) {
  243. wx.worklet.runOnJS(this.hideDraggableSheet.bind(this))()
  244. }
  245. },
  246. showDraggableSheet() {
  247. getDraggableSheetContext.call(this).scrollTo({
  248. size: 0.5,
  249. pixels: 600,
  250. animated: true,
  251. duration: 300,
  252. easingFunction: 'ease'
  253. });
  254. this.setData({ sheet: true });
  255. },
  256. hideDraggableSheet(event?: any) {
  257. if (event) {
  258. getDraggableSheetContext.call(this).scrollTo({
  259. size: 0,
  260. animated: true,
  261. duration: 300,
  262. easingFunction: 'ease'
  263. });
  264. }
  265. this.setData({ position: {}, sheet: false })
  266. },
  267. async loadScienceList() {
  268. try {
  269. const { data } = await getShortScienceList();
  270. this.setData({ scienceList: data })
  271. } catch (error) {
  272. }
  273. },
  274. observerPatient(model: { patientId: string, sex: '0' | '1' }) {
  275. wx.setStorageSync('patientId', model.patientId);
  276. this._getHealthReport();
  277. this._getAbnormalHealthIndex();
  278. const patientIcon = { 0: 'gender-male', 1: 'gender-female' }[model.sex];
  279. const patientIconColor = { 0: '#0f40f5', 1: '#E560B3' }[model.sex];
  280. this.setData({ patientIcon, patientIconColor })
  281. getPatientDescription(model).then((patientDescription) => { this.setData({ patientDescription }) })
  282. },
  283. })