BaseController.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\plugins\adopt\controllers;
  8. use app\models\Plugins;
  9. use app\models\WechatConfig;
  10. use EasyWeChat\Factory;
  11. use yii\base\Module;
  12. use yii\web\Controller;
  13. class BaseController extends Controller
  14. {
  15. /**
  16. * 原路由
  17. * @var string
  18. */
  19. public $route = null;
  20. public function __construct(string $id, Module $module, array $config = [])
  21. {
  22. $this->route = $config['callback'];
  23. $route = isset($_GET['r']) ? $_GET['r'] : null;
  24. if ($route && strpos($route, 'plugin') !== false) {
  25. if ($from = explode('/', $route)[0]) {
  26. switch ($from) {
  27. case 'client':
  28. $this->clientEvent();
  29. break;
  30. case 'admin':
  31. $this->adminEvent();
  32. break;
  33. default:
  34. break;
  35. }
  36. }
  37. }
  38. }
  39. /**
  40. * 客户端接口事件绑定
  41. */
  42. public function clientEvent() {
  43. $plugin = explode('/', $this->route)[0];
  44. if (!$plugin) {
  45. return;
  46. }
  47. $plugins_data = cache()->get('plugins_data_cache');
  48. // 取缓存
  49. if (!$plugins_data) {
  50. $plugins_data = Plugins::find()->asArray()->all();
  51. if (!$plugins_data) {
  52. return;
  53. }
  54. \Yii::$app->cache->set('plugins_data_cache', $plugins_data, 3600);
  55. }
  56. $temp_key = array_column($plugins_data, 'key');
  57. $data = array_combine($temp_key, $plugins_data);
  58. if (isset($data[$plugin]) && $data[$plugin]['status'] == Plugins::STATUS_OPEN) {
  59. require __DIR__ .'/../PluginInit.php';
  60. $str = '\app\plugins\%s\PluginInit';
  61. $class = sprintf($str, $plugin);
  62. if (method_exists($class, 'init')) {
  63. call_user_func($class. '::init', ['route' => $this->route]);
  64. }
  65. }
  66. }
  67. /**
  68. * 后台接口事件绑定
  69. */
  70. public function adminEvent() {
  71. }
  72. public function init ()
  73. {
  74. parent::init();
  75. }
  76. }