76 lines
2.3 KiB
PHP
76 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace app\controller\manager;
|
|
|
|
use app\service\Jwt;
|
|
use Exception;
|
|
use app\model\{Member, AuthRule, LoginLog};
|
|
use app\controller\BaseController;
|
|
use think\response\Json;
|
|
use think\response\View;
|
|
|
|
class Login extends BaseController
|
|
{
|
|
protected $noNeedLogin = ['index'];
|
|
|
|
/**
|
|
* @return View|Json
|
|
* @throws Exception
|
|
*/
|
|
public function index()
|
|
{
|
|
if (request()->isPost()) {
|
|
$param = input('post.data');
|
|
$username = trim($param['username']);
|
|
$password = trim($param['password']);
|
|
$captcha = trim($param['captcha'] ?? '');
|
|
if (!captcha_check($captcha)) {
|
|
return $this->json(4001, '验证码错误'.$captcha);
|
|
}
|
|
|
|
if (empty($username) || empty($password)) {
|
|
return $this->json(4001, '用户名和密码不能为空');
|
|
}
|
|
$member = Member::getByUserName($username);
|
|
if (empty($member)) {
|
|
return $this->json(4002, '用户名或密码错误');
|
|
}
|
|
if ($member['password'] != md5($password.$username)) {
|
|
return $this->json(4003, '用户名或密码错误');
|
|
}
|
|
if ($member['status'] != Member::STATUS_NORMAL) {
|
|
return $this->json(4004, '账号已被禁用');
|
|
}
|
|
|
|
$userInfo = [
|
|
'user_id' => $member['id'],
|
|
'username' => $member['username'],
|
|
'nickname' => $member['nickname'],
|
|
];
|
|
|
|
$jwtToken = Jwt::generate($userInfo, env('app.expire', 7200));
|
|
|
|
$userInfo['token'] = $jwtToken;//jwt生成token
|
|
|
|
//记录最后登陆时间
|
|
$ip = request()->ip();
|
|
$time = time();
|
|
Member::updateById($member['id'], [
|
|
'login_time' => $time,
|
|
'login_ip' => $ip
|
|
]);
|
|
LoginLog::create([
|
|
'member_id' => $member['id'],
|
|
'name' => $member['username'],
|
|
'ip' => $ip,
|
|
'create_time' => $time
|
|
]);
|
|
session('auth', $userInfo);
|
|
return $this->json(0, 'success', ['url' => '/manager']);
|
|
}
|
|
|
|
$viewData = [];
|
|
return view()->assign($viewData);
|
|
}
|
|
}
|