questionnaire.ts 14 KB

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