questionnaire.ts 9.3 KB

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