AuthService.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace App\Services\Base;
  3. use App\Http\Middleware\SingleLoginLimit;
  4. use App\Repositories\Eloquent\Base\AdminRepositoryEloquent;
  5. use App\Repositories\Eloquent\Base\AuthRepositoryEloquent;
  6. use App\Repositories\Enums\ModelStatusEnum;
  7. use App\Repositories\Models\Base\Auth;
  8. use App\Support\Traits\WeChat;
  9. use Carbon\Carbon;
  10. use Illuminate\Http\Request;
  11. use Illuminate\Support\Facades\Cache;
  12. class AuthService
  13. {
  14. use WeChat;
  15. /**
  16. * @var AuthRepositoryEloquent
  17. */
  18. private $authRepository;
  19. private $adminRepository;
  20. /**
  21. * AuthService constructor.
  22. *
  23. * @param AuthRepositoryEloquent $authRepositoryEloquent
  24. */
  25. public function __construct(AuthRepositoryEloquent $authRepositoryEloquent, AdminRepositoryEloquent $adminRepositoryEloquent)
  26. {
  27. $this->authRepository = $authRepositoryEloquent;
  28. $this->adminRepository = $adminRepositoryEloquent;
  29. }
  30. /**
  31. *
  32. * @param Request $request
  33. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|\Illuminate\Support\Collection|mixed
  34. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  35. * @throws \Prettus\Validator\Exceptions\ValidatorException
  36. */
  37. public function handleMiniProgramLogin(Request $request)
  38. {
  39. $code = $request->code;
  40. $appid = $request->appid;
  41. $type = (int)$request->get('type', 0);
  42. list($session_key, $openID, $unionid) = $this->miniProgramByCodeGetOpenId($appid, $code, $type);
  43. $auth = Auth::query()->where([
  44. 'identifier' => $appid,
  45. 'type' => $type,
  46. 'credential' => $openID,
  47. ])->first();
  48. if ($auth) {
  49. Cache::put("cache:service:auth:session_key:api:" . $auth['id'], $session_key, Carbon::now()->addDay());
  50. return $auth;
  51. }
  52. $auth = $this->authRepository->create([
  53. 'identifier' => $appid,
  54. 'type' => $type,
  55. 'credential' => $openID,
  56. 'user_id' => 0,
  57. ]);
  58. Cache::put("cache:service:auth:session_key:api:" . $auth['id'], $session_key, Carbon::now()->addDay());
  59. return $auth;
  60. }
  61. /**
  62. *
  63. * @param Request $request
  64. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|\Illuminate\Support\Collection|mixed
  65. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  66. * @throws \Prettus\Validator\Exceptions\ValidatorException
  67. */
  68. public function handleIsMiniProgramLogin(Request $request)
  69. {
  70. $code = $request->code;
  71. $appid = $request->appid;
  72. $type = (int)$request->get('type', 0);
  73. list($session_key, $openID, $unionid) = $this->miniProgramByCodeGetOpenId($appid, $code, $type);
  74. $auth = Auth::query()->where([
  75. 'identifier' => $appid,
  76. 'type' => $type,
  77. 'credential' => $openID,
  78. ])->first();
  79. if ($auth) {
  80. Cache::put("cache:service:auth:session_key:api:" . $auth['id'], $session_key, Carbon::now()->addDay());
  81. return $auth;
  82. }
  83. return false;
  84. }
  85. /**
  86. * 刷新token
  87. * @return mixed|object
  88. */
  89. public function handleRefreshToken()
  90. {
  91. $token = auth()->refresh();
  92. return $token;
  93. }
  94. }