scan.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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.checkwxstatus(res, hotelcodeTemp, roomcodeTemp);
  66. }
  67. }
  68. }
  69. })
  70. },
  71. onLoad() {
  72. console.log("scan页面");
  73. },
  74. /**
  75. * 生命周期函数--监听页面显示
  76. */
  77. onShow() {
  78. // const app = getApp();
  79. // app.globalData.selectedTabIndex = 4;
  80. // this.getTabBar().setData({
  81. // selected: 4
  82. // })
  83. this.checkAuth()
  84. },
  85. showDialog() {
  86. this.setData({
  87. showAuthorizedDialog: true
  88. })
  89. },
  90. checkAuth() {
  91. var hasAuth = wx.getStorageSync("hasAuth") || false;
  92. this.setData({
  93. hasAuth: hasAuth
  94. })
  95. if (hasAuth) {
  96. // var userInfo = wx.getStorageSync("userInfo");
  97. // this.setData({
  98. // userName: userInfo.userName || "用户登录",
  99. // avatarUrl: userInfo.avatarUrl || defaultAvatarUrl,
  100. // phoneNumber: userInfo.phoneNumber || ""
  101. // })
  102. // 判断扫码控制权限
  103. wx.request({
  104. url: `${homeApi_empower}/wxstatus`,
  105. data: {
  106. unionid: wx.getStorageSync('unionid'),
  107. openid: wx.getStorageSync('openid'),
  108. token: wx.getStorageSync('token'),
  109. },
  110. method: 'POST',
  111. success: function (intervalRes) {
  112. console.log('intervalRes=' + JSON.stringify(intervalRes));
  113. console.log('intervalRes.data=' + JSON.stringify(intervalRes.data));
  114. //empower.js? [sm]:152 intervalRes.data={"st":"success","status":"0"}
  115. if (intervalRes.data && intervalRes.data.st && intervalRes.data.status && intervalRes.data.st == 'success' && intervalRes.data.status == '1') {
  116. wx.setStorageSync('hasHotelAuth', true);
  117. wx.setStorageSync('hotelEmpower', intervalRes.data.hotel);
  118. wx.setStorageSync('roomEmpower', intervalRes.data.room);
  119. wx.setStorageSync('hname', intervalRes.data.hname);
  120. wx.setStorageSync('rname', intervalRes.data.rname);
  121. } else {
  122. wx.setStorageSync('hasHotelAuth', false);
  123. }
  124. },
  125. fail: function (error) {
  126. }
  127. });
  128. }
  129. return hasAuth;
  130. },
  131. // 扫码后判断是否有权限 针对特权用户
  132. checkwxstatus(res, hotelcodeTem, roomcodeTemp) {
  133. const that = this;
  134. wx.request({
  135. url: `${homeApi_empower}/wxstatus`,
  136. data: {
  137. unionid: wx.getStorageSync('unionid'),
  138. openid: wx.getStorageSync('openid'),
  139. token: wx.getStorageSync('token'),
  140. "hotel": hotelcodeTem,
  141. "room": roomcodeTemp
  142. },
  143. method: 'POST',
  144. success: function (intervalRes) {
  145. console.log('intervalRes=' + JSON.stringify(intervalRes));
  146. console.log('intervalRes.data=' + JSON.stringify(intervalRes.data));
  147. //empower.js? [sm]:152 intervalRes.data={"st":"success","status":"0"}
  148. if (intervalRes.data && intervalRes.data.st && intervalRes.data.status && intervalRes.data.st == 'success' && intervalRes.data.status == '1') {
  149. wx.setStorageSync('hasHotelAuth', true);
  150. wx.setStorageSync('hotelEmpower', intervalRes.data.hotel);
  151. wx.setStorageSync('roomEmpower', intervalRes.data.room);
  152. wx.setStorageSync('hname', intervalRes.data.hname);
  153. wx.setStorageSync('rname', intervalRes.data.rname);
  154. wx.setStorageSync('res', res);
  155. wx.setStorageSync('scanResultExpiresAt', Date.now() + 2 * 60 * 60 * 1000); // 超时时间戳,例如设置为2小时后过期
  156. wx.reLaunch({
  157. url: '/subpages/main/main?res=' + res,//传res
  158. })
  159. } else {
  160. wx.setStorageSync('hasHotelAuth', false);
  161. that.setData({
  162. showTipsA: true
  163. })
  164. }
  165. },
  166. fail: function (error) {
  167. that.setData({
  168. showTipsA: true
  169. })
  170. }
  171. });
  172. },
  173. authorizationSuccessful() {
  174. console.log("授权成功");
  175. this.setData({ showAuthorizedDialog: false })
  176. this.checkAuth();
  177. },
  178. login: function () {
  179. var that = this;
  180. wx.login({
  181. success: res => {
  182. if (res.code) {
  183. // 发送 code 到服务器换取 session_key, openid
  184. wx.request({
  185. url: 'https://yourserver.com/api/login',
  186. data: {
  187. code: res.code
  188. },
  189. success: function (loginRes) {
  190. if (loginRes.data.success) {
  191. wx.setStorageSync('session_key', loginRes.data.session_key);
  192. wx.setStorageSync('openid', loginRes.data.openid);
  193. // 可以在这里提示用户进行手机号授权
  194. this.setData({
  195. canGetPhoneNumber: true
  196. });
  197. }
  198. }
  199. });
  200. }
  201. }
  202. });
  203. },
  204. getPhoneNumber: function (e) {
  205. if (e.detail.errMsg !== "getPhoneNumber:ok") {
  206. return;
  207. }
  208. const { encryptedData, iv } = e.detail;
  209. wx.request({
  210. url: 'https://yourserver.com/api/decrypt_phone',
  211. data: {
  212. encryptedData,
  213. iv,
  214. session_key: wx.getStorageSync('session_key')
  215. },
  216. success: function (decryptRes) {
  217. if (decryptRes.data.success) {
  218. console.log('解密后的手机号:', decryptRes.data.phoneNumber);
  219. // 处理解密后的手机号
  220. }
  221. }
  222. });
  223. },
  224. // 绑定按钮点击事件
  225. bindGetPhoneNumber: function () {
  226. if (this.data.canGetPhoneNumber) {
  227. wx.getPhoneNumber({
  228. success: this.getPhoneNumber
  229. });
  230. }
  231. }
  232. })