questionnaire.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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) {
  242. console.error("咨询开始失败", error);
  243. }
  244. }
  245. } else if (this.data.messageType === 2) {
  246. let data: any = {};
  247. const res = await Post(
  248. `/dialogueManage/dialogTreat`,
  249. this.data._next
  250. );
  251. data = res.data;
  252. data.nextQuestions?.forEach((question: any, index: number) => {
  253. // isAnalysis 2是随访 3健康管家 4健康评估 5在线咨询
  254. if (isAnalysis === 2) {
  255. // 随访
  256. if (question.css === "tongue") {
  257. this._createMessage({
  258. id: `${question.classify}.${question.id}.${index}`,
  259. type: "analysis",
  260. payload: { title: question.title },
  261. });
  262. }
  263. } else {
  264. // 对话管家
  265. if (question.classify === "tongue_result") {
  266. this._updateMessage({
  267. id: `${question.classify}.${question.id}.${index}`,
  268. type: "text",
  269. payload: { title: question.content },
  270. });
  271. } else {
  272. if (question.css === "tongue") {
  273. this._createMessage({
  274. id: `${question.classify}.${question.id}.${index}`,
  275. type: "analysis",
  276. payload: { title: question.title },
  277. });
  278. } else if (question.css === "text") {
  279. this._createMessage({
  280. id: `${question.classify}.${question.id}.${index}`,
  281. type: "text",
  282. payload: { title: question.content },
  283. });
  284. } else if (["select", "checkbox"].includes(question.css)) {
  285. if (question.options && question.options.length > 0) {
  286. // 检查所有 item.options 的长度是否都为 0
  287. const allOptionsEmpty = question.options.every(
  288. (item: any) => !item.options || item.options.length === 0
  289. );
  290. question.required = allOptionsEmpty;
  291. }
  292. this._createMessage({
  293. id: `${question.classify}.${question.id}.${index}`,
  294. type: "select",
  295. payload: {
  296. title: question.title,
  297. options: question.options.map((item: AnyObject) => {
  298. if (Array.isArray(item.options)) {
  299. item.options = item.options.map((item) => {
  300. return { ...item, hide: item.css === "hide" };
  301. });
  302. }
  303. return { ...item, hide: item.css === "hide" };
  304. }),
  305. multiple: question.css === "checkbox",
  306. required: question.required,
  307. belongNew: question.belongNew,
  308. },
  309. });
  310. } else if (question.over) {
  311. return this._end();
  312. }
  313. }
  314. }
  315. });
  316. // 对话管家
  317. if (
  318. (isAnalysis === 3 && data.classify === "report") ||
  319. (isAnalysis === 4 && data.classify === "report")
  320. ) {
  321. if (data.classify === "report") {
  322. const diff = dayjs().diff(this.data._timestamp, "m");
  323. this._createMessage({
  324. id: "report",
  325. type: "report",
  326. payload: {
  327. title: `本次问答已结束,历时${
  328. diff || 1
  329. }分钟,非常感谢您的配合!请查看您本次的健康评估情况。`,
  330. url: `/module/health/pages/report/report?id=${data.healthAnalysisReportId}`,
  331. },
  332. });
  333. // 立即触发一次滚动,确保在最后一个问题生成报告卡片后页面滚动到底部
  334. this.triggerEvent("to");
  335. }
  336. if (data.over) {
  337. // 延迟触发滚动,确保报告消息已经渲染完成
  338. setTimeout(() => {
  339. this.triggerEvent("to");
  340. }, 100);
  341. return this._end();
  342. }
  343. }
  344. this.setData({
  345. [`_next.classify`]: data.classify,
  346. [`_next.dialogId`]: data.dialogId,
  347. [`_next.questions`]: data.nextQuestions,
  348. });
  349. this.triggerEvent("to");
  350. }
  351. } catch (error) {
  352. if (this.data._next.classify === "tongue") {
  353. this._updateMessage({
  354. id: `tongue-error-${Date.now()}`,
  355. type: "text",
  356. payload: { title: error.errMsg ?? `图像检测失败,请重新拍摄上传` },
  357. });
  358. this.triggerEvent("to");
  359. // setTimeout(() => this._start(), 20);
  360. } else {
  361. const date = Date.now();
  362. this._createMessage({
  363. id: `system-${date}`,
  364. type: "system",
  365. payload: { date, title: error.errMsg ?? `分析错误,请重试!` },
  366. });
  367. this._end();
  368. }
  369. } finally {
  370. // 恢复请求锁
  371. this.setData({ _requesting: false });
  372. }
  373. },
  374. _createMessage(body: Message, data?: Record<string, any>) {
  375. const messages = this.data.messages;
  376. const index = Object.keys(messages).length;
  377. this.setData({
  378. [`messages.${index}`]: body,
  379. ...data,
  380. });
  381. },
  382. _updateMessage(body: Message) {
  383. const messages = this.data.messages;
  384. const index = Object.keys(messages).length;
  385. this.setData({
  386. [`messages.${index - 1}`]: body,
  387. });
  388. },
  389. // 处理咨询事件
  390. handleConsultEvent(event: { detail: { type: string } }) {
  391. if (event.detail.type === "end") {
  392. // 结束咨询时,通知父组件显示guide菜单组件
  393. this.triggerEvent("next", { component: "guide", scroll: true });
  394. setTimeout(() => {
  395. this.triggerEvent("to");
  396. }, 100);
  397. }
  398. },
  399. },
  400. });