Login.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace app\controller\common;
  3. use app\extra\basic\Base;
  4. use app\middleware\AuthMiddleware;
  5. use app\model\saas\SaasStore;
  6. use app\model\system\SystemConfig;
  7. use app\model\system\SystemUser;
  8. use DI\Attribute\Inject;
  9. use LinFly\Annotation\Attributes\Route\{GetMapping, Controller, Middleware, PostMapping};
  10. use support\{Request,Response};
  11. use Shopwwi\WebmanAuth\Auth;
  12. use think\facade\Db;
  13. use Tinywan\Captcha\Captcha;
  14. #[Controller("/api/login"),Middleware(AuthMiddleware::class)]
  15. class Login extends Base
  16. {
  17. /**
  18. * @var array|string[]
  19. */
  20. protected array $noNeedLogin = ["getSystemData","setLogin"];
  21. #[Inject]
  22. protected SystemConfig $config;
  23. /**
  24. * 登陆
  25. * @param Request $request
  26. * @return Response
  27. */
  28. #[PostMapping('user')]
  29. public function setLogin(Request $request): Response
  30. {
  31. try {
  32. $param = $this->_valid([
  33. "username.require" => trans("empty.user"),
  34. "password.require" => trans("empty.passwd"),
  35. "code.require" => trans("empty.code"),
  36. "key.require" => trans("empty.data"),
  37. ],"post");
  38. if (!is_array($param)) return error($param);
  39. if (Captcha::check($param['code'],$param['key']) === false) return errorTrans("error.captcha");
  40. $map = ["is_deleted" => 0,"username" => $param['username']];
  41. [$state,$msg,$user] = $this->checkLogin($map,2,$param);
  42. if (!$state) return error($msg);
  43. return successTrans("success.login",get_object_vars((new Auth)->guard("admin")->login($user)));
  44. } catch (\Throwable $throwable) {
  45. return error($throwable->getMessage());
  46. }
  47. }
  48. /**
  49. * 登录验证处理
  50. * @param array $map
  51. * @param int $type
  52. * @param array $param
  53. * @return array
  54. */
  55. protected function checkLogin(array $map = [],int $type = 1,array $param = []): array
  56. {
  57. $user = (new SystemUser)->where($map)->findOrEmpty();
  58. if ($user->isEmpty()) return [0,trans("error.user-empty"),[]];
  59. if ($user['status'] <> 1) return [0,trans("error.user-status"),[]];
  60. if (md5($param['password'].$user['salt']) <> $user['password']) return [0,trans("error.passwd"),[]];
  61. $user->login_at = getDateFull();
  62. $user->login_ip = request()->getRealIp();
  63. $user->login_num = Db::raw("login_num+1");
  64. $user->save();
  65. return [1,'success',$user->toArray()];
  66. }
  67. #[GetMapping('profile')]
  68. public function getLoginUser(): Response
  69. {
  70. try {
  71. $userData = (new Auth)->guard("admin")->user()->toArray();
  72. return successTrans("success.data",[
  73. "username" => $userData['username'],
  74. "userId" => $userData['id'],
  75. "truename" => $userData['truename'],
  76. "super" => $userData['is_super']
  77. ]);
  78. } catch (\Throwable $throwable) {
  79. return error($throwable->getMessage());
  80. }
  81. }
  82. #[GetMapping('data')]
  83. public function getSystemData(): Response
  84. {
  85. try {
  86. $captcha = Captcha::base64();
  87. $service = $this->config->where("type","service")->column("value","name");
  88. return successTrans("success.data",compact("captcha",'service'));
  89. } catch (\Throwable $throwable) {
  90. return error($throwable->getMessage());
  91. }
  92. }
  93. }