auth.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // component/auth/auth.js
  2. const api = require('../../config/api')
  3. const util = require('../../utils/util.js');
  4. import {
  5. getSubOpen
  6. } from './api'
  7. Component({
  8. /**
  9. * 页面的初始数据
  10. */
  11. data: {
  12. show: false,
  13. canIUse: wx.canIUse('button.open-type.getUserInfo')
  14. },
  15. attached() {
  16. this.callback = () => {
  17. this.setData({
  18. show: !getApp().globalData.loginProps.isLogin
  19. })
  20. let pages = getCurrentPages()
  21. let currPage = pages[pages.length - 1].route
  22. if (currPage === 'pages/discover/discover') {
  23. this.setData({
  24. bottom: '50px'
  25. })
  26. } else {
  27. this.setData({
  28. bottom: 0
  29. })
  30. }
  31. }
  32. this.callback()
  33. getApp().addLoginListener(this.callback)
  34. getSubOpen()
  35. },
  36. detached() {
  37. getApp().removeLoginListener(this.callback)
  38. },
  39. methods: {
  40. quitHandle: function () {
  41. getApp().setLoginProps(true)
  42. },
  43. getCode: function () {
  44. return new Promise(r => {
  45. wx.login({
  46. success: function (res) {
  47. console.log(res)
  48. if (res.code) {
  49. r(res.code)
  50. }
  51. }
  52. });
  53. })
  54. },
  55. authLogin() {
  56. this.triggerEvent('login')
  57. },
  58. getUserProfile: async function (e) {
  59. // wx.getUserProfile({
  60. // desc: '用于完善用户资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  61. // success: async (userInfo_res) => {
  62. // console.log(userInfo_res.userInfo)
  63. let info = {
  64. userInfo: {
  65. }
  66. }
  67. let code = await this.getCode()
  68. //登录远程服务器
  69. util.request(api.AuthLoginByWeixin, {
  70. code: code,
  71. userInfo: info
  72. }, 'POST', 'application/json').then(res => {
  73. if (res.errno === 0) {
  74. console.log('&&&&&&&')
  75. console.log('login-Info', res.data)
  76. //存储用户信息
  77. res.data.userInfo.userId = res.data.userId
  78. res.data.userInfo.sessionKey = res.data.sessionKey
  79. console.log('-------', res.data.userInfo)
  80. wx.setStorageSync('userInfo', res.data.userInfo);
  81. wx.setStorageSync('token', res.data.token);
  82. wx.setStorageSync('userId', res.data.userId);
  83. wx.setStorageSync('isLogin', true);
  84. getApp().setLoginProps(true)
  85. this.authLogin()
  86. getSubOpen()
  87. }
  88. });
  89. }
  90. }
  91. })