const app = getApp();
const util = require("../../../utils/util.js")
const md5 = require('../../../utils/md5.js');
Page({
  data: {
    hidden: true,
  },
  wxLogin(){
    let that = this
    wx.login({
      success(wxres) {
        if( !wxres.code){
          util.showMsg("登入失败")
        }else{
          that.doLoginByCode( wxres.code )
        }
      }
    })
  },
  doLoginByCode( code ){
    app.formPost( "base/wxLogin", {code}).then( (res)=>{
      if( res.code == 200){
        app.setUserInfo(  res.data )
        util.showSuccess("登入成功")
        wx.navigateBack({
          delta: 1,
        })
      }
    })
  },
  doIdentify( e ){
    let {username, nickname, password} = e.detail.value;
    if( !username || !nickname || !password ){
      return;
    }
    let param = {username, nickname}
    console.log("param",{username, nickname, password} )
    param.password = md5.md5( password ).toLocaleLowerCase();
    let that = this
    wx.login({
      success(wxres) {
        if (wxres.code) {
          param.code = wxres.code
          that.doRegister( param )
        }
      }
    })
  },
  doRegister( param ){
    app.formPost( 'base/register', param).then( res =>{
      if( res.code == 200 ){
        console.log( "register", res)
        app.setUserInfo(  res.data )
        util.showSuccess("绑定成功")
        wx.navigateBack({
          delta: 1,
        })
      }else{
        util.showMsg( res.msg  )
      }
    })
  }

})