questionnaire.ts 9.4 KB

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