auth.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // component/auth/auth.js
  2. const api = require('../../config/api')
  3. const util = require('../../utils/util.js');
  4. Component({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. show: false,
  10. canIUse: wx.canIUse('button.open-type.getUserInfo')
  11. },
  12. attached() {
  13. this.callback = () => {
  14. console.error('aocoaozzasd', getApp().globalData.loginProps.isLogin)
  15. this.setData({show: !getApp().globalData.loginProps.isLogin})
  16. // let pages = getCurrentPages()
  17. // let currPage = pages[pages.length - 1].route
  18. // if (currPage === 'pages/discover/discover') {
  19. // this.setData({bottom: '50px'})
  20. // } else {
  21. // this.setData({bottom: 0})
  22. // }
  23. }
  24. this.callback()
  25. getApp().addLoginListener(this.callback)
  26. },
  27. detached() {
  28. getApp().removeLoginListener(this.callback)
  29. },
  30. methods: {
  31. quitHandle: function() {
  32. getApp().setLoginProps(true)
  33. },
  34. getCode: function() {
  35. return new Promise(r => {
  36. wx.login({
  37. success: function(res) {
  38. if (res.code) {
  39. r(res.code)
  40. }
  41. }
  42. });
  43. })
  44. },
  45. authLogin() {
  46. this.triggerEvent('login')
  47. },
  48. bindGetUserInfo: async function(e) {
  49. let code = await this.getCode()
  50. //登录远程服务器
  51. util.request(api.AuthLoginByWeixin, {
  52. code: code,
  53. userInfo: e.detail
  54. }, 'POST', 'application/json').then(res => {
  55. if (res.errno === 0) {
  56. //存储用户信息
  57. res.data.userInfo.userId = res.data.userId
  58. wx.setStorageSync('userInfo', res.data.userInfo);
  59. wx.setStorageSync('token', res.data.token);
  60. wx.setStorageSync('userId', res.data.userId);
  61. wx.setStorageSync('isLogin', true);
  62. getApp().setLoginProps(true)
  63. this.authLogin()
  64. } else {
  65. // util.showErrorToast(res.errmsg)
  66. // wx.showModal({
  67. // title: '提示',
  68. // content: res.errmsg,
  69. // showCancel: false
  70. // });
  71. }
  72. });
  73. },
  74. }
  75. })