WechatJsapi.php 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * @note: 公众号相关
  4. * @author: jdzor <895947580@qq.com>
  5. * @date: 2019/4/2
  6. */
  7. namespace payment\wechat;
  8. /**
  9. * 接口文档:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842
  10. *
  11. * Class WechatJsapi
  12. * @package app\common\extend\wechat
  13. */
  14. class WechatJsapi extends BaseWechat
  15. {
  16. const AUTHORIZE_URL = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=%s&state=STATE#wechat_redirect";
  17. const ACCESS_TOKEN_URL = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code";
  18. const REFRESH_TOKEN_URL = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=%s&grant_type=refresh_token&refresh_token=%s";
  19. const USERINFO_URL = "https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s&lang=zh_CN";
  20. /*
  21. 1、以snsapi_base为scope发起的网页授权,是用来获取进入页面的用户的openid的,并且是静默授权并自动跳转到回调页的。用户感知的就是直接进入了回调页(往往是业务页面)
  22. 2、以snsapi_userinfo为scope发起的网页授权,是用来获取用户的基本信息的。但这种授权需要用户手动同意,并且由于用户同意过,所以无须关注,就可在授权后获取该用户的基本信息。
  23. 3、用户管理类接口中的“获取用户基本信息接口”,是在用户和公众号产生消息交互或关注后事件推送后,
  24. 才能根据用户openid来获取用户基本信息。这个接口,包括其他微信接口,都是需要该用户(即openid)关注了公众号后,才能调用成功的。
  25. */
  26. const BASE_SCOPE = 'snsapi_base';
  27. const USERINFO_SCOPE = 'snsapi_userinfo';
  28. private $jsapiAppId;
  29. private $jsapiSecret;
  30. public function __construct($config)
  31. {
  32. parent::__construct($config);
  33. isset($config['jsapi_appid']) || json_error('缺少参数:jsapi_appid');
  34. isset($config['jsapi_secret']) || json_error('缺少参数:jsapi_secret');
  35. $this->jsapiAppId = $config['jsapi_appid'];
  36. $this->jsapiSecret = $config['jsapi_secret'];
  37. }
  38. //获取code
  39. public function getCode($redirectUrl, $scope = self::BASE_SCOPE)
  40. {
  41. $authorizeUrl = sprintf(self::AUTHORIZE_URL, $this->jsapiAppId, urlencode($redirectUrl), $scope);
  42. //重定向请求微信用户信息
  43. header("Location: " . $authorizeUrl);
  44. }
  45. //获取access_token
  46. public function getAccessToken($code)
  47. {
  48. $accessTokenUrl = sprintf(self::ACCESS_TOKEN_URL, $this->jsapiAppId, $this->jsapiSecret, $code);
  49. return $this->getCurl($accessTokenUrl);
  50. }
  51. //刷新access_token
  52. public function refreshAccessToken($refreshToken)
  53. {
  54. $refreshTokenUrl = sprintf(self::REFRESH_TOKEN_URL, $this->jsapiAppId, $refreshToken);
  55. return $this->getCurl($refreshTokenUrl);
  56. }
  57. //获取用户信息
  58. public function getUserInfo($accessToken, $openid)
  59. {
  60. $userInfoUrl = sprintf(self::USERINFO_URL, $accessToken, $openid);
  61. return $this->getCurl($userInfoUrl);
  62. }
  63. //生成微信公众号分享参数
  64. public function getShareData()
  65. {
  66. $data['appId'] = $this->appId;
  67. $data['nonceStr'] = $this->getRandomString();
  68. $data['timeStamp'] = time();
  69. $data['paySign'] = $this->makeSign($data);
  70. return $data;
  71. }
  72. }