Login.php 4.6 KB

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