MiniApp.php 3.2 KB

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