<?php

/* ========================================================================
 * $Id: login.php 6079 2020-05-17 11:37:16Z onez $
 * 
 * Email: www@onez.cn
 * QQ: 6200103
 * HomePage: http://www.onez.cn
 * ========================================================================
 * Copyright 2016-2017 佳蓝科技.
 * 
 * ======================================================================== */

!defined('IN_ONEZ') && exit('Access Denied');
$method=onez()->gp('method');
!$method && $method='login';
$action=onez()->gp('action');
if($action=='login'){
  $username=onez()->gp('username');
  $password=onez()->gp('password');
  $or=array();
  $or[]="username='$username'";
  if(strpos($username,'@')!==false){
    $or[]="email='$username'";
  }elseif(is_numeric($username) && strlen($username)==11 && $username[0]=='1'){
    $or[]="mobile='$username'";
  }
  $T=onez('db')->open('member')->one(implode(' or ',$or));
  !$T && onez()->error('账号不存在');
  #$password_md5=hash_hmac('md5',$password,$T['rndkey']);
  $password_md5=md5($password);
  $T['password']!=$password_md5 && onez()->error('密码不正确');
  onez('cache')->cookie($G['this']->cname,"$T[userid]\t$T[username]\t".time(),0);
  onez()->ok('操作成功','?');
}elseif($action=='register'){
  !$G['config']['login_is_register'] && onez()->error('本站未开放账号注册功能');
  $username=onez()->gp('username');
  !$username && onez()->error('账号不能为空');
  strlen($username)<6 && onez()->error('账号不能小于6位英文符或2个中文');
  $password=onez()->gp('password');
  $password2=onez()->gp('password2');
  !$password && onez()->error('密码不能为空');
  strlen($password)<6 && onez()->error('密码不能小于6位');
  $password!=$password2 && onez()->error('两次密码不一致');
  if(onez('db')->open('member')->rows("username='$username'")>0){
    onez()->error('此账号已被其他用户使用，请更换');
  }
  $password_md5=md5($password);
  $item=array();
  $item['nickname']=$username;
  $item['username']=$username;
  $item['password']=$password_md5;
  $item['infotime']=time();
  $item['infoip']=onez()->ip();
  $userid=onez('db')->open('member')->insert($item);
  onez('cache')->cookie($G['this']->cname,"$userid\t$T[username]\t".time(),0);
  onez()->ok('操作成功','?');
}
$title=$G['config']['login_title']?$G['config']['login_title']:'佳蓝智能开发';
$titleSmall=$G['config']['login_title_small']?$G['config']['login_title_small']:'System Management Center';
$bgurl=$G['config']['login_bgpic']?$G['config']['login_bgpic']:($this->url.'/www/login/images/bg.jpg');
?>
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title><?php echo $title?></title>
<link rel="stylesheet" type="text/css" href="<?php echo $this->url?>/www/login/css/onez.css" media="all">
<link rel="stylesheet" type="text/css" href="<?php echo $this->url?>/www/login/css/login.css" media="all">
<link href="<?php echo $this->url?>/www/login/css/animate.min.css" rel="stylesheet">
<link href="<?php echo $this->url?>/www/login/css/font-awesome.min.css" rel="stylesheet">	 
<style> body { background-image: url(<?php echo $bgurl?>);}</style>
<?php onez('layui')->head();?>
</head>
<body>
<div id="main-box"></div>
<div id="main-content"> 
<div class="login-body  animated fadeInLeft">
    <div class="login-main pr">
<?php if($method=='login'){?>
        <form action="javascript:;" method="post" class="login-form">
        <h3> <?php echo $title?> </h3>
        <h5 style="padding-bottom: 10px"> <?php echo $titleSmall?> </h5>
        <div id="MobileBox" class="item-box"  >
            <div class="input-group user-name"> <span class="input-group-addon"><i class="icon-user"></i></span>
            <input type="text" id="username" name="username" class="form-control" placeholder="用户名">
         </div>
         <div class="input-group password"> <span class="input-group-addon"><i class="icon-lock"></i></span>
            <input type="password" id="password" name="password" class="form-control" placeholder="密码">
         </div>
<?php if($G['config']['login_is_register']){?>
         <div class="use-qrcode-a"><a class="use-ding" href="?method=register"> <?php echo $G['config']['login_reg_linkname']?$G['config']['login_reg_linkname']:'免费注册'?></a> </div>
<?php }?>
          <div class="login_btn_panel">
            <button class=" btn btn-primary btn-block btn-lg" data-ajax="post" type="submit" data-callback="success">登录</button>
            <div class="check-tips"></div>
          </div>
          <input type="hidden" name="action" value="<?php echo $method?>" />
      </form>
<?php }elseif($method=='register'){?>
        <form action="javascript:;" method="post" class="login-form">
        <h3> 免费注册 </h3>
        <h5 style="padding-bottom: 10px">  </h5>
        <div id="MobileBox" class="item-box"  >
            <div class="input-group user-name"> <span class="input-group-addon"><i class="icon-user"></i></span>
            <input type="text" id="username" name="username" class="form-control" placeholder="用户名">
         </div>
         <div class="input-group password"> <span class="input-group-addon"><i class="icon-lock"></i></span>
            <input type="password" id="password" name="password" class="form-control" placeholder="密码">
         </div>
         <div class="input-group password"> <span class="input-group-addon"><i class="icon-lock"></i></span>
            <input type="password" id="password2" name="password2" class="form-control" placeholder="再次输入密码">
         </div>
         <div class="use-qrcode-a"><a class="use-ding" href="?method=login"> 返回登录</a> </div>
          <div class="login_btn_panel">
            <button class=" btn btn-primary btn-block btn-lg" data-ajax="post" type="submit" data-callback="success">提交申请</button>
            <div class="check-tips"></div>
          </div>
          <input type="hidden" name="action" value="<?php echo $method?>" />
      </form>
<?php }?>
     </div>
  </div>
</div>
<script type="text/javascript">
layui.$('.login-form').bind('submit',function(){
  if(layui.$('#username').val().length<1){
    layer.alert('请填写您的登录账号');
    return;
  }
  if(layui.$('#password').val().length<1){
    layer.alert('请填写您的登录密码');
    return;
  }
  layui.$.post(window.location.href,layui.$(this).serialize()+'&ajax=1',function(data){
    if(data.error){
      layer.alert(data.error);
    }else{
      location.href='?';
    }
  },'json');
  return false;
});
</script>
</body>
</html>
