BaseController.php 2.2 KB

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