scan.js 5.7 KB

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