authorizedLoginDialog.js 9.6 KB

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