questionnaire.ts 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. payload: AnyObject;
  15. }
  16. interface HandleEvent {
  17. target: { id: string };
  18. detail: AnyObject;
  19. type: "next";
  20. }
  21. interface MessageType {
  22. messageType: number;
  23. }
  24. Component({
  25. lifetimes: {
  26. attached: function () {
  27. let isAnalysis: number;
  28. isAnalysis = wx.getStorageSync("isAnalysis");
  29. console.log("attached-isAnalysis", isAnalysis);
  30. if (isAnalysis === 3) {
  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. boxBottom(event: boxBottom) {
  75. console.log(event, "监听页面高度event");
  76. this.setData({ inputBoxBottom: event.detail.inputBoxBottom });
  77. this.triggerEvent("boxBottom", {
  78. inputBoxBottom: this.data.inputBoxBottom,
  79. });
  80. console.log(this.data.inputBoxBottom, "监听页面高度boxbottom");
  81. },
  82. handle(event: HandleEvent) {
  83. const isAnalysis = wx.getStorageSync("isAnalysis");
  84. if (isAnalysis === 3) {
  85. console.log(this.data._next, "handle", isAnalysis);
  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. this._createMessage({
  93. id: 2,
  94. type: "follow",
  95. payload: { title: "" },
  96. });
  97. }
  98. },
  99. async _start() {
  100. try {
  101. const count = await Post(
  102. `/patientInfoManage/rechargeUseDetail`,
  103. {},
  104. {
  105. transform({ data }: any) {
  106. return data?.residuedCou;
  107. },
  108. }
  109. );
  110. // console.log("count",count);
  111. if (count > 0) {
  112. // if (count < 0) {
  113. this.setData({
  114. [`_next.classify`]: "",
  115. [`_next.dialogId`]: "",
  116. [`_next.questions`]: [],
  117. _timestamp: Date.now(),
  118. });
  119. this._next();
  120. } else {
  121. throw { errMsg: `您的健康分析次数已用完,请联系工作人员。` };
  122. }
  123. } catch (error) {
  124. this._createMessage({
  125. id: `system-start`,
  126. type: "system",
  127. payload: {
  128. date: Date.now(),
  129. title: error.errMsg ?? `分析错误,请重试!`,
  130. },
  131. });
  132. this._end();
  133. }
  134. },
  135. _end() {
  136. this.setData({
  137. [`_next.classify`]: "",
  138. [`_next.dialogId`]: "",
  139. [`_next.questions`]: [],
  140. });
  141. this.triggerEvent("next", { component: "guide", scroll: true });
  142. },
  143. async _next() {
  144. let isAnalysis: number;
  145. isAnalysis = wx.getStorageSync("isAnalysis");
  146. if (isAnalysis === 3) {
  147. // 对话管家
  148. if (this.data._next.classify === "tongue") {
  149. this._createMessage({
  150. id: `tongue-loading.${Date.now()}`,
  151. type: "text",
  152. payload: { title: "分析中", loading: true },
  153. });
  154. this.triggerEvent("to");
  155. }
  156. }
  157. try {
  158. if (this.data.messageType === 1) {
  159. this._createMessage({
  160. id: 1,
  161. type: "again",
  162. payload: { title: "" },
  163. });
  164. } else {
  165. const { data } = await Post(
  166. `/dialogueManage/dialog`,
  167. this.data._next
  168. );
  169. data.nextQuestions?.forEach((question: any, index: number) => {
  170. if (isAnalysis === 2) {
  171. // 随访
  172. if (question.css === "tongue") {
  173. console.log(question, "tonguetongue");
  174. this._createMessage({
  175. id: `${question.classify}.${question.id}.${index}`,
  176. type: "analysis",
  177. payload: { title: question.title },
  178. });
  179. }
  180. } else {
  181. // 对话管家
  182. if (question.classify === "tongue_result") {
  183. this._updateMessage({
  184. id: `${question.classify}.${question.id}.${index}`,
  185. type: "text",
  186. payload: { title: question.content },
  187. });
  188. } else {
  189. if (question.css === "tongue") {
  190. console.log(question, "tonguetongue");
  191. this._createMessage({
  192. id: `${question.classify}.${question.id}.${index}`,
  193. type: "analysis",
  194. payload: { title: question.title },
  195. });
  196. } else if (question.css === "text") {
  197. this._createMessage({
  198. id: `${question.classify}.${question.id}.${index}`,
  199. type: "text",
  200. payload: { title: question.content },
  201. });
  202. } else if (["select", "checkbox"].includes(question.css)) {
  203. this._createMessage({
  204. id: `${question.classify}.${question.id}.${index}`,
  205. type: "select",
  206. payload: {
  207. title: question.title,
  208. options: question.options.map((item: AnyObject) => {
  209. if (Array.isArray(item.options)) {
  210. item.options = item.options.map((item) => {
  211. return { ...item, hide: item.css === "hide" };
  212. });
  213. }
  214. return { ...item, hide: item.css === "hide" };
  215. }),
  216. multiple: question.css === "checkbox",
  217. required: question.required,
  218. },
  219. });
  220. } else if (question.over) {
  221. return this._end();
  222. }
  223. }
  224. }
  225. });
  226. // 对话管家
  227. if (isAnalysis === 3) {
  228. if (data.classify === "report") {
  229. const diff = dayjs().diff(this.data._timestamp, "m");
  230. this._createMessage({
  231. id: "report",
  232. type: "report",
  233. payload: {
  234. title: `本次问答已结束,历时${
  235. diff || 1
  236. }分钟,非常感谢您的配合!请查看您本次的健康评估情况。`,
  237. url: `/module/health/pages/report/report?id=${data.healthAnalysisReportId}`,
  238. },
  239. });
  240. }
  241. if (data.over) return this._end();
  242. this.setData({
  243. [`_next.classify`]: data.classify,
  244. [`_next.dialogId`]: data.dialogId,
  245. [`_next.questions`]: data.nextQuestions,
  246. });
  247. this.triggerEvent("to");
  248. }
  249. }
  250. } catch (error) {
  251. if (this.data._next.classify === "tongue") {
  252. this._updateMessage({
  253. id: `tongue-error-${Date.now()}`,
  254. type: "text",
  255. payload: { title: error.errMsg ?? `图像检测失败,请重新拍摄上传` },
  256. });
  257. this.triggerEvent("to");
  258. setTimeout(() => this._start(), 20);
  259. } else {
  260. const date = Date.now();
  261. this._createMessage({
  262. id: `system-${date}`,
  263. type: "system",
  264. payload: { date, title: error.errMsg ?? `分析错误,请重试!` },
  265. });
  266. this._end();
  267. }
  268. }
  269. },
  270. _createMessage(body: Message, data?: Record<string, any>) {
  271. const messages = this.data.messages;
  272. const index = Object.keys(messages).length;
  273. this.setData({
  274. [`messages.${index}`]: body,
  275. ...data,
  276. });
  277. },
  278. _updateMessage(body: Message) {
  279. const messages = this.data.messages;
  280. const index = Object.keys(messages).length;
  281. this.setData({
  282. [`messages.${index - 1}`]: body,
  283. });
  284. },
  285. },
  286. });