You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

134 lines
3.1 KiB

  1. <?php
  2. namespace Kuxin\Oauth;
  3. use Kuxin\Helper\Http;
  4. use Kuxin\Helper\Json;
  5. class Weixin extends Oauth{
  6. /**
  7. * 获取requestCode的api接口
  8. *
  9. * @var string
  10. */
  11. protected $getRequestCodeURL = 'https://open.weixin.qq.com/connect/qrconnect';
  12. /**
  13. * 获取access_token的api接口
  14. *
  15. * @var string
  16. */
  17. protected $getAccessTokenURL = 'https://api.weixin.qq.com/sns/oauth2/access_token';
  18. /**
  19. * 获取request_code的额外参数,可在配置中修改 URL查询字符串格式
  20. *
  21. * @var array
  22. */
  23. protected $authorizeParam = [
  24. //'scope'=>'all'
  25. //'forcelogin' => 'true',
  26. ];
  27. /**
  28. * 获取accesstoekn时候的附加参数
  29. *
  30. * @var array
  31. */
  32. protected $getTokenParam = [
  33. ];
  34. /**
  35. * API根路径
  36. *
  37. * @var string
  38. */
  39. protected $apiBase = 'https://api.weixin.qq.com/';
  40. /**
  41. * 构造函数
  42. *
  43. * @param array $config
  44. * @param null $token
  45. */
  46. public function __construct(array $config, $token = null)
  47. {
  48. parent::__construct($config, $token);
  49. $this->getTokenParam = [
  50. 'appid' => $this->appid,
  51. 'secret' => $this->appsecret,
  52. ];
  53. }
  54. /**
  55. * 组装接口调用参数 并调用接口
  56. *
  57. * @param string $api 微博API
  58. * @param array $param 调用API的额外参数
  59. * @param string $method HTTP请求方法 默认为GET
  60. * @param bool $multi
  61. * @return string json
  62. */
  63. public function call($api, $param = [], $method = 'GET', $multi = false)
  64. {
  65. /* 腾讯QQ调用公共参数 */
  66. $params = [
  67. 'access_token' => $this->token,
  68. ];
  69. $params = array_merge($params, $param);
  70. $data = Http::get($this->apiBase . $api, $params);
  71. return Json::decode($data, true);
  72. }
  73. /**
  74. * 解析access_token方法请求后的返回值
  75. *
  76. * @param $result
  77. * @return mixed
  78. * @throws \Exception
  79. */
  80. protected function parseToken($result)
  81. {
  82. $data = Json::decode($result, true);
  83. if (isset($data['access_token'])) {
  84. $this->token = $data['access_token'];
  85. $this->openid = $data['openid'];
  86. return [
  87. 'openid' => $this->openid,
  88. 'token' => $this->token,
  89. 'expires' => $data['expires_in'],
  90. 'refresh' => $data['refresh_token'],
  91. ];
  92. } else
  93. return "获取 ACCESS_TOKEN 出错:{$result}";
  94. }
  95. /**
  96. * 获取openid
  97. *
  98. * @return mixed
  99. * @throws \Exception
  100. */
  101. public function getOpenId()
  102. {
  103. if ($this->openid) return $this->openid;
  104. return false;
  105. }
  106. /**
  107. * 获取用户信息
  108. *
  109. * @return array
  110. */
  111. public function getInfo()
  112. {
  113. $data = $this->call('sns/userinfo', ['openid' => $this->getOpenId()]);
  114. return [
  115. 'id' => $this->openid,
  116. 'name' => $data['nickname'],
  117. 'gender' => $data['sex'],
  118. 'avatar' => $data['headimgurl'],
  119. ];
  120. }
  121. }