MiniApp.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace App\Master\Framework\Library\Easywechat;
  3. use App\Master\Enum\RedisKeyEnum;
  4. use App\Utils\RedisUtil;
  5. use EasyWeChat\MiniApp\Application;
  6. use EasyWeChat\Kernel\Contracts\Config;
  7. /**
  8. * 微信小程序开发组
  9. * class MiniAppService
  10. */
  11. class MiniApp extends EasyModule
  12. {
  13. protected Application $app;
  14. protected Config $config;
  15. /**
  16. * 实例化
  17. *
  18. */
  19. public function __construct()
  20. {
  21. parent::__construct();
  22. $this->app = $this->miniApp();
  23. $this->config = $this->app->getConfig();
  24. }
  25. /**
  26. * openid 授权
  27. *
  28. * @param string $code
  29. * @return bool
  30. * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
  31. */
  32. public function jscode2session(string $code): bool
  33. {
  34. $app = $this->app;
  35. $api = $app->getClient();
  36. $response = $api->get('/sns/jscode2session', [
  37. 'appid' => $this->config['app_id'],
  38. 'secret' => $this->config['secret'],
  39. 'js_code' => $code,
  40. 'grant_type' => 'authorization_code',
  41. ]);
  42. if (!$this->response($response)) {
  43. return false;
  44. }
  45. return true;
  46. }
  47. /**
  48. * 获取手机号
  49. *
  50. * @param string $code
  51. * @return bool
  52. * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
  53. */
  54. public function getUserPhone(string $code): bool
  55. {
  56. $app = $this->app;
  57. if (!$access_token = $this->getAccessToken()) {
  58. return false;
  59. }
  60. $api = $app->getClient();
  61. $response = $api->postJson("/wxa/business/getuserphonenumber?access_token={$access_token}", [
  62. 'code' => $code
  63. ]);
  64. if (!$this->response($response)) {
  65. return false;
  66. }
  67. return true;
  68. }
  69. /**
  70. * 获取 access_token
  71. * @return false|mixed|\Redis|string
  72. * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
  73. */
  74. private function getAccessToken()
  75. {
  76. if ($access_token = RedisUtil::getInstance(RedisKeyEnum::WX_MINI_APP_ACCESS_TOKEN)->get()) {
  77. return $access_token;
  78. }
  79. if (!$this->stable_token()) {
  80. return false;
  81. }
  82. $data = $this->get();
  83. if (empty($data['access_token'])) {
  84. return false;
  85. }
  86. RedisUtil::getInstance(RedisKeyEnum::WX_MINI_APP_ACCESS_TOKEN)->setex($data['access_token'], (int)($data['expires_in'] ?? 0));
  87. return $data['access_token'];
  88. }
  89. /**
  90. * 获取 access_token
  91. * @return bool
  92. * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
  93. */
  94. private function stable_token(): bool
  95. {
  96. $app = $this->app;
  97. $api = $app->getClient();
  98. $response = $api->postJson('/cgi-bin/stable_token', [
  99. 'grant_type' => 'client_credential',
  100. 'appid' => $this->config['app_id'],
  101. 'secret' => $this->config['secret'],
  102. 'force_refresh' => false,// 默认false:普通模式false;强制刷新模式true;
  103. ]);
  104. if (!$this->response($response)) {
  105. return false;
  106. }
  107. return true;
  108. }
  109. }