海南旅游SAAS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.0 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <?php
  2. namespace App\Service;
  3. use App\Models\AdminSetting;
  4. use EasyWeChat\Factory;
  5. use EasyWeChat\OpenPlatform\Application;
  6. use EasyWeChat\OpenPlatform\Server\Guard;
  7. /**
  8. * 微信第三方开放平台
  9. * Class OpenPlatform
  10. * @package App\Service
  11. */
  12. class OpenPlatform
  13. {
  14. public array $config = [];
  15. private Application $openPlatform;
  16. // private static self $instance;
  17. public function __construct()
  18. {
  19. $setting = AdminSetting::val(['service_appid', 'service_appsecret', 'service_token', 'service_aeskey']);
  20. $this->config = [
  21. 'app_id' => $setting['service_appid'],
  22. 'secret' => $setting['service_appsecret'],
  23. 'token' => $setting['service_token'],
  24. 'aes_key' => $setting['service_aeskey'],
  25. ];
  26. $this->openPlatform = Factory::openPlatform($this->config);
  27. }
  28. public function openPlatform(): Application
  29. {
  30. return $this->openPlatform;
  31. }
  32. /*public static function getInstance(): self
  33. {
  34. if (!self::$instance instanceof self) {
  35. self::$instance = new self();
  36. }
  37. return self::$instance;
  38. }*/
  39. public function refreshToken($appid)
  40. {
  41. return $this->openPlatform->getAuthorizer($appid)['authorization_info']['authorizer_refresh_token'] ?? null;
  42. }
  43. public function miniProgram($appid, $refreshToken = null)
  44. {
  45. if (is_null($refreshToken)) {
  46. $refreshToken = $this->refreshToken($appid);
  47. }
  48. return $this->openPlatform->miniProgram($appid, $refreshToken);
  49. }
  50. public function codeTemplate()
  51. {
  52. return $this->openPlatform['code_template'] ?? null;
  53. }
  54. public function code($appid, $refreshToken = null)
  55. {
  56. if (is_null($refreshToken)) {
  57. $refreshToken = $this->refreshToken($appid);
  58. }
  59. return $this->openPlatform->miniProgram($appid, $refreshToken)['code'] ?? null;
  60. }
  61. public function component()
  62. {
  63. return $this->openPlatform['component'] ?? null;
  64. }
  65. public function server(): Guard
  66. {
  67. return $this->openPlatform->server;
  68. }
  69. public function __call($method, $args)
  70. {
  71. return $this->openPlatform->$method(...$args);
  72. }
  73. public function __get(string $name)
  74. {
  75. return $this->openPlatform->$name;
  76. }
  77. }