questionnaire.ts 12 KB

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