questionnaire.ts 14 KB

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