index.js 967 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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 token= data;
  29. app.getUserInfo(token);
  30. app.isLogin = true;
  31. toHomePage();
  32. } else {
  33. app.isLogin = false;
  34. toHomePage();
  35. }
  36. }).catch(() => {
  37. toHomePage();
  38. })
  39. },
  40. fail() {
  41. toHomePage();
  42. }
  43. })
  44. },
  45. onHide() {
  46. wx.hideLoading();
  47. }
  48. })