// pages/scan/scan.js const app = getApp() import { userEntityRecharge } from '../../api/charge' Page({ /** * 页面的初始数据 */ data: { scanFunctionUse: true, scanOverShow: false, scanTimer: null, scanSuccess: true, cardNo: '', cardSecret: '' }, // 用户不允许使用摄像头时触发 error () { console.log('eooro'); app.globalData.auth.camera = false }, // 重新扫码 handleRescan () { this.setData({ scanOverShow: false, scanFunctionUse: true }) }, // 在扫码识别成功时触发,仅在 mode="scanCode" 时生效 handlescancode (e) { var that = this if (that.data.scanFunctionUse) { that.setData({ scanFunctionUse: false }) var response = e.detail.result if (response.indexOf('|') !== -1) { var code = response.split('|')[0] var password = response.split('|')[1] that.setData({ cardNo: code, cardSecret: password }) that.userEntityRechargeFn() } else { wx.showModal({ content: '请扫描正确的二维码', confirmColor: '#333', showCancel: false, success (res) { if (res.confirm) { that.setData({ scanFunctionUse: true }) } } }) } } }, // 实体卡充值 userEntityRechargeFn () { var that = this wx.showLoading({ title: '充值中...', mask: true }) var data = { cardNo: that.data.cardNo, cardSecret: that.data.cardSecret } userEntityRecharge(data).then(res => { wx.hideLoading() this.setData({ scanOverShow: true, scanResMsg: res.msg, scanSuccess: true }) }).catch(e => { wx.hideLoading() this.setData({ scanOverShow: true, scanResMsg: e, scanSuccess: false }) }) }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { console.log(options, 'options'); }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow(option) { if (!app.globalData.auth.camera) { wx.getSetting({ success (res) { console.log(res.authSetting['scope.camera'], 'getSetting') if (!res.authSetting['scope.camera']) { wx.showModal({ title: '系统提示', content: '请打开相机权限', success (res) { if (res.confirm) { console.log('用户点击确定') wx.openSetting({ success (res) { console.log(res, 'openSetting') app.globalData.auth.camera = res.authSetting['scope.camera'] } }) } else if (res.cancel) { console.log('用户点击取消') } } }) } } }) } }, /** * 生命周期函数--监听页面隐藏 */ onHide() { this.setData({ scanOverShow: false, scanFunctionUse: true }) }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { }, /** * 用户点击右上角分享 */ onShareAppMessage() { } })