ability-test.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. <template>
  2. <view class="ability-test-page">
  3. <view class="ability-test-header">
  4. <view class="ability-test-header-title">能力测试</view>
  5. <view class="ability-test-header-desc">请认真完成以下合规测评内容</view>
  6. </view>
  7. <view v-for="quizItem in pkgQuizList" :key="quizItem.quiz.quizId" class="quiz-card">
  8. <view class="quiz-card-header">
  9. <view class="quiz-card-title">
  10. {{ quizItem.quiz.title }}
  11. </view>
  12. <view class="quiz-card-meta">
  13. <text>共 {{ quizItem.items.length }} 题</text>
  14. </view>
  15. </view>
  16. <view v-for="(item, index) in quizItem.items" :key="item.itemId" class="question-card">
  17. <view class="question-title-row">
  18. <view class="question-index">
  19. {{ index + 1 }}
  20. </view>
  21. <view class="question-title">
  22. {{ item.label }}
  23. </view>
  24. </view>
  25. <radio-group class="question-radio-group" @change="handleRadioChange(item, $event)">
  26. <label
  27. v-for="optionItem in item.options"
  28. :key="optionItem.no"
  29. class="option-item"
  30. :class="item.userAnswer === optionItem.no ? 'option-item-active' : ''"
  31. >
  32. <radio
  33. class="option-radio"
  34. :value="optionItem.no"
  35. :checked="item.userAnswer === optionItem.no"
  36. color="#2f73ff"
  37. />
  38. <view class="option-content">
  39. <text class="option-no">{{ optionItem.no }}</text>
  40. <text class="option-text">{{ optionItem.text }}</text>
  41. </view>
  42. </label>
  43. </radio-group>
  44. </view>
  45. </view>
  46. <view v-if="!pkgQuizList.length" class="empty">
  47. {{ loading ? '加载中...' : '暂无测试内容' }}
  48. </view>
  49. <view class="ability-test-footer">
  50. <button
  51. class="submit-button"
  52. :class="submitting ? 'submit-button-disabled' : ''"
  53. :disabled="submitting"
  54. hover-class="submit-button-hover"
  55. @click="submitFn"
  56. >
  57. {{ submitting ? '提交中...' : '提 交' }}
  58. </button>
  59. </view>
  60. </view>
  61. </template>
  62. <script setup lang="ts">
  63. import { ref } from 'vue'
  64. import { onLoad } from '@dcloudio/uni-app'
  65. import type {
  66. QuizInfo,
  67. QuizItem,
  68. QuizPltDetailResponse,
  69. QuizPltSubmitParams,
  70. } from '@/services/modules/mine/complianceEvaluation'
  71. import {
  72. getQuizPltAvailApi,
  73. getQuizPltDetailsApi,
  74. quizTestResultCreateApi,
  75. } from '@/services/modules/mine/complianceEvaluation'
  76. interface PageQuery {
  77. fromHome?: string
  78. }
  79. interface RadioChangeEvent {
  80. detail: {
  81. value: string
  82. }
  83. }
  84. interface AnswerQuizItem extends QuizItem {
  85. userAnswer: string
  86. }
  87. interface AnswerQuizDetail extends Omit<QuizPltDetailResponse, 'items'> {
  88. quizId: number
  89. items: AnswerQuizItem[]
  90. }
  91. interface PassedQuizResult extends AnswerQuizDetail {
  92. finalMark: number
  93. }
  94. const fromHome = ref('')
  95. const loading = ref(false)
  96. const submitting = ref(false)
  97. const originPkgQuizList = ref<AnswerQuizDetail[]>([])
  98. const pkgQuizList = ref<AnswerQuizDetail[]>([])
  99. const passedQuizResultMap = ref<Record<number, PassedQuizResult>>({})
  100. const cloneData = <T,>(data: T): T => {
  101. return JSON.parse(JSON.stringify(data))
  102. }
  103. const normalizeQuizDetail = (data: QuizPltDetailResponse): AnswerQuizDetail => {
  104. return {
  105. ...data,
  106. quizId: data.quiz.quizId,
  107. items: data.items.map((item) => ({
  108. ...item,
  109. userAnswer: '',
  110. })),
  111. }
  112. }
  113. const handleRadioChange = (item: AnswerQuizItem, event: RadioChangeEvent) => {
  114. item.userAnswer = event.detail.value
  115. }
  116. const validateAnswered = () => {
  117. const hasUnanswered = pkgQuizList.value.some((quizItem) => {
  118. return quizItem.items.some((item) => !item.userAnswer)
  119. })
  120. if (!hasUnanswered) return true
  121. uni.showToast({
  122. title: '请完成所有题目后提交',
  123. icon: 'none',
  124. })
  125. return false
  126. }
  127. const calcQuizMark = (quizItem: AnswerQuizDetail) => {
  128. return quizItem.items.reduce((total, item) => {
  129. return item.userAnswer === item.answer ? total + item.mark : total
  130. }, 0)
  131. }
  132. const buildSubmitItems = (quizItem: PassedQuizResult) => {
  133. return quizItem.items.map((item) => {
  134. const { userAnswer, ...rest } = item
  135. return {
  136. ...rest,
  137. answered: userAnswer,
  138. }
  139. })
  140. }
  141. const buildSubmitParams = (): QuizPltSubmitParams => {
  142. const quizResults = originPkgQuizList.value.map((originItem) => {
  143. const quizId = originItem.quiz.quizId
  144. const passedQuizItem = passedQuizResultMap.value[quizId]
  145. const quiz = originItem.quiz
  146. return {
  147. quizId: quiz.quizId,
  148. title: quiz.title,
  149. itemQty: quiz.expand.itemQty,
  150. totalMark: quiz.expand.totalMark,
  151. passingMark: quiz.expand.passingMark,
  152. finalMark: passedQuizItem.finalMark,
  153. items: buildSubmitItems(passedQuizItem),
  154. }
  155. })
  156. return {
  157. quizResults,
  158. }
  159. }
  160. const resetCurrentQuizAnswers = () => {
  161. pkgQuizList.value = pkgQuizList.value.map((quizItem) => ({
  162. ...quizItem,
  163. items: quizItem.items.map((item) => ({
  164. ...item,
  165. userAnswer: '',
  166. })),
  167. }))
  168. }
  169. const updatePassedQuizResult = () => {
  170. pkgQuizList.value.forEach((quizItem) => {
  171. const finalMark = calcQuizMark(quizItem)
  172. const passingMark = quizItem.quiz.expand.passingMark
  173. if (finalMark >= passingMark) {
  174. passedQuizResultMap.value[quizItem.quiz.quizId] = {
  175. ...cloneData(quizItem),
  176. finalMark,
  177. }
  178. }
  179. })
  180. }
  181. const getFailedQuizList = () => {
  182. return originPkgQuizList.value.filter((quizItem) => {
  183. return !passedQuizResultMap.value[quizItem.quiz.quizId]
  184. })
  185. }
  186. const isEveryQuizPassed = () => {
  187. return originPkgQuizList.value.every((quizItem) => {
  188. return !!passedQuizResultMap.value[quizItem.quiz.quizId]
  189. })
  190. }
  191. const cancelFn = () => {
  192. uni.navigateBack()
  193. }
  194. const confirmFn = () => {
  195. pkgQuizList.value = cloneData(getFailedQuizList())
  196. resetCurrentQuizAnswers()
  197. }
  198. const showFailTip = () => {
  199. uni.showModal({
  200. title: '测试未通过',
  201. content: '您可以选择重新进行答题',
  202. confirmText: '重新测试',
  203. cancelText: '返回',
  204. success: (result) => {
  205. if (result.confirm) {
  206. confirmFn()
  207. return
  208. }
  209. cancelFn()
  210. },
  211. })
  212. }
  213. const submitFn = async () => {
  214. if (submitting.value) return
  215. if (!validateAnswered()) return
  216. updatePassedQuizResult()
  217. if (!isEveryQuizPassed()) {
  218. showFailTip()
  219. return
  220. }
  221. try {
  222. submitting.value = true
  223. const params = buildSubmitParams()
  224. const res = await quizTestResultCreateApi(params)
  225. if (res.code !== 0) {
  226. uni.showToast({
  227. title: '提交失败',
  228. icon: 'none',
  229. })
  230. return
  231. }
  232. uni.showToast({
  233. title: '测验通过',
  234. icon: 'none',
  235. })
  236. if (fromHome.value === 'home') {
  237. uni.reLaunch({
  238. url: '/pages/index/index',
  239. })
  240. return
  241. }
  242. uni.reLaunch({
  243. url: '/pages/mine/index',
  244. })
  245. } catch (error) {
  246. console.error('quizTestResultCreateApi error:', error)
  247. uni.showToast({
  248. title: '提交失败',
  249. icon: 'none',
  250. })
  251. } finally {
  252. submitting.value = false
  253. }
  254. }
  255. const getPltAvailQuizListFn = async () => {
  256. try {
  257. loading.value = true
  258. const res = await getQuizPltAvailApi()
  259. if (res.code !== 0) {
  260. uni.showToast({
  261. title: '获取测试列表失败',
  262. icon: 'none',
  263. })
  264. return
  265. }
  266. const quizList: QuizInfo[] = res.data || []
  267. const detailList = await Promise.all(
  268. quizList.map(async (quizItem) => {
  269. const detailRes = await getQuizPltDetailsApi(quizItem.quizId)
  270. if (detailRes.code !== 0 || !detailRes.data) {
  271. return null
  272. }
  273. return normalizeQuizDetail(detailRes.data)
  274. })
  275. )
  276. const validDetailList = detailList.filter(Boolean) as AnswerQuizDetail[]
  277. originPkgQuizList.value = cloneData(validDetailList)
  278. pkgQuizList.value = cloneData(validDetailList)
  279. resetCurrentQuizAnswers()
  280. } catch (error) {
  281. console.error('getPltAvailQuizListFn error:', error)
  282. uni.showToast({
  283. title: '获取测试列表失败',
  284. icon: 'none',
  285. })
  286. } finally {
  287. loading.value = false
  288. }
  289. }
  290. onLoad((query?: PageQuery) => {
  291. fromHome.value = query?.fromHome || ''
  292. getPltAvailQuizListFn()
  293. })
  294. </script>
  295. <style lang="scss" scoped>
  296. .ability-test-page {
  297. min-height: 100vh;
  298. padding: 28rpx 28rpx 180rpx;
  299. box-sizing: border-box;
  300. background: linear-gradient(180deg, #f1f6ff 0%, #f7f8fa 360rpx, #f7f8fa 100%);
  301. }
  302. .ability-test-header {
  303. padding: 38rpx 32rpx 42rpx;
  304. margin-bottom: 28rpx;
  305. box-sizing: border-box;
  306. border-radius: 28rpx;
  307. background: linear-gradient(135deg, #2f73ff 0%, #438dff 100%);
  308. box-shadow: 0 20rpx 44rpx rgba(47, 115, 255, 0.22);
  309. }
  310. .ability-test-header-title {
  311. font-size: 44rpx;
  312. font-weight: 700;
  313. line-height: 60rpx;
  314. color: #ffffff;
  315. }
  316. .ability-test-header-desc {
  317. margin-top: 12rpx;
  318. font-size: 28rpx;
  319. line-height: 40rpx;
  320. color: rgba(255, 255, 255, 0.84);
  321. }
  322. .quiz-card {
  323. margin-bottom: 28rpx;
  324. border-radius: 28rpx;
  325. background-color: #ffffff;
  326. box-shadow: 0 18rpx 44rpx rgba(32, 52, 89, 0.06);
  327. overflow: hidden;
  328. }
  329. .quiz-card-header {
  330. padding: 34rpx 30rpx 28rpx;
  331. box-sizing: border-box;
  332. border-bottom: 1rpx solid #eef1f6;
  333. }
  334. .quiz-card-title {
  335. font-size: 38rpx;
  336. font-weight: 700;
  337. line-height: 54rpx;
  338. color: #202938;
  339. }
  340. .quiz-card-meta {
  341. display: flex;
  342. align-items: center;
  343. margin-top: 12rpx;
  344. font-size: 26rpx;
  345. line-height: 36rpx;
  346. color: #8a94a6;
  347. }
  348. .question-card {
  349. padding: 32rpx 30rpx 36rpx;
  350. box-sizing: border-box;
  351. border-bottom: 1rpx solid #f0f2f6;
  352. }
  353. .question-card:last-child {
  354. border-bottom: none;
  355. }
  356. .question-title-row {
  357. display: flex;
  358. align-items: flex-start;
  359. margin-bottom: 26rpx;
  360. }
  361. .question-index {
  362. display: flex;
  363. align-items: center;
  364. justify-content: center;
  365. flex-shrink: 0;
  366. width: 46rpx;
  367. height: 46rpx;
  368. margin-right: 18rpx;
  369. border-radius: 14rpx;
  370. font-size: 26rpx;
  371. font-weight: 700;
  372. color: #2f73ff;
  373. background-color: #eef4ff;
  374. }
  375. .question-title {
  376. flex: 1;
  377. min-width: 0;
  378. font-size: 34rpx;
  379. font-weight: 600;
  380. line-height: 48rpx;
  381. color: #263041;
  382. }
  383. .question-radio-group {
  384. width: 100%;
  385. }
  386. .option-item {
  387. display: flex;
  388. align-items: center;
  389. width: 100%;
  390. margin-bottom: 20rpx;
  391. padding: 24rpx;
  392. box-sizing: border-box;
  393. border: 2rpx solid #eef1f6;
  394. border-radius: 20rpx;
  395. background-color: #f8faff;
  396. }
  397. .option-item:last-child {
  398. margin-bottom: 0;
  399. }
  400. .option-item-active {
  401. border-color: #2f73ff;
  402. background-color: #eef4ff;
  403. box-shadow: 0 12rpx 26rpx rgba(47, 115, 255, 0.1);
  404. }
  405. .option-radio {
  406. flex-shrink: 0;
  407. transform: scale(0.86);
  408. }
  409. .option-content {
  410. display: flex;
  411. align-items: center;
  412. flex: 1;
  413. min-width: 0;
  414. margin-left: 12rpx;
  415. }
  416. .option-no {
  417. display: flex;
  418. align-items: center;
  419. justify-content: center;
  420. flex-shrink: 0;
  421. width: 42rpx;
  422. height: 42rpx;
  423. margin-right: 18rpx;
  424. border-radius: 50%;
  425. font-size: 24rpx;
  426. font-weight: 700;
  427. color: #2f73ff;
  428. background-color: #ffffff;
  429. }
  430. .option-text {
  431. flex: 1;
  432. min-width: 0;
  433. font-size: 30rpx;
  434. line-height: 42rpx;
  435. color: #303846;
  436. }
  437. .empty {
  438. padding: 120rpx 0;
  439. text-align: center;
  440. font-size: 28rpx;
  441. color: #9aa3b2;
  442. }
  443. .ability-test-footer {
  444. position: fixed;
  445. left: 0;
  446. right: 0;
  447. bottom: 0;
  448. z-index: 20;
  449. padding: 20rpx 28rpx calc(20rpx + env(safe-area-inset-bottom));
  450. box-sizing: border-box;
  451. background-color: #ffffff;
  452. box-shadow: 0 -8rpx 24rpx rgba(33, 57, 96, 0.06);
  453. }
  454. .submit-button {
  455. display: flex;
  456. align-items: center;
  457. justify-content: center;
  458. width: 100%;
  459. height: 88rpx;
  460. padding: 0;
  461. border: none;
  462. border-radius: 44rpx;
  463. font-size: 32rpx;
  464. font-weight: 600;
  465. line-height: 88rpx;
  466. color: #ffffff;
  467. background: linear-gradient(135deg, #2f73ff 0%, #438dff 100%);
  468. box-shadow: 0 14rpx 28rpx rgba(47, 115, 255, 0.24);
  469. }
  470. .submit-button::after {
  471. border: none;
  472. }
  473. .submit-button-hover {
  474. opacity: 0.86;
  475. }
  476. .submit-button-disabled {
  477. opacity: 0.65;
  478. }
  479. </style>