Login.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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['store_id']);
  61. if (empty($typeUser)) return [0,trans("empty.agent"),[]];
  62. if ($typeUser['status'] <> 1) return [0,trans("error.agent"),[]];
  63. if (time() > strtotime($typeUser['vip_end'])) return [0,trans("error.agent-out"),[]];
  64. $user['shop_name'] = $typeUser['poi_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 (new SaasStore)->where("poi_id",$agentId)->findOrEmpty()->toArray();
  83. }
  84. #[GetMapping('profile')]
  85. public function getLoginUser(): Response
  86. {
  87. try {
  88. $userData = (new Auth)->guard("admin")->user()->toArray();
  89. if (isset($userData['password'])) unset($userData['password']);
  90. $agent = (new SaasStore)->where("poi_id",$userData['store_id'])->findOrEmpty();
  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. "userId" => $userData['id'],
  100. "truename" => $userData['truename'],
  101. "vip_at" => $userData['vip_end'],
  102. "agent_id" => $userData['agent_id'],
  103. "store_id" => $userData['store_id'],
  104. "super" => $userData['is_super'],
  105. "shop" => $agent['poi_name']??'',
  106. "trade" => $agent['trade_type']??0,
  107. "type" => $userData['type'],
  108. "logo" => $agent['poi_logo']??'-'
  109. ]);
  110. } catch (\Throwable $throwable) {
  111. return error($throwable->getMessage());
  112. }
  113. }
  114. #[GetMapping('data')]
  115. public function getSystemData(): Response
  116. {
  117. try {
  118. $captcha = Captcha::base64();
  119. $service = $this->config->where("type","service")->column("value","name");
  120. return successTrans("success.data",compact("captcha",'service'));
  121. } catch (\Throwable $throwable) {
  122. return error($throwable->getMessage());
  123. }
  124. }
  125. }