guide.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // module/chats/components/guide/guide.ts
  2. import { getPatients } from "../../../../pages/home/request";
  3. import { toCertificationPage } from "../../../../pages/home/router";
  4. import { Post } from "../../../../lib/request/method";
  5. import I18nBehavior from "../../../../i18n/behavior";
  6. let next: "handleA" | "handleB" | "handleC";
  7. Component({
  8. behaviors: [I18nBehavior],
  9. lifetimes: {
  10. attached(this: any) {
  11. const showGuideActive = wx.getStorageSync("showGuideActive");
  12. // 从主页点击健康评估进来的
  13. if (showGuideActive) {
  14. this.handleA();
  15. wx.removeStorageSync("showGuideActive");
  16. }
  17. // 首次挂载时也拉取一次,避免初次渲染为 0
  18. this.setAnalysisCount();
  19. },
  20. },
  21. pageLifetimes: {
  22. show(this: any) {
  23. // 获取评估剩余次数
  24. this.setAnalysisCount().then(() => {
  25. if (next) {
  26. const patientId = wx.getStorageSync("patientId");
  27. if (patientId) this[next]();
  28. else wx.showToast({ title: "请先完善信息!", icon: "none" });
  29. next = undefined;
  30. }
  31. });
  32. },
  33. },
  34. properties: {
  35. id: { type: String, value: "" },
  36. active: { type: Boolean, value: false },
  37. },
  38. data: {
  39. i18n: {
  40. chats: {
  41. analysis: '测评',
  42. healthData: '个人信息管理',
  43. healthIndex: '数据信息管理',
  44. consult: '',
  45. },
  46. },
  47. analysisCount: 0,
  48. result: [] as string[],
  49. result2: false,
  50. patient: {} as any,
  51. },
  52. methods: {
  53. // 获取评估剩余次数
  54. async setAnalysisCount(this: any) {
  55. try {
  56. // 获取剩余次数
  57. const residuedCount = await Post(
  58. `/patientInfoManage/rechargeUseDetail`,
  59. {},
  60. {
  61. transform({ data }: any) {
  62. if (data?.usedCou === 0 && data.residuedCou === 0)
  63. data.residuedCou = 3;
  64. return data?.residuedCou;
  65. },
  66. }
  67. );
  68. this.setData({ analysisCount: residuedCount });
  69. } catch (error) {
  70. console.error("获取剩余次数失败:", error);
  71. }
  72. },
  73. async handleA() {
  74. const { patient } = await getPatients(/*this.data.patientId*/);
  75. if (!patient) {
  76. await toCertificationPage();
  77. next = "handleA";
  78. } else {
  79. if (this.data.active) {
  80. // 设置 isAnalysis 为 3(健康分析/对话管家)
  81. wx.setStorageSync("isAnalysis", 3);
  82. // 先触发 nextType 更新 messageType,然后触发 next 切换组件
  83. this.triggerEvent("nextType", { MessageType: 2 });
  84. setTimeout(() => {
  85. this.triggerEvent("next", {
  86. component: "questionnaire",
  87. scroll: true,
  88. });
  89. }, 100);
  90. }
  91. }
  92. },
  93. async handleB() {
  94. const { patient } = await getPatients(/*this.data.patientId*/);
  95. if (!patient) {
  96. await toCertificationPage();
  97. next = "handleB";
  98. } else {
  99. if (this.data.active)
  100. wx.navigateTo({
  101. url: "/module/health/pages/status/status",
  102. events: { update: this._update.bind(this) },
  103. });
  104. }
  105. },
  106. async handleC() {
  107. const { patient } = await getPatients(/*this.data.patientId*/);
  108. if (!patient) {
  109. await toCertificationPage();
  110. next = "handleC";
  111. } else {
  112. if (this.data.active)
  113. wx.navigateTo({
  114. url: "/module/user/pages/user-edit/user-edit",
  115. events: {
  116. update2: () => {
  117. this.setData({ result2: true });
  118. this.triggerEvent("next", { component: "guide", scroll: true });
  119. },
  120. },
  121. });
  122. }
  123. },
  124. handleD() {
  125. // if (this.data.active) wx.navigateBack();
  126. if (this.data.active) {
  127. wx.redirectTo({ url: "/pages/home/home" });
  128. }
  129. },
  130. // 在线咨询
  131. async handleE() {
  132. const { patient } = await getPatients(/*this.data.patientId*/);
  133. if (!patient) await toCertificationPage();
  134. else {
  135. if (this.data.active) {
  136. // 清除之前的咨询结束状态,开始新咨询
  137. wx.setStorageSync("consultEnded", false);
  138. wx.setStorageSync("lastConsultTime", Date.now());
  139. wx.setStorageSync("isAnalysis", 5);
  140. // 先触发 nextType 更新 messageType,然后立即触发 next 切换组件
  141. // 由于 nextType 事件处理是同步的,所以可以立即触发 next
  142. this.triggerEvent("nextType", { MessageType: 3 });
  143. setTimeout(() => {
  144. this.triggerEvent("next", {
  145. component: "questionnaire",
  146. scroll: true,
  147. });
  148. }, 100);
  149. }
  150. }
  151. },
  152. _update(result: string[]) {
  153. if (result.length) {
  154. this.setData({ result });
  155. this.triggerEvent("next", { component: "guide", scroll: true });
  156. } else {
  157. setTimeout(
  158. () => wx.showToast({ title: `没有更改项`, icon: "none" }),
  159. 300
  160. );
  161. }
  162. },
  163. },
  164. });