questionnaire.ts 12 KB

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