// component/auth/auth.js const api = require('../../config/api') const util = require('../../utils/util.js'); import { getSubOpen } from './api' Component({ /** * 页面的初始数据 */ data: { show: false, canIUse: wx.canIUse('button.open-type.getUserInfo') }, attached() { this.callback = () => { this.setData({ show: !getApp().globalData.loginProps.isLogin }) let pages = getCurrentPages() let currPage = pages[pages.length - 1].route if (currPage === 'pages/discover/discover') { this.setData({ bottom: '50px' }) } else { this.setData({ bottom: 0 }) } } this.callback() getApp().addLoginListener(this.callback) getSubOpen() }, detached() { getApp().removeLoginListener(this.callback) }, methods: { quitHandle: function () { getApp().setLoginProps(true) }, getCode: function () { return new Promise(r => { wx.login({ success: function (res) { console.log(res) if (res.code) { r(res.code) } } }); }) }, authLogin() { this.triggerEvent('login') }, getUserProfile: async function (e) { // wx.getUserProfile({ // desc: '用于完善用户资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写 // success: async (userInfo_res) => { // console.log(userInfo_res.userInfo) let info = { userInfo: { } } let code = await this.getCode() //登录远程服务器 util.request(api.AuthLoginByWeixin, { code: code, userInfo: info }, 'POST', 'application/json').then(res => { if (res.errno === 0) { console.log('&&&&&&&') console.log('login-Info', res.data) //存储用户信息 res.data.userInfo.userId = res.data.userId res.data.userInfo.sessionKey = res.data.sessionKey console.log('-------', res.data.userInfo) wx.setStorageSync('userInfo', res.data.userInfo); wx.setStorageSync('token', res.data.token); wx.setStorageSync('userId', res.data.userId); wx.setStorageSync('isLogin', true); getApp().setLoginProps(true) this.authLogin() getSubOpen() } }); } } })