const app = getApp()
import {
    Base
} from '../../../utils/base.js';
const base = new Base();
Page({

    data: {
        canIUseGetUserProfile: false,
    },

    onLoad: function (options) {
        var that = this
        if (wx.canIUse('getUserProfile') && wx.getUserProfile) {
            that.setData({
                canIUseGetUserProfile: true
            })
        } else {
            that.setData({
                canIUseGetUserProfile: false
            })
        }
        console.log(this.data.canIUseGetUserProfile, '11111111111111')
    },
    // 授权获取用户信息
    getUserProfile() {
        wx.getUserProfile({
            desc: '用于完善会员资料',
            success: (res) => {
                // console.log("res------用于完善会员资料", res)
                if(res.errMsg == "getUserProfile:ok") {
                    app.saveUserInfo(res, this.showUserInfo)
                } else {
                    // console.log("res.errMsg------用于完善会员资料", res.errMsg)
                    base.toast(res.errMsg)
                }
            },
            fail: (err) => {
                base.toast('请先授权，才能验证身份')
            }
        })
    },
    // 旧版本 授权获得用户信息
    userInfoHandler: function (e) {
        let that = this
        if (e.detail.errMsg == 'getUserInfo:ok') {
            app.saveUserInfo(e.detail, this.showUserInfo)
        } else {
            base.toast('请先授权，才能验证身份')
        }
    },
    showUserInfo: function () {
		console.log(app.globalData.hasTel)
		if (app.globalData.hasTel) { 
			base.toast('登录成功')
			setTimeout(() => {
				if (app.globalData.backurl) {
					if (app.globalData.backurl.indexOf('/pages/tabbar/my/my') != -1 || app.globalData.backurl.indexOf('/pages/tabbar/index/index') != -1 || app.globalData.backurl.indexOf('/pages/tabbar/index/index') != -1) {
						wx.switchTab({
							url: app.globalData.backurl,
						})
					} else {
						wx.redirectTo({
							url: app.globalData.backurl
						})
					}
				} else {
					wx.switchTab({
						url: '/pages/personal/personal',
					})
				}
			}, 800)
			
		} else {
			wx.navigateTo({
				url: '/pages/bindTel/bindTel',
			})
		}
	},
})