Login.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace app\controller\exe;
  3. use app\extra\basic\Base;
  4. use app\middleware\ExeMiddleware;
  5. use app\model\saas\SaasShop;
  6. use app\model\system\SystemUser;
  7. use LinFly\Annotation\Route\Controller;
  8. use LinFly\Annotation\Route\Route;
  9. use Shopwwi\WebmanAuth\Auth;
  10. use support\Request;
  11. use support\Response;
  12. use think\facade\Db;
  13. use Tinywan\Captcha\Captcha;
  14. use LinFly\Annotation\Route\Middleware;
  15. #[Controller(prefix: "/exe/login")]
  16. class Login extends Base
  17. {
  18. /**
  19. * 登陆
  20. * @param Request $request
  21. * @return Response
  22. */
  23. #[Route(path: "user",methods: "post")]
  24. public function setLogin(Request $request): Response
  25. {
  26. try {
  27. $param = $this->_valid([
  28. "username.require" => trans("empty.user"),
  29. "password.require" => trans("empty.passwd"),
  30. ],"post");
  31. if (!is_array($param)) return error($param);
  32. $map = ["is_deleted" => 0,"username" => $param['username']];
  33. [$state,$msg,$user] = $this->checkLogin($map,2,$param);
  34. print_r($msg);
  35. if (!$state) return error($msg);
  36. return successTrans("success.login",$user);
  37. } catch (\Throwable $throwable) {
  38. return error($throwable->getMessage());
  39. }
  40. }
  41. /**
  42. * 登录验证处理
  43. * @param array $map
  44. * @param int $type
  45. * @param array $param
  46. * @return array
  47. */
  48. protected function checkLogin(array $map = [],int $type = 1,array $param = []): array
  49. {
  50. $user = (new SystemUser)->where($map)->findOrEmpty();
  51. if ($user->isEmpty()) return [0,trans("error.user-empty"),[]];
  52. if ($user['status'] <> 1) return [0,trans("error.user-status"),[]];
  53. if ($user['type'] > 1) {
  54. $typeUser = $this->getTypeUser($user['agent_id']);
  55. if (empty($typeUser)) return [0,trans("empty.agent"),[]];
  56. if ($typeUser['status'] <> 0) return [0,trans("error.agent"),[]];
  57. if (time() > strtotime($typeUser['vip_end'])) return [0,trans("error.agent-out"),[]];
  58. $user['shop_name'] = $typeUser['shop_name'];
  59. }
  60. if ($type == 2) {
  61. if (md5(md5($param['password']).$user['salt']) <> $user['password']) return [0,trans("error.passwd"),[]];
  62. }
  63. $user->login_at = getDateFull();
  64. $user->login_ip = request()->getRealIp();
  65. $user->login_num = Db::raw("login_num+1");
  66. $user->save();
  67. return [1,'success',$user->toArray()];
  68. }
  69. /**
  70. * 获取代理信息
  71. * @param int $agentId
  72. * @return array
  73. */
  74. protected function getTypeUser(int $agentId = 0): array
  75. {
  76. return (new SaasShop)->where("shop_id",$agentId)->findOrEmpty()->toArray();
  77. }
  78. /**
  79. * @return Response
  80. */
  81. #[Route(path: "profile",methods: "get"),Middleware(ExeMiddleware::class)]
  82. public function getLoginUser(Request $request): Response
  83. {
  84. try {
  85. $agent = (new SaasShop)->where("shop_id",$request->uuid)->findOrEmpty();
  86. return successTrans("success.data",[
  87. "vip_at" => empty($agent['vip_end'])?'无限期':date('Y-m-d',strtotime($agent['vip_end'])),
  88. "agent_id" => $agent['agent_id'],
  89. "shop" => $agent['shop_name']??'',
  90. "address" => $agent['shop_address']??'',
  91. "sys" => sConf("service.title"),
  92. "logo" => sConf("service.logo")
  93. ]);
  94. } catch (\Throwable $exception){
  95. return error($exception->getMessage());
  96. }
  97. }
  98. }