//login.js
// const loginUrl = require('../../config').loginUrl;
//获取应用实例
var app = getApp();
var baseUrl = app.globalData.baseUrl;

Page({
  data: {
    mobile: '',
    verifyCode: "",
    tenant_id: "",
    wx_open_id: "",
    verifyCodeTime: "获取验证码",
    buttonDisable: false
  },

  mobileInputEvent: function (e) {
    this.setData({
      mobile: e.detail.value
    });
  },

  verifyInputEvent: function (e) {
    this.setData({
      verifyCode: e.detail.value
    });
    console.log("verifyCode", this.data.verifyCode);
  },

  verifyCodeEvent: function () {
    console.log("buttonDisable", this.data.buttonDisable);
    if (this.data.buttonDisable) return;
    var that = this;
    var mobile = this.data.mobile;
    var regMobile = /^1[3|4|5|7|8][0-9]{9}$/;
    console.log("mobile---", mobile);
    if (!regMobile.test(mobile)) {
      wx.showToast({
        title: '手机号有误!'
      })
      return;
    }
    var timelength = 60

    var intervalId = setInterval(function () {
      timelength = timelength - 1;
      that.setData({
        verifyCodeTime: timelength + 's后重发',
        buttonDisable: true
      })
      if (timelength == 0) {
        clearInterval(intervalId);
        that.setData({
          verifyCodeTime: '获取验证码',
          buttonDisable: false
        })
      }
    }, 1000);
    that.getVerifyCode(mobile);
  },

  getVerifyCode: function (mobile) {
    var that = this;
    var Authorization = app.globalData.Authorization;
    that.setData({
      tenant_id: app.globalData.tenant_id,
      wx_open_id: app.globalData.wx_open_id
    })
    wx.request({
      url: baseUrl + 'sms/v1/sms_codes',
      method: "POST",
      data: {
        "mobile": mobile,
        "type": "bindmobile",
        "scope": "global_access:end_user"
      },
      header: {
        'content-type': 'application/json', // 默认值
        "Authorization": Authorization
      },
      success: function (res) {
        console.log(res.data)
      },
      fail(res) {
        console.log(res.data)
      }
    })
  },

  login: function () {
    var that = this;
    var Authorization = getApp().globalData.Authorization;
    wx.request({
      url: baseUrl + "uaa/v1/users/op/bind-mobile",
      data: {
        "mobile": that.data.mobile,
        "sms_code": that.data.verifyCode,
        "wx_open_id": that.data.wx_open_id,  //可选,如果传了,绑定到对应的微信号
        "tenant_id": that.data.tenant_id,
        "source": "wx_app",
        "scope": "end_user"
      },
      method: "POST",
      header: {
        'content-type': 'application/json', // 默认值
        "Authorization": Authorization
      },
      success: function (res) {
        console.log("succ", res);
        if (200 == res.statusCode) {
          app.globalData.Authorization = "Bearer " + res.data.access_token;
          app.globalData.refresh_token = res.data.refresh_token;
          var user = res.data.user;
          var tenant = res.data.tenant;
          app.globalData.tenant_id = tenant.id;
          app.globalData.userInfo = user;
          app.globalData.wx_open_id = user.wx_open_id;

          wx.reLaunch({
            url: '../main/home/home'
          })
        } else {
          wx.showToast({
            title: '请重新登录',
          })
        }
      },
      fail: function (res) {
        console.log("fail ", res.data)
      },
       complete: function (res) {
        wx.reLaunch({
          url: '../main/home/home'
        })
      }
    })
  },

  onLoad: function () {
    
  }
})