questionnaire.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. this.triggerEvent("next", { component: "guide", scroll: true });
  152. },
  153. async _next() {
  154. let isAnalysis: number;
  155. isAnalysis = wx.getStorageSync("isAnalysis");
  156. if (isAnalysis === 3 || isAnalysis === 4) {
  157. // 对话管家
  158. if (this.data._next.classify === "tongue") {
  159. this._createMessage({
  160. id: `tongue-loading.${Date.now()}`,
  161. type: "text",
  162. payload: { title: "分析中...", loading: true },
  163. });
  164. this.triggerEvent("to");
  165. }
  166. }
  167. try {
  168. // messageType 1 是随访。messageType 2 是健康评估和对话管家
  169. if (this.data.messageType === 1) {
  170. this._createMessage({
  171. id: `again.${Date.now()}`,
  172. type: "again",
  173. payload: { title: "" },
  174. });
  175. } else if (this.data.messageType === 2) {
  176. // todo 如果是对话管家 调用dialogueManage/dialog 这个接口
  177. // 如果是健康评估 调用其他接口 但后端返回的数据是一样的
  178. let data: any = {};
  179. const res = await Post(
  180. `/dialogueManage/dialogTreat`,
  181. this.data._next
  182. );
  183. data = res.data;
  184. // if (isAnalysis === 3) {
  185. // const res = await Post(`/dialogueManage/dialog`, this.data._next);
  186. // data = res.data;
  187. // } else {
  188. // const res = await Post(
  189. // `/dialogueManage/dialogTreat`,
  190. // this.data._next
  191. // );
  192. // data = res.data;
  193. // }
  194. // const { data } = await Post(
  195. // `/dialogueManage/dialog`,
  196. // this.data._next
  197. // );
  198. data.nextQuestions?.forEach((question: any, index: number) => {
  199. // isAnalysis 2是随访 3健康管家 4健康评估
  200. if (isAnalysis === 2) {
  201. // 随访
  202. if (question.css === "tongue") {
  203. this._createMessage({
  204. id: `${question.classify}.${question.id}.${index}`,
  205. type: "analysis",
  206. payload: { title: question.title },
  207. });
  208. }
  209. } else {
  210. // 对话管家
  211. if (question.classify === "tongue_result") {
  212. this._updateMessage({
  213. id: `${question.classify}.${question.id}.${index}`,
  214. type: "text",
  215. payload: { title: question.content },
  216. });
  217. } else {
  218. if (question.css === "tongue") {
  219. this._createMessage({
  220. id: `${question.classify}.${question.id}.${index}`,
  221. type: "analysis",
  222. payload: { title: question.title },
  223. });
  224. } else if (question.css === "text") {
  225. this._createMessage({
  226. id: `${question.classify}.${question.id}.${index}`,
  227. type: "text",
  228. payload: { title: question.content },
  229. });
  230. } else if (["select", "checkbox"].includes(question.css)) {
  231. if (question.options && question.options.length > 0) {
  232. // 检查所有 item.options 的长度是否都为 0
  233. const allOptionsEmpty = question.options.every(
  234. (item: any) => !item.options || item.options.length === 0
  235. );
  236. question.required = allOptionsEmpty;
  237. }
  238. this._createMessage({
  239. id: `${question.classify}.${question.id}.${index}`,
  240. type: "select",
  241. payload: {
  242. title: question.title,
  243. options: question.options.map((item: AnyObject) => {
  244. if (Array.isArray(item.options)) {
  245. item.options = item.options.map((item) => {
  246. return { ...item, hide: item.css === "hide" };
  247. });
  248. }
  249. return { ...item, hide: item.css === "hide" };
  250. }),
  251. multiple: question.css === "checkbox",
  252. required: question.required,
  253. belongNew: question.belongNew,
  254. },
  255. });
  256. } else if (question.over) {
  257. return this._end();
  258. }
  259. }
  260. }
  261. });
  262. // 对话管家
  263. if (
  264. (isAnalysis === 3 && data.classify === "report") ||
  265. (isAnalysis === 4 && data.classify === "report")
  266. ) {
  267. if (data.classify === "report") {
  268. const diff = dayjs().diff(this.data._timestamp, "m");
  269. this._createMessage({
  270. id: "report",
  271. type: "report",
  272. payload: {
  273. title: `本次问答已结束,历时${
  274. diff || 1
  275. }分钟,非常感谢您的配合!请查看您本次的健康评估情况。`,
  276. url: `/module/health/pages/report/report?id=${data.healthAnalysisReportId}`,
  277. },
  278. });
  279. }
  280. if (data.over) return this._end();
  281. }
  282. this.setData({
  283. [`_next.classify`]: data.classify,
  284. [`_next.dialogId`]: data.dialogId,
  285. [`_next.questions`]: data.nextQuestions,
  286. });
  287. this.triggerEvent("to");
  288. }
  289. } catch (error) {
  290. if (this.data._next.classify === "tongue") {
  291. this._updateMessage({
  292. id: `tongue-error-${Date.now()}`,
  293. type: "text",
  294. payload: { title: error.errMsg ?? `图像检测失败,请重新拍摄上传` },
  295. });
  296. this.triggerEvent("to");
  297. setTimeout(() => this._start(), 20);
  298. } else {
  299. const date = Date.now();
  300. this._createMessage({
  301. id: `system-${date}`,
  302. type: "system",
  303. payload: { date, title: error.errMsg ?? `分析错误,请重试!` },
  304. });
  305. this._end();
  306. }
  307. }
  308. },
  309. _createMessage(body: Message, data?: Record<string, any>) {
  310. const messages = this.data.messages;
  311. const index = Object.keys(messages).length;
  312. this.setData({
  313. [`messages.${index}`]: body,
  314. ...data,
  315. });
  316. },
  317. _updateMessage(body: Message) {
  318. const messages = this.data.messages;
  319. const index = Object.keys(messages).length;
  320. this.setData({
  321. [`messages.${index - 1}`]: body,
  322. });
  323. },
  324. },
  325. });