Wxqrcode.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. /**
  3. * Wxqrcode.php UTF-8
  4. * 微信网站扫码登陆Api
  5. *
  6. * @date : 2018/4/25 12:30
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. * https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419316505&token=&lang=zh_CN
  12. */
  13. namespace huolib\oauth\driver;
  14. use huolib\oauth\OAuth;
  15. use huolib\tool\Http;
  16. use think\Exception;
  17. class Wxqrcode extends OAuth {
  18. /**
  19. * 获取requestCode的api接口
  20. *
  21. * @var string
  22. */
  23. protected $request_code_url = 'https://open.weixin.qq.com/connect/qrconnect';
  24. /**
  25. * 获取Access token的api接口
  26. *
  27. * @var String
  28. */
  29. protected $access_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token';
  30. /**
  31. * 更新Access token的api接口
  32. *
  33. * @var String
  34. */
  35. protected $refresh_token_url = 'https://api.weixin.qq.com/sns/oauth2/refresh_token';
  36. /**
  37. * API根路径
  38. *
  39. * @var string
  40. */
  41. protected $api_base = 'https://api.weixin.qq.com/sns/';
  42. /**
  43. * 获取request_code的额外参数 URL查询字符串格式
  44. *
  45. * @var string
  46. */
  47. protected $authorize = 'snsapi_login';
  48. /**
  49. * WxQrcode constructor.
  50. *
  51. * @param array $config
  52. * @param array|null $token
  53. *
  54. * @throws \think\Exception
  55. */
  56. public function __construct(array $config = [], array $token = null) {
  57. $_config = $config;
  58. if (empty($config)) {
  59. if (file_exists(GLOBAL_CONF_PATH."extra/oauth/wxqrcode.php")) {
  60. $_config = include GLOBAL_CONF_PATH."extra/oauth/wxqrcode.php";
  61. } else {
  62. $_config = array();
  63. }
  64. }
  65. parent::__construct($_config, $token);
  66. }
  67. /**
  68. *
  69. * 第一步:请求CODE 跳转URL
  70. * 请求Authorize访问地址
  71. *
  72. * @param string $display
  73. *
  74. * @return string 跳转地址
  75. */
  76. public function getRequestCodeUrl($display = 'pc') {
  77. $params = array(
  78. 'appid' => $this->app_key,
  79. 'redirect_uri' => $this->callback,
  80. 'response_type' => $this->response_type,
  81. 'scope' => $this->authorize,
  82. 'state' => $this->getState(),
  83. );
  84. return $this->request_code_url.'?'.http_build_query($params).'#wechat_redirect';
  85. }
  86. /**
  87. * 第二步:通过code获取access_token
  88. *
  89. * @param $code
  90. * @param null $extend
  91. *
  92. * @return array|null
  93. * @throws Exception
  94. */
  95. public function getAccessToken($code, $extend = null) {
  96. $_params = array(
  97. 'appid' => $this->app_key,
  98. 'secret' => $this->app_secret,
  99. 'grant_type' => $this->grant_type,
  100. 'code' => $code,
  101. );
  102. $_rdata = Http::get($this->access_token_url, $_params);
  103. $this->token = $this->parseToken($_rdata);
  104. return $this->token;
  105. }
  106. /**
  107. * 刷新access_token有效期
  108. *
  109. * @param $refresh_token
  110. *
  111. * @return array|mixed|null
  112. * @throws Exception
  113. */
  114. public function getRefreshAccessToken($refresh_token) {
  115. $_params = array(
  116. 'appid' => $this->app_key,
  117. 'refresh_token' => $refresh_token,
  118. 'grant_type' => $this->grant_type,
  119. );
  120. $_rdata = Http::get($this->refresh_token_url, $_params);
  121. $this->token = $this->parseToken($_rdata);
  122. return $this->token;
  123. }
  124. /**
  125. * 第三步:通过access_token调用接口
  126. *
  127. * @param string $api API
  128. * @param string $param 调用API的额外参数
  129. * @param string $method HTTP请求方法 默认为GET
  130. * @param null $multi
  131. *
  132. * @return mixed
  133. * @throws Exception
  134. */
  135. public function call($api, $param = '', $method = 'GET', $multi = null) {
  136. $params = array(
  137. 'access_token' => $this->token['access_token'],
  138. 'openid' => $this->getOpenid(),
  139. 'lang' => 'zh_CN'
  140. );
  141. $data = Http::request($this->url($api), $params, $method);
  142. return json_decode($data, true);
  143. }
  144. /**
  145. * 获取授权用户的用户信息
  146. *
  147. * @return array
  148. * @throws \Exception
  149. * https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419316518&lang=zh_CN
  150. *
  151. * openid 普通用户的标识,对当前开发者帐号唯一
  152. * nickname 普通用户昵称
  153. * sex 普通用户性别,1为男性,2为女性
  154. * province 普通用户个人资料填写的省份
  155. * city 普通用户个人资料填写的城市
  156. * country 国家,如中国为CN
  157. * headimgurl 用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空
  158. * privilege 用户特权信息,json数组,如微信沃卡用户为(chinaunicom)
  159. * unionid 用户统一标识。针对一个微信开放平台帐号下的应用,同一用户的unionid是唯一的。
  160. *
  161. */
  162. public function getUserInfo() {
  163. $_response = $this->call('userinfo');
  164. if (empty($_response) || (isset($_response['errcode']) && $_response['errcode'] != 0)) {
  165. throw new Exception('接口访问失败!'.$_response['errmsg']);
  166. } else {
  167. $_oauth_data = array(
  168. 'openid' => $_response['openid'],
  169. 'unionid' => isset($_response['unionid']) ? $_response['unionid'] : '',
  170. 'channel' => 'wxqrcode',
  171. 'nickname' => $_response['nickname'],
  172. 'gender' => $_response['sex'],
  173. 'avatar' => $_response['headimgurl'],
  174. 'token' => $this->token
  175. );
  176. return $_oauth_data;
  177. }
  178. }
  179. /**
  180. * 获取当前授权用户的SNS标识
  181. *
  182. * @throws Exception
  183. */
  184. public function getOpenid() {
  185. $_data = $this->token;
  186. if (isset($_data['openid'])) {
  187. return $_data['openid'];
  188. } else {
  189. throw new Exception('没有获取到微信用户ID!');
  190. }
  191. }
  192. /**
  193. * @param string $refresh_token
  194. *
  195. * @return array
  196. */
  197. protected function getRefreshParam($refresh_token = '') {
  198. $_params = array(
  199. 'appid' => $this->app_key,
  200. 'grant_type' => 'refresh_token',
  201. 'refresh_token' => $refresh_token,
  202. );
  203. return $_params;
  204. }
  205. /**
  206. * 解析access_token方法请求后的返回值
  207. *
  208. * @param $result
  209. * @param null $extend
  210. *
  211. * @return mixed
  212. * @throws Exception
  213. */
  214. protected function parseToken($result, $extend = null) {
  215. $_rdata = json_decode($result, true);
  216. if ($_rdata['access_token'] && $_rdata['expires_in']) {
  217. $this->token = $_rdata;
  218. $_rdata['openid'] = $this->getOpenid();
  219. return $_rdata;
  220. } else {
  221. throw new Exception("获取微信 ACCESS_token 出错:{$result}");
  222. }
  223. }
  224. }