// pages/login/login.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    username:'',
    password:'',
    show:false,
    openId:'', 
  },

  //获取填写的账号信息
  name:function(even){
    this.setData({
      username:even.detail.value
    })
  },
  //获取填写的密码信息
  psw:function(even){
    this.setData({
      password:even.detail.value
    })
  },

  onClickShow() {
    this.setData({ show: true });
  },

  onClickHide() {
    this.setData({ show: false });
  },

  onLoad: function (option) {
     this.getOPenId();
  },

  /**
   * 获取用户openid
   */
  getOPenId:function () {
    wx.login({
      success :res=>{
          //启动蒙版
          let oid = wx.getStorageSync("openId");
          if(!oid){
            this.onClickShow();
            wx.request({
              url: app.globalData.reqUrl+'/wechat/getOpenId', 
              data:{
                  "code": res.code,
                  "appId": app.globalData.appId,
              },
              success:(res)=>{
                res = res.data;
                  this.onClickHide();
                  this.setData({
                      openId:res.data.openid,
                  })
                  wx.setStorageSync('openId', res.data.openid)
                   //验证登录用户 
                  this.selectUserInfoByOpenId(res.data.openid);  
              }
          })
          }else{
            //验证登录用户 
            this.selectUserInfoByOpenId(oid);
          }
      }
    })
},
   
  //验证用户是否是第一次登陆
  selectUserInfoByOpenId:function(openId){
    //跳转页面
    let isRelogin=wx.getStorageSync("isReload");
    if(isRelogin){
      return;
    }
    this.onClickShow();
    var that = this;
    wx.request({
      url: app.globalData.reqUrl+'/wechat/openIdLogin',
      data:{
        "openId":openId
      },
      method:"POST",
      success:function(res){
        if(res.data.code == '200'){
          //把数据存到缓存中
          wx.setStorage({
            key: 'userinfo',
            data: res.data.data.userinfo
          });
          // wx.setStorageSync({
          //   key: 'token',
          //   data: res.data.token
          // });
          that.onClickHide();
          let rd = res.data.data;
          //只有一个项目时直接跳转详情页
          if(rd.projectInfo!=null || rd.projectInfo!=undefined){
            //赋值到公共参数
            app.globalData.projectId = rd.projectInfo.projectId;
            app.globalData.projectName = rd.projectInfo.projectName;
            app.globalData.projectInfoList = [];
            app.globalData.projectInfoList.push(rd.projectInfo);
            app.globalData.value1 = rd.projectInfo.shengName;
            app.globalData.value2 = rd.projectInfo.shiName+'/'+rd.projectInfo.quName;
            app.globalData.value3 = rd.projectInfo.logCompanyName;
            wx.redirectTo({
              url: '../xiangmugaikuang/xiangmugaikuang',
            })
          }else{
            //跳转页面
            wx.redirectTo({
              url: '../map/map',
            })
          }
        }else{
          that.onClickHide();
        }
      }
    })
   },
  
  //登录
  userLogin:function(openid){
    var that = this;
    var username = that.data.username;
    var password = that.data.password;
    if(username == ""){
      app.toast("账号不能为空");
      return;
    }
    if(password == ""){
      app.toast("密码不能为空");
      return;
    }
    let openId = that.data.openId
    if(openId==""){
      openId = wx.getStorageSync("openId");
      if(openId==""){
        app.toast("获取微信授权失败，请卸载后重新进入应用！！");
        return;
      }
    }
    //启动蒙版
    that.onClickShow();
    //发送请求
    wx.request({
      url: app.globalData.reqUrl+'/wechat/login',
      data:{
        "username":username,
        "password":password,
        "openId":openId,
      },
      method:"POST",
      success(res){
          //关闭蒙版
          that.onClickHide();
          if(res.data.code=='200'){
            //把数据存到缓存中
            wx.setStorage({
              key: 'userinfo',
              data: res.data.data.userinfo
            });
            // wx.setStorageSync({
            //   key: 'token',
            //   data: res.data.token
            // });
            wx.setStorageSync('isReload', "");
            let rd = res.data.data;
            //只有一个项目时直接跳转详情页
            if(rd.projectInfo!=null || rd.projectInfo!=undefined){
              //赋值到公共参数
              app.globalData.projectId = rd.projectInfo.projectId;
              app.globalData.projectName = rd.projectInfo.projectName;
              app.globalData.projectInfoList = [];
              app.globalData.projectInfoList.push(rd.projectInfo);
              app.globalData.value1 = rd.projectInfo.shengName;
              app.globalData.value2 = rd.projectInfo.shiName+'/'+rd.projectInfo.quName;
              app.globalData.value3 = rd.projectInfo.logCompanyName;
              wx.redirectTo({
                url: '../xiangmugaikuang/xiangmugaikuang',
              })
            }else{
              //跳转页面
              wx.redirectTo({
                url: '../map/map',
              })
            }
          }else{
            app.toast(res.data.msg);
          }
      }
    })
  },

})