
<script type="text/html" template>
  <link rel="stylesheet" href="{{ layui.setter.base }}style/login.css?v={{ layui.admin.v }}-1" media="all">
</script>


<div class="layadmin-user-login layadmin-user-display-show" id="LAY-user-login" style="display: none;">

  <div class="layadmin-user-login-main">
    <div class="layadmin-user-login-box layadmin-user-login-header">
      <h2>登录</h2>
    </div>
    <div class="layadmin-user-login-box layadmin-user-login-body layui-form">
      <div class="layui-form-item">
        <label class="layadmin-user-login-icon layui-icon layui-icon-username" for="LAY-user-login-username"></label>
        <input type="text" name="account" id="LAY-user-login-username" lay-verify="required" placeholder="手机、邮箱或用户名" class="layui-input">
      </div>
      <div class="layui-form-item">
        <label class="layadmin-user-login-icon layui-icon layui-icon-password" for="LAY-user-login-password"></label>
        <input type="password" name="password" id="LAY-user-login-password" lay-verify="required" placeholder="密码" class="layui-input">
      </div>
      <div class="layui-form-item code layui-hide">
        <div class="layui-row">
          <div class="layui-col-xs7">
            <label class="layadmin-user-login-icon layui-icon layui-icon-vercode" for="LAY-user-login-vercode"></label>
            <input type="text" name="code" id="LAY-user-login-vercode" placeholder="图形验证码" class="layui-input">
          </div>
          <div class="layui-col-xs5">
            <div style="margin-left: 10px;">
              <img src="/common/captcha" class="layadmin-user-login-codeimg" id="LAY-user-get-vercode">
            </div>
          </div>
        </div>
      </div>
      <div class="layui-form-item">
        <button id="login" class="layui-btn layui-btn-fluid" lay-submit lay-filter="LAY-user-login-submit">登 入</button>
      </div>
      <div class="layui-trans layui-form-item layadmin-user-login-other">

        <a lay-href="/user/forget" class="layadmin-user-jump-change layadmin-link">忘记密码</a>
        <a lay-href="/user/reg" class="layadmin-user-jump-change layadmin-link">注册帐号</a>
      </div>
    </div>
  </div>
  
  
</div>

<script>
layui.use(['admin', 'form', 'user'], function(){
  var $ = layui.$
  ,setter = layui.setter
  ,admin = layui.admin
  ,form = layui.form
  ,router = layui.router()
  ,search = router.search;

  var access_token = search.access_token
  var username = search.username
  var uid = search.uid

  $("#LAY-user-login-username").focus()

  var set_title = function (argument) {
    var sys_name = layui.data('layuiAdmin').sys_name
    $("title").text(sys_name+"系统登录")
    $("h2").text(sys_name+"系统登录")
  }

  // 获取系统信息
  admin.get_sys_info(set_title)


  if (access_token) {
    // 处理access_token
    access_token = access_token.replace(/_/g,"/")
    access_token = access_token.replace(/,/g,"=")

    //写入 access_token
    layui.data(setter.tableName, {
      key: setter.request.tokenName
      ,value: access_token
    });

    //写入 user_name
    layui.data(setter.tableName, {
      key: "username"
      ,value: username
    });

    //写入 uid
    layui.data(setter.tableName, {
      key: "uid"
      ,value: uid
    });  

    // 跳转到用户端
    location.href = "/console/index.html#/user_home"

  }


  form.render();


  $(document).keyup(function(event){  
    if(event.keyCode ==13){  
      $("#login").click()
    }  
  });   

  //提交
  form.on('submit(LAY-user-login-submit)', function(obj){
    function fail(res) {
      $("#LAY-user-get-vercode").click()
      if (res.code == "user_login-7" || res.code == "user_login-8" || res.code == "user_login-12") {
        $(".code").removeClass("layui-hide")
      }
    }
    //请求登入接口
    admin.req({
      url: '/login' //实际使用请改成服务端真实接口
      ,data: JSON.stringify({"account": obj.field.account,"password":obj.field.password,"code":obj.field.code})
      ,type: "post"
      ,fail: fail
      ,contentType:"application/json"
      ,dataType: "json"
      ,done: function(res){
      
        //请求成功后，写入 access_token
        layui.data(setter.tableName, {
          key: setter.request.tokenName
          ,value: res.data.access_token
        });

        //请求成功后，写入 user_name
        layui.data(setter.tableName, {
          key: "username"
          ,value: res.data.username
        });

        //请求成功后，写入 uid
        layui.data(setter.tableName, {
          key: "uid"
          ,value: res.data.uid
        });

        //请求成功后，写入 user_type
        layui.data(setter.tableName, {
          key: "user_type"
          ,value: res.data.type
        });

        //登入成功的提示与跳转
        layer.msg('登入成功', {
          offset: '15px'
          ,icon: 1
          ,time: 1000
        }, function(){
          if (res.data.type == 1) {
            $("title").text(layui.data('layuiAdmin').admin_console_title)
            location.hash = search.redirect ? decodeURIComponent(search.redirect) : '/home';
          } else {
            $("title").text(layui.data('layuiAdmin').user_console_title)
            location.hash = search.redirect ? decodeURIComponent(search.redirect) : '/user_home';
          }
          
        });
      }
    });
    
  });
  
  
});
</script>