AuthService.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. namespace App\Services\Vendor\QQprogram;
  3. use App\Exceptions\AlertException;
  4. use App\Jobs\WelcomeJob;
  5. use App\Models\Log\FpdxMiniprogramIntoQueueModel;
  6. use App\Services\Service;
  7. use Illuminate\Support\Arr;
  8. use Illuminate\Support\Carbon;
  9. use Ixudra\Curl\Facades\Curl;
  10. class AuthService extends Service
  11. {
  12. /**
  13. * 通过code换取session
  14. * @param string $jscode
  15. * @return Arr|mixed
  16. * @throws AlertException
  17. */
  18. public function code2Session(string $jscode)
  19. {
  20. $appid = config("qqprogram.app_id");
  21. $secret = config("qqprogram.app_secret");
  22. $url = "https://api.q.qq.com/sns/jscode2session?appid={$appid}&secret={$secret}&js_code={$jscode}&grant_type=authorization_code";
  23. $result = Curl::to($url)->asJsonResponse(true)->get();
  24. if (0 == $result['errcode']) {
  25. try {
  26. if ($result['openid'] && !FpdxMiniprogramIntoQueueModel::where('openid', $result['openid'])->exists()) {
  27. WelcomeJob::dispatch($result['openid'])->delay(Carbon::now()->addHours(1));
  28. FpdxMiniprogramIntoQueueModel::create(['openid' => $result['openid'], 'created_at' => time()]);
  29. }
  30. } catch (\Exception $exception) {
  31. }
  32. return $result;
  33. } else {
  34. throw new AlertException($result['errmsg'], $result['errcode']);
  35. }
  36. }
  37. }