// pages/index/index.js const WX_REG = "http://wxapp2.qmth.com.cn/" const ZH_CN = ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '十一', '十二', '十三', '十四', '十五', '十六', '十七', '十八', '十九', '二十', '三十一', '三十二', '三十三', '三十四', '三十五', '三十六', '三十七', '三十八', '三十九', '四十', '四十一', '四十二', '四十三', '四十四', '四十五', '四十六', '四十七', '四十八', '四十九', '五十' ] const app = getApp() const API = app.globalData.api Page({ /** * 页面的初始数据 */ data: { paper: { courseId: 0, courseName: "", examRecordDataId: 0, examStudentId: 0, questionMainNumber: 0, questionOrder: 0, subNumber: 0, isTokenInValid: false }, subTitle: '', questionType: '', //AUDIO PIC enableScan: true, //录音时不允许扫描 }, secret: '', reset: function() { this.secret = '' this.setData({ paper: null, subTitle: '', questionType: '', enableScan: true, isTokenInValid: false }) }, scan: function (e) { console.log(e) if (!this.data.enableScan) { return false } var hasAnswer = false //是否有未提交的答案 var component = this.selectComponent('#audio') if (component != null && component.data.audio != '') { hasAnswer = true } if (!hasAnswer) { component = this.selectComponent('#pic') if (component != null && component.data.pictures.length > 0) { hasAnswer = true } } if (hasAnswer) { wx.showModal({ title: '提示', content: '有未提交的答案,是否放弃', showCancel: true, cancelText: '放弃', cancelColor: '#FF5252', confirmText: '取消', success(res) { if (!res.confirm) { wx.reLaunch({ url: '/pages/index/index', }) } } }) return } const that = this wx.scanCode({ onlyFromCamera: true, scanType: ['qrCode'], success(res) { console.log("res") var parameter = decodeURIComponent(res.result.replace(WX_REG, "")) console.log('parameter::' + parameter) var type = that.getQueryVariable(parameter, 'transferFileType') var host = that.getQueryVariable(parameter, 'apiServer') console.log("host::" + host); API.initBaseUrl(host); parameter = parameter.replace("&apiServer=" + host, "") if (type == 'PIC' || type == 'AUDIO') { wx.setNavigationBarTitle({ title: type == 'PIC' ? '图片上传' : '音频上传' }) that.setData({ questionType: type }) // 获取试题信息 that.secret = parameter console.log(that.secret) that.checkQrcode(that.secret, true) } else { wx.showToast({ title: '二维码有误!', icon: 'none', duration: 2500, mask: true }) } }, fail(res) { console.log(res) console.log(res != null && res.errMsg == 'scanCode:fail cancel') if (res != null && res.errMsg == 'scanCode:fail cancel') { return } wx.showToast({ title: '二维码读取异常!', icon: 'none', duration: 2500, mask: true }) } }) }, onScanEnable: function(event) { console.log('event::onScanEnable') console.log(event) this.setData({ enableScan: event.detail.enabled }) }, checkQrcode: function (code, fromScan) { if (fromScan) { wx.showLoading({ title: '正在加载', mask: true }) } const that = this try { API.checkQrcode(code) .then(function (res) { console.log(res) const title = `第${ZH_CN.length >= res.questionMainNumber ? ZH_CN[res.questionMainNumber - 1] : res.questionMainNumber}大题 第${res.subNumber}小题 ` that.setData({ paper: res, subTitle: title }) if (fromScan) { wx.hideLoading() wx.showToast({ title: '扫码成功', icon: 'success', mask: true }) } }) .catch(function (error) { console.log(error) wx.hideLoading() var errorMsg = error if ((errorMsg && errorMsg.indexOf('request:fail') == 0) || errorMsg == undefined) { errorMsg = "网络请求失败,请稍后重试" } that.showConfirmDialog(`${errorMsg}`, '确定', function () { console.log('callback') wx.reLaunch({ url: '/pages/index/index', }) }) }) } catch(e) { console.log('========+++++++++++++') } }, showConfirmDialog: function (msg, confirm, callback) { const confirmText = confirm ? confirm : '确定' wx.showModal({ title: '提示', content: msg, showCancel: false, confirmText: confirmText, success(res) { if (res.confirm) { console.log('用户点击确定') if (callback) { callback.apply() } } } }) }, getQueryVariable: function(url, variable) { var vars = url.split("&") for (var i = 0; i < vars.length; i++) { var pair = vars[i].split("=") if (pair[0] == variable) { return pair[1] } } return (false) }, tokenValidCallback: function () { if (this.data.isTokenInValid) { return; } this.setData({ isTokenInValid: true }) this.showConfirmDialog('二维码已过期,请重新扫描题目', '确定', function () { console.log('callback') wx.reLaunch({ url: '/pages/index/index' }) }) }, /** * 生命周期函数--监听页面加载 */ onLoad: function(options) { console.log(options) if (options && options.q) { const query = decodeURIComponent(options.q) /*if (query.indexOf("http") == 0) { var result = query.replace("//", '/').replace("//", '/').split('/') var baseUrl = result[0] + "//" + result[1]; API.initBaseUrl(baseUrl); this.secret = query.replace(baseUrl, '').replace('/', '') }*/ var type = this.getQueryVariable(decodeURIComponent(query), 'transferFileType') var host = this.getQueryVariable(decodeURIComponent(query), 'apiServer') console.log("host::" + host); API.initBaseUrl(host); this.secret = query.replace(host, '').replace('/', '') console.log(type) if (type == 'PIC' || type == 'AUDIO') { wx.setNavigationBarTitle({ title: type == 'PIC' ? '图片上传' : '音频上传' }) this.setData({ questionType: type }) } else { wx.showToast({ title: '二维码有误!', icon: 'none', duration: 2500, mask: true }) } } if (this.secret) { this.checkQrcode(this.secret, false) } API.setTokenValidCallback(this.tokenValidCallback) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function() { }, /** * 生命周期函数--监听页面显示 */ onShow: function() { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function() { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function() { }, /** * 用户点击右上角分享 */ onShareAppMessage: function() { } })