WeChatMnpService.php 3.1 KB

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