questionnaire.ts 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. console.log("attached-isAnalysis", isAnalysis);
  31. if (isAnalysis === 3) {
  32. // 对话管家
  33. this._start();
  34. } else if (isAnalysis === 2) {
  35. // 随访提醒
  36. this.setData({
  37. [`_next.classify`]: "",
  38. [`_next.dialogId`]: "",
  39. [`_next.questions`]: [],
  40. _timestamp: Date.now(),
  41. });
  42. this._next();
  43. }
  44. },
  45. },
  46. properties: {
  47. messageType: { type: Number, value: 0 },
  48. workId: { type: Number, value: 0 },
  49. },
  50. /**
  51. * 组件的初始数据
  52. */
  53. data: {
  54. inputBoxBottom: 0,
  55. messages: {} as Record<number, Message>,
  56. lastId: "",
  57. _next: {
  58. classify: "",
  59. dialogId: "",
  60. questions: [],
  61. } as AnyObject,
  62. _timestamp: Date.now(),
  63. },
  64. observers: {
  65. "messages.**"(messages) {
  66. const message = Object.values(messages).pop() as Message;
  67. this.setData({ lastId: message?.id });
  68. },
  69. },
  70. methods: {
  71. nextType(event: MessageType) {
  72. this.setData({ messageType: event.detail.MessageType });
  73. this._next();
  74. },
  75. boxBottom(event: boxBottom) {
  76. console.log(event, "监听页面高度event");
  77. this.setData({ inputBoxBottom: event.detail.inputBoxBottom });
  78. this.triggerEvent("boxBottom", {
  79. inputBoxBottom: this.data.inputBoxBottom,
  80. });
  81. console.log(this.data.inputBoxBottom, "监听页面高度boxbottom");
  82. },
  83. handle(event: HandleEvent) {
  84. const isAnalysis = wx.getStorageSync("isAnalysis");
  85. if (isAnalysis === 3) {
  86. console.log(this.data._next, "handle", isAnalysis);
  87. const index = event.target?.id.split(".").pop() ?? 0;
  88. const questions = this.data._next.questions;
  89. Object.assign(questions[index], event.detail);
  90. this.setData({ "_next.questions": questions });
  91. this._next();
  92. } else {
  93. this._createMessage({
  94. id: 2,
  95. type: "follow",
  96. payload: { title: "" },
  97. });
  98. }
  99. },
  100. async _start() {
  101. try {
  102. const count = await Post(
  103. `/patientInfoManage/rechargeUseDetail`,
  104. {},
  105. {
  106. transform({ data }: any) {
  107. return data?.residuedCou;
  108. },
  109. }
  110. );
  111. this.triggerEvent("count", { count });
  112. console.log("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. console.log("isAnalysis",isAnalysis);
  158. if (isAnalysis === 3) {
  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. if (this.data.messageType === 1) {
  171. this._createMessage({
  172. id: 1,
  173. type: "again",
  174. payload: { title: "" },
  175. });
  176. } else {
  177. const { data } = await Post(
  178. `/dialogueManage/dialog`,
  179. this.data._next
  180. );
  181. data.nextQuestions?.forEach((question: any, index: number) => {
  182. if (isAnalysis === 2) {
  183. // 随访
  184. if (question.css === "tongue") {
  185. console.log(question, "tonguetongue");
  186. this._createMessage({
  187. id: `${question.classify}.${question.id}.${index}`,
  188. type: "analysis",
  189. payload: { title: question.title },
  190. });
  191. }
  192. } else {
  193. // 对话管家
  194. if (question.classify === "tongue_result") {
  195. this._updateMessage({
  196. id: `${question.classify}.${question.id}.${index}`,
  197. type: "text",
  198. payload: { title: question.content },
  199. });
  200. } else {
  201. if (question.css === "tongue") {
  202. console.log(question, "tonguetongue");
  203. this._createMessage({
  204. id: `${question.classify}.${question.id}.${index}`,
  205. type: "analysis",
  206. payload: { title: question.title },
  207. });
  208. } else if (question.css === "text") {
  209. this._createMessage({
  210. id: `${question.classify}.${question.id}.${index}`,
  211. type: "text",
  212. payload: { title: question.content },
  213. });
  214. } else if (["select", "checkbox"].includes(question.css)) {
  215. this._createMessage({
  216. id: `${question.classify}.${question.id}.${index}`,
  217. type: "select",
  218. payload: {
  219. title: question.title,
  220. options: question.options.map((item: AnyObject) => {
  221. if (Array.isArray(item.options)) {
  222. item.options = item.options.map((item) => {
  223. return { ...item, hide: item.css === "hide" };
  224. });
  225. }
  226. return { ...item, hide: item.css === "hide" };
  227. }),
  228. multiple: question.css === "checkbox",
  229. required: question.required,
  230. },
  231. });
  232. } else if (question.over) {
  233. return this._end();
  234. }
  235. }
  236. }
  237. });
  238. // 对话管家
  239. if (isAnalysis === 3) {
  240. if (data.classify === "report") {
  241. const diff = dayjs().diff(this.data._timestamp, "m");
  242. this._createMessage({
  243. id: "report",
  244. type: "report",
  245. payload: {
  246. title: `本次问答已结束,历时${
  247. diff || 1
  248. }分钟,非常感谢您的配合!请查看您本次的健康评估情况。`,
  249. url: `/module/health/pages/report/report?id=${data.healthAnalysisReportId}`,
  250. },
  251. });
  252. }
  253. if (data.over) return this._end();
  254. this.setData({
  255. [`_next.classify`]: data.classify,
  256. [`_next.dialogId`]: data.dialogId,
  257. [`_next.questions`]: data.nextQuestions,
  258. });
  259. this.triggerEvent("to");
  260. }
  261. }
  262. } catch (error) {
  263. if (this.data._next.classify === "tongue") {
  264. this._updateMessage({
  265. id: `tongue-error-${Date.now()}`,
  266. type: "text",
  267. payload: { title: error.errMsg ?? `图像检测失败,请重新拍摄上传` },
  268. });
  269. this.triggerEvent("to");
  270. setTimeout(() => this._start(), 20);
  271. } else {
  272. const date = Date.now();
  273. this._createMessage({
  274. id: `system-${date}`,
  275. type: "system",
  276. payload: { date, title: error.errMsg ?? `分析错误,请重试!` },
  277. });
  278. this._end();
  279. }
  280. }
  281. },
  282. _createMessage(body: Message, data?: Record<string, any>) {
  283. const messages = this.data.messages;
  284. const index = Object.keys(messages).length;
  285. this.setData({
  286. [`messages.${index}`]: body,
  287. ...data,
  288. });
  289. },
  290. _updateMessage(body: Message) {
  291. const messages = this.data.messages;
  292. const index = Object.keys(messages).length;
  293. this.setData({
  294. [`messages.${index - 1}`]: body,
  295. });
  296. },
  297. },
  298. });