| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- import { CACHE_USERINFO, CACHE_TOKEN, CACHE_EXPIRES_TIME } from '../../config.js'
- import Util from '../../utils/util.js'
- import { getLogo } from '../../api/api.js'
- import { login } from '../../api/user.js'
- let app = getApp()
- Component({
- properties: {
- // 当前授权窗口是否隐藏
- isHidden: {
- type: Boolean,
- value: true
- },
- // 是否自动登录
- isAuto: {
- type: Boolean,
- value: true
- },
- // 是否跳转到首页
- isGoIndex: {
- type: Boolean,
- value: true
- }
- }, // properties
- data: {
- errorCount: 0,
- errorMaxNum: 3,
- code: null, // wx login code
- canIUseGetUserProfile: false, // 判断是否为最新获取用户信息函数
- canIUseGetUserInfo: false
- }, // data
- attached() {
- this.get_logo_url()
- this.setAuthStatus()
- if (wx.getUserProfile) {
- this.setData({
- canIUseGetUserProfile: true
- })
- }
- if (wx.getUserInfo) {
- this.setData({
- canIUseGetUserInfo: true
- })
- }
- }, // attached
- methods: {
- close() {
- wx.hideLoading()
- let pages = getCurrentPages()
- let currPage = pages[pages.length - 1]
- if (this.data.isGoIndex) {
- wx.switchTab({
- url: '/pages/index/index'
- })
- } else {
- this.setData({
- isHidden: true
- })
- if (currPage && currPage.data.isHidden != undefined) {
- currPage.setData({
- isHidden: true
- })
- }
- }
- }, // close()
- get_logo_url: function () {
- var that = this
- if (wx.getStorageSync('logo_url')) {
- return this.setData({
- logo_url: wx.getStorageSync('logo_url')
- })
- }
- getLogo().then(res => {
- wx.setStorageSync('logo_url', res.data.logo_url)
- that.setData({
- logo_url: res.data.logo_url
- })
- })
- }, // get_logo_url()
- //检测登录状态并执行自动登录
- setAuthStatus() {
- var that = this
- Util.checkWxLogin()
- .then(res => {
- let pages = getCurrentPages()
- let currPage = pages[pages.length - 1]
- if (currPage && currPage.data.isHidden != undefined) {
- currPage.setData({
- isHidden: true
- })
- }
- that.triggerEvent('onLoadFun', app.globalData.userInfo)
- })
- .catch(err => {
- console.log(err)
- })
- }, // setAuthStatus()
- wxSilentLogin: function () {
- return new Promise((resolve, reject) => {
- wx.login({
- success(res) {
- resolve(res.code)
- },
- fail(err) {
- reject(err)
- }
- })
- })
- }, // wxSilentLogin
- // 获取用户信息
- agreeAndAuth() {
- let that = this
- wx.showLoading({
- title: '正在登录中'
- })
- let p1 = this.wxSilentLogin()
- let p2 = Util.wxGetUserProfile()
- Promise.all([p1, p2])
- .then(res => {
- let userInfo = res[1]
- userInfo.code = res[0]
- // console.log(userInfo);
- that.loginAuth(userInfo)
- })
- .catch(err => {
- wx.hideLoading()
- console.log('err:', err)
- })
- }, // agreeAndAuth
- // 从后台获取详细信息
- loginAuth: function (userInfo) {
- let that = this
- userInfo.spread_spid = app.globalData.spid //获取推广人ID
- userInfo.spread_code = app.globalData.code //获取推广人分享二维码ID
- userInfo.ch = app.globalData.channel // 注册渠道
- // 发送到后台
- login(userInfo)
- .then(res => {
- app.globalData.token = res.data.token
- app.globalData.isLog = true
- app.globalData.userInfo = res.data.userInfo
- app.globalData.expiresTime = res.data.expires_time
- wx.setStorageSync(CACHE_TOKEN, res.data.token)
- wx.setStorageSync(CACHE_EXPIRES_TIME, res.data.expires_time)
- wx.setStorageSync(CACHE_USERINFO, JSON.stringify(res.data.userInfo))
- if (res.data.cache_key) {
- wx.setStorage({
- key: 'cache_key',
- data: res.data.cache_key
- })
- }
- //取消登录提示
- wx.hideLoading()
- //关闭登录弹出窗口
- that.setData({
- isHidden: true,
- errorCount: 0
- })
- //执行登录完成回调
- that.triggerEvent('onLoadFun', app.globalData.userInfo)
- })
- .catch(err => {
- wx.hideLoading()
- that.data.errorCount++
- that.setData({
- errorCount: that.data.errorCount
- })
- if (that.data.errorCount >= that.data.errorMaxNum) {
- Util.Tips({
- title: err
- })
- }
- })
- }, // loginAuth()
- // 点击打开用户协议
- onTabRegular: function () {
- wx.navigateTo({
- url: `/pages/user_license/license`
- })
- } //onTabRegular
- } // methods
- })
|