authorizedLoginDialog.js 9.7 KB

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