123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- <?php
- namespace App\Services\Base;
- use App\Http\Middleware\SingleLoginLimit;
- use App\Repositories\Eloquent\Base\AdminRepositoryEloquent;
- use App\Repositories\Eloquent\Base\AuthRepositoryEloquent;
- use App\Repositories\Enums\ModelStatusEnum;
- use App\Repositories\Models\Base\Auth;
- use App\Support\Traits\WeChat;
- use Carbon\Carbon;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Cache;
- class AuthService
- {
- use WeChat;
- /**
- * @var AuthRepositoryEloquent
- */
- private $authRepository;
- private $adminRepository;
- /**
- * AuthService constructor.
- *
- * @param AuthRepositoryEloquent $authRepositoryEloquent
- */
- public function __construct(AuthRepositoryEloquent $authRepositoryEloquent, AdminRepositoryEloquent $adminRepositoryEloquent)
- {
- $this->authRepository = $authRepositoryEloquent;
- $this->adminRepository = $adminRepositoryEloquent;
- }
- /**
- *
- * @param Request $request
- * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|\Illuminate\Support\Collection|mixed
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
- * @throws \Prettus\Validator\Exceptions\ValidatorException
- */
- public function handleMiniProgramLogin(Request $request)
- {
- $code = $request->code;
- $appid = $request->appid;
- $type = (int)$request->get('type', 0);
- list($session_key, $openID, $unionid) = $this->miniProgramByCodeGetOpenId($appid, $code, $type);
- $auth = Auth::query()->where([
- 'identifier' => $appid,
- 'type' => $type,
- 'credential' => $openID,
- ])->first();
- if ($auth) {
- Cache::put("cache:service:auth:session_key:api:" . $auth['id'], $session_key, Carbon::now()->addDay());
- return $auth;
- }
- $auth = $this->authRepository->create([
- 'identifier' => $appid,
- 'type' => $type,
- 'credential' => $openID,
- 'user_id' => 0,
- ]);
- Cache::put("cache:service:auth:session_key:api:" . $auth['id'], $session_key, Carbon::now()->addDay());
- return $auth;
- }
- /**
- *
- * @param Request $request
- * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|\Illuminate\Support\Collection|mixed
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
- * @throws \Prettus\Validator\Exceptions\ValidatorException
- */
- public function handleIsMiniProgramLogin(Request $request)
- {
- $code = $request->code;
- $appid = $request->appid;
- $type = (int)$request->get('type', 0);
- list($session_key, $openID, $unionid) = $this->miniProgramByCodeGetOpenId($appid, $code, $type);
- $auth = Auth::query()->where([
- 'identifier' => $appid,
- 'type' => $type,
- 'credential' => $openID,
- ])->first();
- if ($auth) {
- Cache::put("cache:service:auth:session_key:api:" . $auth['id'], $session_key, Carbon::now()->addDay());
- return $auth;
- }
- return false;
- }
- /**
- * 刷新token
- * @return mixed|object
- */
- public function handleRefreshToken()
- {
- $token = auth()->refresh();
- return $token;
- }
- }
|