小说推广数据系统

UserAuthorizeService.php 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: shensong
  5. * Date: 2021/5/13
  6. * Time: 13:43
  7. */
  8. namespace App\Services;
  9. use App\Log;
  10. use App\Models\UserAuthorization;
  11. class UserAuthorizeService
  12. {
  13. public static function requestToken($authCode)
  14. {
  15. $params['authorization_code'] = $authCode;
  16. $params['grant_type'] = 'authorization_code';
  17. $result = self::getToken($params);
  18. if(0 == $result['code']) {
  19. $result['data']['auth_code'] = $authCode;
  20. $res = UserAuthorization::inserOrUpdate(0, $result['data']);
  21. if($res) {
  22. return true;
  23. }
  24. }
  25. return false;
  26. }
  27. public static function refreshToken($id, $refreshToken)
  28. {
  29. $params['grant_type'] = 'refresh_token';
  30. $params['refresh_token'] = $refreshToken;
  31. $result = self::getToken($params);
  32. if(0 == $result['code']) {
  33. $res = UserAuthorization::inserOrUpdate($id, $result['data']);
  34. if($res) {
  35. return true;
  36. }
  37. }
  38. return false;
  39. }
  40. public static function getToken($params)
  41. {
  42. $route = '/oauth/token?';
  43. $clientConfig = config('client');
  44. $url = $clientConfig['request_url'] . $route;
  45. $params['client_id'] = $clientConfig['client_id'];
  46. $params['client_secret'] = $clientConfig['client_secret'];
  47. $params['redirect_uri'] = $clientConfig['redirect_uri'];
  48. $url = $url.http_build_query($params);
  49. $result = HttpService::httpGet($url);
  50. $result = json_decode($result, 1);
  51. if(0 == $result['code']) {
  52. Log::logInfo(json_encode($result), $params, 'token-success');
  53. } else {
  54. Log::logError(json_encode($result), $params, 'token-error');
  55. }
  56. return $result;
  57. }
  58. }