scan.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // pages/scan/scan.js
  2. const app = getApp()
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. scanFunctionUse: true,
  9. show: false,
  10. scanTimer: null,
  11. rescanTimer: null
  12. },
  13. // 用户不允许使用摄像头时触发
  14. error () {
  15. console.log('eooro');
  16. app.globalData.auth.camera = false
  17. },
  18. // 在扫码识别成功时触发,仅在 mode="scanCode" 时生效
  19. handlescancode (e) {
  20. if (this.data.scanFunctionUse) {
  21. this.setData({
  22. scanFunctionUse: false,
  23. show: true
  24. })
  25. console.log('扫码识别成功');
  26. }
  27. },
  28. // 重新扫码
  29. handleRescan () {
  30. this.setData({
  31. show: false
  32. })
  33. this.data.rescanTimer = setTimeout(() => {
  34. this.setData({
  35. scanFunctionUse: true
  36. })
  37. clearTimeout(this.data.rescanTimer)
  38. }, 800);
  39. },
  40. /**
  41. * 生命周期函数--监听页面加载
  42. */
  43. onLoad(options) {
  44. console.log(options, 'options');
  45. },
  46. /**
  47. * 生命周期函数--监听页面初次渲染完成
  48. */
  49. onReady() {
  50. },
  51. /**
  52. * 生命周期函数--监听页面显示
  53. */
  54. onShow(option) {
  55. if (!app.globalData.auth.camera) {
  56. wx.getSetting({
  57. success (res) {
  58. console.log(res.authSetting['scope.camera'], 'getSetting')
  59. if (!res.authSetting['scope.camera']) {
  60. wx.showModal({
  61. title: '系统提示',
  62. content: '请打开相机权限',
  63. success (res) {
  64. if (res.confirm) {
  65. console.log('用户点击确定')
  66. wx.openSetting({
  67. success (res) {
  68. console.log(res, 'openSetting')
  69. app.globalData.auth.camera = res.authSetting['scope.camera']
  70. }
  71. })
  72. } else if (res.cancel) {
  73. console.log('用户点击取消')
  74. }
  75. }
  76. })
  77. }
  78. }
  79. })
  80. }
  81. },
  82. /**
  83. * 生命周期函数--监听页面隐藏
  84. */
  85. onHide() {
  86. this.setData({
  87. show: false,
  88. scanFunctionUse: true
  89. })
  90. },
  91. /**
  92. * 生命周期函数--监听页面卸载
  93. */
  94. onUnload() {
  95. },
  96. /**
  97. * 页面相关事件处理函数--监听用户下拉动作
  98. */
  99. onPullDownRefresh() {
  100. },
  101. /**
  102. * 页面上拉触底事件的处理函数
  103. */
  104. onReachBottom() {
  105. },
  106. /**
  107. * 用户点击右上角分享
  108. */
  109. onShareAppMessage() {
  110. }
  111. })