猫妈狗爸伴宠师小程序前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

186 lines
3.9 KiB

  1. <template>
  2. <view class="mt32 question__view" :class="[props.mode]">
  3. <view class="size-28 mb20 question">
  4. {{ props.data.title }}
  5. <!-- {{ `${props.index + 1}${props.data.title}` }} -->
  6. </view>
  7. <template v-if="props.mode === 'edit'">
  8. <template v-if="props.data.options?.length">
  9. <view class="size-28 option"
  10. v-for="(option, oIdx) in props.data.options"
  11. :key="`${props.index}-option-${oIdx}`"
  12. :class="[value === option.id ? 'is-selected' : '']"
  13. @click="onChange(option.id)"
  14. >
  15. {{ option.title }}
  16. </view>
  17. </template>
  18. <template v-else>
  19. <view class="textarea">
  20. <textarea
  21. v-model="value"
  22. placeholder="请输入您的答案,不得低于700个字"
  23. :rows="10"
  24. @blur="onChange($event.detail.value)"
  25. ></textarea>
  26. </view>
  27. </template>
  28. </template>
  29. <template v-else>
  30. <template v-if="props.data.options?.length">
  31. <view class="size-28 option"
  32. v-for="(option, oIdx) in props.data.options"
  33. :key="`${props.index}-option-${oIdx}`"
  34. :class="[
  35. props.data.answer === option.id ? 'is-correct' : '',
  36. props.data.value === option.id && props.data.answer !== option.id ? 'is-error' : '',
  37. ]"
  38. >
  39. {{ option.title }}
  40. <view class="icon icon-correct">
  41. <up-icon name="checkmark" color="#05C160" size="35rpx"></up-icon>
  42. </view>
  43. <view class="icon icon-error">
  44. <up-icon name="close" color="#FF2A2A" size="35rpx"></up-icon>
  45. </view>
  46. </view>
  47. </template>
  48. <template v-else>
  49. <view class="textarea">
  50. <view>{{ props.data.value }}</view>
  51. <view class="highlight">{{ props.data.answer }}</view>
  52. </view>
  53. </template>
  54. </template>
  55. </view>
  56. </template>
  57. <script setup>
  58. import { computed, watch } from 'vue'
  59. import { addBaseAnswer, addTrainAnswer } from '@/api/examination'
  60. import { store } from '@/store'
  61. const userId = computed(() => {
  62. return store.state.user.userInfo.userId
  63. })
  64. const props = defineProps({
  65. index: {
  66. type: Number,
  67. default: null,
  68. },
  69. data: {
  70. type: Object,
  71. default() {
  72. return {}
  73. }
  74. },
  75. modelValue: {
  76. type: String | Number,
  77. default: null,
  78. },
  79. mode: {
  80. type: String,
  81. default: 'edit', // edit | display
  82. },
  83. type: {
  84. type: String,
  85. default: null, // '基本' | '培训'
  86. }
  87. })
  88. const emit = defineEmits(['update:modelValue'])
  89. const value = computed({
  90. set(val) {
  91. emit('update:modelValue', val)
  92. },
  93. get() {
  94. return props.modelValue
  95. }
  96. })
  97. const onChange = (val) => {
  98. value.value = val
  99. const data = {
  100. userId: userId.value,
  101. questionId: props.data.id,
  102. }
  103. if (props.type === '基本') {
  104. data.answerId = val
  105. addBaseAnswer(data)
  106. } else if (props.type === '培训') {
  107. data.answer = val
  108. addTrainAnswer(data)
  109. }
  110. }
  111. </script>
  112. <style lang="scss" scoped>
  113. .question {
  114. color: #000000;
  115. }
  116. .option {
  117. background-color: #F3F3F3;
  118. color: #707070;
  119. line-height: 37rpx;
  120. padding: 23rpx;
  121. border-radius: 28rpx;
  122. position: relative;
  123. & + & {
  124. margin-top: 20rpx;
  125. }
  126. .icon {
  127. position: absolute;
  128. right: 45rpx;
  129. bottom: 23rpx;
  130. display: none;
  131. }
  132. }
  133. .textarea {
  134. background-color: #F3F3F3;
  135. padding: 23rpx;
  136. border-radius: 16rpx;
  137. .highlight {
  138. color: #FF2A2A;
  139. font-size: 28rpx;
  140. }
  141. }
  142. .question__view.edit {
  143. .option.is-selected {
  144. background-color: rgba($color: #FFBF60, $alpha: 0.22);
  145. color: #FFBF60;
  146. }
  147. }
  148. .question__view.display {
  149. .option {
  150. &.is-correct {
  151. background-color: rgba($color: #05C160, $alpha: 0.08);
  152. color: #05C160;
  153. .icon-correct {
  154. display: block;
  155. }
  156. }
  157. &.is-error {
  158. background-color: rgba($color: #FFEBCE, $alpha: 0.36);
  159. color: #FF2A2A;
  160. .icon-error {
  161. display: block;
  162. }
  163. }
  164. }
  165. }
  166. </style>