useQuestionTranslate.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import {array} from "@/uni_modules/uv-ui-tools/libs/function/test";
  2. export function useQuestionTranslate(question) {
  3. // 将旧的题属性转换为新题属性,以方便
  4. // 使用mx-question-content 和mx-question-parse
  5. // 自动获得MathJax支持
  6. const makeOptions = () => {
  7. const result = []
  8. if (array(question.options)) result.push(...question.options)
  9. const opts = Object.keys(question)
  10. .filter(k => k.startsWith('option') && k != 'options' && question[k])
  11. .sort()
  12. .map(k => question[k])
  13. result.push(...opts)
  14. return result
  15. }
  16. const fixTypeId = (q) => {
  17. if (q.typeId) return
  18. if (['单选题', '选择题', '判断题'].includes(q.type)) return q.typeId = 1
  19. if (['多选题'].includes(q.type)) return q.typeId = 3
  20. }
  21. const formatted = {
  22. ...question,
  23. questionId: question.id || question.questionId,
  24. type: question.qtpye || question.type,
  25. options: makeOptions(),
  26. answers: [question.answer1, question.answer2],
  27. knowledge: question.knownledgeName || question.knowledges || question.knowledge,
  28. _raw: question
  29. }
  30. fixTypeId(formatted)
  31. return formatted
  32. }