questionnaire.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. let data: any = {};
  205. const res = await Post(
  206. `/dialogueManage/dialogTreat`,
  207. this.data._next
  208. );
  209. data = res.data;
  210. data.nextQuestions?.forEach((question: any, index: number) => {
  211. // isAnalysis 2是随访 3健康管家 4健康评估
  212. if (isAnalysis === 2) {
  213. // 随访
  214. if (question.css === "tongue") {
  215. this._createMessage({
  216. id: `${question.classify}.${question.id}.${index}`,
  217. type: "analysis",
  218. payload: { title: question.title },
  219. });
  220. }
  221. } else {
  222. // 对话管家
  223. if (question.classify === "tongue_result") {
  224. this._updateMessage({
  225. id: `${question.classify}.${question.id}.${index}`,
  226. type: "text",
  227. payload: { title: question.content },
  228. });
  229. } else {
  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. } else if (question.css === "text") {
  237. this._createMessage({
  238. id: `${question.classify}.${question.id}.${index}`,
  239. type: "text",
  240. payload: { title: question.content },
  241. });
  242. } else if (["select", "checkbox"].includes(question.css)) {
  243. if (question.options && question.options.length > 0) {
  244. // 检查所有 item.options 的长度是否都为 0
  245. const allOptionsEmpty = question.options.every(
  246. (item: any) => !item.options || item.options.length === 0
  247. );
  248. question.required = allOptionsEmpty;
  249. }
  250. this._createMessage({
  251. id: `${question.classify}.${question.id}.${index}`,
  252. type: "select",
  253. payload: {
  254. title: question.title,
  255. options: question.options.map((item: AnyObject) => {
  256. if (Array.isArray(item.options)) {
  257. item.options = item.options.map((item) => {
  258. return { ...item, hide: item.css === "hide" };
  259. });
  260. }
  261. return { ...item, hide: item.css === "hide" };
  262. }),
  263. multiple: question.css === "checkbox",
  264. required: question.required,
  265. belongNew: question.belongNew,
  266. },
  267. });
  268. } else if (question.over) {
  269. return this._end();
  270. }
  271. }
  272. }
  273. });
  274. // 对话管家
  275. if (
  276. (isAnalysis === 3 && data.classify === "report") ||
  277. (isAnalysis === 4 && data.classify === "report")
  278. ) {
  279. if (data.classify === "report") {
  280. const diff = dayjs().diff(this.data._timestamp, "m");
  281. this._createMessage({
  282. id: "report",
  283. type: "report",
  284. payload: {
  285. title: `本次问答已结束,历时${
  286. diff || 1
  287. }分钟,非常感谢您的配合!请查看您本次的健康评估情况。`,
  288. url: `/module/health/pages/report/report?id=${data.healthAnalysisReportId}`,
  289. },
  290. });
  291. // 立即触发一次滚动,确保在最后一个问题生成报告卡片后页面滚动到底部
  292. this.triggerEvent("to");
  293. }
  294. if (data.over) {
  295. // 延迟触发滚动,确保报告消息已经渲染完成
  296. setTimeout(() => {
  297. this.triggerEvent("to");
  298. }, 100);
  299. return this._end();
  300. }
  301. }
  302. this.setData({
  303. [`_next.classify`]: data.classify,
  304. [`_next.dialogId`]: data.dialogId,
  305. [`_next.questions`]: data.nextQuestions,
  306. });
  307. this.triggerEvent("to");
  308. }
  309. } catch (error) {
  310. if (this.data._next.classify === "tongue") {
  311. this._updateMessage({
  312. id: `tongue-error-${Date.now()}`,
  313. type: "text",
  314. payload: { title: error.errMsg ?? `图像检测失败,请重新拍摄上传` },
  315. });
  316. this.triggerEvent("to");
  317. // setTimeout(() => this._start(), 20);
  318. } else {
  319. const date = Date.now();
  320. this._createMessage({
  321. id: `system-${date}`,
  322. type: "system",
  323. payload: { date, title: error.errMsg ?? `分析错误,请重试!` },
  324. });
  325. this._end();
  326. }
  327. } finally {
  328. // 恢复请求锁
  329. this.setData({ _requesting: false });
  330. }
  331. },
  332. _createMessage(body: Message, data?: Record<string, any>) {
  333. const messages = this.data.messages;
  334. const index = Object.keys(messages).length;
  335. this.setData({
  336. [`messages.${index}`]: body,
  337. ...data,
  338. });
  339. },
  340. _updateMessage(body: Message) {
  341. const messages = this.data.messages;
  342. const index = Object.keys(messages).length;
  343. this.setData({
  344. [`messages.${index - 1}`]: body,
  345. });
  346. },
  347. },
  348. });