questionnaire.ts 13 KB

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