questionnaire.ts 11 KB

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