test.vue 7.3 KB

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