WorkerWeChatMnpService.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace app\common\service\wechat;
  3. use EasyWeChat\Kernel\Exceptions\Exception;
  4. use EasyWeChat\MiniApp\Application;
  5. /**
  6. * 微信功能类
  7. * Class WeChatMnpService
  8. * @package app\common\service
  9. */
  10. class WorkerWeChatMnpService
  11. {
  12. protected $app;
  13. protected $config;
  14. public function __construct()
  15. {
  16. $this->config = $this->getConfig();
  17. $this->app = new Application($this->config);
  18. }
  19. /**
  20. * @notes 配置
  21. * @return array
  22. * @throws \Exception
  23. * @author 段誉
  24. * @date 2023/2/27 12:03
  25. */
  26. protected function getConfig()
  27. {
  28. $config = WorkerWeChatConfigService::getMnpConfig();
  29. if (empty($config['app_id']) || empty($config['secret'])) {
  30. throw new \Exception('请先设置小程序配置');
  31. }
  32. return $config;
  33. }
  34. /**
  35. * @notes 小程序-根据code获取微信信息
  36. * @param string $code
  37. * @return array
  38. * @throws Exception
  39. * @throws \EasyWeChat\Kernel\Exceptions\HttpException
  40. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  41. * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
  42. * @throws \Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface
  43. * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
  44. * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
  45. * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
  46. * @author 段誉
  47. * @date 2023/2/27 11:03
  48. */
  49. public function getMnpResByCode(string $code)
  50. {
  51. $utils = $this->app->getUtils();
  52. $response = $utils->codeToSession($code);
  53. if (!isset($response['openid']) || empty($response['openid'])) {
  54. throw new Exception('获取openID失败');
  55. }
  56. return $response;
  57. }
  58. /**
  59. * @notes 获取手机号
  60. * @param string $code
  61. * @return \EasyWeChat\Kernel\HttpClient\Response|\Symfony\Contracts\HttpClient\ResponseInterface
  62. * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
  63. * @author 段誉
  64. * @date 2023/2/27 11:46
  65. */
  66. public function getUserPhoneNumber(string $code)
  67. {
  68. return $this->app->getClient()->postJson('wxa/business/getuserphonenumber', [
  69. 'code' => $code,
  70. ]);
  71. }
  72. public function getUnlimitedQRCode($scene,$page,$env_version='release',$check_path=true)
  73. {
  74. return $this->app->getClient()->postJson('wxa/getwxacodeunlimit', [
  75. 'scene' => $scene,
  76. 'page'=>$page,
  77. 'env_version'=>$env_version,//trial小程序版本
  78. 'check_path'=>$check_path
  79. ]);
  80. }
  81. }