authorizedLoginDialog.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. showLoading: false,
  44. encryptphone: "",
  45. phoneNumber: "",
  46. nickName: "",
  47. disableCommitBtn: true,
  48. hasUserInfo: false,
  49. wxCode: "",
  50. unionid: "",
  51. openid: "",
  52. session_key: "",
  53. },
  54. /**
  55. * 组件的方法列表
  56. */
  57. methods: {
  58. // 关闭
  59. onCloseDialog() {
  60. this.setData({ showDialog: false });
  61. },
  62. // 选择头像 后期需要上传
  63. onChooseAvatar(e) {
  64. const { avatarUrl } = e.detail
  65. this.setData({
  66. avatarUrl: avatarUrl,
  67. })
  68. this.checkHasUserInfo();
  69. },
  70. // 输入昵称
  71. onInputNameChange(e) {
  72. console.log('e.detail==' + JSON.stringify(e.detail));
  73. const nickName = e.detail.value
  74. this.setData({
  75. nickName: nickName,
  76. })
  77. this.checkHasUserInfo();
  78. },
  79. // 输入手机号
  80. onInputPhoneChange(e) {
  81. console.log('e.detail==' + JSON.stringify(e.detail));
  82. const phoneNumber = e.detail.value
  83. this.setData({
  84. phoneNumber: phoneNumber,
  85. })
  86. this.checkHasUserInfo();
  87. },
  88. // 获取手机号
  89. onGetPhoneNumber: function (e) {
  90. console.log('onGetPhoneNumber=' + JSON.stringify(e));
  91. var that = this;
  92. if (e.detail.errMsg === "getPhoneNumber:ok") {
  93. // 发送 encryptedData, iv, 和 sessionKey 到你的后台服务器
  94. wx.request({
  95. url: `${homeApi_empower}/wxtel`,
  96. method: 'POST',
  97. data: {
  98. encryptedData: e.detail.encryptedData,
  99. iv: e.detail.iv,
  100. code: that.data.wxCode,
  101. token: token_empower,
  102. },
  103. success: function (res) {
  104. // 处理服务器返回的结果
  105. console.log('手机号获取结果:', res.data);
  106. console.log('手机号获取结果:', res.data.encryptphone);
  107. console.log('手机号获取结果:', res.data.shieldphone);
  108. if (!res.data || !res.data.encryptphone || !res.data.shieldphone) {
  109. wx.showToast({
  110. title: '无法获取用户手机号',
  111. icon: 'none'
  112. });
  113. return;
  114. }
  115. console.log('e==' + JSON.stringify(e));
  116. console.log('e.detail==' + JSON.stringify(e.detail));
  117. that.setData({
  118. encryptphone: res.data.encryptphone,
  119. phoneNumber: res.data.shieldphone,
  120. })
  121. that.checkHasUserInfo();
  122. wx.setStorageSync('phoneNumber', res.data.shieldphone);
  123. }
  124. })
  125. }
  126. },
  127. checkHasUserInfo() {
  128. var disableCommitBtn = this.data.avatarUrl == defaultAvatarUrl || !this.data.phoneNumber || !this.data.nickName;
  129. this.setData({
  130. hasUserInfo: this.data.avatarUrl != defaultAvatarUrl && this.data.phoneNumber && this.data.nickName,
  131. disableCommitBtn: disableCommitBtn
  132. })
  133. },
  134. // 提交
  135. handleUserInfo: function (e) {
  136. if (!this.data.hasUserInfo) {
  137. wx.showToast({
  138. title: '请设置头像、昵称和手机号',
  139. icon: 'none'
  140. });
  141. return;
  142. }
  143. var that = this;
  144. if (e.detail.userInfo) {
  145. // 用户点击允许,获取到用户信息
  146. this.setData({
  147. showLoading:true,
  148. disableCommitBtn: true,
  149. });
  150. wx.request({
  151. url: `${homeApi_empower}/wxlogin`,
  152. data: {
  153. code: that.data.wxCode,
  154. token: token_empower,
  155. nickname: that.data.nickName,
  156. encryptphone: that.data.encryptphone
  157. },
  158. method: 'POST',
  159. success: function (loginRes) {
  160. console.log('loginRes=' + JSON.stringify(loginRes));
  161. console.log('loginRes.data=' + JSON.stringify(loginRes.data));
  162. if (loginRes.data.codes && loginRes.data.codes == 'success') {
  163. console.log('123123123');
  164. console.log('loginRes.data.back.unionid=' + loginRes.data.back.unionid);
  165. console.log('loginRes.data.back.openid=' + loginRes.data.back.openid);
  166. wx.setStorageSync('unionid', loginRes.data.back.unionid);
  167. wx.setStorageSync('openid', loginRes.data.back.openid);
  168. // 获取后台授权
  169. that.startInterval();
  170. // 可以在这里提示用户进行手机号授权
  171. that.setData({
  172. unionid: loginRes.data.back.unionid,
  173. openid: loginRes.data.back.openid,
  174. session_key: loginRes.data.back.session_key,
  175. });
  176. } else {
  177. that.setData({ showLoading: false });
  178. wx.showToast({
  179. title: '后台授权失败',
  180. icon: 'none'
  181. });
  182. }
  183. },
  184. fail: function () {
  185. that.setData({ showLoading: false });
  186. wx.showToast({
  187. title: '后台授权失败',
  188. icon: 'none'
  189. });
  190. }
  191. });
  192. } else {
  193. // 用户拒绝授权
  194. wx.showToast({
  195. title: '您拒绝了授权',
  196. icon: 'none'
  197. });
  198. // 可以选择在这里处理用户拒绝授权后的逻辑,如跳转到其他页面或显示提示信息
  199. }
  200. },
  201. // 轮询后台信息
  202. startInterval: function () {
  203. var that = this;
  204. this.data.intervalId = setInterval(() => {
  205. console.log('后台授权中...');
  206. console.log('startInterval-unionid=' + that.data.unionid);
  207. console.log('startInterval-openid=' + that.data.openid);
  208. wx.request({
  209. url: `${homeApi_empower}/wxstatus`,
  210. data: {
  211. //nickName: that.data.userInfo.nickName,
  212. unionid: wx.getStorageSync('unionid'),
  213. openid: wx.getStorageSync('openid'),
  214. //phoneNumber: that.data.phoneNumber,
  215. token: token_empower,
  216. },
  217. method: 'POST',
  218. success: function (intervalRes) {
  219. that.setData({ showLoading: false });
  220. console.log('intervalRes=' + JSON.stringify(intervalRes));
  221. console.log('intervalRes.data=' + JSON.stringify(intervalRes.data));
  222. //empower.js? [sm]:152 intervalRes.data={"st":"success","status":"0"}
  223. if (intervalRes.data && intervalRes.data.st && intervalRes.data.status && intervalRes.data.st == 'success' && intervalRes.data.status == '1') {
  224. wx.setStorageSync('hasAuth', 'hasAuth');
  225. wx.setStorageSync('hotelEmpower', intervalRes.data.hotel);
  226. wx.setStorageSync('roomEmpower', intervalRes.data.room);
  227. that.setData({
  228. disableCommitBtn: false,
  229. });
  230. wx.showToast({
  231. title: '后台授权成功',
  232. icon: 'none'
  233. });
  234. that.clearInterval();
  235. that.triggerEvent('authorizationSuccessful', true);
  236. } else {
  237. wx.showToast({
  238. title: '授权失败,请先办理入住',
  239. icon: 'none'
  240. });
  241. }
  242. },
  243. fail: function (error) {
  244. that.setData({ showLoading: false });
  245. that.clearInterval();
  246. wx.showToast({
  247. title: '后台授权失败',
  248. icon: 'none'
  249. });
  250. }
  251. });
  252. }, 3000);
  253. },
  254. clearInterval: function () {
  255. var that = this;
  256. if (this.data.intervalId) {
  257. clearInterval(that.data.intervalId);
  258. that.data.intervalId = null;
  259. }
  260. },
  261. }
  262. })