Mp.php 7.8 KB

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