const app = getApp();
const webSocket = require('../../../utils/websocket.js');
Page({


  data: {
    user: '',     // 账号
    password: '',    // 密码

    qtzh:"手机号码",
    qtPassword:"密码",
    color1:"",
    color2:"",

    inputValue1: null,
    inputValue2: null,
    usertext:'',   //储存手机号码
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this =this;
    setTimeout(function () {
      wx.hideLoading()

 
      // 记录登陆过的手机
    if (wx.getStorageSync('user')){
        _this.setData({
          inputValue1: wx.getStorageSync('user'),
          user: wx.getStorageSync('user'),
        });
      }
 
    })

    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.userInfo']) {

          // wx.reLaunch({
          //   url: '/pages/Landpage/Land/Land'
          // });
          console.log('已经授权');


        } else {
      wx.reLaunch({
        url: '/pages/Jurisdiction/click_Jurisdiction/click_Jurisdiction'   //未授权  进入授权页
      })
          console.log('没有授权');
        }
      }
    })
  },

  //获取账号
  Telephone: function (e) {
    var user = e.detail.value;
    // console.log(user);
    this.setData({
      user: user
    });
  },

  //获取密码
  password: function (e) {
    var password = e.detail.value;
    // console.log(password);
    this.setData({
      password: password
    });
  },


  //登陆事件
  Landbutton: function (e) {
    var that = this;

    //  登陆ajax
    let Land_url = 'public/login';
    let Land_data = {
      user: this.data.user,    // 账号
      password: this.data.password,    // 密码
    };
    app.postRequest(Land_url, Land_data, function (res) {

      if (res.code == 201) {
        console.log(res.data);
        // console.log(res.data.access_key);
        // console.log(res.data.access_token);
        webSocket.sendSocketMessage({
          msg: JSON.stringify({
            form_id: res.data.access_token,
            type: 'login'
          }),
          success: function (e) {
            console.log('socket登录成功');
          }
        })
        // 全局同步传参
        try {
          wx.setStorageSync('access_key', res.data.access_key)
          wx.setStorageSync('access_token', res.data.access_token)
          wx.setStorageSync('access_user', res.data.access_user)
          wx.setStorageSync('user', that.data.user)
        } catch (e) {

        }
                  wx.switchTab({ // 跳转首页
                    url: '/pages/index/index'
                  })

               
 
         return;
                 
      }
        // console.log(res.data.user);  //账号
        // console.log(res.data.password);  //密码
     
        // console.log(that.data.user);  //账号
        // console.log(that.data.password);  //账号
    
    
           
              wx.showToast({
                title: '登陆失败',
                icon: 'none',  // 不显示图标
                mask: true,   //  是否显示透明蒙层,防止触摸穿透	
                duration: 1500
              })
             
            if (res.data.user){
                that.setData({
                  qtzh: res.data.user,    //失败时返回账号提示
                  color1: "color:red;font-size: 25rpx;",    //字体变红色
                  inputValue1: '',   //清空input
                  user: '',     // 清空获取账号   
                });
             }
             
            if (res.data.password){
               that.setData({
                 qtPassword: res.data.password,  //失败时返回密码提示
                 color2: "color:red;font-size: 25rpx;",    //字体变红色
                 inputValue2: '',   //清空input
                 password: '',    // 清空获取密码
               });
             }else{
              that.setData({
                qtPassword: '密码',  //失败时返回密码提示
                inputValue2: '',   //清空input
                color2:'',
                password: '',    // 清空获取密码
              });
             }

    
         
      

    })
  },


})