AuthService.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /**
  3. * 支付宝授权服务
  4. * Class AuthService
  5. * Author SHUI
  6. * @package alipayAop
  7. */
  8. define('AOP_SDK_DIR', __DIR__);
  9. require_once AOP_SDK_DIR . '/AopSdk.php';
  10. require_once AOP_SDK_DIR . '/AlipayConfig.php';
  11. require_once AOP_SDK_DIR . '/aop/AopClient.php';
  12. require_once AOP_SDK_DIR . '/aop/SignData.php';
  13. require_once AOP_SDK_DIR . '/aop/request/AlipaySystemOauthTokenRequest.php';
  14. class AuthService {
  15. public function getAlipayOauthUrl() {
  16. $param = [
  17. 'apiname' => 'com.alipay.account.auth',
  18. 'method' => 'alipay.open.auth.sdk.code.get',
  19. 'app_id' => AlipayConfig::APPID,
  20. 'app_name' => 'mc',
  21. 'biz_type' => 'openservice',
  22. 'pid' => AlipayConfig::PID,
  23. 'product_id' => 'APP_FAST_LOGIN',
  24. 'scope' => 'kuaijie',
  25. 'target_id' => md5(uniqid()),
  26. 'auth_type' => 'AUTHACCOUNT',
  27. 'sign_type' => 'RSA2',
  28. ];
  29. $aopClient = new \AopClient;
  30. $paramStr = $aopClient->getSignContent($param);
  31. $param['sign'] = $aopClient->alonersaSign($paramStr, AlipayConfig::APPPRIVATEKEY, 'RSA2');
  32. return $aopClient->getSignContentUrlencode($param);
  33. }
  34. public function getUseridFromAlipay($code) {
  35. $aop = new \AopClient;
  36. $aop->gatewayUrl = AlipayConfig::GATEWAY;
  37. $aop->appId = AlipayConfig::APPID;
  38. $aop->rsaPrivateKey = AlipayConfig::APPPRIVATEKEY;
  39. $aop->alipayrsaPublicKey = AlipayConfig::ALIPAYPUBLICKEY;
  40. $aop->apiVersion = '1.0';
  41. $aop->signType = 'RSA2';
  42. $aop->postCharset = 'UTF-8';
  43. $aop->format = 'json';
  44. $request = new \AlipaySystemOauthTokenRequest;
  45. $request->setGrantType('authorization_code');
  46. $request->setCode($code);
  47. $result = $aop->execute($request);
  48. $responseNode = str_replace('.', '_', $request->getApiMethodName()) . '_response';
  49. $userid = property_exists($result, $responseNode) ? $result->$responseNode->user_id : 0;
  50. return $userid;
  51. }
  52. }