test.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <template>
  2. <view class="test">
  3. <zs-header title="研学MBTI测试" :background="background"></zs-header>
  4. <view v-if="!isOver" class="answer-box">
  5. <view class="question-box">
  6. <view class="num">
  7. 第{{step+1}}题
  8. </view>
  9. <view class="question">
  10. {{list[step].questionsName}}
  11. </view>
  12. </view>
  13. <view class="answer-item" :class="[answer[step] == item.columnId?'active':'']" @click="choose(item.columnId)" v-for="(item,index) in list[step].answers" :key="index">
  14. {{index+1}}、{{item.answer}}
  15. </view>
  16. </view>
  17. <view v-else class="result">
  18. <view class="result-title">
  19. 恭喜您完成测试
  20. </view>
  21. <view class="result-content">
  22. <view class="title">
  23. {{resultInfo.resultTitle}}
  24. </view>
  25. <view class="desc-title">
  26. 人格解析
  27. </view>
  28. <view class="desc">
  29. {{resultInfo.resultMsg}}
  30. </view>
  31. </view>
  32. </view>
  33. <view class="btn-group">
  34. <template v-if="isOver">
  35. <view class="btn" @click="jump">
  36. 个性化研学路线推荐
  37. </view>
  38. <view class="btn pre-btn" @click="handleAgain">
  39. 再测一次
  40. </view>
  41. </template>
  42. <template v-else>
  43. <view class="btn" @click="handleBtn(1)">
  44. {{step == list.length-1?'提交测试':'下一题'}}
  45. </view>
  46. <view class="btn pre-btn" v-show="step>0" @click="handleBtn(-1)">
  47. 上一题
  48. </view>
  49. </template>
  50. </view>
  51. </view>
  52. </template>
  53. <script>
  54. import {getTestList,getPaper,studyTest,getResult} from '@/api/study.js'
  55. export default {
  56. data() {
  57. return {
  58. isOver:false,//是否答题完毕
  59. background: false,
  60. step: 0,
  61. paperId:0,
  62. list: [],
  63. answer:{},
  64. resultInfo:{
  65. resultTitle:'',
  66. resultMsg:'',
  67. }
  68. }
  69. },
  70. methods: {
  71. filterData(arr){
  72. var obj = {}
  73. //将数组中的数和出现的次数利用对象键值对的方式进行存储
  74. for(var i = 0 ;i < arr.length ; i++){
  75. if(!obj[arr[i]]){
  76. obj[arr[i]] = 1;
  77. }
  78. else{
  79. obj[arr[i]] = Number(obj[arr[i]])+ 1
  80. }
  81. }
  82. //把对象的key值变成数组并通过value值的大小进行排序
  83. var sortArr = Object.keys(obj).sort((i,j) => obj[j]-obj[i])//排序方法的使用
  84. return sortArr.join(",")
  85. },
  86. choose(id){
  87. this.$set(this.answer,this.step,id)
  88. },
  89. handleBtn(val) {
  90. if(val && this.answer[this.step]){//下一步 必须选择答案
  91. if(this.step == this.list.length -1){
  92. this.isOver = true
  93. let arr = []
  94. for (const key in this.answer) {
  95. if (Object.hasOwnProperty.call(this.answer, key)) {
  96. arr.push(this.answer[key])
  97. }
  98. }
  99. let studyColumnId = this.filterData(arr)//按照答案的出现次数排序
  100. console.log(studyColumnId);
  101. return this.studyTest(studyColumnId)
  102. }
  103. }else if(val){
  104. return uni.showToast({
  105. title:'请选择答案',
  106. icon:'error'
  107. })
  108. }
  109. this.step += val
  110. },
  111. // 再测一次
  112. handleAgain(){
  113. this.step = 0
  114. this.isOver = false
  115. this.answer = {}
  116. this.getList()
  117. },
  118. jump(){
  119. uni.navigateTo({
  120. url:'/study/index'
  121. })
  122. },
  123. getList(){
  124. getPaper({currentPage:1,pageSize:1}).then(res=>{
  125. if(res.state == 'Success'){
  126. this.paperId = res.content.records[0].id
  127. getTestList({paperId:this.paperId}).then(r=>{
  128. this.list = r.content
  129. })
  130. }
  131. })
  132. },
  133. studyTest(studyColumnId){
  134. studyTest({studyColumnId,paperId:this.paperId}).then(res=>{
  135. if(res.state == 'Success'){
  136. this.getResult()
  137. }
  138. })
  139. },
  140. // 获取测试结果
  141. getResult(){
  142. let userId = uni.getStorageSync('userInfo')?JSON.parse(uni.getStorageSync('userInfo')).userId:''
  143. getResult({userId}).then(r=>{
  144. if(r.state == 'Success'&&r.content.createTime){
  145. this.isOver = true
  146. this.resultInfo = r.content
  147. }
  148. })
  149. }
  150. },
  151. created() {
  152. this.getList()
  153. this.getResult()
  154. },
  155. onPageScroll(e) {
  156. if (e.scrollTop >= 50) {
  157. this.background = true
  158. } else {
  159. this.background = false
  160. }
  161. },
  162. }
  163. </script>
  164. <style lang="scss" scoped>
  165. .test {
  166. height: 100vh;
  167. background: url('http://zswl-dev.oss-cn-chengdu.aliyuncs.com/63b7c68b71a69169d1b33f92/store/bdb/user/avatar/YAsTZ3gcQFu701357708df6e5f3e01c18c6e27b7be25.png/1.png') no-repeat;
  168. background-size: 100% 100%;
  169. padding-top: 180rpx;
  170. box-sizing: border-box;
  171. .answer-box {
  172. padding: 0 10rpx;
  173. .question-box {
  174. width: 730rpx;
  175. min-height: 430rpx;
  176. box-sizing: border-box;
  177. padding: 160rpx 80rpx 100rpx;
  178. background: url('http://zswl-dev.oss-cn-chengdu.aliyuncs.com/63b7c68b71a69169d1b33f92/store/bdb/user/avatar/4lDYiZSDBEJ660c2408e36a13a153bad64d5ff3a1786.png/1.png') no-repeat;
  179. background-size: 100% 100%;
  180. position: relative;
  181. .num {
  182. position: absolute;
  183. top: 28rpx;
  184. left: 30rpx;
  185. width: 230rpx;
  186. text-align: center;
  187. line-height: 72rpx;
  188. font-weight: bold;
  189. color: #181818;
  190. font-size: 36rpx;
  191. }
  192. }
  193. .answer-item.active{
  194. background: #ABD2FF;
  195. }
  196. .answer-item {
  197. width: 690rpx;
  198. line-height: 80rpx;
  199. border: 2rpx solid #181818;
  200. background: #FFFFFF;
  201. border-radius: 52rpx 52rpx 52rpx 52rpx;
  202. padding: 0 30rpx;
  203. margin-left: 20rpx;
  204. margin-top: 30rpx;
  205. word-break: break-all;
  206. box-sizing: border-box;
  207. }
  208. }
  209. .result{
  210. .result-title{
  211. width: 596rpx;
  212. line-height: 138rpx;
  213. text-align: center;
  214. background: linear-gradient(180deg, #F2F6FF 0%, #A1C1FF 100%);
  215. border-radius: 16rpx 16rpx 0 0;
  216. font-weight: bold;
  217. color: #181818;
  218. font-size: 36rpx;
  219. margin: 60rpx auto 0;
  220. }
  221. .result-content{
  222. width: 686rpx;
  223. padding: 40rpx 30rpx;
  224. background: linear-gradient(180deg, #F2F6FF 0%, #A1C1FF 100%);
  225. border-radius: 16rpx 16rpx 16rpx 16rpx;
  226. box-sizing: border-box;
  227. margin: 0 auto;
  228. .title{
  229. font-weight: 800;
  230. color: #3879F9;
  231. font-size: 60rpx;
  232. text-align: center;
  233. }
  234. .desc-title{
  235. font-weight: bold;
  236. color: #181818;
  237. font-size: 36rpx;
  238. }
  239. .desc{
  240. color: #181818;
  241. font-size: 28rpx;
  242. margin-top: 20rpx;
  243. line-height: 46rpx;
  244. min-height: 440rpx;
  245. }
  246. }
  247. }
  248. .btn-group {
  249. position: fixed;
  250. bottom: 100rpx;
  251. left: 0%;
  252. width: 100%;
  253. display: flex;
  254. flex-direction: column;
  255. align-items: center;
  256. .btn {
  257. width: 688rpx;
  258. line-height: 80rpx;
  259. text-align: center;
  260. background: #3879F9;
  261. border-radius: 40rpx 40rpx 40rpx 40rpx;
  262. color: #FFFFFF;
  263. font-weight: bold;
  264. font-size: 28rpx;
  265. }
  266. .btn.pre-btn {
  267. background: none;
  268. color: #3879F9;
  269. border: 2rpx solid #3879F9;
  270. margin-top: 20rpx;
  271. }
  272. }
  273. }
  274. </style>