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

Page({
  data: {
    bg: getApp().globalData.OSSImgHeader + "bg.png",
    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 = /^(13[0-9]|14[5-9]|15[012356789]|166|17[0-8]|18[0-9]|19[8-9])[0-9]{8}$/;
    console.log("mobile---", mobile);
    if (!regMobile.test(mobile)) {
      wx.showToast({
        image: "/images/error.png",
        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",
        "app_id": app.globalData.appId
      },
      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.real_auth_status = res.data.user.real_auth_status;
          app.globalData.wx_open_id = user.wx_open_id;
          app.configOssUrl()
          wx.reLaunch({
            url: '../main/guide/guide'
          })
        } else {
          wx.showToast({
            image: "/images/error.png",
            title: '请重新登录',
          })
        }
      },
      fail: function (res) {
        console.log("fail ", res.data)
      },
       complete: function (res) {
        wx.reLaunch({
          url: '../main/guide/guide'
        })
      }
    })
  },

  onLoad: function () {
    
  }
})