scan.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. onClickLeft() {
  24. wx.navigateBack({
  25. delta: 1
  26. })
  27. },
  28. onChangePrivacy(event) {
  29. const check = event.detail;
  30. this.setData({
  31. checkedPrivacy: check
  32. });
  33. },
  34. onCloseTipsDialog() {
  35. this.setData({
  36. showTipsA: false,
  37. })
  38. },
  39. //扫码
  40. tapScan() {
  41. const that = this;
  42. if (!this.data.hasAuth) {
  43. this.showDialog();
  44. return;
  45. }
  46. if (!this.data.checkedPrivacy) {
  47. wx.showToast({
  48. title: '请先阅读并同意《舒眠大健康用户服务协议》和《舒眠大健康隐私保护政策》',
  49. icon: 'none',
  50. duration: 2000
  51. })
  52. return;
  53. }
  54. wx.scanCode({
  55. success: function (res) {
  56. console.log('扫码获取的参数', res);
  57. if (res.result) {
  58. let hotelcodeTemp = res.result.split('|')[0];
  59. let roomcodeTemp = res.result.split('|')[1];
  60. // wx.clearStorage();
  61. var hotelEmpower = wx.getStorageSync("hotelEmpower");
  62. var roomEmpower = wx.getStorageSync("roomEmpower");
  63. if (hotelcodeTemp == hotelEmpower && roomcodeTemp == roomEmpower) {
  64. wx.setStorageSync('res', res);
  65. wx.setStorageSync('scanResultExpiresAt', Date.now() + 2 * 60 * 60 * 1000); // 超时时间戳,例如设置为2小时后过期
  66. wx.reLaunch({
  67. url: '/subpages/main/main?res=' + res,//传res
  68. })
  69. } else {
  70. that.checkwxstatus(res, hotelcodeTemp, roomcodeTemp);
  71. }
  72. }
  73. }
  74. })
  75. },
  76. onLoad() {
  77. console.log("scan页面");
  78. },
  79. /**
  80. * 生命周期函数--监听页面显示
  81. */
  82. onShow() {
  83. // const app = getApp();
  84. // app.globalData.selectedTabIndex = 4;
  85. // this.getTabBar().setData({
  86. // selected: 4
  87. // })
  88. this.checkAuth()
  89. },
  90. showDialog() {
  91. this.setData({
  92. showAuthorizedDialog: true
  93. })
  94. },
  95. checkAuth() {
  96. var hasAuth = wx.getStorageSync("hasAuth") || false;
  97. this.setData({
  98. hasAuth: hasAuth
  99. })
  100. if (hasAuth) {
  101. // var userInfo = wx.getStorageSync("userInfo");
  102. // this.setData({
  103. // userName: userInfo.userName || "用户登录",
  104. // avatarUrl: userInfo.avatarUrl || defaultAvatarUrl,
  105. // phoneNumber: userInfo.phoneNumber || ""
  106. // })
  107. // 判断扫码控制权限
  108. wx.request({
  109. url: `${homeApi_empower}/wxstatus`,
  110. data: {
  111. unionid: wx.getStorageSync('unionid'),
  112. openid: wx.getStorageSync('openid'),
  113. token: wx.getStorageSync('token'),
  114. },
  115. method: 'POST',
  116. success: function (intervalRes) {
  117. console.log('intervalRes6666666666666=' + JSON.stringify(intervalRes));
  118. console.log('intervalRes.data=' + JSON.stringify(intervalRes.data));
  119. //empower.js? [sm]:152 intervalRes.data={"st":"success","status":"0"}
  120. if (intervalRes.data && intervalRes.data.st && intervalRes.data.status && intervalRes.data.st == 'success' && intervalRes.data.status == '1') {
  121. wx.setStorageSync('hasHotelAuth', true);
  122. } else {
  123. wx.setStorageSync('hasHotelAuth', false);
  124. }
  125. try {
  126. wx.setStorageSync('hotelEmpower', intervalRes?.data?.hotel);
  127. wx.setStorageSync('roomEmpower', intervalRes?.data?.room);
  128. wx.setStorageSync('hname', intervalRes?.data?.hname);
  129. wx.setStorageSync('rname', intervalRes?.data?.rname);
  130. }catch ( e){
  131. console.log(e)
  132. }
  133. },
  134. fail: function (error) {
  135. }
  136. });
  137. }
  138. return hasAuth;
  139. },
  140. // 扫码后判断是否有权限 针对特权用户
  141. checkwxstatus(res, hotelcodeTem, roomcodeTemp) {
  142. const that = this;
  143. wx.request({
  144. url: `${homeApi_empower}/wxstatus`,
  145. data: {
  146. unionid: wx.getStorageSync('unionid'),
  147. openid: wx.getStorageSync('openid'),
  148. token: wx.getStorageSync('token'),
  149. "hotel": hotelcodeTem,
  150. "room": roomcodeTemp
  151. },
  152. method: 'POST',
  153. success: function (intervalRes) {
  154. console.log('intervalRes25252252=' + JSON.stringify(intervalRes));
  155. console.log('intervalRes.data=' + JSON.stringify(intervalRes.data));
  156. //empower.js? [sm]:152 intervalRes.data={"st":"success","status":"0"}
  157. try {
  158. wx.setStorageSync('hotelEmpower', intervalRes?.data?.hotel);
  159. wx.setStorageSync('roomEmpower', intervalRes?.data?.room);
  160. wx.setStorageSync('hname', intervalRes?.data?.hname);
  161. wx.setStorageSync('rname', intervalRes?.data?.rname);
  162. }catch ( e){
  163. console.log(e)
  164. }
  165. if (intervalRes.data && intervalRes.data.st && intervalRes.data.status && intervalRes.data.st == 'success' && intervalRes.data.status == '1') {
  166. wx.setStorageSync('hasHotelAuth', true);
  167. wx.setStorageSync('res', res);
  168. wx.setStorageSync('scanResultExpiresAt', Date.now() + 2 * 60 * 60 * 1000); // 超时时间戳,例如设置为2小时后过期
  169. wx.reLaunch({
  170. url: '/subpages/main/main?res=' + res,//传res
  171. })
  172. } else {
  173. wx.setStorageSync('hasHotelAuth', false);
  174. that.setData({
  175. showTipsA: true
  176. })
  177. }
  178. },
  179. fail: function (error) {
  180. that.setData({
  181. showTipsA: true
  182. })
  183. }
  184. });
  185. },
  186. authorizationSuccessful() {
  187. console.log("授权成功");
  188. this.setData({ showAuthorizedDialog: false })
  189. this.checkAuth();
  190. },
  191. login: function () {
  192. var that = this;
  193. wx.login({
  194. success: res => {
  195. if (res.code) {
  196. // 发送 code 到服务器换取 session_key, openid
  197. wx.request({
  198. url: 'https://yourserver.com/api/login',
  199. data: {
  200. code: res.code
  201. },
  202. success: function (loginRes) {
  203. if (loginRes.data.success) {
  204. wx.setStorageSync('session_key', loginRes.data.session_key);
  205. wx.setStorageSync('openid', loginRes.data.openid);
  206. // 可以在这里提示用户进行手机号授权
  207. this.setData({
  208. canGetPhoneNumber: true
  209. });
  210. }
  211. }
  212. });
  213. }
  214. }
  215. });
  216. },
  217. getPhoneNumber: function (e) {
  218. if (e.detail.errMsg !== "getPhoneNumber:ok") {
  219. return;
  220. }
  221. const { encryptedData, iv } = e.detail;
  222. wx.request({
  223. url: 'https://yourserver.com/api/decrypt_phone',
  224. data: {
  225. encryptedData,
  226. iv,
  227. session_key: wx.getStorageSync('session_key')
  228. },
  229. success: function (decryptRes) {
  230. if (decryptRes.data.success) {
  231. console.log('解密后的手机号:', decryptRes.data.phoneNumber);
  232. // 处理解密后的手机号
  233. }
  234. }
  235. });
  236. },
  237. // 绑定按钮点击事件
  238. bindGetPhoneNumber: function () {
  239. if (this.data.canGetPhoneNumber) {
  240. wx.getPhoneNumber({
  241. success: this.getPhoneNumber
  242. });
  243. }
  244. }
  245. })