questionnaire.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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: 'system' | 'analysis' | 'select' | 'text' | 'report';
  7. payload: AnyObject;
  8. }
  9. interface HandleEvent {
  10. target: { id: string; };
  11. detail: AnyObject;
  12. type: 'next';
  13. }
  14. Component({
  15. lifetimes: {
  16. attached() { this._start(); }
  17. },
  18. properties: {
  19. },
  20. /**
  21. * 组件的初始数据
  22. */
  23. data: {
  24. messages: {} as Record<number, Message>,
  25. lastId: '',
  26. _next: {
  27. dialogId: "",
  28. questions: []
  29. } as AnyObject,
  30. _timestamp: Date.now(),
  31. },
  32. observers: {
  33. 'messages.**'(messages) {
  34. const message = Object.values(messages).pop() as Message;
  35. this.setData({ lastId: message?.id });
  36. }
  37. },
  38. methods: {
  39. handle(event: HandleEvent) {
  40. const index = event.target.id.split('.').pop() ?? 0;
  41. console.log(index, event.target.id);
  42. const questions = this.data._next.questions;
  43. Object.assign(questions[index], event.detail);
  44. this.setData({ '_next.questions': questions })
  45. console.log(this.data._next);
  46. this._next()
  47. },
  48. async _start() {
  49. try {
  50. const count = await Post(`/patientInfoManage/rechargeUseDetail`, {}, {
  51. transform({ data }: any) {
  52. return data?.residuedCou
  53. }
  54. });
  55. if (count > 0) {
  56. this.setData({
  57. [`_next.dialogId`]: '',
  58. [`_next.questions`]: [],
  59. _timestamp: Date.now(),
  60. })
  61. this._next();
  62. } else {
  63. throw {errMsg: `您的健康分析次数已用完,请联系工作人员。`}
  64. }
  65. } catch (error) {
  66. this._createMessage({
  67. id: `system-start`, type: 'system',
  68. payload: { date: Date.now(), title: error.errMsg ?? `分析错误,请重试!`, }
  69. });
  70. this._end();
  71. }
  72. },
  73. _end() {
  74. this.setData({
  75. [`_next.dialogId`]: '',
  76. [`_next.questions`]: [],
  77. })
  78. this.triggerEvent('next', { component: 'guide', scroll: true });
  79. },
  80. _pre() { },
  81. async _next() {
  82. try {
  83. this._pre();
  84. const { data } = await Post(`/dialogueManage/dialog`, this.data._next);
  85. data.nextQuestions?.forEach((question: any, index: number) => {
  86. if (question.classify === 'tongue_result') {
  87. this._updateMessage({
  88. id: `${question.classify}.${question.id}.${index}`, type: 'text',
  89. payload: { title: question.content }
  90. })
  91. } else {
  92. if (question.classify === 'tongue') {
  93. this._pre = () => {
  94. this._createMessage({
  95. id: `${question.classify}.${question.id}.loading`, type: 'text',
  96. payload: { title: '分析中', loading: true }
  97. });
  98. this.triggerEvent('to');
  99. }
  100. } else {
  101. this._pre = () => { }
  102. }
  103. if (question.css === 'tongue') {
  104. this._createMessage({
  105. id: `${question.classify}.${question.id}.${index}`, type: 'analysis',
  106. payload: { title: question.title }
  107. });
  108. } else if (question.css === 'text') {
  109. this._createMessage({
  110. id: `${question.classify}.${question.id}.${index}`, type: 'text',
  111. payload: { title: question.content }
  112. });
  113. } else if (['select', 'checkbox'].includes(question.css)) {
  114. this._createMessage({
  115. id: `${question.classify}.${question.id}.${index}`, type: 'select',
  116. payload: {
  117. title: question.title,
  118. options: question.options.map((item: AnyObject) => {
  119. if (Array.isArray(item.options)) {
  120. item.options = item.options.map(item => {
  121. return { ...item, hide: item.css === 'hide' }
  122. })
  123. }
  124. return { ...item, hide: item.css === 'hide' }
  125. }),
  126. multiple: question.css === 'checkbox',
  127. required: question.required
  128. }
  129. })
  130. } else if (question.over) {
  131. return this._end();
  132. }
  133. }
  134. });
  135. if (data.classify === 'report') {
  136. const diff = dayjs().diff(this.data._timestamp, 'm');
  137. this._createMessage({
  138. id: 'report', type: 'report',
  139. payload: {
  140. title: `本次问答已结束,历史${diff || 1}分钟,非常感谢您的配合!请查看您本次的健康评估情况。`,
  141. url: `/module/health/pages/report/report?id=${data.healthAnalysisReportId}`
  142. }
  143. })
  144. }
  145. if (data.over) return this._end();
  146. this.setData({
  147. [`_next.dialogId`]: data.dialogId,
  148. [`_next.questions`]: data.nextQuestions,
  149. })
  150. this.triggerEvent('to');
  151. } catch (error) {
  152. console.log(error);
  153. const date = Date.now();
  154. this._createMessage({
  155. id: `system-${date}`, type: 'system',
  156. payload: { date, title: error.errMsg ?? `分析错误,请重试!`, }
  157. });
  158. this._end();
  159. }
  160. },
  161. _createMessage(body: Message, data?: Record<string, any>) {
  162. const messages = this.data.messages;
  163. const index = Object.keys(messages).length;
  164. this.setData({
  165. [`messages.${index}`]: body,
  166. ...data,
  167. })
  168. console.log(this.data.messages);
  169. },
  170. _updateMessage(body: Message) {
  171. const messages = this.data.messages;
  172. const index = Object.keys(messages).length;
  173. this.setData({
  174. [`messages.${index - 1}`]: body,
  175. })
  176. },
  177. }
  178. })