kaishi.js 4.6 KB
// pages/kaishi/kaishi.js
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgUrls: [
            app.globalData.imgUrl + 'lunbo1@3x.png',
            app.globalData.imgUrl + 'lunbo2@3x.png',
            app.globalData.imgUrl + 'lunbo3@3x.png'
        ],
        indexs: 0,
        color: '#cccccc',
        indicatorDots: false,
        autoplay: true,
        activecolor: '#ffffff',
        interval: 5000,
        duration: 1000,
        imgUrl: app.globalData.imgUrl
    },

    start(e) {
        let that = this;
        app.globalData.userInfo = e.detail.userInfo;
        wx.login({
            success: function (s) {
                let url = 'wxapp/public/getSessionKey';
                if (s.code) {
                    var code = s.code;
                    var param = {
                        code: s.code
                    }
                    app.post(url, param).then((res) => {
                        // console.log(res);
                        // console.log(e);
                        that.login(res.data.openid, res.data.session_key, e.detail.encryptedData, e.detail.iv);
                    }).catch((errMsg) => {
                        console.log(errMsg);
                    })
                }
            }
        });
    },
    login(openid, session_key, encrypted_data, iv) {
        let that = this;
        wx.checkSession({
            success: function () {
                console.log('未过期');
                //session_key 未过期,并且在本生命周期一直有效
                let param = {
                    openid: openid,
                    session_key: session_key,
                    encrypted_data: encrypted_data,
                    iv: iv,
                }
                let url = 'wxapp/public/login';

                app.post(url, param).then((res) => {
                    // console.log(res)
                    wx.setStorageSync('token', res.data.token);
                    let u = "wxapp/public/isMobile";
                    let header = {
                        "XX-Token": res.data.token
                    }
                    let data = {
                        type: 1
                    }
                    app.post(u, data, header).then((r) => {
                        console.log(r);
                        if (r.data.is_mobile == 2) {
                            wx.redirectTo({
                                url: '/pages/login/login',
                                success: function (res) { },
                                fail: function (res) { },
                                complete: function (res) { },
                            })
                        } else if (r.data.is_mobile == 1) {
                            wx.redirectTo({
                                url: '/pages/index/index',
                                success: function (res) { },
                                fail: function (res) { },
                                complete: function (res) { },
                            })
                        }
                    }).catch((err) => {

                    })
                    // wx.navigateTo({
                    //     url: '/pages/login/login',
                    //     success: function (res) { },
                    //     fail: function (res) { },
                    //     complete: function (res) { },
                    // })
                }).catch((errMsg) => {
                    console.log(errMsg);
                })
            },
            fail: function () {
                console.log('session_key 已经失效');
                // session_key 已经失效,需要重新执行登录流程
                that.start() //重新登录
            }
        })
        
    },
    changeindexs(e) {
        this.setData({
            indexs: e.detail.current
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    }
})