authorizedLoginDialog.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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.request({
  152. url: `${homeApi_empower}/wxlogin`,
  153. data: {
  154. code: that.data.wxCode,
  155. token: token_empower,
  156. nickname: that.data.nickName,
  157. encryptphone: that.data.encryptphone
  158. },
  159. method: 'POST',
  160. success: function (loginRes) {
  161. console.log('loginRes=' + JSON.stringify(loginRes));
  162. console.log('loginRes.data=' + JSON.stringify(loginRes.data));
  163. if (loginRes.data.codes && loginRes.data.codes == 'success') {
  164. console.log('123123123');
  165. console.log('loginRes.data.back.unionid=' + loginRes.data.back.unionid);
  166. console.log('loginRes.data.back.openid=' + loginRes.data.back.openid);
  167. wx.setStorageSync('unionid', loginRes.data.back.unionid);
  168. wx.setStorageSync('openid', loginRes.data.back.openid);
  169. // 获取后台授权
  170. that.startInterval();
  171. // 可以在这里提示用户进行手机号授权
  172. that.setData({
  173. unionid: loginRes.data.back.unionid,
  174. openid: loginRes.data.back.openid,
  175. session_key: loginRes.data.back.session_key,
  176. });
  177. } else {
  178. that.setData({ showLoading: false });
  179. wx.showToast({
  180. title: '后台授权失败',
  181. icon: 'none'
  182. });
  183. }
  184. },
  185. fail: function () {
  186. that.setData({ showLoading: false });
  187. wx.showToast({
  188. title: '后台授权失败',
  189. icon: 'none'
  190. });
  191. }
  192. });
  193. } else {
  194. // 用户拒绝授权
  195. wx.showToast({
  196. title: '您拒绝了授权',
  197. icon: 'none'
  198. });
  199. // 可以选择在这里处理用户拒绝授权后的逻辑,如跳转到其他页面或显示提示信息
  200. }
  201. },
  202. // 轮询后台信息
  203. startInterval: function () {
  204. var that = this;
  205. this.data.intervalId = setInterval(() => {
  206. console.log('后台授权中...');
  207. console.log('startInterval-unionid=' + that.data.unionid);
  208. console.log('startInterval-openid=' + that.data.openid);
  209. wx.request({
  210. url: `${homeApi_empower}/wxstatus`,
  211. data: {
  212. //nickName: that.data.userInfo.nickName,
  213. unionid: wx.getStorageSync('unionid'),
  214. openid: wx.getStorageSync('openid'),
  215. //phoneNumber: that.data.phoneNumber,
  216. token: token_empower,
  217. },
  218. method: 'POST',
  219. success: function (intervalRes) {
  220. that.setData({ showLoading: false });
  221. console.log('intervalRes=' + JSON.stringify(intervalRes));
  222. console.log('intervalRes.data=' + JSON.stringify(intervalRes.data));
  223. //empower.js? [sm]:152 intervalRes.data={"st":"success","status":"0"}
  224. if (intervalRes.data && intervalRes.data.st && intervalRes.data.status && intervalRes.data.st == 'success' && intervalRes.data.status == '1') {
  225. wx.setStorageSync('hasAuth', true);
  226. wx.setStorageSync('hotelEmpower', intervalRes.data.hotel);
  227. wx.setStorageSync('roomEmpower', intervalRes.data.room);
  228. wx.setStorageSync('userInfo', {
  229. hotel: intervalRes.data.hotel,
  230. room: intervalRes.data.room,
  231. unionid: that.data.unionid,
  232. openid: that.data.openid,
  233. session_key: that.data.session_key,
  234. nickName: that.data.nickName,
  235. avatarUrl: that.data.avatarUrl,
  236. encryptphone: that.data.encryptphone,
  237. phoneNumber: that.data.phoneNumber
  238. })
  239. that.setData({
  240. disableCommitBtn: false,
  241. });
  242. wx.showToast({
  243. title: '后台授权成功',
  244. icon: 'none'
  245. });
  246. that.clearInterval();
  247. that.triggerEvent('authorizationSuccessful', true);
  248. } else {
  249. wx.showToast({
  250. title: '授权失败,请先办理入住',
  251. icon: 'none'
  252. });
  253. }
  254. },
  255. fail: function (error) {
  256. that.setData({ showLoading: false });
  257. that.clearInterval();
  258. wx.showToast({
  259. title: '后台授权失败',
  260. icon: 'none'
  261. });
  262. }
  263. });
  264. }, 3000);
  265. },
  266. clearInterval: function () {
  267. var that = this;
  268. if (this.data.intervalId) {
  269. clearInterval(that.data.intervalId);
  270. that.data.intervalId = null;
  271. }
  272. },
  273. }
  274. })