PluginController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace backend\controllers;
  3. use backend\models\ModuleConfig;
  4. use common\models\Module;
  5. use Yii;
  6. use yii\base\Model;
  7. use yii\data\ArrayDataProvider;
  8. use yii\filters\VerbFilter;
  9. use yii\helpers\Json;
  10. use yii\web\Controller;
  11. /**
  12. * ModuleController implements the CRUD actions for Module model.
  13. */
  14. class PluginController extends Controller
  15. {
  16. public function behaviors()
  17. {
  18. return [
  19. 'verbs' => [
  20. 'class' => VerbFilter::className(),
  21. 'actions' => [
  22. 'open' => ['post'],
  23. 'close' => ['post'],
  24. 'install' => ['post'],
  25. 'uninstall' => ['post']
  26. ],
  27. ],
  28. ];
  29. }
  30. /**
  31. * Lists all Module models.
  32. * @return mixed
  33. */
  34. public function actionIndex()
  35. {
  36. $plugins = Yii::$app->pluginManager->findAll();
  37. $dataProvider = new ArrayDataProvider([
  38. 'allModels' => $plugins
  39. ]);
  40. return $this->render('index', [
  41. 'dataProvider' => $dataProvider,
  42. ]);
  43. }
  44. // 安装
  45. public function actionInstall()
  46. {
  47. $id = Yii::$app->request->post('id');
  48. $pluginManager = Yii::$app->pluginManager;
  49. $plugin = $pluginManager->findOne($id);
  50. if(!$pluginManager->install($plugin)){
  51. Yii::$app->session->setFlash('error', '插件安装失败');
  52. } else {
  53. Yii::$app->session->setFlash('success', '插件安装成功');
  54. }
  55. return $this->redirect(['index']);
  56. }
  57. //卸载
  58. public function actionUninstall()
  59. {
  60. $id = Yii::$app->request->post('id');
  61. $pluginManager = Yii::$app->pluginManager;
  62. $plugin = $pluginManager->findOne($id);
  63. if(!$pluginManager->uninstall($plugin)){
  64. Yii::$app->session->setFlash('error', '插件卸载失败');
  65. } else {
  66. Yii::$app->session->setFlash('success', '插件卸载成功');
  67. }
  68. return $this->redirect(['index']);
  69. }
  70. // 开启
  71. public function actionOpen()
  72. {
  73. $id = Yii::$app->request->post('id');
  74. $pluginManager = Yii::$app->pluginManager;
  75. $plugin = $pluginManager->findOne($id);
  76. if(!$plugin->install){
  77. Yii::$app->session->setFlash('error', '插件没安装');
  78. }
  79. if(!$pluginManager->open($plugin)){
  80. Yii::$app->session->setFlash('error', '插件打开失败');
  81. } else {
  82. Yii::$app->session->setFlash('success', '插件打开成功');
  83. }
  84. return $this->redirect(['index']);
  85. }
  86. // 关闭
  87. public function actionClose()
  88. {
  89. $id = Yii::$app->request->post('id');
  90. $pluginManager = Yii::$app->pluginManager;
  91. $plugin = $pluginManager->findOne($id);
  92. if(!$plugin->install){
  93. Yii::$app->session->setFlash('error', '插件没安装');
  94. }
  95. if(!$pluginManager->close($plugin)){
  96. Yii::$app->session->setFlash('error', '插件关闭失败');
  97. } else {
  98. Yii::$app->session->setFlash('success', '插件关闭成功');
  99. }
  100. return $this->redirect(['index']);
  101. }
  102. /**
  103. * 插件配置
  104. * @param $id
  105. * @return string|\yii\web\Response
  106. * @throws \yii\base\InvalidConfigException
  107. */
  108. public function actionConfig($id)
  109. {
  110. $pluginManager = Yii::$app->pluginManager;
  111. $plugin = $pluginManager->findOne($id);
  112. if(!$plugin->install){
  113. Yii::$app->session->setFlash('error', '插件没安装');
  114. return $this->redirect(['index']);
  115. }
  116. $configs = Json::decode($plugin->getModel()->config);
  117. $configModels = [];
  118. if (!empty($configs)) {
  119. foreach ($configs as $k => $config) {
  120. $configModel = new ModuleConfig();
  121. $configModel->scenario = 'init';
  122. $configModel->attributes = $config;
  123. $configModels[$k] = $configModel;
  124. }
  125. }
  126. $model = $plugin->getModel();
  127. if (\Yii::$app->request->isPost && Model::loadMultiple($configModels, \Yii::$app->request->post()) && Model::validateMultiple($configModels)) {
  128. $configs = Json::encode($configModels);
  129. $model->config = $configs;
  130. $model->save();
  131. return $this->redirect(['index']);
  132. }
  133. return $this->render('config', [
  134. 'model' => $model,
  135. 'configModels' => $configModels
  136. ]);
  137. }
  138. }