questionnaire.ts 10 KB

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