Login.php 5.2 KB

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