guide.ts 4.7 KB

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