Login.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 ($user['type'] > 1) {
  61. $typeUser = $this->getTypeUser($user['agent_id']);
  62. if (empty($typeUser)) return [0,trans("empty.agent"),[]];
  63. if ($typeUser['status'] <> 0) return [0,trans("error.agent"),[]];
  64. if (time() > strtotime($typeUser['vip_end'])) return [0,trans("error.agent-out"),[]];
  65. $user['shop_name'] = $typeUser['poi_name'];
  66. }
  67. if ($type == 2) {
  68. if (md5($param['password'].$user['salt']) <> $user['password']) return [0,trans("error.passwd"),[]];
  69. }
  70. $user->login_at = getDateFull();
  71. $user->login_ip = request()->getRealIp();
  72. $user->login_num = Db::raw("login_num+1");
  73. $user->save();
  74. return [1,'success',$user->toArray()];
  75. }
  76. /**
  77. * 获取代理信息
  78. * @param int $agentId
  79. * @return array
  80. */
  81. protected function getTypeUser(int $agentId = 0): array
  82. {
  83. return (new SaasStore)->where("poi_id",$agentId)->findOrEmpty()->toArray();
  84. }
  85. #[GetMapping('profile')]
  86. public function getLoginUser()
  87. {
  88. try {
  89. $userData = (new Auth)->guard("admin")->user()->toArray();
  90. if (isset($userData['password'])) unset($userData['password']);
  91. $agent = [];
  92. if (empty($agent['vip_end']))
  93. {
  94. $userData['vip_end'] = 0;
  95. } else {
  96. $userData['vip_end'] = strtotime($agent['vip_end']);
  97. }
  98. return successTrans("success.data",[
  99. "username" => $userData['username'],
  100. "truename" => $userData['truename'],
  101. "vip_at" => $userData['vip_end'],
  102. "agent_id" => $userData['agent_id'],
  103. "super" => $userData['is_super'],
  104. "shop" => $agent['shop_name']??'',
  105. "type" => $userData['type']
  106. ]);
  107. } catch (\Throwable $throwable) {
  108. return error($throwable->getMessage());
  109. }
  110. }
  111. #[GetMapping('data')]
  112. public function getSystemData(): Response
  113. {
  114. try {
  115. $captcha = Captcha::base64();
  116. $service = $this->config->where("type","service")->column("value","name");
  117. return successTrans("success.data",compact("captcha",'service'));
  118. } catch (\Throwable $throwable) {
  119. return error($throwable->getMessage());
  120. }
  121. }
  122. }