index.tsx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import { Tag, Image, Button } from 'vant'
  2. import { computed, defineComponent, nextTick, onMounted, PropType, reactive } from 'vue'
  3. import { AnswerType, labelOptions, QuestionType } from '../../unit'
  4. import Draggable from 'vuedraggable'
  5. import styles from './index.module.less'
  6. import Sortable from 'sortablejs'
  7. import deepClone from '@/helpers/deep-clone'
  8. import AnserTitle from '../anser-title'
  9. import AnswerAnalysis from '../answer-analysis'
  10. // 单选和多选题
  11. export default defineComponent({
  12. name: 'choice-question',
  13. props: {
  14. value: {
  15. type: Array,
  16. default: () => []
  17. },
  18. index: {
  19. // 题目是第几道
  20. type: Number,
  21. default: 1
  22. },
  23. data: {
  24. type: Object,
  25. default: () => ({})
  26. },
  27. /* 只读 */
  28. readOnly: {
  29. type: Boolean,
  30. default: false
  31. },
  32. showAnalysis: {
  33. // 是否显示解析
  34. type: Boolean,
  35. default: false
  36. },
  37. analysis: {
  38. type: Object,
  39. default: () => ({
  40. message: '',
  41. topic: false, // 是否显示结果
  42. userResult: true // 用户答题对错
  43. })
  44. }
  45. },
  46. emits: ['update:value'],
  47. setup(props, { emit }) {
  48. const state = reactive({
  49. domId: 'draggableContainer' + +new Date(),
  50. drag: false,
  51. sortable: null as any,
  52. list: [] as any,
  53. options: [] as any
  54. })
  55. onMounted(() => {
  56. nextTick(() => {
  57. const el = document.getElementById(state.domId)
  58. state.sortable = Sortable.create(el, {
  59. disabled: props.readOnly,
  60. animation: 150,
  61. sort: true,
  62. fallbackTolerance: 3,
  63. onUpdate: (evt: any) => {
  64. const updatePosition = (list: any) =>
  65. list.splice(evt.newIndex, 0, list.splice(evt.oldIndex, 1)[0])
  66. if (state.list && state.list.length > 0) {
  67. updatePosition(state.list)
  68. } else {
  69. state.list = [...state.options]
  70. updatePosition(state.list)
  71. }
  72. onSelect()
  73. }
  74. })
  75. })
  76. })
  77. // 返回选中的结果
  78. const onSelect = () => {
  79. const options = state.options || []
  80. const list = state.list || []
  81. const result: any = []
  82. list.forEach((item: any, index: number) => {
  83. const rightOption = options[index]
  84. result.push({
  85. answerId: item.index,
  86. answer: item.leftValue,
  87. extra: rightOption.rightValue
  88. })
  89. })
  90. emit('update:value', result)
  91. }
  92. const initOptions = () => {
  93. const answers = props.data.answers || []
  94. const userAnswer = props.data.userAnswer || [] // 用户填写的答案
  95. if (userAnswer.length > 0) {
  96. userAnswer.forEach((answer: any) => {
  97. const rightOption = answers.find(
  98. (child: any) => answer.answerId === child.examinationQuestionAnswerId
  99. )
  100. const rightValue = answers.find((child: any) => answer.extra === child.questionExtra)
  101. const tmp = {
  102. index: answer.answerId, // 左边的值
  103. leftValue: answer.answer, // 左边的值
  104. rightValue: answer.extra, // 右边的值
  105. leftType: rightOption.questionAnswerTypeCode || 'TXT', // 左边类型
  106. rightType: rightOption.questionExtraTypeCode || 'TXT', // 右边类型
  107. rightIndex: rightValue ? rightValue.examinationQuestionAnswerId : ''
  108. }
  109. state.options.push(tmp)
  110. })
  111. } else {
  112. const resultValue: any = []
  113. answers.forEach((answer: any) => {
  114. const tmp = {
  115. index: answer.examinationQuestionAnswerId, // 左边的值
  116. leftValue: answer.questionAnswer, // 左边的值
  117. rightValue: answer.questionExtra, // 右边的值
  118. leftType: answer.questionAnswerTypeCode || 'TXT', // 左边类型
  119. rightType: answer.questionExtraTypeCode || 'TXT' // 右边类型
  120. }
  121. resultValue.push({
  122. answerId: answer.examinationQuestionAnswerId,
  123. answer: answer.questionAnswer,
  124. extra: answer.questionExtra
  125. })
  126. state.options.push(tmp)
  127. })
  128. // 进页面就默认初始化答案
  129. // emit('update:value', resultValue)
  130. }
  131. }
  132. onMounted(() => {
  133. initOptions()
  134. })
  135. return () => (
  136. <>
  137. <div class={styles.unitSubject}>
  138. {/* 标题 */}
  139. <AnserTitle
  140. index={props.index}
  141. name={props.data.name}
  142. score={props.data.totalScore}
  143. answerType={QuestionType.SORT}
  144. extra={{
  145. questionDetail: props.data.questionDetail,
  146. mediaUrls: props.data.mediaUrls
  147. }}
  148. />
  149. <div class={[styles.unitAnswers, 'van-hairline--top']}>
  150. <div class={styles.sortReset}>
  151. <span class={styles.tips}>请长按拖拽答案进行排序</span>
  152. <Button
  153. type="primary"
  154. round
  155. disabled={props.readOnly}
  156. onClick={() => {
  157. const ids: any = []
  158. const answers = props.data.answers || []
  159. answers.forEach((item: any) => {
  160. ids.push(item.examinationQuestionAnswerId)
  161. })
  162. state.sortable.sort(
  163. ids.sort((a: any, b: any) => a - b),
  164. true
  165. )
  166. onSelect()
  167. }}
  168. >
  169. 重置
  170. </Button>
  171. </div>
  172. <div id={state.domId}>
  173. {state.options.map((item: any) => (
  174. <>
  175. {item.leftType === AnswerType.TXT && (
  176. <Tag class={[styles.items]} data-id={item.index}>
  177. {item.leftValue}
  178. </Tag>
  179. )}
  180. {item.leftType === AnswerType.IMAGE && (
  181. <Image
  182. src={item.leftValue}
  183. data-id={item.index}
  184. class={styles.imgs}
  185. fit="cover"
  186. />
  187. )}
  188. </>
  189. ))}
  190. </div>
  191. </div>
  192. </div>
  193. {props.showAnalysis && (
  194. <div class={styles.unitSubject}>
  195. <AnswerAnalysis
  196. answerAnalysis={props.analysis.message}
  197. topic={props.analysis.topic}
  198. userResult={props.analysis.userResult}
  199. />
  200. </div>
  201. )}
  202. </>
  203. )
  204. }
  205. })