let util = require('../../../utils/util.js');
var that
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tel: '',
    yzm: '',
    isloading: false,
    djs: 60,
    btnname: '获取验证码',
    imageurl: util.imgurl
  },
  onGotUserInfo1: function(e) {
    var ui = e.detail.userInfo
    wx.login({
      success: function(res) {
        if (res.code) {
          console.log(res.code);
          util.query(
            "/cuser/wxapi/getopenid", {
              code: res.code
            },
            function(da) {
              if (da.code === 10000) {
                wx.setStorageSync("token", da.token)
                wx.setStorageSync('userinfo', da.userinfo)
                setTimeout(function() {
                  wx.navigateBack({
                    delta: 1
                  })
                }, 2000)



              } else {
                wx.showToast({
                  title: '登录失败',
                  icon: 'none',
                  duration: 2000
                })
              }
            }, "get", 1
          )
        } else {
          wx.showToast({
            title: '登录失败',
            icon: 'none',
            duration: 2000
          })
          console.log('获取用户登录态失败！' + res.errMsg)
        }
      }
    })
  },
  onGotUserInfo: function(e) {
    this.wxlogin(e.detail.userInfo)
  },
  wxlogin(ui) {
    wx.login({
      success: function(res) {
        if (res.code) {
          console.log(res.code);
          util.query(
            "/cuser/wxapi/getopenid", {
              code: res.code
            },
            function(da) {
              if (da.code === 10000) {
                wx.setStorageSync("token", da.token)
                that.setData({
                  token: da.token,
                  ui: ui
                })
                that.bindphone()
              } else {
                wx.showToast({
                  title: '登录失败',
                  icon: 'none',
                  duration: 2000
                })
              }
            }, "get", 1
          )
        } else {
          wx.showToast({
            title: '登录失败',
            icon: 'none',
            duration: 2000
          })
          console.log('获取用户登录态失败！' + res.errMsg)
        }
      }
    })
  },
  sendyzm() {
    if (!this.data.tel) {
      util.showToast('请输入手机号码')
      return
    }
    if (!util.checkPhone(this.data.tel)) {
      util.showToast('请输入正确的手机号码')
      return
    }
    this.setData({
      isloading: true,
      djs: 60
    })
    this.setdjs()
  },
  setdjs() {
    if (that.data.djs == 1) {
      that.setData({
        btnname: '获取验证码',

        isloading: false
      });
    } else {
      var djs = that.data.djs - 1;
      that.setData({
        btnname: djs + 's',
        djs: djs
      });
      setTimeout(function() {
        that.setdjs();
      }, 1000)
    }
  },

  bindphone() {
    if (!this.data.tel) {
      util.showToast('请输入手机号码')
      return
    }
    if (!util.checkPhone(this.data.tel)) {
      util.showToast('请输入正确的手机号码')
      return
    }


    wx.request({
      url: util.baseurl + 'cuser/wxapi/binduserinfo',
      data: {
        phone: this.data.tel,
        avatarUrl: this.data.ui.avatarUrl,
        nickName: this.data.ui.nickName
      },
      dataType: 'json',
      success: function(d) {
        wx.hideLoading();
        if (d.statusCode === 200) {
          wx.showToast({
            title: '登录成功',
            icon: 'none',
            duration: 2000
          })
          wx.setStorageSync('token', that.data.token)
          wx.setStorageSync('userinfo', d.data.userInfo)
          setTimeout(function() {
            wx.navigateBack({
              delta: 1
            })
          }, 2000)
        } else {
          wx.showToast({
            title: '请求失败',
            icon: 'none',
            duration: 2000
          })
        }

      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'token': this.data.token
      }
    })
  },
  settel(e) {
    this.setData({
      tel: e.detail.value
    })
  },
  setyzm(e) {
    this.setData({
      yzm: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    that = this
    if (options.ui) {
      this.setData({
        ui: JSON.parse(options.ui),
        token: options.token
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})