tiankong.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <template>
  2. <view class="ezy-tiankong-box">
  3. <!-- 标题区域 -->
  4. <view class="tiankongn-title"></view>
  5. <!-- 显示填空后的文本 -->
  6. <view class="ezy-shiti-question" v-html="formattedText" :question="question" :myflag="myflag"
  7. :change:myflag="TK.updateFlag" :change:question="TK.watchQuestionChange"></view>
  8. <!-- 提供与占位符数量相匹配的输入框 -->
  9. <!-- <template v-for="(placeholder, index) in placeholders" :key="index">
  10. <FillItem :value="question.reply[index]" :ref="`popup${index}Ref`" :index="index" @blur="onBlur"></FillItem>
  11. </template> -->
  12. </view>
  13. </template>
  14. <script>
  15. // import FillItem from "./FillItem.vue";
  16. export default {
  17. // components: {
  18. // FillItem
  19. // },
  20. props: {
  21. placeholders: { // 占位符
  22. type: Array,
  23. required: true
  24. },
  25. question: {
  26. type: Object,
  27. },
  28. showError: {
  29. type: Boolean,
  30. default: false
  31. }
  32. },
  33. data() {
  34. return {
  35. myflag: 0
  36. }
  37. },
  38. computed: {
  39. // 计算属性,用于生成填空后的文本
  40. formattedText() {
  41. let result = this.question.name;
  42. this.placeholders.forEach((placeholder, index) => {
  43. const regex2 = new RegExp(`\\<img `, 'g');
  44. result = result.replace(regex2, '<image ');
  45. // 使用正则表达式全局替换占位符
  46. const regex = new RegExp(`\\${placeholder}`, 'g');
  47. if (this.showError) {
  48. // 答题状态默认错误
  49. let answer_status = 'showError';
  50. // 更新答题状态正确
  51. this.question.reply[index] && this.question.result[index].some(citem => citem === this
  52. .question.reply[index].trim()) && (answer_status = 'active_right');
  53. result = result.replace(regex,
  54. `<view class="tk-input-box ${answer_status}"><input class="tiankong-input ${answer_status}" style="width: 50px;margin: 0 3px" id="t_${this.question.stId}_${index}" readonly data-index="${index}" value="${this.question.reply[index]}" /></view>`
  55. );
  56. } else {
  57. result = result.replace(regex,
  58. `<view class="tk-input-box"><input class="tiankong-input" style="width: 50px;margin: 0 3px" id="t_${this.question.stId}_${index}" readonly data-index="${index}" value="${this.question.reply[index]}" /></view>`
  59. );
  60. }
  61. });
  62. this.myflag++;
  63. return result;
  64. },
  65. },
  66. methods: {
  67. setResult(data) {
  68. const {
  69. index,
  70. result
  71. } = data;
  72. this.question.reply[index] = result;
  73. },
  74. onBlur(data) {
  75. this.setResult(data);
  76. },
  77. showPopup({index}) {
  78. // this.$refs[str][0].showPopup();
  79. uni.$emit('tiankong-fillItem',{ index,question:this.question})
  80. }
  81. },
  82. created() {
  83. uni.$on('tiankong-setResult',({index,stId,result}) => {
  84. if (this.question.stId === stId) {
  85. this.setResult({index, result})
  86. }
  87. })
  88. }
  89. }
  90. </script>
  91. <script module="TK" lang="renderjs">
  92. export default {
  93. props: {
  94. showError: {
  95. type: Boolean,
  96. default: false
  97. }
  98. },
  99. data() {
  100. return {
  101. myQ: null
  102. }
  103. },
  104. methods: {
  105. updateFlag() {
  106. this.initListener(this.myQ)
  107. },
  108. updateFn(e) {
  109. // const str = `popup${e.target.dataset.index}Ref`;
  110. this.$ownerInstance.callMethod('showPopup', {index: e.target.dataset.index});
  111. },
  112. watchQuestionChange(newValue, oldValue, ownerInstance, instance) {
  113. if (newValue) {
  114. this.myQ = newValue;
  115. this.initListener(newValue)
  116. }
  117. },
  118. initListener(question) {
  119. if (!question) {
  120. return;
  121. }
  122. if (this.showError) {
  123. return;
  124. }
  125. question.result.forEach((item, index) => {
  126. const dom = document.getElementById(`t_${question.stId}_${index}`)
  127. dom && dom.addEventListener('focus', (e) => this.updateFn(e))
  128. })
  129. },
  130. },
  131. }
  132. </script>