replace-answer-question.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <template>
  2. <!-- 替换题 -->
  3. <view class="replace-answer-area">
  4. <view class="question_title">
  5. <text class="question_number">
  6. {{ questionNumberEndIsBracket(questionData.property.question_number) }}
  7. </text>
  8. <text class="question_stem" v-html="sanitizeHTML(questionData.stem)"
  9. :ref="'richText-1-1'+questionData.question_id"
  10. @longpress="previewByRichTextImg(-1,-1,questionData.question_id)"></text>
  11. </view>
  12. <view class="description"
  13. v-if="isEnable(questionData.property.is_enable_description)&&questionData.description.length > 0"
  14. v-html="sanitizeHTML(questionData.description)" :ref="'richText-2-2'+questionData.question_id"
  15. @longpress="previewByRichTextImg(-2,-2,questionData.question_id)">
  16. </view>
  17. <view class="replace-box">
  18. <picker-view :indicator-style="indicatorStyle" :value="indexArr" @change="bindChange" class="picker-view"
  19. v-if="isShow">
  20. <picker-view-column v-for="(item,index) in replaceList" :key="index" v-if="item.length>0">
  21. <view class="item" v-for="(option,indexO) in item" :key="indexO" v-if="option.content!=''">
  22. {{option.content}}
  23. </view>
  24. </picker-view-column>
  25. </picker-view>
  26. <view class="disable-view" v-if="answer_control[questionData.question_id].isReadOnly">
  27. </view>
  28. </view>
  29. <!-- 语音作答 -->
  30. <SoundRecord @setAudioId="setFileId" :disabled="answer_control[questionData.question_id].isReadOnly"
  31. :wav-blob.sync="questionData.user_answer[questionData.question_id].answer_list[0].audio_file_id"
  32. :viewPopup="false">
  33. </SoundRecord>
  34. <view class="reference" v-if="isViewReference&&answer_control[questionData.question_id].isViewRightAnswer">
  35. <text class="reference-title">参考答案</text>
  36. <text class="reference-answer" v-html="sanitizeHTML(questionData.reference_answer)"
  37. :ref="'richText-3-3'+questionData.question_id"
  38. @longpress="previewByRichTextImg(-3,-3,questionData.question_id)"></text>
  39. </view>
  40. <view class="reference" v-if="isViewAnalysis&&answer_control[questionData.question_id].isViewRightAnswer">
  41. <text class="reference-title">解析</text>
  42. <text class="reference-answer" v-html="sanitizeHTML(questionData.analysis)"
  43. :ref="'richText-4-4'+questionData.question_id"
  44. @longpress="previewByRichTextImg(-4,-4,questionData.question_id)">
  45. </text>
  46. </view>
  47. </view>
  48. </template>
  49. <script>
  50. import {
  51. questionData,
  52. sanitizeHTML,
  53. isEnable,
  54. answer_control,
  55. } from '@/pages/answer_question/common/data/common.js';
  56. import SoundRecord from '@/components/sound-record/sound-record.vue';
  57. import AnswerControlMixin from '@/pages/answer_question/common/data/AnswerControlMixin.js';
  58. export default {
  59. name: "replace-answer-question",
  60. mixins: [AnswerControlMixin],
  61. components: {
  62. SoundRecord
  63. },
  64. props: {
  65. questionData: questionData,
  66. },
  67. data() {
  68. return {
  69. sanitizeHTML,
  70. isEnable,
  71. answer_control,
  72. columnsNum: 0,
  73. replaceList: [],
  74. markList: [],
  75. isShow: false,
  76. indexArr: [],
  77. };
  78. },
  79. watch: {
  80. 'questionData.question_id': {
  81. handler(val) {
  82. this.commonComputedAnswerControl(val);
  83. this.isShow = false;
  84. this.replaceList = [];
  85. this.indexArr = [];
  86. this.columnsNum = this.questionData.property.column_count;
  87. var opl = this.questionData.option_list.slice();
  88. // 将数据转换为多个数组
  89. for (let i = 0; i < this.columnsNum; i++) {
  90. const array = opl.map(row => row[i]);
  91. this.replaceList.push(array);
  92. }
  93. this.setUserAnswer();
  94. },
  95. immediate: true,
  96. deep: true
  97. },
  98. markList: {
  99. handler(val) {
  100. for (let i = 0; i < this.columnsNum; i++) {
  101. if (val.length == i) {
  102. val.push('');
  103. } else {
  104. if (!val[i])
  105. val[i] = ''
  106. }
  107. }
  108. this.questionData.user_answer[this.questionData.question_id].answer_list[0].mark_list = val;
  109. this.saveUserAnswer();
  110. },
  111. immediate: true,
  112. deep: true
  113. }
  114. },
  115. computed: {
  116. isViewReference: function() {
  117. return isEnable(this.questionData.property.is_enable_reference_answer);
  118. },
  119. isViewAnalysis: function() {
  120. return isEnable(this.questionData.property.is_enable_analysis);
  121. },
  122. indicatorStyle: function() {
  123. var opl = this.questionData.option_list.slice();
  124. var eleWidth = 0;
  125. uni.createSelectorQuery().select('.replace-box').boundingClientRect(function(rect) {
  126. eleWidth = rect.width; // 获取元素的宽度
  127. }).exec();
  128. var singleWidth = eleWidth / this.columnsNum;
  129. var heis = [];
  130. // 将数据转换为多个数组
  131. for (let i = 0; i < this.columnsNum; i++) {
  132. opl.map(row => {
  133. heis.push(Math.round((row[i].content.length * 16) / singleWidth));
  134. });
  135. }
  136. var maxRow = Math.max(...heis);
  137. if (maxRow > 1) {
  138. const height = 30 + maxRow * 20;
  139. return `height: ${height}px;background-color: #777777;opacity: 0.1;`;
  140. }
  141. return `height: 34px;background-color: #777777;opacity: 0.1;`;
  142. }
  143. },
  144. methods: {
  145. bindChange: function(e) {
  146. if (this.answer_control[this.questionData.question_id].isReadOnly) return;
  147. const val = e.detail.value;
  148. // console.log('变化', val);
  149. this.markList = [];
  150. for (let i = 0; i < this.columnsNum; i++) {
  151. // this.markList[i] = this.replaceList[i][val[i]];
  152. if (this.replaceList[i][val[i]]) {
  153. this.markList[i] = this.replaceList[i][val[i]].mark;
  154. }
  155. }
  156. },
  157. //切换选项保存答案
  158. saveUserAnswer: function() {
  159. var questionId = this.questionData.question_id;
  160. this.questionData.user_answer[questionId].isEdit = true;
  161. var ansed = this.questionData.user_answer[questionId].answer_list;
  162. this.questionData.user_answer[questionId].is_fill_answer = ansed.length > 0;
  163. this.questionData.user_answer[questionId].content = JSON.stringify(ansed);
  164. this.questionData.user_answer[questionId].answer_list = ansed;
  165. },
  166. //回显
  167. setUserAnswer: function() {
  168. var that = this;
  169. var callback = function() {
  170. var userAnswer = [];
  171. var questionId = that.questionData.question_id;
  172. var _ua = that.questionData.user_answer[questionId];
  173. if (_ua && _ua.answer_list && _ua.answer_list.length > 0)
  174. userAnswer = _ua.answer_list;
  175. // console.log('2', userAnswer);
  176. if (userAnswer.length > 0 && userAnswer[0].mark_list.length > 0) {
  177. // console.log(that.replaceList);
  178. userAnswer[0].mark_list.forEach((p, i) => {
  179. if (p) {
  180. var idx = that.replaceList[i].findIndex(x => x.mark == p);
  181. that.indexArr.push(idx);
  182. } else {
  183. that.indexArr.push(0);
  184. }
  185. })
  186. }
  187. that.isShow = true;
  188. };
  189. this.$emit("getUserAnswer", this.questionData.question_id, callback);
  190. },
  191. //异步更新声音ID有问题,所以采用方法手动赋值
  192. setFileId(fileId) {
  193. this.questionData.user_answer[this.questionData.question_id].isEdit = true;
  194. this.$forceUpdate();
  195. }
  196. },
  197. }
  198. </script>
  199. <style lang="scss" scoped>
  200. .replace-answer-area {
  201. .replace-box {
  202. margin-top: 32rpx;
  203. position: relative;
  204. /deep/ .picker-view {
  205. min-width: 100%;
  206. height: 600rpx;
  207. margin-top: 20rpx;
  208. .uni-picker-view-mask {
  209. border-left: 1px solid #EFEFEF;
  210. border-right: none;
  211. }
  212. .uni-picker-view-mask:last-child {
  213. border-right: 1px solid #EFEFEF;
  214. }
  215. .uni-picker-view-content {
  216. left: 4px !important;
  217. .item {
  218. display: flex;
  219. align-items: center;
  220. justify-content: left;
  221. }
  222. }
  223. }
  224. .disable-view {
  225. position: absolute;
  226. height: 600rpx;
  227. min-width: 100%;
  228. top: 0;
  229. left: 0;
  230. }
  231. }
  232. .reference {
  233. margin: 32rpx 0;
  234. background-color: $uni-bg-color-grey;
  235. padding: 24rpx;
  236. font-size: 28rpx;
  237. .reference-title {
  238. display: block;
  239. line-height: 64rpx;
  240. color: #4E5969;
  241. }
  242. .reference-answer {
  243. color: #1D2129;
  244. line-height: 48rpx;
  245. }
  246. }
  247. }
  248. </style>