<?php
declare (strict_types = 1);
namespace app\login\controller;
use app\student\model\Student as stu;use WhiteHat101\Crypt\APR1_MD5;use think\captcha\Captcha;use think\facade\View;use app\login\validate\Yanzheng;use think\exception\ValidateException;use think\facade\Config;
class Student
{
public function index()
{
$mobile = request()->isMobile();
cookie('userid', null);
cookie('username', null);
cookie('password', null);
session(null);
$list['webtitle'] = Config::get('shangma.webtitle');
$list['version'] = Config::get('shangma.version');
$list['mobile'] = $mobile;
View::assign('list',$list);
return View::fetch();
}
public function yanzheng()
{
$data = request()->only(['username','password','online']);
try {
$validate = new \app\login\validate\Yanzheng;
validate(Yanzheng::class)->scene('student')->check($data);
} catch (ValidateException $e) {
$data=['msg'=>$e->getError(),'status'=>0];
return json($data);
}
$check = $this->check($data['username'],$data['password']);
if($check['status']==1)
{
if( request()->post('online') == true )
{
cookie('userid', session('userid'));
cookie('username', $data['username']);
cookie('password', $data['password']);
}
$userinfo = stu::where('shenfenzhenghao',$data['username'])
->field('lastip,ip,denglucishu,lasttime,thistime')
->find();
$userinfo->lastip = $userinfo->ip;
$userinfo->ip = request()->ip();
$userinfo->denglucishu = ['inc', 1];
$userinfo->lasttime = $userinfo->getData('thistime');
$userinfo->thistime = time();
$userinfo->save();
$data=['msg'=>'验证成功','status'=>1];
}else{
$data=['msg'=>$check['msg'],'status'=>0];
}
return json($data);
}
public function check($username,$password)
{
$md5 = new APR1_MD5();
$userinfo = stu::where('shenfenzhenghao', $username)->where('status', 1)->find();
if($userinfo == null)
{
session(null);
$data=['msg'=>'学生不存在或被禁用','status'=>0];
return $data;
}
$check = $md5->check($password,$userinfo->password);
if($check)
{
session('student', null);
session('onlineCategory', 'student');
session('student.userid', $userinfo->id);
session('student.username', $username);
session('student.password', $password);
$data=['msg'=>'验证成功','status'=>1];
}else{
cookie('userid', null);
cookie('username', null);
cookie('password', null);
session(null);
$data=['msg'=>'学生身份证号或密码错误','status'=>0];
}
return $data;
}
}