authorizedLoginDialog.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. // componets/authorized-login-dialog/authorizedLoginDialog.js
  2. const defaultAvatarUrl = "../../static/images/no-avatar.png"
  3. const homeApi_empower = "https://aipush.aidsleep.cn";
  4. const token_empower = "b74fd5754c5ef24cf600c39194abdaeb";
  5. Component({
  6. /**
  7. * 组件的属性列表
  8. */
  9. properties: {
  10. showDialog: {
  11. type: Boolean,
  12. value: false,
  13. observer(newVal, oldVal, changePath) {
  14. console.log(newVal, oldVal, changePath)
  15. const that = this;
  16. if (newVal) {
  17. console.log("开始登录");
  18. wx.login({
  19. success: res => {
  20. if (res.code) {
  21. // 发送 code 到服务器换取 session_key, openid
  22. console.log('res.code==' + res.code);
  23. that.setData({ wxCode: res.code })
  24. }
  25. }
  26. });
  27. } else {
  28. console.log('that.data.wxCode==' + that.data.wxCode);
  29. }
  30. }
  31. }
  32. },
  33. attached() {
  34. },
  35. /**
  36. * 组件的初始数据
  37. */
  38. data: {
  39. intervalId: null,
  40. canIUseGetUserProfile: wx.canIUse('getUserProfile'),
  41. canIUseNicknameComp: wx.canIUse('input.type.nickname'),
  42. avatarUrl: defaultAvatarUrl,
  43. encryptphone: "",
  44. phoneNumber: "",
  45. nickName: "",
  46. disableCommitBtn: false,
  47. hasUserInfo: false,
  48. wxCode: "",
  49. unionid: "",
  50. openid: "",
  51. session_key: "",
  52. },
  53. /**
  54. * 组件的方法列表
  55. */
  56. methods: {
  57. onCloseDialog() {
  58. this.setData({ showDialog: false });
  59. },
  60. onChooseAvatar(e) {
  61. const { avatarUrl } = e.detail
  62. this.setData({
  63. avatarUrl: avatarUrl,
  64. })
  65. },
  66. onInputNameChange(e) {
  67. console.log('e.detail==' + JSON.stringify(e.detail));
  68. const nickName = e.detail.value
  69. this.setData({
  70. nickName: nickName,
  71. })
  72. },
  73. onInputPhoneChange(e) {
  74. console.log('e.detail==' + JSON.stringify(e.detail));
  75. const phoneNumber = e.detail.value
  76. this.setData({
  77. phoneNumber: phoneNumber,
  78. })
  79. },
  80. onGetPhoneNumber: function (e) {
  81. console.log('onGetPhoneNumber=' + JSON.stringify(e));
  82. var that = this;
  83. if (e.detail.errMsg === "getPhoneNumber:ok") {
  84. // 发送 encryptedData, iv, 和 sessionKey 到你的后台服务器
  85. wx.request({
  86. url: `${homeApi_empower}/wxtel`,
  87. method: 'POST',
  88. data: {
  89. encryptedData: e.detail.encryptedData,
  90. iv: e.detail.iv,
  91. code: that.data.wxCode,
  92. token: token_empower,
  93. },
  94. success: function (res) {
  95. // 处理服务器返回的结果
  96. console.log('手机号获取结果:', res.data);
  97. console.log('手机号获取结果:', res.data.encryptphone);
  98. console.log('手机号获取结果:', res.data.shieldphone);
  99. if (!res.data || !res.data.encryptphone || !res.data.shieldphone) {
  100. wx.showToast({
  101. title: '无法获取用户手机号',
  102. icon: 'none'
  103. });
  104. return;
  105. }
  106. console.log('e==' + JSON.stringify(e));
  107. console.log('e.detail==' + JSON.stringify(e.detail));
  108. that.setData({
  109. encryptphone: res.data.encryptphone,
  110. phoneNumber: res.data.shieldphone,
  111. })
  112. wx.setStorageSync('phoneNumber', res.data.shieldphone);
  113. }
  114. })
  115. }
  116. },
  117. handleUserInfo: function (e) {
  118. if (!this.data.hasUserInfo) {
  119. wx.showToast({
  120. title: '请设置头像、昵称和手机号',
  121. icon: 'none'
  122. });
  123. return;
  124. }
  125. var that = this;
  126. if (e.detail.userInfo) {
  127. // 用户点击允许,获取到用户信息
  128. this.setData({
  129. disableCommitBtn: true,
  130. });
  131. wx.showLoading()
  132. wx.request({
  133. url: `${homeApi_empower}/wxlogin`,
  134. data: {
  135. code: that.data.wxCode,
  136. token: token_empower,
  137. nickname: that.data.userInfo.nickName,
  138. encryptphone: that.data.encryptphone
  139. },
  140. method: 'POST',
  141. success: function (loginRes) {
  142. console.log('loginRes=' + JSON.stringify(loginRes));
  143. console.log('loginRes.data=' + JSON.stringify(loginRes.data));
  144. if (loginRes.data.codes && loginRes.data.codes == 'success') {
  145. console.log('123123123');
  146. console.log('loginRes.data.back.unionid=' + loginRes.data.back.unionid);
  147. console.log('loginRes.data.back.openid=' + loginRes.data.back.openid);
  148. wx.setStorageSync('unionid', loginRes.data.back.unionid);
  149. wx.setStorageSync('openid', loginRes.data.back.openid);
  150. // 获取后台授权
  151. that.startInterval();
  152. // 可以在这里提示用户进行手机号授权
  153. that.setData({
  154. unionid: loginRes.data.back.unionid,
  155. openid: loginRes.data.back.openid,
  156. session_key: loginRes.data.back.session_key,
  157. });
  158. }
  159. },
  160. fail: function () {
  161. wx.hideLoading()
  162. wx.showToast({
  163. title: '后台授权失败',
  164. icon: 'none'
  165. });
  166. }
  167. });
  168. } else {
  169. // 用户拒绝授权
  170. wx.showToast({
  171. title: '您拒绝了授权',
  172. icon: 'none'
  173. });
  174. // 可以选择在这里处理用户拒绝授权后的逻辑,如跳转到其他页面或显示提示信息
  175. }
  176. },
  177. startInterval: function () {
  178. var that = this;
  179. this.data.intervalId = setInterval(() => {
  180. console.log('后台授权中...');
  181. console.log('startInterval-unionid=' + that.data.unionid);
  182. console.log('startInterval-openid=' + that.data.openid);
  183. wx.request({
  184. url: `${homeApi_empower}/wxstatus`,
  185. data: {
  186. //nickName: that.data.userInfo.nickName,
  187. unionid: wx.getStorageSync('unionid'),
  188. openid: wx.getStorageSync('openid'),
  189. //phoneNumber: that.data.phoneNumber,
  190. token: token_empower,
  191. },
  192. method: 'POST',
  193. success: function (intervalRes) {
  194. wx.hideLoading()
  195. console.log('intervalRes=' + JSON.stringify(intervalRes));
  196. console.log('intervalRes.data=' + JSON.stringify(intervalRes.data));
  197. //empower.js? [sm]:152 intervalRes.data={"st":"success","status":"0"}
  198. if (intervalRes.data && intervalRes.data.st && intervalRes.data.status && intervalRes.data.st == 'success' && intervalRes.data.status == '1') {
  199. wx.setStorageSync('hasAuth', 'hasAuth');
  200. wx.setStorageSync('hotelEmpower', intervalRes.data.hotel);
  201. wx.setStorageSync('roomEmpower', intervalRes.data.room);
  202. that.setData({
  203. disableCommitBtn: false,
  204. });
  205. wx.showToast({
  206. title: '后台授权成功',
  207. icon: 'none'
  208. });
  209. that.clearInterval();
  210. that.triggerEvent('authorizationSuccessful', true);
  211. } else {
  212. wx.showToast({
  213. title: '授权失败,请先办理入住',
  214. icon: 'none'
  215. });
  216. }
  217. },
  218. fail: function (error) {
  219. wx.hideLoading()
  220. that.clearInterval();
  221. wx.showToast({
  222. title: '后台授权失败',
  223. icon: 'none'
  224. });
  225. }
  226. });
  227. }, 3000);
  228. },
  229. clearInterval: function () {
  230. var that = this;
  231. if (this.data.intervalId) {
  232. clearInterval(that.data.intervalId);
  233. that.data.intervalId = null;
  234. }
  235. },
  236. }
  237. })