Questions.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <template>
  2. <view
  3. class="question-item"
  4. :class="{
  5. 'question-item-selected':
  6. question.checked && !props.showResult && styleCount !== 6,
  7. 'question-item-correct':
  8. question.checked &&
  9. props.showResult &&
  10. question.isRight &&
  11. styleCount !== 6,
  12. 'question-item-wrong':
  13. question.checked &&
  14. props.showResult &&
  15. !question.isRight &&
  16. styleCount !== 6,
  17. }"
  18. :style="{
  19. border: styleCount !== 6 ? '1px solid #ccc' : 0,
  20. padding: styleCount !== 6 ? '28rpx;' : 0,
  21. }"
  22. @tap="handleClick"
  23. >
  24. <view
  25. class="option-label"
  26. :class="{
  27. 'option-label-selected': question.checked && !props.showResult,
  28. 'option-label-correct':
  29. question.checked && props.showResult && question.isRight,
  30. 'option-label-wrong':
  31. question.checked && props.showResult && !question.isRight,
  32. }"
  33. >
  34. {{ question.value }}
  35. </view>
  36. <uvParse
  37. :content="replaceImageDimensions(question.label)"
  38. class="option-text"
  39. ></uvParse>
  40. <view
  41. class="option-icon"
  42. v-if="
  43. question.checked &&
  44. props.showResult &&
  45. !question.isRight &&
  46. styleCount !== 6
  47. "
  48. >
  49. <uni-icons type="closeempty" color="#f00"></uni-icons>
  50. </view>
  51. <view
  52. class="option-icon"
  53. v-if="
  54. question.checked &&
  55. props.showResult &&
  56. question.isRight &&
  57. styleCount !== 6
  58. "
  59. >
  60. <uni-icons type="checkmarkempty" color="#00be00"></uni-icons>
  61. </view>
  62. </view>
  63. </template>
  64. <script setup>
  65. import { computed } from "vue";
  66. import uvParse from "../../uni_modules/uv-parse/components/uv-parse/uv-parse.vue";
  67. // Props 定义
  68. const props = defineProps({
  69. answerList: {
  70. type: Array,
  71. default: () => [],
  72. },
  73. selectCount: {
  74. type: Array,
  75. default: () => [],
  76. },
  77. question: {
  78. type: Object,
  79. required: true,
  80. default: () => ({
  81. label: "",
  82. value: 0,
  83. checked: false,
  84. }),
  85. },
  86. parindex: {
  87. type: Number,
  88. required: true,
  89. },
  90. showResult: {
  91. type: Boolean,
  92. default: false,
  93. },
  94. styleCount: Number,
  95. });
  96. // Emits 定义
  97. const emit = defineEmits(["select"]);
  98. const replaceImageDimensions = (str) => {
  99. // 将图片宽度设为100%
  100. str = str
  101. .replace(
  102. /<img([^>]*?)src="([^"]*?)"([^>]*?)\/?>/g,
  103. '<img$1src="$2"$3 style="width:100%;height:100%;"/>'
  104. )
  105. .replace(
  106. /<img([^>]*?)style="([^"]*?)width:\s*[^;]+;\s*height:\s*[^;]+;([^"]*?)"([^>]*?)\/?>/g,
  107. '<img$1style="$2width:100%;height:100%;$3"$4/>'
  108. )
  109. .replace(
  110. /<img([^>]*?)width="[^"]*"([^>]*?)height="[^"]*"([^>]*?)\/?>/g,
  111. '<img$1width="100%"$2height="100%"$3/>'
  112. );
  113. console.log(str);
  114. return str;
  115. };
  116. // 方法
  117. const handleClick = () => {
  118. if (props.showResult) return;
  119. emit("select", {
  120. ...props.question,
  121. pid: props.parindex,
  122. style: props.styleCount,
  123. });
  124. };
  125. </script>
  126. <style lang="scss" scoped>
  127. @import "@/uni.scss";
  128. .question-item {
  129. display: flex;
  130. border-radius: 8px;
  131. background-color: #fafbfd;
  132. gap: 12px;
  133. align-items: center;
  134. color: #333;
  135. }
  136. .question-item-correct {
  137. border-color: $success;
  138. background-color: #e8f5e9;
  139. }
  140. .question-item-wrong {
  141. border-color: $error;
  142. background-color: #ffebee;
  143. }
  144. .question-item-selected {
  145. border-color: $uni-primary;
  146. background-color: #e3f2fd;
  147. }
  148. .option-label {
  149. border: 1px solid #ccc;
  150. border-radius: 50%;
  151. padding: 0 5px;
  152. }
  153. .option-label-correct {
  154. border-color: $success;
  155. background-color: $success;
  156. color: #fff;
  157. }
  158. .option-label-wrong {
  159. border-color: $error;
  160. background-color: $error;
  161. color: #fff;
  162. }
  163. .option-label-selected {
  164. border-color: $uni-primary;
  165. background-color: $uni-primary;
  166. color: #fff;
  167. }
  168. .option-text {
  169. flex: 1;
  170. white-space: normal;
  171. }
  172. </style>