| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- <?php
- namespace app\controller\common;
- use app\model\system\SystemConfig;
- use app\model\system\SystemUser;
- use app\validate\system\LoginValidate;
- use Shopwwi\WebmanAuth\Auth;
- use support\Request;
- use support\Response;
- use think\exception\ValidateException;
- use think\facade\Db;
- use Tinywan\Captcha\Captcha;
- class Login
- {
- protected array $noNeedLogin = ["getSystemData","setLogin"];
- public function setLogin(Request $request): Response
- {
- try {
- $param = $request->post();
- validate(LoginValidate::class)->scene("login")->check($param);
- if (Captcha::check($param['code'],$param['key']) === false) return errorTrans("error.captcha");
- $map = ["is_deleted" => 0,"username" => $param['username']];
- [$state,$msg,$user] = $this->checkLogin($map,$param);
- if (!$state) return error($msg);
- return successTrans("success.login",get_object_vars((new Auth)->guard("admin")->login($user)));
- } catch (ValidateException $th) {
- return error($th->getError());
- } catch (\Throwable $throwable) {
- return error($throwable->getMessage());
- }
- }
- protected function checkLogin(array $map = [],array $param = []): array
- {
- $user = (new SystemUser)->where($map)->findOrEmpty();
- if ($user->isEmpty()) return [0,trans("error.user-empty"),[]];
- if ($user['status'] <> 1) return [0,trans("error.user-status"),[]];
- if (md5($param['password'].$user['salt']) <> $user['password']) return [0,trans("error.passwd"),[]];
- $user->login_at = getDateFull();
- $user->login_ip = request()->getRealIp();
- $user->login_num = Db::raw("login_num+1");
- $user->save();
- return [1,'success',$user->toArray()];
- }
- public function getLoginUser(): Response
- {
- try {
- $userData = (new Auth)->guard("admin")->user()->toArray();
- if (isset($userData['password'])) unset($userData['password']);
- return successTrans("success.data",[
- "username" => $userData['username'],
- "userId" => $userData['id'],
- "name" => $userData['truename'],
- "super" => $userData['is_super'],
- "type" => $userData['type'],
- ]);
- } catch (\Throwable $throwable) {
- return error($throwable->getMessage());
- }
- }
- public function getSystemData(): Response
- {
- try {
- $captcha = Captcha::base64();
- $service = (new SystemConfig)->where("type","service")->column("value","name");
- return successTrans("success.data",compact("captcha",'service'));
- } catch (\Throwable $throwable) {
- return error($throwable->getMessage());
- }
- }
- }
|