scan.js 3.9 KB

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