scan.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // pages/scan/scan.js
  2. const app = getApp()
  3. import { userEntityRecharge } from '../../api/charge'
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. scanFunctionUse: true,
  10. scanOverShow: false,
  11. scanTimer: null,
  12. scanSuccess: true,
  13. cardNo: '',
  14. cardSecret: ''
  15. },
  16. // 用户不允许使用摄像头时触发
  17. error () {
  18. console.log('eooro');
  19. app.globalData.auth.camera = false
  20. },
  21. // 重新扫码
  22. handleRescan () {
  23. this.setData({
  24. scanOverShow: false,
  25. scanFunctionUse: true
  26. })
  27. },
  28. // 在扫码识别成功时触发,仅在 mode="scanCode" 时生效
  29. handlescancode (e) {
  30. var that = this
  31. if (that.data.scanFunctionUse) {
  32. that.setData({
  33. scanFunctionUse: false
  34. })
  35. var response = e.detail.result
  36. if (response.indexOf('|') !== -1) {
  37. var code = response.split('|')[0]
  38. var password = response.split('|')[1]
  39. that.setData({
  40. cardNo: code,
  41. cardSecret: password
  42. })
  43. that.userEntityRechargeFn()
  44. } else {
  45. wx.showModal({
  46. content: '请扫描正确的二维码',
  47. confirmColor: '#333',
  48. showCancel: false,
  49. success (res) {
  50. if (res.confirm) {
  51. that.setData({
  52. scanFunctionUse: true
  53. })
  54. }
  55. }
  56. })
  57. }
  58. }
  59. },
  60. // 实体卡充值
  61. userEntityRechargeFn () {
  62. var that = this
  63. wx.showLoading({
  64. title: '充值中...',
  65. mask: true
  66. })
  67. var data = {
  68. cardNo: that.data.cardNo,
  69. cardSecret: that.data.cardSecret
  70. }
  71. userEntityRecharge(data).then(res => {
  72. wx.hideLoading()
  73. this.setData({
  74. scanOverShow: true,
  75. scanResMsg: res.msg,
  76. scanSuccess: true
  77. })
  78. }).catch(e => {
  79. wx.hideLoading()
  80. this.setData({
  81. scanOverShow: true,
  82. scanResMsg: e,
  83. scanSuccess: false
  84. })
  85. })
  86. },
  87. /**
  88. * 生命周期函数--监听页面加载
  89. */
  90. onLoad(options) {
  91. console.log(options, 'options');
  92. },
  93. /**
  94. * 生命周期函数--监听页面初次渲染完成
  95. */
  96. onReady() {
  97. },
  98. /**
  99. * 生命周期函数--监听页面显示
  100. */
  101. onShow(option) {
  102. if (!app.globalData.auth.camera) {
  103. wx.getSetting({
  104. success (res) {
  105. console.log(res.authSetting['scope.camera'], 'getSetting')
  106. if (!res.authSetting['scope.camera']) {
  107. wx.showModal({
  108. title: '系统提示',
  109. content: '请打开相机权限',
  110. success (res) {
  111. if (res.confirm) {
  112. console.log('用户点击确定')
  113. wx.openSetting({
  114. success (res) {
  115. console.log(res, 'openSetting')
  116. app.globalData.auth.camera = res.authSetting['scope.camera']
  117. }
  118. })
  119. } else if (res.cancel) {
  120. console.log('用户点击取消')
  121. }
  122. }
  123. })
  124. }
  125. }
  126. })
  127. }
  128. },
  129. /**
  130. * 生命周期函数--监听页面隐藏
  131. */
  132. onHide() {
  133. this.setData({
  134. scanOverShow: false,
  135. scanFunctionUse: true
  136. })
  137. },
  138. /**
  139. * 生命周期函数--监听页面卸载
  140. */
  141. onUnload() {
  142. },
  143. /**
  144. * 页面相关事件处理函数--监听用户下拉动作
  145. */
  146. onPullDownRefresh() {
  147. },
  148. /**
  149. * 页面上拉触底事件的处理函数
  150. */
  151. onReachBottom() {
  152. },
  153. /**
  154. * 用户点击右上角分享
  155. */
  156. onShareAppMessage() {
  157. }
  158. })