普兆健康管家前端代码仓库
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.

533 lines
14 KiB

  1. <template>
  2. <view class="page__view">
  3. <navbar :title="`问题 ${current + 1}/${total}`" leftClick @leftClick="$utils.navigateBack" color="#191919" bgColor="transparent" />
  4. <view class="bar">
  5. <view class="flex tools">
  6. <view class="left">
  7. <button class="flex btn" v-if="showPreBtn" @click="pre">
  8. <image class="btn-icon" src="@/pages_order/static/report/icon-wrapper.png" mode="widthFix"></image>
  9. <text>上一题</text>
  10. </button>
  11. </view>
  12. <view class="right">
  13. 选择你的答案进入下一题
  14. </view>
  15. </view>
  16. <view class="progress">
  17. <view class="progress-bar" :style="{ width: `${progress}%` }"></view>
  18. </view>
  19. </view>
  20. <view class="main">
  21. <view class="question">{{ currentQuestion.question }}</view>
  22. <template v-if="currentQuestion.component === 'select'">
  23. <view class="select">
  24. <view
  25. v-for="item in currentQuestion.options"
  26. :key="item.id"
  27. :class="['flex', 'select-option', item.id === value ? 'is-active' : '']"
  28. @click="onSelect(item.id)"
  29. >
  30. {{ item.content }}
  31. </view>
  32. </view>
  33. </template>
  34. <template v-else-if="currentQuestion.component === 'select-box'">
  35. <view class="select-box">
  36. <view
  37. v-for="item in currentQuestion.options"
  38. :key="item.id"
  39. :class="['flex', 'flex-column', 'select-box-option', item.id === value ? 'is-active' : '']"
  40. @click="onSelect(item.id)"
  41. >
  42. <image class="img" :src="item.id === value ? item.imageAfter : item.image" mode="aspectFit"></image>
  43. <view class="text">{{ item.content }}</view>
  44. </view>
  45. </view>
  46. </template>
  47. <template v-else-if="currentQuestion.component === 'input'">
  48. <view class="input">
  49. <view class="flex input-box">
  50. <view class="input-label" v-if="currentQuestion.options[0].prefix">{{ currentQuestion.options[0].prefix }}</view>
  51. <input class="input-doc" v-model="value" placeholder="请输入内容" placeholder-style="font-family: PingFang SC; font-weight: 400; line-height: 1.4; font-size: 32rpx; color: #AAAACA;" />
  52. <view class="input-unit" v-if="currentQuestion.options[0].suffix">{{ currentQuestion.options[0].suffix }}</view>
  53. </view>
  54. </view>
  55. </template>
  56. <template v-else-if="currentQuestion.component === 'input-group'">
  57. <view class="input">
  58. <view class="flex input-box" v-for="(item, index) in currentQuestion.options" :key="item.id">
  59. <view class="input-label" v-if="item.prefix">{{ item.prefix }}</view>
  60. <input class="input-doc" v-model="value[index]" placeholder="请输入内容" placeholder-style="font-family: PingFang SC; font-weight: 400; line-height: 1.4; font-size: 32rpx; color: #AAAACA;" />
  61. <view class="input-unit" v-if="item.suffix">{{ item.suffix }}</view>
  62. </view>
  63. </view>
  64. </template>
  65. <view class="flex btns">
  66. <button :class="['btn', 'btn-palin', noneFlag ? 'is-active' : '']" v-if="!currentQuestion.required" @click="onSelectNone"> </button>
  67. <button class="btn" v-if="showConfirmBtn" @click="onConfirm"> </button>
  68. <button class="btn" v-if="showSubmitBtn" @click="onSubmit"> </button>
  69. </view>
  70. <view class="desc" v-if="currentQuestion.desc">
  71. <uv-parse :content="currentQuestion.desc"></uv-parse>
  72. </view>
  73. </view>
  74. </view>
  75. </template>
  76. <script>
  77. import { mapState } from 'vuex'
  78. export default {
  79. data() {
  80. return {
  81. step: 0,
  82. current: 0,
  83. value: null,
  84. noneFlag: false,
  85. answers: [],
  86. questionsList: [],
  87. total: 0,
  88. }
  89. },
  90. computed: {
  91. ...mapState(['paperInfo']),
  92. showPreBtn() {
  93. return this.current > 0
  94. },
  95. progress() {
  96. return 100 * (this.current + 1) / this.total
  97. },
  98. showSubmitBtn() {
  99. return this.current + 1 === this.total
  100. },
  101. currentQuestion() {
  102. return this.questionsList[this.current]
  103. },
  104. showConfirmBtn() {
  105. if (this.showSubmitBtn) {
  106. return false
  107. }
  108. return ['input', 'input-group'].includes(this.currentQuestion?.component)
  109. },
  110. },
  111. onLoad(arg) {
  112. this.step = parseInt(arg.step)
  113. this.current = parseInt(arg.current || 0)
  114. this.fetchQuestionList()
  115. console.log('currentQuestion', this.currentQuestion)
  116. },
  117. methods: {
  118. fetchQuestionList() {
  119. const { category: categoryList } = this.paperInfo
  120. const category = categoryList[this.step]
  121. const { questionsList } = category
  122. this.questionsList = questionsList.map(item => {
  123. const { id, text, type, options, required, content } = item
  124. let component = 'select'
  125. switch(type) { // 0-单选题 1-填空题 2-图片单选题
  126. case '1':
  127. component = options?.length > 1 ? 'input-group' : 'input'
  128. break
  129. case '2':
  130. component = 'select-box'
  131. break
  132. default:
  133. component = 'select'
  134. break
  135. }
  136. return {
  137. id,
  138. question: text,
  139. type,
  140. component,
  141. options,
  142. required: required == 'Y',
  143. desc: content,
  144. }
  145. })
  146. this.total = this.questionsList.length
  147. this.answers = this.questionsList.map(item => {
  148. let val = null
  149. if (item.component === 'input-group') {
  150. val = new Array(item.options.length).fill(0).map(() => null)
  151. }
  152. return val
  153. })
  154. this.value = this.answers[this.current]
  155. console.log('questionsList', this.questionsList)
  156. console.log('answers', this.answers)
  157. },
  158. pre() {
  159. this.current -= 1
  160. this.value = this.answers[this.current]
  161. this.noneFlag = false
  162. },
  163. next() {
  164. if (this.showSubmitBtn) {
  165. return
  166. }
  167. this.current += 1
  168. this.value = this.answers[this.current]
  169. this.noneFlag = false
  170. },
  171. async fetchAnswer() {
  172. try {
  173. const { id: questionsId, type, required, options } = this.currentQuestion
  174. console.log('paperInfo', this.paperInfo)
  175. console.log('currentQuestion', this.currentQuestion)
  176. console.log('required', required, 'noneFlag', this.noneFlag, 'value', this.value)
  177. if (required && !this.noneFlag && (!this.value || this.value?.every?.(val => !val))) {
  178. console.log('未答题')
  179. uni.showToast({
  180. title: '请答题',
  181. icon:'none'
  182. })
  183. return false
  184. }
  185. let answer
  186. // 0-单选题 1-填空题 2-图片单选题
  187. if (type == 1) {
  188. answer = options.reduce((obj, option, oIdx) => {
  189. const { id: optionsId } = option
  190. obj[optionsId] = this.value[oIdx]
  191. return obj
  192. }, {})
  193. answer = JSON.stringify(answer)
  194. } else {
  195. answer = this.value
  196. }
  197. let params = {
  198. id: this.paperInfo.reportId,
  199. questionsId,
  200. answer
  201. }
  202. console.log('params', params)
  203. await this.$fetch('answerPaper', params)
  204. this.answers[this.current] = this.value
  205. return true
  206. } catch (err) {
  207. console.log('answerPaper', err)
  208. return false
  209. }
  210. },
  211. async onSelect(id) {
  212. this.value = id
  213. if (this.showSubmitBtn) {
  214. return
  215. }
  216. let succ = await this.fetchAnswer()
  217. console.log('fetchAnswer', succ)
  218. if (!succ) {
  219. return
  220. }
  221. this.$nextTick(() => {
  222. setTimeout(() => {
  223. this.next()
  224. }, 500)
  225. })
  226. },
  227. async onConfirm() {
  228. let succ = await this.fetchAnswer()
  229. console.log('fetchAnswer', succ)
  230. if (!succ) {
  231. return
  232. }
  233. this.next()
  234. },
  235. async onSelectNone() {
  236. this.noneFlag = true
  237. let val = null
  238. if (this.currentQuestion.component === 'input-group') {
  239. val = new Array(this.currentQuestion.options.length).fill(0).map(() => '')
  240. }
  241. this.value = val
  242. let succ = await this.fetchAnswer()
  243. console.log('fetchAnswer', succ)
  244. if (!succ) {
  245. return
  246. }
  247. this.next()
  248. },
  249. async onSubmit() {
  250. let succ = await this.fetchAnswer()
  251. console.log('fetchAnswer', succ)
  252. if (!succ) {
  253. return
  254. }
  255. const { category } = this.paperInfo
  256. if (this.step == category.length - 1) {
  257. await this.$fetch('submitPaper', { id: this.paperInfo.reportId })
  258. uni.reLaunch({
  259. url: '/pages/index/report'
  260. })
  261. return
  262. }
  263. this.$utils.redirectTo(`/pages_order/report/test/step?step=${this.step + 1}`)
  264. },
  265. },
  266. }
  267. </script>
  268. <style scoped lang="scss">
  269. .page__view {
  270. width: 100vw;
  271. min-height: 100vh;
  272. background-color: $uni-bg-color;
  273. position: relative;
  274. }
  275. .bar {
  276. margin-top: 24rpx;
  277. width: 100%;
  278. padding: 0 32rpx;
  279. box-sizing: border-box;
  280. .left {
  281. text-align: left;
  282. .btn {
  283. display: inline-flex;
  284. font-family: PingFang SC;
  285. font-weight: 400;
  286. font-size: 30rpx;
  287. line-height: 1.4;
  288. color: #7451DE;
  289. &-icon {
  290. width: 32rpx;
  291. height: auto;
  292. margin-right: 8rpx;
  293. }
  294. }
  295. }
  296. .right {
  297. flex: 1;
  298. text-align: right;
  299. font-family: PingFang SC;
  300. font-weight: 400;
  301. font-size: 26rpx;
  302. line-height: 1.4;
  303. color: #989898;
  304. }
  305. }
  306. .progress {
  307. margin-top: 24rpx;
  308. width: 100%;
  309. height: 24rpx;
  310. background: #E5E5E5;
  311. border-radius: 12rpx;
  312. &-bar {
  313. height: 100%;
  314. background-image: linear-gradient(to right, #4B348F, #845CFA);
  315. border-radius: 12rpx;
  316. }
  317. }
  318. .main {
  319. width: 100%;
  320. padding: 70rpx 104rpx 0 104rpx;
  321. box-sizing: border-box;
  322. }
  323. .question {
  324. text-align: center;
  325. font-family: PingFang SC;
  326. font-weight: 400;
  327. font-size: 40rpx;
  328. line-height: 1.4;
  329. color: #000000;
  330. margin-bottom: 64rpx;
  331. }
  332. .select {
  333. &-option {
  334. padding: 30rpx 0;
  335. font-family: PingFang SC;
  336. font-weight: 400;
  337. line-height: 1.4;
  338. font-size: 32rpx;
  339. color: #252545;
  340. background-image: linear-gradient(to right, #FFFFFF, #F4F4F6, #F2F2F4);
  341. border-radius: 52rpx;
  342. box-shadow: -2rpx -2rpx 10rpx 0 #FFFFFFC4,
  343. 4rpx 4rpx 20rpx 0 #AAAACC1F,
  344. 2rpx 2rpx 4rpx 0 #AAAACC40,
  345. -1rpx -1rpx 2rpx 0 #FFFFFF;
  346. & + & {
  347. margin-top: 40rpx;
  348. }
  349. &.is-active {
  350. color: #FFFFFF;
  351. background: #7451DE;
  352. }
  353. }
  354. }
  355. .select-box {
  356. display: grid;
  357. grid-template-columns: repeat(3, 1fr);
  358. gap: 32rpx;
  359. &-option {
  360. padding: 24rpx 0;
  361. border-radius: 24rpx;
  362. background-image: linear-gradient(to right, #FFFFFF, #F2F2F4);
  363. box-shadow: -2rpx -2rpx 10rpx 0 #FFFFFFC4,
  364. 4rpx 4rpx 20rpx 0 #AAAACC1F,
  365. 2rpx 2rpx 4rpx 0 #AAAACC40,
  366. -1rpx -1rpx 2rpx 0 #FFFFFF;
  367. .img {
  368. width: 100%;
  369. height: 64rpx;
  370. }
  371. .text {
  372. margin-top: 8rpx;
  373. font-family: PingFang SC;
  374. font-weight: 400;
  375. font-size: 28rpx;
  376. line-height: 1.4;
  377. color: #969696;
  378. }
  379. &.is-active {
  380. background: #7451DE;
  381. .text {
  382. color: #FFFFFFC4;
  383. }
  384. }
  385. }
  386. }
  387. .input {
  388. &-box {
  389. width: 100%;
  390. height: 104rpx;
  391. padding: 0 40rpx;
  392. box-sizing: border-box;
  393. border-radius: 52rpx;
  394. background-image: linear-gradient(to right, #FFFFFF, #F4F4F6, #F2F2F4);
  395. box-shadow: -2rpx -2rpx 10rpx 0 #FFFFFFC4,
  396. 4rpx 4rpx 20rpx 0 #AAAACC1F,
  397. 2rpx 2rpx 4rpx 0 #AAAACC40,
  398. -1rpx -1rpx 2rpx 0 #FFFFFF;
  399. & + & {
  400. margin-top: 40rpx;
  401. }
  402. }
  403. &-doc {
  404. text-align: center;
  405. flex: 1;
  406. }
  407. &-label {
  408. padding: 21rpx 40rpx 21rpx 0;
  409. font-family: PingFang SC;
  410. font-weight: 600;
  411. font-size: 30rpx;
  412. line-height: 1.4;
  413. color: #252545;
  414. border-right: 2rpx solid #E0DFF0;
  415. }
  416. &-unit {
  417. padding: 21rpx 0 21rpx 40rpx;
  418. font-family: PingFang SC;
  419. font-weight: 600;
  420. font-size: 30rpx;
  421. line-height: 1.4;
  422. color: #252545;
  423. border-left: 2rpx solid #E0DFF0;
  424. }
  425. }
  426. .btns {
  427. margin-top: 64rpx;
  428. column-gap: 36rpx;
  429. .btn {
  430. flex: 1;
  431. padding: 24rpx 0;
  432. font-family: PingFang SC;
  433. font-weight: 600;
  434. font-size: 30rpx;
  435. line-height: 1.4;
  436. color: #FFFFFF;
  437. background-image: linear-gradient(to right, #4B348F, #845CFA);
  438. border-radius: 45rpx;
  439. &-palin {
  440. padding: 22rpx 0;
  441. font-weight: 400;
  442. color: #252545;
  443. background: transparent;
  444. border: 2rpx solid #252545;
  445. &.is-active {
  446. color: $uni-color;
  447. border-color: $uni-color;
  448. }
  449. }
  450. }
  451. }
  452. .desc {
  453. margin-top: 220rpx;
  454. font-family: PingFang SC;
  455. font-weight: 400;
  456. line-height: 1.4;
  457. font-size: 26rpx;
  458. color: #989898;
  459. }
  460. </style>