// pages/splash/splash.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    spashTextValue: '- 智慧服务  智创未来 -',
    splashImageUrl: '/images/tu.png',
    splashImageUrl2: '/images/zi.png'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.getScene(options);
    this.setData({
      type: options.type?options.type:''
    });
    if(options.type){
      wx.setStorageSync("visitorType",options.type);
      if(options.type == 'visitorAdd'){
        wx.setStorageSync("visitorToUserId",options.userId?options.userId:0);
        wx.setStorageSync("visitorOrgId",options.orgId?options.orgId:0);
      }
    }
  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
  
    setTimeout(function(){
      if(that.data.type == 'visitorAdd'){
        var token = wx.getStorageSync("visitorToken");
        if (!token || token == undefined) { //未登录
          app.loginVisitor(that.data.type);
        } else {
          wx.redirectTo({
            url: '/pages/visitor/add/index'
          });
        }
      }else if(that.data.type == 'visitor'){
        var token = wx.getStorageSync("visitorToken");
        if (!token || token == undefined) { //未登录
          app.loginVisitor(that.data.type);
        } else {
          wx.redirectTo({
            url: '/pages/visitor/index'
          });
        }
      }else{
        var userId = wx.getStorageSync("userId");
        if (!userId || userId == undefined) { //未登录
        //   wx.redirectTo({
        //     url: '/pages/userLogin/userLogin'
        //   })
            wx.login({
                success: function (ret) {
                    wx.showLoading({
                        title: '加载中',
                        mask: true
                    });
                    let orgId = wx.getStorageSync("orgId");
                    wx.request({
                    url: app.globalData.serverUrl + 'login/wxPreLogin',
                    data: {
                        orgId: orgId,
                        code: ret.code,
                    },
                    method: 'POST',
                    header: {
                        'content-type': 'application/x-www-form-urlencoded'
                    },
                    success: function (res) {
                        console.log(res);
                        wx.hideLoading();
                        if (res.data.code == 0) {
                            try {
                                app.logout();
                                wx.setStorageSync("userId", res.data.data.userId);
                                wx.setStorageSync("token", res.data.data.token);
                            } catch (e) { };

                            var userinfo = {
                                userId: res.data.data.userId,
                                token: res.data.data.token,
                            };

                            app.globalData.userinfo = userinfo;

                            wx.switchTab({
                                url: '/pages/index/index'
                            });
                        }else{
                            wx.showModal({
                                title: '提示',
                                content: res.data.message,
                                showCancel: false
                            })
                        }
                    },
                    fail:function(e){
                        console.log(e);
                        wx.hideLoading();
                        wx.showModal({
                            title: '提示',
                            content: '登录失败,请重试',
                            showCancel: false
                        })
                        }
                    })

                }
            });
        } else {
          wx.switchTab({
            url: '/pages/index/index'
          });
        }
      }
    },1500);
  },

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

  },

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

  },

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

  },

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

  },

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

  }
})