// pages/userLogin/userLogin.js
//获取应用实例
const app = getApp()

Page({

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

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.getScene(options);
    this.getCode();
  },

  getCode(){
    let that = this;
    wx.login({
      success: function (ret) {
        that.setData({
          code: ret.code
        })
      }
    });
  },

  getPhoneNumber(e){
    // console.log(e);
    var that = this;
    wx.showLoading({
      title: '加载中',
      mask: true
    });
    let orgId = wx.getStorageSync("orgId");
    wx.request({
      url: app.globalData.serverUrl + 'login/wxpLogin',
      data: {
        orgId: orgId,
        code: this.data.code,
        iv: e.detail.iv,
        encryptedData: e.detail.encryptedData
      },
      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);
            wx.setStorageSync("orgId", res.data.data.orgId);
          } 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
          })
        }

        that.getCode();
      },
      fail:function(e){
        console.log(e);
        wx.hideLoading();
        wx.showModal({
          title: '提示',
          content: '登录失败,请重试',
          showCancel: false
        })
        that.getCode();
      }
    })
    
  },

  getUserInfo(){
    wx.getUserProfile({
      desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (e) => {
        wx.showLoading({
          title: '加载中',
          mask: true
        });
        let orgId = wx.getStorageSync("orgId");
        wx.request({
          url: app.globalData.serverUrl + 'login/wxLogin',
          data: {
            orgId: orgId,
            code: this.data.code,
            iv: e.iv,
            encryptedData: e.encryptedData
          },
          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(){
            wx.hideLoading();
            wx.showModal({
              title: '提示',
              content: '登录失败,请重试',
              showCancel: false
            })
          }
        })
      }
    });
  },

  
  //提交
  onSubmit: function(){
    var username = this.data.username;
    var password = this.data.password;

    //去除开始结尾空格
    if (username.replace(/^(\s|\xA0)+|(\s|\xA0)+$/g, '') && password){
      wx.showLoading({
        title: '登录中',
        mask: true
      });
      wx.login({
        success: function (loginRes) {
          
        },
        fail:function(e){
          wx.hideLoading();
          wx.showModal({
            title: '提示',
            content: '登录失败,请重试',
            showCancel: false
          })
        }
      })
    }else{
      wx.showModal({
        title: '提示',
        content: '账号或密码为空！',
        showCancel: false
      })
    }
  },

  goYY(){
    var token = wx.getStorageSync("visitorToken");
    if (!token || token == undefined) { //未登录
      wx.login({
        success: function (loginRes) {
          var serverUrl = app.globalData.visitorServerUrl;
          if (loginRes) {
            wx.request({
              url: serverUrl + 'oauth/checkCode',
              data: {
                type: 2,
                code: loginRes.code//临时登录凭证
              },
              method: 'POST',
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              success: function (res) {
                if (res.data.code == -101) {
                  wx.removeStorageSync("visitorToken");
                  wx.redirectTo({
                    url: '/pages/splash/splash?type=visitor'
                  })
                }else if (res.data.code == 0) {
                  try {
                    wx.setStorageSync("visitorToken", res.data.data.token);
                    wx.setStorageSync("visitorUserId", res.data.data.userId);
                  } catch (e) {
                    app.logout();
                    wx.showModal({
                      title: '提示',
                      content: '登录异常！',
                      showCancel: false
                    })
                    return false;
                  };
                  wx.redirectTo({
                    url: '/pages/visitor/index'
                  });
                }
              },
              fail: function (err) {
                wx.showModal({
                  title: '提示',
                  content: '登录异常！',
                  showCancel: false
                })
              }
            })
          } else {
            wx.showModal({
              title: '提示',
              content: '登录异常！',
              showCancel: false
            })
          }
        }
      })
    }else{
      wx.navigateTo({
        url: '/pages/visitor/index'
      });
    }

    
  },

})