scan.js 4.5 KB

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