WeChatRequestService.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\common\service\wechat;
  15. use app\common\logic\BaseLogic;
  16. use WpOrg\Requests\Requests;
  17. /**
  18. * 自定义微信请求
  19. * Class WeChatRequestService
  20. * @package app\common\service\wechat
  21. */
  22. class WeChatRequestService extends BaseLogic
  23. {
  24. /**
  25. * @notes 获取网站扫码登录地址
  26. * @param $appId
  27. * @param $redirectUri
  28. * @param $state
  29. * @return string
  30. * @author 段誉
  31. * @date 2022/10/20 18:20
  32. */
  33. public static function getScanCodeUrl($appId, $redirectUri, $state)
  34. {
  35. $url = 'https://open.weixin.qq.com/connect/qrconnect?';
  36. $url .= 'appid=' . $appId . '&redirect_uri=' . $redirectUri . '&response_type=code&scope=snsapi_login';
  37. $url .= '&state=' . $state . '#wechat_redirect';
  38. return $url;
  39. }
  40. /**
  41. * @notes 通过code获取用户信息(access_token,openid,unionid等)
  42. * @param $code
  43. * @return mixed
  44. * @author 段誉
  45. * @date 2022/10/21 10:16
  46. */
  47. public static function getUserAuthByCode($code)
  48. {
  49. $config = WeChatConfigService::getOpConfig();
  50. $url = 'https://api.weixin.qq.com/sns/oauth2/access_token';
  51. $url .= '?appid=' . $config['app_id'] . '&secret=' . $config['secret'] . '&code=' . $code;
  52. $url .= '&grant_type=authorization_code';
  53. $requests = Requests::get($url);
  54. return json_decode($requests->body, true);
  55. }
  56. /**
  57. * @notes 通过授权信息获取用户信息
  58. * @param $accessToken
  59. * @param $openId
  60. * @return mixed
  61. * @author 段誉
  62. * @date 2022/10/21 10:21
  63. */
  64. public static function getUserInfoByAuth($accessToken, $openId)
  65. {
  66. $url = 'https://api.weixin.qq.com/sns/userinfo';
  67. $url .= '?access_token=' . $accessToken . '&openid=' . $openId;
  68. $response = Requests::get($url);
  69. return json_decode($response->body, true);
  70. }
  71. }