Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

113 righe
3.0 KiB

  1. <?php
  2. namespace backend\modules\motorcade\controllers;
  3. use Yii;
  4. use common\models\BaseRole;
  5. use yii\filters\AccessControl;
  6. use yii\helpers\Url;
  7. use yii\web\Controller;
  8. use yii\web\ForbiddenHttpException;
  9. use yii\filters\VerbFilter;
  10. use yii\web\View;
  11. define('LOG_TYPE_ORDER', 1);
  12. /**
  13. * BusController implements the CRUD actions for BaseBus model.
  14. * @property bool $accessControl 是否启用权限控制
  15. */
  16. class BaseController extends Controller
  17. {
  18. public $accessControl = false;
  19. public $layout = 'main';
  20. /**
  21. * @inheritdoc
  22. */
  23. public function behaviors()
  24. {
  25. $result = [
  26. 'verbs' => [
  27. 'class' => VerbFilter::className(),
  28. 'actions' => [
  29. 'delete' => ['POST'],
  30. ],
  31. ]
  32. ];
  33. if ($this->accessControl) {
  34. $result['access'] = [
  35. 'class' => AccessControl::className(),
  36. // 'only' => [Yii::$app->controller->action->id],
  37. 'rules' => [
  38. [
  39. 'actions' => [$this->getActionId()],
  40. 'allow' => true,
  41. 'matchCallback' => function ($rule, $action) {
  42. return true;
  43. }
  44. ],
  45. ],
  46. ];
  47. }
  48. return $result;
  49. }
  50. /**
  51. * User: wangxj
  52. *
  53. * 控制器结束后调用函数
  54. *
  55. * @params
  56. *
  57. * @return
  58. */
  59. public function beforeAction($action)
  60. {
  61. $this->triggerTip();
  62. return parent::beforeAction($action);
  63. }
  64. /**
  65. * User: wangxj
  66. *
  67. * 注册显示右上角提示的js代码
  68. * 只支持 info,error,success,warning
  69. *
  70. */
  71. protected function triggerTip()
  72. {
  73. $flashes = \Yii::$app->session->getAllFlashes();
  74. if (count($flashes) > 0) {
  75. foreach ($flashes as $type => $flash) {
  76. if (is_array($flash)) {
  77. foreach ($flash as $msg) {
  78. $this->view->registerJs("z.showTip('{$type}','{$msg}')", View::POS_LOAD);
  79. }
  80. } else {
  81. $this->view->registerJs("z.showTip('{$type}','{$flash}')", View::POS_LOAD);
  82. }
  83. }
  84. }
  85. }
  86. /**
  87. * 获取当前控制器的action ID
  88. * @param $action string action url
  89. */
  90. public function getActionId()
  91. {
  92. if ($this->accessControl) {
  93. $actionID = Yii::$app->controller->action->id;
  94. $action = BaseRole::findOne(['ROLE_SYS' => BaseRole::ROLE_SYS_FO, 'CANCEL_FLAG' => 0, 'ROLE_NAME' => Url::to([$actionID])]);
  95. $access = Yii::$app->user->identity->MENU_PERMISSION;
  96. if ($action == null || !in_array($action->ID, $access)) {
  97. throw new ForbiddenHttpException();
  98. }
  99. } else {
  100. $actionID = Yii::$app->controller->action->id;
  101. }
  102. return $actionID;
  103. }
  104. }