海南旅游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.

73 lines
1.7 KiB

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. /**
  7. * 微信第三方开放平台
  8. * Class OpenPlatform
  9. * @package App\Service
  10. */
  11. class OpenPlatform
  12. {
  13. public array $config = [];
  14. private Application $openPlatform;
  15. // private static self $instance;
  16. public function __construct()
  17. {
  18. $setting = AdminSetting::val(['service_appid', 'service_appsecret', 'service_token', 'service_aeskey']);
  19. $this->config = [
  20. 'app_id' => $setting['service_appid'],
  21. 'secret' => $setting['service_appsecret'],
  22. 'token' => $setting['service_token'],
  23. 'aes_key' => $setting['service_aeskey'],
  24. ];
  25. $this->openPlatform = Factory::openPlatform($this->config);
  26. return $this->openPlatform;
  27. }
  28. /*public static function getInstance(): self
  29. {
  30. if (!self::$instance instanceof self) {
  31. self::$instance = new self();
  32. }
  33. return self::$instance;
  34. }*/
  35. public function refreshToken($appid)
  36. {
  37. return $this->openPlatform->getAuthorizer($appid)['authorization_info']['authorizer_refresh_token'] ?? null;
  38. }
  39. public function miniProgram($appid, $refreshToken = null)
  40. {
  41. if (is_null($refreshToken)) {
  42. $refreshToken = $this->refreshToken($appid);
  43. }
  44. return $this->openPlatform->miniProgram($appid, $refreshToken);
  45. }
  46. public function codeTemplate()
  47. {
  48. return $this->openPlatform['code_template'] ?? null;
  49. }
  50. public function code($appid, $refreshToken = null)
  51. {
  52. if (is_null($refreshToken)) {
  53. $refreshToken = $this->refreshToken($appid);
  54. }
  55. return $this->openPlatform->miniProgram($appid, $refreshToken)['code'] ?? null;
  56. }
  57. public function component()
  58. {
  59. return $this->openPlatform['component'] ?? null;
  60. }
  61. }