| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- // component/auth/auth.js
- const api = require('../../config/api')
- const util = require('../../utils/util.js');
- 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)
- },
- detached() {
- getApp().removeLoginListener(this.callback)
- },
- methods: {
- quitHandle: function () {
- getApp().setLoginProps(true)
- },
- getCode: function () {
- return new Promise(r => {
- wx.login({
- success: function (res) {
- if (res.code) {
- r(res.code)
- }
- }
- });
- })
- },
- authLogin() {
- this.triggerEvent('login')
- },
- //获取用户信息接口
- getUserProfile: function () {
- return new Promise((resovle, reject) => {
- wx.getUserProfile({
- desc: "用于完善用户资料",
- //异步请求:回调函数中调用下一个函数
- success(res) {
- resovle(res)
- },
- fail(err) {
- wx.showModal({
- title: "登录失败",
- content: "网络异常,请重试",
- })
- reject(err)
- },
- });
- })
- },
- bindGetUserInfo: async function (e) {
- // let UserProfile = await this.getUserProfile()
- let UserProfile ={}
- console.error(UserProfile)
- return new Promise(async (resolve, reject) => {
- let code = await wxLogin()
- UserProfile.code = code
- // UserProfile.userInfo.signature = UserProfile.signature
- // UserProfile.userInfo.rawData = UserProfile.rawData
- // UserProfile.userInfo.encryptedData = UserProfile.encryptedData
- console.log(UserProfile)
- util.request(api.AuthLoginByWeixin, UserProfile, 'POST', 'application/json').then(res => {
- if (res.errno === 0) {
- //存储用户信息
- res.data.userInfo.userId = res.data.userId
- 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()
- } else {
- // util.showErrorToast(res.errmsg)
- // wx.showModal({
- // title: '提示',
- // content: res.errmsg,
- // showCancel: false
- // });
- }
- });
- // wx.getUserInfo({
- // withCredentials: true,
- // success(res) {
- // console.log(res)
- // //登录远程服务器
- // util.request(api.AuthLoginByWeixin, UserProfile, 'POST', 'application/json').then(res => {
- // if (res.errno === 0) {
- // //存储用户信息
- // res.data.userInfo.userId = res.data.userId
- // 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()
- // } else {
- // // util.showErrorToast(res.errmsg)
- // // wx.showModal({
- // // title: '提示',
- // // content: res.errmsg,
- // // showCancel: false
- // // });
- // }
- // });
- // },
- // fail(err) {
- // wx.hideLoading()
- // reject(err)
- // }
- // })
- })
- // let code = await this.getCode()
- // //登录远程服务器
- // util.request(api.AuthLoginByWeixin, {
- // code: code,
- // userInfo: e.detail
- // }, 'POST', 'application/json').then(res => {
- // if (res.errno === 0) {
- // //存储用户信息
- // res.data.userInfo.userId = res.data.userId
- // 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()
- // } else {
- // // util.showErrorToast(res.errmsg)
- // // wx.showModal({
- // // title: '提示',
- // // content: res.errmsg,
- // // showCancel: false
- // // });
- // }
- // });
- },
- }
- })
- function wxLogin() {
- return new Promise((resovle, reject) => {
- wx.login({
- success(res) {
- resovle(res.code)
- },
- fail(err) {
- wx.showModal({
- title: "登录失败",
- content: "网络异常,请重试",
- })
- reject(err)
- },
- })
- })
- }
|