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

    /**
     * 页面的初始数据
     */
    data: {
        openid: '',
        session_key: '',
    },
    // 获取用户信息
    start(e) {
        let that = this;
        console.log('点击');
        // wx.redirectTo({
        //     url: '/pages/index/index',
        // })
        let url = '/wxapp/public/login';
        // console.log(that.data.openid)
        let params = {
            encrypted_data: e.detail.encryptedData,
            iv: e.detail.iv,
            raw_data: e.detail.rawData,
            signature: e.detail.signature,
            openid: that.data.openid,
            session_key: that.data.session_key,
        }
        app.post(url, params).then((ret) => {
            wx.setStorageSync('token', ret.token)
            app.globalData.userInfo = ret.user_type
            // console.log('userInfo',app.globalData.userInfo,ret);
            if (ret.user_type == 2) {
                wx.switchTab({
                    url: '/pages/index/index',
                })
            }
        })
    },
    example() {
        app.post(url, {
            id: 1
        }, {}).then((res) => {
            this.setData({
                code: res
            })
        }).catch((err) => {
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let that = this
        let url = '/wxapp/public/getSessionKey'
        wx.login({
            success: function (res) {
                console.log(res)
                app.post(url, {
                    code: res.code
                }, {}).then((ret) => {
                    console.log('res',ret);
                    that.setData({
                        openid: ret.openid,
                        session_key: ret.session_key
                    })
                })
            },
            fail: function (res) {
            },
            complete: function (res) {
            },
        })
    },

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

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

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

    },

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

    },

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

    },

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

    },

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

    }
})