index.js 998 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // pages/login/index.js
  2. const app = getApp();
  3. const toHomePage = () => {
  4. wx.reLaunch({
  5. url: '/pages/index/index',
  6. })
  7. }
  8. Page({
  9. data: {
  10. },
  11. onShow() {
  12. wx.showLoading({
  13. title: '登录中...',
  14. })
  15. // 登录
  16. wx.login({
  17. success: res => {
  18. // 发送 res.code 到后台换取 openId, sessionKey, unionId
  19. app.$http.postForm('/user/openidLogin', {
  20. code: res.code,
  21. customerId: app.$util.customerId
  22. }).then(({
  23. status,
  24. data = {},
  25. msg
  26. }) => {
  27. if (status == 0) {
  28. const {
  29. token
  30. } = data;
  31. app.getUserInfo(token);
  32. app.isLogin = true;
  33. toHomePage();
  34. } else {
  35. app.isLogin = false;
  36. toHomePage();
  37. }
  38. }).catch(() => {
  39. toHomePage();
  40. })
  41. },
  42. fail() {
  43. toHomePage();
  44. }
  45. })
  46. },
  47. onHide() {
  48. wx.hideLoading();
  49. }
  50. })