question.js 13.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
const app = getApp()
const innerAudioContext = wx.createInnerAudioContext();
var Interval;
Page({

    /**
     * 页面的初始数据阿萨d
     */
    data: {
        id: '',
        nav: -1,
        previous_num:0,//点击上一题的数量
        list: '', //第一道题
        clockData: {}, //所有题型
        curent: '', //当前题号
        countdown: '', //倒计时
        player: false, //语音
        show: false, //解析
        analysis: '',
        sss: '', //时
        mmm: '', //分
        hhh: "", //秒
        media:'',
        error_media:'',
        previous:0,
        // 用于重置播放组件
        Reset:true,
    },
    onLoad: function (options) {
        console.log(options)
        this.setData({
            id: options.id
        })
        this.getlist()
        //获取回答正确语音
        app.post("common/correct_media", {}).then(res => {
            this.setData({
                media: 'http://english.brofirst.cn' + res.data.media,
                error_media: 'http://english.brofirst.cn' +res.data.error_media
            })
        })

        wx.enableAlertBeforeUnload({
            message: "你确定退出当前任务?",
            success: function (res) {
              console.log("方法注册成功:", res);
            },
            fail: function (errMsg) {
              console.log("方法注册失败:", errMsg);
            },
        });
    },

    onShow: function () {
        var that = this
        // that.getlist()
        let time = this.data.hhh + ':' + this.data.mmm + ':' + this.data.sss
        if(time != '::'){
            this.countDown(time)
        }
    },
    previewImage(e){
      var img = e.currentTarget.dataset.img;
      wx.previewImage({
        current: img, // 当前显示图片的http链接
        urls: [img]
      })
    },
    time() {
        // 倒计时
        var that = this
        var countDown_time = that.data.clockData.Limit;
        // var countDown_time = 110;
        // 分钟转化为时分秒
        var h = parseInt(countDown_time / 60)
        var m = parseInt(countDown_time % 60);
        var s = parseInt(m / 60);
        // console.log(h)
        // console.log(m)
        // console.log(s)
        // 小于10的时候
        var a = "" + parseInt(h);
        if (a < 10) {
            a = '0' + a;
        }
        var b = "" + parseInt(m);
        if (b < 10) {
            b = '0' + b;
        }
        var c = "" + parseInt(s);
        if (c < 10) {
            c = '0' + c;
        }
        let time = a + ':' + b + ':' + c

        this.countDown(time)
    },
    // 倒计时
    countDown: function (countDown_time) {
        var that = this;
        var time = countDown_time.split(':')
        var hhh = parseInt(time[0])
        var mmm = parseInt(time[1])
        var sss = parseInt(time[2])
        this.setData({
            sss: (sss < 10) ? '0' + sss : sss,
            mmm: (mmm < 10) ? '0' + mmm : mmm,
            hhh: (hhh < 10) ? '0' + hhh : hhh
        })
        Interval = setInterval(function () {
            if (sss > 0) {
                sss--
            } else {

                if (hhh * 1 == 0 && mmm * 1 == 0 && sss * 1 == 0) {
                    clearInterval(Interval)
                    that.gosubmit()

                }
            }
            if (sss == 0) {
                if (mmm > 0) {
                    mmm--
                    sss = 59;
                }
                if (mmm == 0 && hhh > 0) {
                    hhh--
                    sss = 59;
                    mmm = 59;
                }
            }

            that.setData({
                sss: (sss < 10) ? '0' + sss : sss,
                mmm: (mmm < 10) ? '0' + mmm : mmm,
                hhh: (hhh < 10) ? '0' + hhh : hhh
            })
        }, 1000)
    },
    // 获取题目 
    getlist() {
        var that = this;
        app.post("evaluation/EvaluationQuestion", {
            evaluation_id: that.data.id
        }).then(res => {
           
            let list = res.data.List
            //调试
            // var obj = JSON.parse(JSON.stringify(list))
            // var data = res.data.List.concat(obj)
            // res.data.List = data
            // res.data.Count = data.length
            //调试
            list.forEach(item => {
                item.UserChoose = ''
            })
            // console.log(data)
            if (res.code == 1) {
                res.data.List[0].option.forEach((val) => {
                    val.image_select = false
                })
                that.setData({
                    list: res.data.List[0], //第一道题
                    clockData: res.data, //所有题型
                    curent: 1
                })
                
                let time = this.data.hhh + ':' + this.data.mmm + ':' + this.data.sss
               
                if(time == '::'){
                    that.time()
                }
            }
        }).catch(err => {

        })
    },

    // 选择答案
    bind_choose(e) {
        let Index = e.currentTarget.dataset.index
        let answer = e.currentTarget.dataset.answer
        // e.currentTarget.dataset.evaluation_subject_id
        if(this.data.nav != -1  && this.data.previous_num != 0){
            wx.showToast({
                title: '答案不可修改',
                icon: 'none'
            })
            return 
        }
        this.setData({
            nav: Index
        })
        let listindex = this.data.curent
        let list = this.data.clockData.List

        list.forEach((item, index) => {
            if (this.data.curent - 1 == index) {
                item.UserChoose = answer
                item.answer_index = Index
               
            }
        })

        this.setData({
            clockData: this.data.clockData
        })

    },
    // 
    PlayState() {
        if (this.selectComponent("#audio")) {
            this.selectComponent("#audio").changePlayState()
        }
    },
    previous(){
        
        var curent = this.data.curent * 1 - 1
        var previous = this.data.previous
        previous++
        var previous_num = this.data.previous_num
        previous_num++
        this.audioPause() //暂停后台播放的音乐
        this.setData({
          list: [],
      })
        this.setData({
            previous:previous,
            nav:  this.data.clockData.List[curent - 1].answer_index,
            curent: curent,
            list: this.data.clockData.List[curent - 1],
            previous_num:previous_num
        })
    },
    // 下一题 提交
    nextClick() {
        var previous = this.data.previous
        var previous_num = this.data.previous_num
      
       if(previous_num != 0){
          previous_num--
          this.setData({
            previous_num:previous_num
        })
       }
        var _this = this;
        if (this.data.list.UserChoose == '') {
            wx.showToast({
                title: '请选择答案',
                icon: 'none'
            })
            return
        }
        this.audioPause() //暂停后台播放的音乐
        innerAudioContext.stop()//选项答案
        if (_this.data.curent < _this.data.clockData.Count) {
            var curent = _this.data.curent * 1 + 1
            if(_this.data.clockData.List[curent - 1].answer_index != '' && _this.data.clockData.List[curent - 1].answer_index != undefined){
                _this.setData({
                    list: [],
                    current: 0
                })
                _this.setData({
                    nav:_this.data.clockData.List[curent - 1].answer_index,
                    curent: curent,
                    list: _this.data.clockData.List[curent - 1]
                })
            }else{
                if(previous == 0){
                    if(_this.data.list.option[this.data.list.answer_index].daan == 1){
                        // innerAudioContext.src = this.data.media;
                        // innerAudioContext.play();
                        // wx.showToast({
                        //   title: '你答对了',
                        //   icon:"none",
                        //   duration:30000,
                        //   mask:true,
                        // })
                    }else{
                        // innerAudioContext.src = this.data.error_media;
                        // innerAudioContext.play();
                        // wx.showToast({
                        //     title: '你答错了',
                        //     icon:"none",
                        //     mask:true,
                        //     duration:30000,
                        // })
                    }
                    // innerAudioContext.onEnded(function(){
                    //     wx.hideToast({})
                        _this.setData({
                          list: [],
                        })
                        _this.setData({
                            previous:previous,
                            current: 0,
                            nav: -1,
                            curent: curent,
                            list: _this.data.clockData.List[curent - 1]
                        })
                    // })
                }else{
                    if(previous != 0){
                        previous--
                    }
                    _this.setData({
                      list: [],
                    })
                    _this.setData({
                        previous:previous,
                        current: 0,
                        nav: -1,
                        curent: curent,
                        list: _this.data.clockData.List[curent - 1]
                    })
                }
               
            }
        } else {
            if(_this.data.list.option[this.data.list.answer_index].daan == 1){
                // innerAudioContext.src = this.data.media;
                // innerAudioContext.play();
                // wx.showToast({
                //   title: '你答对了',
                //   icon:"none",
                //   duration:30000,
                //   mask:true,
                // })
            }else{
                // innerAudioContext.src = this.data.error_media;
                // innerAudioContext.play();
                // wx.showToast({
                //     title: '你答错了',
                //     icon:"none",
                //     mask:true,
                //     duration:30000,
                // })
            }
            // innerAudioContext.onEnded(function(){
            //     wx.hideToast({})
                _this.setData({
                    list: [],
                    current: 0
                })
                _this.gosubmit()
            // })
        }
    },
    //交卷
    gosubmit() {
        var that = this;
        app.post("evaluation/EvaluationTask", {
            evaluation_id: that.data.id,
            json: JSON.stringify(that.data.clockData)

        }).then(res => {

            if (res.code == 1) {
                let analysis = res.data

                that.setData({
                    show: true,
                    analysis: analysis
                })
            } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'none'
                })
            }
        }).catch(err => {

        })

    },



    //播放单词
    goAudio(e) {
        // console.log(e, '4444')
        // this.setData({
        //     player: !this.data.player
        // })

        // if (this.data.player) {
        let audio = e.currentTarget.dataset.audio
        let index = e.currentTarget.dataset.index
        var list = this.data.list
        
        if(list.option[index].image_select == false){
            innerAudioContext.stop()
            list.option.forEach((val) => {
                val.image_select = false
            })
            list.option[index].image_select = true
            this.setData({
                list:list,
            })
            innerAudioContext.src = audio;
            innerAudioContext.play();
    
            innerAudioContext.onPlay(() => {
                console.log('开始播放');
            });
            innerAudioContext.onError((res) => {
                console.log(res.errMsg);
                console.log(res.errCode);
            });
        }else{
            list.option.forEach((val) => {
                val.image_select = false
            })
            this.setData({
                list:list,
            })
            innerAudioContext.stop()
        }
        
      
        // } else {
        //     console.log('停止');
        //     innerAudioContext.stop()

        // }

    },


    // 测评分析 
    gototram() {
        let data = JSON.stringify(this.data.analysis)
        wx.setStorageSync('gototram', data)
        wx.navigateTo({
            url: '/pages/quest-5/quest-5?data=' + data,
        })

    },
    // 播放音频
    // PlayState() {
    //     this.selectComponent("#audio").changePlayState()
    // },

    onReady: function () {

    },




    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
        this.audioPause() //暂停后台播放的音乐
        innerAudioContext.stop()
        clearInterval(Interval)
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

        this.audioPause() //暂停后台播放的音乐
        innerAudioContext.stop()
        clearInterval(Interval)
    },
    
    audioPause(){
        if(this.selectComponent("#audio")){
          this.selectComponent("#audio").audioPause()
        }
      },
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})