// var appjs = require("../../app.js");
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        openid: '',
        session_key: '',
        is_charge:true
    },
    // 获取用户信息
    start(e) {
        let self = this;
        wx.checkSession({
            success: function () {
                console.log('未过期');
                //session_key 未过期,并且在本生命周期一直有效
                let url = '/wxapp/public/login';
                let params = {
                    encrypted_data: e.detail.encryptedData,
                    iv: e.detail.iv,
                    raw_data: e.detail.rawData,
                    signature: e.detail.signature,
                    openid: self.data.openid,
                    session_key: self.data.session_key,
                };
                app.post(url, params).then((ret) => {
                    wx.setStorageSync('token', ret.token);
                    app.globalData.userInfo = ret.user_type;
                    if (wx.getStorageSync('token') !== '') {
                        self.checkMobile();


                    }
                    // console.log('userInfo',app.globalData.userInfo,ret);
                })
            },
            fail: function () {
                console.log('session_key 已经失效');
                // session_key 已经失效,需要重新执行登录流程
                self.login() //重新登录
            }
        })
    },
    login() {
        let self = this;
        let url = '/wxapp/public/getSessionKey';
        wx.login({
            success: function (res) {
                // console.log(res)
                app.post(url, {
                    code: res.code
                }, {}).then((ret) => {
                    self.setData({
                        openid: ret.openid,
                        session_key: ret.session_key
                    });
                })
            },
            fail: function (res) {
            },
            complete: function (res) {
            },
        })
    },
    //判断手机号是否绑定
    checkMobile() {
        let url = '/portal/Index/check_mobile';
        let header = {
            "XX-token": wx.getStorageSync('token')
        };
        app.post(url, {}, header).then((res) => {
            console.log('start判断手机号是否绑定', res);
            if(res.is_binding) {
                app.globalData.is_bingding = true;
                console.log('is_charge',this.data.is_charge);
                if(+this.data.is_charge === 1 && this.data.is_buy === '') {
                    console.log('从兑换页进来的,再回到上一页');
                    // wx.navigateTo({url:'/pages/my/charge/charge'})
                    wx.navigateBack({delta:1})
                }else if(this.data.is_charge === '' && this.data.is_buy === ''){
                    console.log('不是从兑换页或者购买页进来的');
                    setTimeout(function () {
                        wx.switchTab({
                            url: '/pages/index/index',
                        })
                    }, 200);
                }else if(+this.data.is_buy === 1 && this.data.is_charge === '') {
                    console.log('从购买页进来的,再回到上一页');
                    wx.navigateBack({delta:1})
                }
            } else {
                app.globalData.is_bingding = false;
                setTimeout(function () {
                    wx.switchTab({
                        url: '/pages/index/index',
                    })
                }, 200);
            }

        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log('启动页参数',options);
        this.setData({is_charge: +options.is_charge?+options.is_charge:'',
            is_buy: +options.is_buy?+options.is_buy:''
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        let self = this;
        wx.showLoading({title: '加载中', mask: true});
        self.login();
        setTimeout(function () {
            wx.hideLoading()
        }, 1000);
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})