scan.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // logs.js
  2. const util = require('../../utils/util.js')
  3. Page({
  4. data: {
  5. showAuthorizedDialog: false,
  6. hasAuth: false,
  7. activeIcon: "/subpages/icons/ic_selected_radio.svg",
  8. normalIcon: "/subpages/icons/ic_radio.svg",
  9. logs: [],
  10. checkedPrivacy: false,
  11. canGetPhoneNumber: false,
  12. },
  13. onSaveExitState: function () {
  14. // wx.clearStorage();
  15. console.log("onSaveExitState=");
  16. // 返回保存的数据和超时时间(可选)
  17. return {
  18. expireTimeStamp: Date.now() + 24 * 60 * 60 * 1000 // 超时时间戳,例如设置为1天后过期
  19. };
  20. },
  21. onChangePrivacy(event) {
  22. const check = event.detail;
  23. this.setData({
  24. checkedPrivacy: check
  25. });
  26. },
  27. //扫码
  28. tapScan() {
  29. if (!this.data.hasAuth) {
  30. this.showDialog();
  31. return;
  32. }
  33. if (!this.data.checkedPrivacy) {
  34. wx.showToast({
  35. title: '请先阅读并同意《舒眠大健康用户服务协议》和《舒眠大健康隐私保护政策》',
  36. icon: 'none',
  37. duration: 2000
  38. })
  39. return;
  40. }
  41. wx.scanCode({
  42. success: function (res) {
  43. console.log('扫码获取的参数', res);
  44. if (res.result) {
  45. hotelcodeTemp = res.result.split('|')[0];
  46. roomcodeTemp = res.result.split('|')[1];
  47. // wx.clearStorage();
  48. var userInfo = wx.getStorageSync("userInfo") || {};
  49. if (hotelcodeTemp == userInfo.hotel && roomcodeTemp == userInfo.room) {
  50. wx.setStorageSync('res', res);
  51. wx.setStorageSync('scanResultExpiresAt', Date.now() + 2 * 60 * 60 * 1000); // 超时时间戳,例如设置为2小时后过期
  52. wx.reLaunch({
  53. url: '/pages/index/index?res=' + res,//传res
  54. })
  55. } else {
  56. wx.showToast({
  57. title: '暂无权限,请先办理入住',
  58. icon: 'none'
  59. });
  60. }
  61. }
  62. }
  63. })
  64. },
  65. onLoad() {
  66. console.log("scan页面");
  67. },
  68. /**
  69. * 生命周期函数--监听页面显示
  70. */
  71. onShow() {
  72. const app = getApp();
  73. app.globalData.selectedTabIndex = 4;
  74. this.getTabBar().setData({
  75. selected: 4
  76. })
  77. this.checkAuth()
  78. },
  79. showDialog() {
  80. this.setData({
  81. showAuthorizedDialog: true
  82. })
  83. },
  84. checkAuth() {
  85. var hasAuth = wx.getStorageSync("hasAuth") || false;
  86. this.setData({
  87. hasAuth: hasAuth
  88. })
  89. // if (hasAuth) {
  90. // var userInfo = wx.getStorageSync("userInfo");
  91. // this.setData({
  92. // userName: userInfo.userName || "用户登录",
  93. // avatarUrl: userInfo.avatarUrl || defaultAvatarUrl,
  94. // phoneNumber: userInfo.phoneNumber || ""
  95. // })
  96. // }
  97. return hasAuth;
  98. },
  99. authorizationSuccessful() {
  100. console.log("授权成功");
  101. this.setData({ showAuthorizedDialog: false })
  102. this.checkAuth();
  103. },
  104. login: function () {
  105. var that = this;
  106. wx.login({
  107. success: res => {
  108. if (res.code) {
  109. // 发送 code 到服务器换取 session_key, openid
  110. wx.request({
  111. url: 'https://yourserver.com/api/login',
  112. data: {
  113. code: res.code
  114. },
  115. success: function (loginRes) {
  116. if (loginRes.data.success) {
  117. wx.setStorageSync('session_key', loginRes.data.session_key);
  118. wx.setStorageSync('openid', loginRes.data.openid);
  119. // 可以在这里提示用户进行手机号授权
  120. this.setData({
  121. canGetPhoneNumber: true
  122. });
  123. }
  124. }
  125. });
  126. }
  127. }
  128. });
  129. },
  130. getPhoneNumber: function (e) {
  131. if (e.detail.errMsg !== "getPhoneNumber:ok") {
  132. return;
  133. }
  134. const { encryptedData, iv } = e.detail;
  135. wx.request({
  136. url: 'https://yourserver.com/api/decrypt_phone',
  137. data: {
  138. encryptedData,
  139. iv,
  140. session_key: wx.getStorageSync('session_key')
  141. },
  142. success: function (decryptRes) {
  143. if (decryptRes.data.success) {
  144. console.log('解密后的手机号:', decryptRes.data.phoneNumber);
  145. // 处理解密后的手机号
  146. }
  147. }
  148. });
  149. },
  150. // 绑定按钮点击事件
  151. bindGetPhoneNumber: function () {
  152. if (this.data.canGetPhoneNumber) {
  153. wx.getPhoneNumber({
  154. success: this.getPhoneNumber
  155. });
  156. }
  157. }
  158. })