IT评测·应用市场-qidao123.com技术社区

标题: UNI-APP小程序答题功能开发(左右滑动,判断,填空,问答,答题卡,纠错,做题倒计 [打印本页]

作者: 耶耶耶耶耶    时间: 2024-11-16 07:03
标题: UNI-APP小程序答题功能开发(左右滑动,判断,填空,问答,答题卡,纠错,做题倒计
原博:uni-app小程序答题功能开发(左右滑动,判断,填空,问答,答题卡,纠错,做题倒计时等)_uniapp答题模板-CSDN博客
标签: 小程序  uni-app 

 
模板链接:答题模板
html部分
这里没啥好说的,就是根据不同的状态显示不同的内容
  1. <template>
  2.         <view>
  3.                 <view id="top-box" class="cu-bar bg-white bottoms solid-bottom">
  4.                         <view class="action text-black exam-types">
  5.                                 <text v-if="currentType===1">判断题</text>
  6.                                 <text v-else-if="currentType===2">单选题</text>
  7.                                 <text v-else-if="currentType===3">多选题</text>
  8.                                 <text v-else-if="currentType===4">填空题</text>
  9.                                 <text v-else-if="currentType===5">问答题</text>
  10.                         </view>
  11.                         <!-- 题目进度 -->
  12.                         <view class="progress">
  13.                                 <text>{{pageIndex}}</text>
  14.                                 <text>/{{subjectList.length}}</text>
  15.                         </view>
  16.                         <view class="action exam-card">
  17.                                 <button class="cu-btn bg-green shadow" @tap="showCardModal" data-target="modalCard">答题卡</button>
  18.                         </view>
  19.                 </view>
  20.                 <view class="cu-modal" :class="modalCard=='modalCard'?'show':''" @tap="hideCardModal">
  21.                         <view class="cu-dialog" @tap.stop>
  22.                                 <scroll-view class="page padding" :scroll-y=true :style="{'height':swiperHeight}">
  23.                                         <view class="cu-bar solid-bottom">
  24.                                                 <view class="action">
  25.                                                         <text class="cuIcon-title text-red"></text>答题卡
  26.                                                 </view>
  27.                                         </view>
  28.                                         <!-- 选择题目 -->
  29.                                         <view class="grid col-5 ">
  30.                                                 <view class="margin-tb-sm text-center" v-for="(subject,index) in subjectList" :key="index">
  31.                                                         <button class="cu-btn round" :class="[subject.userAnswer.length===0?'line-grey':'bg-red']" @click="assignSubject(index)">{{index+1}}</button>
  32.                                                 </view>
  33.                                         </view>
  34.                                 </scroll-view>
  35.                         </view>
  36.                 </view>
  37.                 <!-- 提交试题纠错部分 -->
  38.                 <view class="cu-modal padding " :class="modalError=='modalError'?'show':''" @tap="hideErrorModal">
  39.                         <view class="cu-dialog bg-white" @tap.stop>
  40.                                 <view class="cu-bar solid-bottom ">
  41.                                         <view class="action">
  42.                                                 <text class="cuIcon-title text-red"></text>试题纠错
  43.                                         </view>
  44.                                 </view>
  45.                                 <radio-group class="block">
  46.                                         <view class="cu-list menu text-left">
  47.                                                 <view class="cu-item cu-item-error" v-for="error in errorList" :key="index">
  48.                                                         <radio :value="error"></radio>
  49.                                                         <view class="title text-black margin-left">{{error}}</view>
  50.                                                 </view>
  51.                                         </view>
  52.                                 </radio-group>
  53.                                 <view class="padding flex flex-direction ">
  54.                                         <button class="cu-btn bg-red margin-tb-sm lg" @click="SubmitError">提 交</button>
  55.                                 </view>
  56.                         </view>
  57.                 </view>
  58.                 <form>
  59.                         <swiper :current="subjectIndex" class="swiper-box" @change="SwiperChange" :style="{'height':swiperHeight}">
  60.                                 <swiper-item v-for="(subject,index) in subjectList" :key="index">
  61.                                         <view v-if="index-subjectIndex>=-1&&index-subjectIndex<=1">
  62.                                                 <scroll-view scroll-y="true">
  63.                                                         <view id="desc">
  64.                                                                 这里是对一些题目的解释这里是对一些题目的解释这里是对一些题目的解释
  65.                                                                 这里是对一些题目的解释这里是对一些题目的解释这里是对一些题目的解释
  66.                                                                 这里是对一些题目的解释这里是对一些题目的解释这里是对一些题目的解释
  67.                                                                 这里是对一些题目的解释这里是对一些题目的解释这里是对一些题目的解释
  68.                                                                 这里是对一些题目的解释这里是对一些题目的解释这里是对一些题目的解释
  69.                                                                 这里是对一些题目的解释这里是对一些题目的解释这里是对一些题目的解释
  70.                                                         </view>
  71.                                                 </scroll-view>
  72.                                                 <!-- 题目类型 -->
  73.                                                 <view class="cu-bar bg-white solid-bottom">
  74.                                                         <view class="action text-black">
  75.                                                                 <text class="cuIcon-title text-red"></text>{{subject.title}}
  76.                                                         </view>
  77.                                                 </view>
  78.                                                 <view>
  79.                                                         <!-- 单选题部分 -->
  80.                                                         <radio-group class="block" @change="RadioboxChange" v-if="subject.type===1||subject.type===2">
  81.                                                                 <view class="cu-form-group" v-for="(option,index) in subject.optionList" :key="index">
  82.                                                                         <radio :value="option.id" :checked="subject.userAnswer.indexOf(option.id) > -1?true:false"></radio>
  83.                                                                         <view class="title text-black">{{option.id}}.{{option.content}}</view>
  84.                                                                 </view>
  85.                                                         </radio-group>
  86.                                                         <!-- 多选题部分 -->
  87.                                                         <checkbox-group class="block" @change="CheckboxChange" v-else-if="subject.type===3">
  88.                                                                 <view class="cu-form-group" v-for="(option,index) in subject.optionList" :key="index">
  89.                                                                         <checkbox :value="option.id" :class="subject.userAnswer.indexOf(option.id) > -1?'checked':''" :checked="subject.userAnswer.indexOf(option.id) > -1?true:false"></checkbox>
  90.                                                                         <view class="title  text-black">{{option.id}}.{{option.content}}</view>
  91.                                                                 </view>
  92.                                                         </checkbox-group>
  93.                                                         <!-- 填空题部分 -->
  94.                                                         <view v-else-if="subject.type===4">
  95.                                                                 <view class="cu-form-group solid-bottom">
  96.                                                                         <view class="title  text-black">
  97.                                                                                 答:
  98.                                                                         </view>
  99.                                                                         <input placeholder="文本输入框" name="input" v-model="subject.userAnswer" @blur="textInput"></input>
  100.                                                                 </view>
  101.                                                         </view>
  102.                                                         <!-- 问答题部分,多行文本输入框 -->
  103.                                                         <view v-else-if="subject.type===5">
  104.                                                                 <view class="cu-bar cu-bar-title bg-white margin-top">
  105.                                                                         <view class="action  text-black">
  106.                                                                                 答:
  107.                                                                         </view>
  108.                                                                 </view>
  109.                                                                 <view class="cu-form-group solid margin">
  110.                                                                         <textarea maxlength="-1" @blur="textInput" v-model="subject.userAnswer" placeholder="多行文本输入框"></textarea>
  111.                                                                 </view>
  112.                                                         </view>
  113.                                                 </view>
  114.                                                 <!-- 是否显示正确答案 -->
  115.                                                 <view v-show="subject.showAnswer" class="margin-top solid-top">
  116.                                                         <view class="cu-bar">
  117.                                                                 <view class="action  text-grey">
  118.                                                                         <text>正确答案:</text>
  119.                                                                         <text class="solid-bottom  padding-left text-green">{{subject.answer}}</text>
  120.                                                                 </view>
  121.                                                         </view>
  122.                                                         <view class="cu-bar cu-bar-title">
  123.                                                                 <view class="action  text-grey">
  124.                                                                         <text>解析:</text>
  125.                                                                 </view>
  126.                                                         </view>
  127.                                                         <view class="text-content padding  text-grey">
  128.                                                                 {{subject.explain}}
  129.                                                         </view>
  130.                                                 </view>
  131.                                                 <view class="submit-exam" v-if="pageIndex == subjectList.length">交卷</view>
  132.                                         </view>
  133.                                 </swiper-item>
  134.                         </swiper>
  135.                 </form>
  136.                 <!-- 底部 -->
  137.                 <view id="foot-box" class="cu-bar tabbar bg-white shadow foot">
  138.                         <view class="action" @click="MoveSubject(-1)">
  139.                                 <view class="cuIcon-cu-image">
  140.                                         <text class="lg cuIcon-back text-gray"></text>
  141.                                 </view>
  142.                                 <view class="text-gray">上一题</view>
  143.                         </view>
  144.                         <view class="action" @click="MoveSubject(1)">
  145.                                 <view class="cuIcon-cu-image">
  146.                                         <text class="lg text-gray cuIcon-right"></text>
  147.                                 </view>
  148.                                 <view class="text-gray">下一题</view>
  149.                         </view>
  150.                         <view class="action" @click="FavorSubject">
  151.                                 <view class="cuIcon-cu-image">
  152.                                         <text class="lg cuIcon-favor" :class="[userFavor?'text-red':'text-gray']"></text>
  153.                                 </view>
  154.                                 <view :class="[userFavor?'text-red':'text-gray']">收藏</view>
  155.                         </view>
  156.                         <view class="action" @click="ShowAnswerChange">
  157.                                 <view class="cuIcon-cu-image">
  158.                                         <text class="lg text-gray cuIcon-question"></text>
  159.                                 </view>
  160.                                 <view class="text-gray">{{str}}</view>
  161.                         </view>
  162.                         <view class="action" @tap="showErrorModal" data-target="modalError">
  163.                                 <view class="cuIcon-cu-image">
  164.                                         <text class="lg text-gray cuIcon-warn"></text>
  165.                                 </view>
  166.                                 <view class="text-gray">纠错</view>
  167.                         </view>
  168.                 </view>
  169.         </view>
  170. </template>
复制代码
 js
  1. <script>
  2.         export default {
  3.                 data() {
  4.                         return {
  5.                                 h: 0, //定义时,分,秒,毫秒并初始化为0;
  6.                                 m: 0,
  7.                                 ms: 0,
  8.                                 s: 0,
  9.                                 time: 0,
  10.                                 str: '',
  11.                                 pageIndex: 1,
  12.                                 mytime: '',
  13.                                 userFavor: false, //是否已收藏
  14.                                 currentType: 0, //当前题型
  15.                                 subjectIndex: 0, //跳转索引
  16.                                 autoShowAnswer: false, //答错是否显答案
  17.                                 autoRadioNext: true, //判断题、单项题,自动移下一题
  18.                                 swiperHeight: '800px', //
  19.                                 title: '2019山东高考原题',
  20.                                 subjectList: [
  21.                                         {
  22.                                                 "title": "水是液体?",
  23.                                                 "type": 1,
  24.                                                 "optionList": [{
  25.                                                         "id": "A",
  26.                                                         "content": "对"
  27.                                                 }, {
  28.                                                         "id": "B",
  29.                                                         "content": "错"
  30.                                                 }],
  31.                                                 "answer": "A",
  32.                                                 "userAnswer": "",
  33.                                                 "userFavor": false,
  34.                                                 "explain": "难到是固体不成?"
  35.                                         },
  36.                                         {
  37.                                                 "title": "电流分有?",
  38.                                                 "type": 2,
  39.                                                 "optionList": [{
  40.                                                         "id": "A",
  41.                                                         "content": "直流"
  42.                                                 }, {
  43.                                                         "id": "B",
  44.                                                         "content": "交流"
  45.                                                 }, {
  46.                                                         "id": "C",
  47.                                                         "content": "直流和交流"
  48.                                                 }],
  49.                                                 "answer": "C",
  50.                                                 "userAnswer": "",
  51.                                                 "userFavor": false,
  52.                                                 "explain": "科技学依据"
  53.                                         },
  54.                                         {
  55.                                                 "title": "酸菜鱼的味道?",
  56.                                                 "type": 3,
  57.                                                 "optionList": [{
  58.                                                         "id": "A",
  59.                                                         "content": "咸味"
  60.                                                 }, {
  61.                                                         "id": "B",
  62.                                                         "content": "辣味"
  63.                                                 }, {
  64.                                                         "id": "C",
  65.                                                         "content": "甜味"
  66.                                                 }, {
  67.                                                         "id": "D",
  68.                                                         "content": "酸味"
  69.                                                 }],
  70.                                                 "answer": "A,B,D",
  71.                                                 "userAnswer": "",
  72.                                                 "userFavor": false,
  73.                                                 "explain": "你怎么想都行,要的就是这个味,答案只能选A,B,D"
  74.                                         },
  75.                                         {
  76.                                                 "title": "床前(____)光,疑是地上霜。",
  77.                                                 "type": 4,
  78.                                                 "optionList": [{
  79.                                                         "id": "",
  80.                                                         "content": ""
  81.                                                 }],
  82.                                                 "answer": "明月",
  83.                                                 "userAnswer": "",
  84.                                                 "userFavor": false,
  85.                                                 "explain": "问答题没有选项,无法做答,且不参与计分"
  86.                                         },
  87.                                         {
  88.                                                 "title": "什么美国要限制华为?",
  89.                                                 "type": 5,
  90.                                                 "optionList": [{
  91.                                                         "id": "",
  92.                                                         "content": ""
  93.                                                 }],
  94.                                                 "answer": "",
  95.                                                 "userAnswer": "",
  96.                                                 "userFavor": false,
  97.                                                 "explain": "问答题没有选项,无法做答,且不参与计分"
  98.                                         }
  99.                                 ],
  100.                                 modalCard: null, //显示答题卡
  101.                                 modalError: null, //纠错卡
  102.                                 errorList: ['题目不完整', '答案不正确', '含有错别字', '图片不存在', '解析不完整', '其他错误']
  103.                         }
  104.                 },
  105.                 onReady() {
  106.                         var tempHeight = 800;
  107.                         var _me = this;
  108.                         uni.getSystemInfo({
  109.                                 //获取手机屏幕高度信息,让swiper的高度和手机屏幕一样高               
  110.                                 success: function(res) {
  111.                                         // console.log(res);                    
  112.                                         tempHeight = res.windowHeight;
  113.                                         // console.log("屏幕可用高度 " + tempHeight);
  114.                                         uni.createSelectorQuery().select("#top-box").fields({
  115.                                                 size: true,
  116.                                                 scrollOffset: true
  117.                                         }, (data) => {
  118.                                                 tempHeight -= data.height;
  119.                                                 // console.log("减掉顶部后的高度 " + tempHeight);
  120.                                                 uni.createSelectorQuery().select("#foot-box").fields({
  121.                                                         size: true,
  122.                                                         scrollOffset: true
  123.                                                 }, (data) => {
  124.                                                         tempHeight -= data.height;
  125.                                                         // console.log("减掉底部后的高度 " + tempHeight);
  126.                                                         _me.swiperHeight = tempHeight + 'px';
  127.                                                         // console.log("滑屏最后高度 " + _me.swiperHeight);
  128.                                                 }).exec();
  129.                                         }).exec();
  130.                                 }
  131.                         });
  132.                 },
  133.                 onLoad() {
  134.                         // 获取题目类型,默认显示数据的第一种题目的类型
  135.                         this.currentType = this.subjectList[0].type;
  136.                         //设置头部标题
  137.                         uni.setNavigationBarTitle({
  138.                                 title: this.title
  139.                         });
  140.                         //添加用户显示答案字段
  141.                         for (var i = 0; i < this.subjectList.length; i++) {
  142.                                 this.$set(this.subjectList[i], "showAnswer", false);
  143.                         }
  144.                         this.getTime()
  145.                 },
  146.                 methods: {
  147.                         //倒计时方法
  148.                         getTime() {
  149.                                 this.time = setInterval(this.timer, 50)
  150.                         },
  151.                         timer() { //定义计时函数
  152.                                 this.ms = this.ms + 50; //毫秒
  153.                                 if (this.ms >= 1000) {
  154.                                         this.ms = 0;
  155.                                         this.s = this.s + 1; //秒
  156.                                 }
  157.                                 if (this.s >= 60) {
  158.                                         this.s = 0;
  159.                                         this.m = this.m + 1; //分钟
  160.                                 }
  161.                                 if (this.m >= 60) {
  162.                                         this.m = 0;
  163.                                         this.h = this.h + 1; //小时
  164.                                 }
  165.                                 this.str = this.toDub(this.h) + ":" + this.toDub(this.m) + ":" + this.toDub(this.s) + "" /*+this.toDubms(this.ms)+"毫秒"*/ ;
  166.                                 // document.getElementById('mytime').innerHTML=h+"时"+m+"分"+s+"秒"+ms+"毫秒";
  167.                         },
  168.                         toDub(n) { //补0操作
  169.                                 if (n < 10) {
  170.                                         return "0" + n;
  171.                                 } else {
  172.                                         return "" + n;
  173.                                 }
  174.                         },
  175.                         //答题板和错题的显示隐藏
  176.                         showCardModal: function(e) {
  177.                                 this.modalCard = e.currentTarget.dataset.target
  178.                         },
  179.                         hideCardModal: function(e) {
  180.                                 this.modalCard = null
  181.                         },
  182.                         showErrorModal: function(e) {
  183.                                 this.modalError = e.currentTarget.dataset.target
  184.                         },
  185.                         hideErrorModal: function(e) {
  186.                                 this.modalError = null
  187.                         },
  188.                          //左右滑动事件
  189.                         SwiperChange: function(e) {
  190.                                 //获取当前滑动的索引,默认为0,所以要+1
  191.                                 console.log(e);
  192.                                 let index = e.target.current;
  193.                                 this.pageIndex = index + 1
  194.                                 if(this.pageIndex == this.subjectList.length){
  195.                                         uni.showModal({
  196.                                                 cancelText:'取消',
  197.                                                 confirmText:'确定',
  198.                                                 title:'已经是最后一题了',
  199.                                                 success: (res) => {
  200.                                                         if(res.confirm){
  201.                                                                 uni.showToast({
  202.                                                                         icon:'none',
  203.                                                                         title:'赶快交卷吧'
  204.                                                                 })
  205.                                                         }
  206.                                                 }
  207.                                         })
  208.                                 }
  209.                                 if (index != undefined) {
  210.                                         //对当前题型和题目类型重新赋值
  211.                                         this.subjectIndex = index;
  212.                                         this.currentType = this.subjectList[index].type;
  213.                                         this.userFavor = this.subjectList[index].userFavor;
  214.                                         console.log(this.subjectIndex);
  215.                                 }
  216.                         },
  217.                          //单选选中
  218.                         RadioboxChange: function(e) {
  219.                                 //获取对应题目所有的选项
  220.                                 var items = this.subjectList[this.subjectIndex].optionList;
  221.                                 //获取对应value
  222.                                 var values = e.detail.value;
  223.                                 this.subjectList[this.subjectIndex].userAnswer = values;
  224.                                 if (this.autoRadioNext && this.subjectIndex < this.subjectList.length - 1) {
  225.                                         this.subjectIndex += 1;
  226.                                 };
  227.                                 console.log(items,values);
  228.                         },
  229.                          //复选选中
  230.                         CheckboxChange: function(e) {
  231.                                 var items = this.subjectList[this.subjectIndex].optionList;
  232.                                 var values = e.detail.value;
  233.                                 this.subjectList[this.subjectIndex].userAnswer = "";
  234.                                 //先循环全部的题目选项
  235.                                 for (var i = 0, lenI = items.length; i < lenI; ++i) {
  236.                                         //在循环点击的选项
  237.                                         for (var j = 0, lenJ = values.length; j < lenJ; ++j) {
  238.                                                 if (items[i].id == values[j]) {
  239.                                                         this.subjectList[this.subjectIndex].userAnswer += items[i].id;
  240.                                                         // console.log(this.subjectList[this.subjectIndex].userAnswer);
  241.                                                         break
  242.                                                 }
  243.                                         }
  244.                                 }
  245.                         },
  246.                          //填空题
  247.                         textInput: function(e) {
  248.                                 this.subjectList[this.subjectIndex].userAnswer = e.detail.value;
  249.                         },
  250.                          //点击显示答案
  251.                         ShowAnswerChange: function(e) {
  252.                                 if (this.subjectList[this.subjectIndex].showAnswer) {
  253.                                         this.subjectList[this.subjectIndex].showAnswer = false;
  254.                                 } else {
  255.                                         this.subjectList[this.subjectIndex].showAnswer = true;
  256.                                 }
  257.                         },
  258.                          //点击收藏题
  259.                         FavorSubject: function(e) {
  260.                                 if (this.userFavor) {
  261.                                         this.userFavor = false;
  262.                                         this.subjectList[this.subjectIndex].userFavor = false;
  263.                                 } else {
  264.                                         this.userFavor = true;
  265.                                         this.subjectList[this.subjectIndex].userFavor = true;
  266.                                 }
  267.                         },
  268.                          //上一题、下一题
  269.                         MoveSubject: function(e) {
  270.                                 if (e === -1 && this.subjectIndex != 0) {
  271.                                         this.subjectIndex -= 1;
  272.                                 }
  273.                                 if (e === 1 && this.subjectIndex < this.subjectList.length - 1) {
  274.                                         this.subjectIndex += 1;
  275.                                 }
  276.                         },
  277.                          //题卡指定,跳转到指定题
  278.                         assignSubject: function(e) {
  279.                                 this.modalCard = null;
  280.                                 this.subjectIndex = e;
  281.                         },
  282.                          //提交纠错
  283.                         SubmitError: function(e) {
  284.                                 this.modalError = null;
  285.                         }
  286.                 }
  287.         }
  288. </script>
复制代码



免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 IT评测·应用市场-qidao123.com技术社区 (https://dis.qidao123.com/) Powered by Discuz! X3.4