scan.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // logs.js
  2. const util = require('../../utils/util.js')
  3. const homeApi = "https://aipush.aidsleep.cn";
  4. const token = "b74fd5754c5ef24cf600c39194abdaeb";
  5. const homeApi_empower = "https://aipush.aidsleep.cn";
  6. const token_empower = "b74fd5754c5ef24cf600c39194abdaeb";
  7. Page({
  8. data: {
  9. logs: [],
  10. canGetPhoneNumber: false,
  11. intervalId1: null,
  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. //扫码
  22. tapScan(){
  23. wx.scanCode({
  24. success: function(res) {
  25. console.log('扫码获取的参数',res);
  26. // wx.clearStorage();
  27. wx.setStorageSync('res',res);
  28. wx.setStorageSync('scanResultExpiresAt', Date.now() + 2 * 60 * 60 * 1000); // 超时时间戳,例如设置为2小时后过期
  29. if (!wx.getStorageSync('res') || !wx.getStorageSync('res').result) {
  30. wx.showModal({
  31. title: '提示',
  32. content: '请先扫描房间二维码',
  33. showCancel: false
  34. });
  35. setTimeout(function () {
  36. wx.reLaunch({
  37. url: '/pages/scan/scan',
  38. })
  39. }, 2000) // 设置延时时间,单位为毫秒
  40. } else if (wx.getStorageSync('res').result.split('|').length != 2) {
  41. wx.showModal({
  42. title: '提示',
  43. content: '房间二维码不正确,请重新扫码',
  44. showCancel: false
  45. });
  46. } else {
  47. let hotelcodeTemp = '';
  48. let roomcodeTemp = '';
  49. hotelcodeTemp = wx.getStorageSync('res').result.split('|')[0];
  50. roomcodeTemp = wx.getStorageSync('res').result.split('|')[1];
  51. console.log('hotelcodeTemp=+='+hotelcodeTemp);
  52. console.log('roomcodeTemp=+='+roomcodeTemp);
  53. const hotelEmpower = wx.getStorageSync('hotelEmpower');
  54. const roomEmpower = wx.getStorageSync('roomEmpower');
  55. console.log('hotelEmpower=+='+hotelEmpower);
  56. console.log('roomEmpower=+='+roomEmpower);
  57. console.log('roomEmpower=+='+JSON.stringify(roomEmpower));
  58. if (hotelcodeTemp != hotelEmpower || roomcodeTemp != roomEmpower) {
  59. wx.showModal({
  60. title: '提示',
  61. content: '授权配置错误,或者您没有使用权限',
  62. showCancel: false
  63. });
  64. }else{
  65. wx.reLaunch({
  66. url: '/pages/index/index?res='+res,//传res
  67. })
  68. }
  69. }
  70. }
  71. })
  72. },
  73. onLoad() {
  74. console.log("scan页面");
  75. console.log('hasAuth===data='+wx.getStorageSync('hasAuth'));
  76. if(!wx.getStorageSync('userInfo_em') || !wx.getStorageSync('unionid') || !wx.getStorageSync('openid') || !wx.getStorageSync('hasAuth')){
  77. // wx.showModal({
  78. // title: '提示',
  79. // content: '请进行授权',
  80. // showCancel: false
  81. // });
  82. wx.showToast({
  83. title: '请进行授权',
  84. icon: 'none',
  85. duration: 2000
  86. });
  87. setTimeout(function() {
  88. wx.reLaunch({
  89. url: '/pages/empower/empower',
  90. })
  91. }, 2500) // 设置延时时间,单位为毫秒
  92. }else{
  93. }
  94. },
  95. startInterval: function () {
  96. // 使用 setInterval 创建定时任务,每8秒执行一次 this.myMethod 方法
  97. var that = this;
  98. that.clearInterval();
  99. // 定时器,执行授权过期判断,每10秒执行一次
  100. this.data.intervalId1 = setInterval(() => {
  101. // 这里执行你的代码
  102. console.log('scan页面:'+Date.now());
  103. if(wx.getStorageSync('userInfo_em') && wx.getStorageSync('unionid') && wx.getStorageSync('openid') && wx.getStorageSync('hasAuth')){
  104. wx.request({
  105. url: `${homeApi_empower}/wxstatus`,
  106. data: {
  107. unionid: wx.getStorageSync('unionid'),
  108. openid: wx.getStorageSync('openid'),
  109. //phoneNumber: that.data.phoneNumber,
  110. token: token_empower,
  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 == '0' ) {
  118. wx.showToast({
  119. title: '授权已过期',
  120. icon: 'none'
  121. });
  122. wx.removeStorageSync('userInfo_em');
  123. wx.removeStorageSync('phoneNumber');
  124. wx.removeStorageSync('unionid');
  125. wx.removeStorageSync('openid');
  126. wx.removeStorageSync('hasAuth');
  127. wx.removeStorageSync('hotelEmpower');
  128. wx.removeStorageSync('roomEmpower');
  129. setTimeout(function() {
  130. wx.reLaunch({
  131. url: '/pages/empower/empower',
  132. })
  133. }, 2000) // 设置延时时间,单位为毫秒
  134. }
  135. },
  136. });
  137. }
  138. }, 10000);
  139. },
  140. clearInterval: function () {
  141. if (this.data.intervalId1) {
  142. clearInterval(this.data.intervalId1);
  143. this.data.intervalId1 = null; // 可选,但建议清除以避免潜在的错误
  144. }
  145. },
  146. onHide() {
  147. this.clearInterval();
  148. },
  149. onUnload() {
  150. this.clearInterval();
  151. },
  152. onShow: function (options) {
  153. this.startInterval();
  154. },
  155. login: function() {
  156. var that = this;
  157. wx.login({
  158. success: res => {
  159. if (res.code) {
  160. // 发送 code 到服务器换取 session_key, openid
  161. wx.request({
  162. url: 'https://yourserver.com/api/login',
  163. data: {
  164. code: res.code
  165. },
  166. success: function(loginRes) {
  167. if (loginRes.data.success) {
  168. wx.setStorageSync('session_key', loginRes.data.session_key);
  169. wx.setStorageSync('openid', loginRes.data.openid);
  170. // 可以在这里提示用户进行手机号授权
  171. this.setData({
  172. canGetPhoneNumber: true
  173. });
  174. }
  175. }
  176. });
  177. }
  178. }
  179. });
  180. },
  181. getPhoneNumber: function(e) {
  182. if (e.detail.errMsg !== "getPhoneNumber:ok") {
  183. return;
  184. }
  185. const { encryptedData, iv } = e.detail;
  186. wx.request({
  187. url: 'https://yourserver.com/api/decrypt_phone',
  188. data: {
  189. encryptedData,
  190. iv,
  191. session_key: wx.getStorageSync('session_key')
  192. },
  193. success: function(decryptRes) {
  194. if (decryptRes.data.success) {
  195. console.log('解密后的手机号:', decryptRes.data.phoneNumber);
  196. // 处理解密后的手机号
  197. }
  198. }
  199. });
  200. },
  201. // 绑定按钮点击事件
  202. bindGetPhoneNumber: function() {
  203. if (this.data.canGetPhoneNumber) {
  204. wx.getPhoneNumber({
  205. success: this.getPhoneNumber
  206. });
  207. }
  208. }
  209. })