home.ts 8.3 KB

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