questionnaire.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. import dayjs from "dayjs";
  2. import I18nBehavior from "../../../../i18n/behavior";
  3. import { Post } from "../../../../lib/request/method";
  4. // module/chats/components/questionnaire/questionnaire.ts
  5. interface Message {
  6. id: string;
  7. type:
  8. | "system"
  9. | "analysis"
  10. | "select"
  11. | "text"
  12. | "report"
  13. | "again"
  14. | "follow"
  15. | "count"
  16. | "consult";
  17. payload: AnyObject;
  18. }
  19. interface HandleEvent {
  20. target: { id: string };
  21. detail: AnyObject;
  22. type: "next";
  23. }
  24. interface MessageType {
  25. messageType: number;
  26. }
  27. Component({
  28. behaviors: [I18nBehavior],
  29. lifetimes: {
  30. attached: function () {
  31. let isAnalysis: number;
  32. isAnalysis = wx.getStorageSync("isAnalysis");
  33. if (isAnalysis === 3 || isAnalysis === 4 || isAnalysis === 5) {
  34. // 对话管家
  35. this._start();
  36. } else if (isAnalysis === 2) {
  37. // 随访提醒
  38. this.setData({
  39. [`_next.classify`]: "",
  40. [`_next.dialogId`]: "",
  41. [`_next.questions`]: [],
  42. _timestamp: Date.now(),
  43. });
  44. this._next();
  45. }
  46. },
  47. },
  48. properties: {
  49. messageType: { type: Number, value: 0 },
  50. workId: { type: Number, value: 0 },
  51. },
  52. /**
  53. * 组件的初始数据
  54. */
  55. data: {
  56. i18n: {
  57. chats: { report: '' },
  58. },
  59. inputBoxBottom: 0,
  60. messages: {} as Record<number, Message>,
  61. lastId: "",
  62. _next: {
  63. classify: "",
  64. dialogId: "",
  65. questions: [],
  66. } as AnyObject,
  67. _timestamp: Date.now(),
  68. // 防止 _next 被并发/重复触发(例如 classify === 'tongue' 时)
  69. _requesting: false,
  70. consultStarted: false,
  71. },
  72. observers: {
  73. "messages.**"(messages) {
  74. const message = Object.values(messages).pop() as Message;
  75. this.setData({ lastId: message?.id });
  76. },
  77. },
  78. methods: {
  79. nextType(event: MessageType) {
  80. this.setData({ messageType: event.detail.MessageType });
  81. this._next();
  82. },
  83. scroll(option: { id: string }) {
  84. this.triggerEvent("to", option.detail.id);
  85. },
  86. boxBottom(event: boxBottom) {
  87. this.setData({ inputBoxBottom: event.detail.inputBoxBottom });
  88. this.triggerEvent("boxBottom", {
  89. inputBoxBottom: this.data.inputBoxBottom + 100,
  90. });
  91. },
  92. handle(event: HandleEvent) {
  93. const isAnalysis = wx.getStorageSync("isAnalysis");
  94. if (isAnalysis === 3 || isAnalysis === 4) {
  95. const index = event.target?.id.split(".").pop() ?? 0;
  96. const questions = this.data._next.questions;
  97. Object.assign(questions[index], event.detail);
  98. this.setData({ "_next.questions": questions });
  99. this._next();
  100. } else if (isAnalysis === 5) {
  101. // 在线咨询暂不需要在问卷组件中处理回答
  102. return;
  103. } else {
  104. // 随访
  105. this._createMessage({
  106. id: `follow.${Date.now()}`,
  107. type: "follow",
  108. payload: { title: "" },
  109. });
  110. }
  111. },
  112. async _start() {
  113. const isAnalysis = wx.getStorageSync("isAnalysis");
  114. // 在线咨询:isAnalysis === 5 且 messageType === 3
  115. if (isAnalysis === 5 && this.data.messageType === 3) {
  116. this.setData({
  117. [`_next.classify`]: "",
  118. [`_next.dialogId`]: "",
  119. [`_next.questions`]: [],
  120. _timestamp: Date.now(),
  121. consultStarted: false,
  122. });
  123. this._next();
  124. return;
  125. }
  126. try {
  127. // 获取剩余次数
  128. const count = await Post(
  129. `/patientInfoManage/rechargeUseDetail`,
  130. {},
  131. {
  132. transform({ data }: any) {
  133. return data?.residuedCou;
  134. },
  135. }
  136. );
  137. this.triggerEvent("count", { count });
  138. if (count > 0) {
  139. this.setData({
  140. [`_next.classify`]: "",
  141. [`_next.dialogId`]: "",
  142. [`_next.questions`]: [],
  143. _timestamp: Date.now(),
  144. });
  145. this._next();
  146. } else {
  147. // throw { errMsg: `您的健康分析次数已用完,请联系工作人员。` };
  148. this._createMessage({
  149. id: `count-${Date.now()}`,
  150. type: "count",
  151. payload: {
  152. date: Date.now(),
  153. title: `您的健康分析次数已用完,请联系工作人员。`,
  154. },
  155. });
  156. this._end();
  157. }
  158. } catch (error) {
  159. this._createMessage({
  160. id: `system-start`,
  161. type: "system",
  162. payload: {
  163. date: Date.now(),
  164. title: error.errMsg ?? `分析错误,请重试!`,
  165. },
  166. });
  167. this._end();
  168. }
  169. },
  170. _end() {
  171. this.setData({
  172. [`_next.classify`]: "",
  173. [`_next.dialogId`]: "",
  174. [`_next.questions`]: [],
  175. consultStarted: false,
  176. });
  177. // 对于 isAnalysis === 4 的情况,不创建新的 guide 组件,直接触发滚动
  178. // 对于 isAnalysis === 3 的情况,仍然创建 guide 组件显示三个业务选项
  179. const isAnalysis = wx.getStorageSync("isAnalysis");
  180. if (isAnalysis === 4) {
  181. // 触发滚动到当前 questionnaire 组件,让父页面滚动到这个组件
  182. setTimeout(() => {
  183. this.triggerEvent("to");
  184. }, 100);
  185. } else {
  186. this.triggerEvent("next", { component: "guide", scroll: true });
  187. // 对于 isAnalysis === 3 的情况,延迟触发滚动,确保报告消息和业务选项都渲染完成
  188. if (isAnalysis === 3) {
  189. setTimeout(() => {
  190. this.triggerEvent("to");
  191. }, 300);
  192. } else {
  193. // 额外触发一次滚动到底部,确保页面滚动到最新内容
  194. setTimeout(() => {
  195. this.triggerEvent("to", { detail: "bottom" });
  196. }, 200);
  197. }
  198. }
  199. },
  200. async _next() {
  201. // 并发与重复触发保护
  202. if (this.data._requesting) {
  203. return;
  204. }
  205. this.setData({ _requesting: true });
  206. let isAnalysis: number;
  207. isAnalysis = wx.getStorageSync("isAnalysis");
  208. if (isAnalysis === 3 || isAnalysis === 4) {
  209. // 对话管家
  210. if (this.data._next.classify === "tongue") {
  211. this._createMessage({
  212. id: `tongue-loading.${Date.now()}`,
  213. type: "text",
  214. payload: { title: "分析中...", loading: true },
  215. });
  216. this.triggerEvent("to");
  217. }
  218. }
  219. try {
  220. // messageType 1 是随访。messageType 2 是健康评估和对话管家。messageType 3 是在线咨询
  221. if (this.data.messageType === 1) {
  222. this._createMessage({
  223. id: `again.${Date.now()}`,
  224. type: "again",
  225. payload: { title: "" },
  226. });
  227. } else if (this.data.messageType === 3) {
  228. // 在线咨询
  229. // 开始咨询,如果咨询已经开始,则不重新开始
  230. if (!this.data.consultStarted) {
  231. try {
  232. // 开始咨询 获取咨询id
  233. const res = await Post(`/consultManage/start`);
  234. if (res.data) {
  235. // 存储咨询中的id
  236. wx.setStorageSync("consultId", res.data);
  237. this._createMessage({
  238. id: `consult.${Date.now()}`,
  239. type: "consult",
  240. payload: { title: "咨询开始" },
  241. });
  242. this.setData({ consultStarted: true });
  243. this.triggerEvent("to");
  244. }
  245. } catch (error: any) {
  246. console.error("咨询开始失败", error);
  247. wx.showToast({
  248. title: error.errMsg ?? `咨询开始失败,请重试!`,
  249. icon: "none",
  250. });
  251. }
  252. }
  253. } else if (this.data.messageType === 2) {
  254. let data: any = {};
  255. const res = await Post(
  256. `/dialogueManage/dialogTreat`,
  257. this.data._next
  258. );
  259. data = res.data;
  260. data.nextQuestions?.forEach((question: any, index: number) => {
  261. // isAnalysis 2是随访 3健康管家 4健康评估 5在线咨询
  262. if (isAnalysis === 2) {
  263. // 随访
  264. if (question.css === "tongue") {
  265. this._createMessage({
  266. id: `${question.classify}.${question.id}.${index}`,
  267. type: "analysis",
  268. payload: { title: question.title },
  269. });
  270. }
  271. } else {
  272. // 对话管家
  273. if (question.classify === "tongue_result") {
  274. this._updateMessage({
  275. id: `${question.classify}.${question.id}.${index}`,
  276. type: "text",
  277. payload: { title: question.content },
  278. });
  279. } else {
  280. if (question.css === "tongue") {
  281. this._createMessage({
  282. id: `${question.classify}.${question.id}.${index}`,
  283. type: "analysis",
  284. payload: { title: question.title },
  285. });
  286. } else if (question.css === "text") {
  287. this._createMessage({
  288. id: `${question.classify}.${question.id}.${index}`,
  289. type: "text",
  290. payload: { title: question.content },
  291. });
  292. } else if (["select", "checkbox"].includes(question.css)) {
  293. if (question.options && question.options.length > 0) {
  294. // 检查所有 item.options 的长度是否都为 0
  295. const allOptionsEmpty = question.options.every(
  296. (item: any) => !item.options || item.options.length === 0
  297. );
  298. question.required = allOptionsEmpty;
  299. }
  300. this._createMessage({
  301. id: `${question.classify}.${question.id}.${index}`,
  302. type: "select",
  303. payload: {
  304. title: question.title,
  305. options: question.options.map((item: AnyObject) => {
  306. if (Array.isArray(item.options)) {
  307. item.options = item.options.map((item) => {
  308. return { ...item, hide: item.css === "hide" };
  309. });
  310. }
  311. return { ...item, hide: item.css === "hide" };
  312. }),
  313. multiple: question.css === "checkbox",
  314. required: question.required,
  315. belongNew: question.belongNew,
  316. },
  317. });
  318. } else if (question.over) {
  319. return this._end();
  320. }
  321. }
  322. }
  323. });
  324. // 对话管家
  325. if (
  326. (isAnalysis === 3 && data.classify === "report") ||
  327. (isAnalysis === 4 && data.classify === "report")
  328. ) {
  329. if (data.classify === "report") {
  330. const diff = dayjs().diff(this.data._timestamp, "m");
  331. this._createMessage({
  332. id: "report",
  333. type: "report",
  334. payload: {
  335. title: `本次问答已结束,历时${diff || 1
  336. }分钟,非常感谢您的配合!${this.data.i18n.chats.report}`,
  337. url: `/module/health/pages/report/report?id=${data.healthAnalysisReportId}`,
  338. },
  339. });
  340. // 立即触发一次滚动,确保在最后一个问题生成报告卡片后页面滚动到底部
  341. this.triggerEvent("to");
  342. }
  343. if (data.over) {
  344. // 延迟触发滚动,确保报告消息已经渲染完成
  345. setTimeout(() => {
  346. this.triggerEvent("to");
  347. }, 100);
  348. return this._end();
  349. }
  350. }
  351. this.setData({
  352. [`_next.classify`]: data.classify,
  353. [`_next.dialogId`]: data.dialogId,
  354. [`_next.questions`]: data.nextQuestions,
  355. });
  356. this.triggerEvent("to");
  357. }
  358. } catch (error) {
  359. if (this.data._next.classify === "tongue") {
  360. this._updateMessage({
  361. id: `tongue-error-${Date.now()}`,
  362. type: "text",
  363. payload: { title: error.errMsg ?? `图像检测失败,请重新拍摄上传` },
  364. });
  365. this.triggerEvent("to");
  366. // setTimeout(() => this._start(), 20);
  367. } else {
  368. const date = Date.now();
  369. this._createMessage({
  370. id: `system-${date}`,
  371. type: "system",
  372. payload: { date, title: error.errMsg ?? `分析错误,请重试!` },
  373. });
  374. this._end();
  375. }
  376. } finally {
  377. // 恢复请求锁
  378. this.setData({ _requesting: false });
  379. }
  380. },
  381. _createMessage(body: Message, data?: Record<string, any>) {
  382. const messages = this.data.messages;
  383. const index = Object.keys(messages).length;
  384. this.setData({
  385. [`messages.${index}`]: body,
  386. ...data,
  387. });
  388. },
  389. _updateMessage(body: Message) {
  390. const messages = this.data.messages;
  391. const index = Object.keys(messages).length;
  392. this.setData({
  393. [`messages.${index - 1}`]: body,
  394. });
  395. },
  396. // 处理咨询事件
  397. handleConsultEvent(event: { detail: { type: string } }) {
  398. if (event.detail.type === "end") {
  399. // 结束咨询时,通知父组件显示guide菜单组件
  400. this.triggerEvent("next", { component: "guide", scroll: true });
  401. setTimeout(() => {
  402. this.triggerEvent("to");
  403. }, 100);
  404. }
  405. },
  406. },
  407. });