MiniApp.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. declare(strict_types=1);
  3. namespace SixShop\Wechat\Service;
  4. use EasyWeChat\MiniApp\Application;
  5. use think\exception\InvalidArgumentException;
  6. use think\facade\Cache;
  7. class MiniApp extends Application
  8. {
  9. public function __construct(string $appID = null)
  10. {
  11. $rawConfig = extension_config('wechat');
  12. if (!isset($rawConfig['app_id'], $rawConfig['secret'])) {
  13. throw new InvalidArgumentException('请先配置微信小程序信息, 插件管理中配置扩展信息app_id和secret');
  14. }
  15. $config = [
  16. 'app_id' => $rawConfig['app_id'],
  17. 'secret' => $rawConfig['secret'],
  18. ];
  19. if ($appID && $appID != $config['app_id']) {
  20. if (!isset($rawConfig['extend'])) {
  21. throw new InvalidArgumentException('请确认配置的微信小程序信息是否正确');
  22. }
  23. foreach ($rawConfig['extend'] as $item) {
  24. if ($item['app_id'] == $appID) {
  25. $config = [
  26. 'app_id' => $item['app_id'],
  27. 'secret' => $item['secret'],
  28. ];
  29. break;
  30. }
  31. }
  32. }
  33. $config['use_stable_access_token'] = true;
  34. parent::__construct($config);
  35. $this->setCache(Cache::instance());
  36. }
  37. }