AppadminController.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * FecShop file.
  4. *
  5. * @link http://www.fecshop.com/
  6. * @copyright Copyright (c) 2016 FecShop Software LLC
  7. * @license http://www.fecshop.com/license/
  8. */
  9. namespace fecshop\app\appadmin\modules;
  10. use fec\controllers\FecController;
  11. use fec\helpers\CConfig;
  12. use Yii;
  13. use yii\base\InvalidValueException;
  14. use yii\web\Controller;
  15. /**
  16. * @author Terry Zhao <2358269014@qq.com>
  17. * @since 1.0
  18. */
  19. class AppadminController extends Controller
  20. {
  21. public $blockNamespace;
  22. public $enableCsrfValidation = true;
  23. /**
  24. * init theme component property : $fecshopThemeDir and $layoutFile
  25. * $fecshopThemeDir is appfront base theme directory.
  26. * layoutFile is current layout relative path.
  27. */
  28. public function init()
  29. {
  30. if (Yii::$app->user->isGuest) {
  31. Yii::$service->url->redirectByUrlKey('/fecadmin/login/index');
  32. }
  33. if (!Yii::$service->page->theme->fecshopThemeDir) {
  34. Yii::$service->page->theme->fecshopThemeDir = Yii::getAlias(CConfig::param('appadminBaseTheme'));
  35. }
  36. if (!Yii::$service->page->theme->layoutFile) {
  37. Yii::$service->page->theme->layoutFile = CConfig::param('appadminBaseLayoutName');
  38. }
  39. // 设置本地模板路径
  40. $localThemeDir = Yii::$app->params['localThemeDir'];
  41. if($localThemeDir){
  42. Yii::$service->page->theme->setLocalThemeDir($localThemeDir);
  43. }
  44. // 设置third模板路径
  45. $thirdThemeDir = Yii::$app->params['thirdThemeDir'];
  46. if($thirdThemeDir){
  47. Yii::$service->page->theme->setThirdThemeDir($thirdThemeDir);
  48. }
  49. /*
  50. * set i18n translate category.
  51. */
  52. Yii::$service->page->translate->category = 'appadmin';
  53. /*
  54. * 自定义Yii::$classMap,用于重写
  55. */
  56. }
  57. public function beforeAction($action)
  58. {
  59. if (parent::beforeAction($action)) {
  60. $moduleId = Yii::$app->controller->module->id;
  61. $controllerId = $this->id;
  62. $actionId = $this->action->id;
  63. $currentUrlKey = "/$moduleId/$controllerId/$actionId";
  64. $resources = Yii::$service->admin->role->getCurrentRoleResources();
  65. if (is_array($resources) && isset($resources[$currentUrlKey]) && $resources[$currentUrlKey]) {
  66. Yii::$service->admin->systemLog->save();
  67. return true;
  68. } else {
  69. echo json_encode([
  70. 'statusCode' => '300',
  71. 'message' => 'you do not have role',
  72. ]);
  73. exit;
  74. }
  75. }
  76. return false;
  77. }
  78. /**
  79. * @param $view|string , (only) view file name ,by this module id, this controller id , generate view relative path.
  80. * @param $params|Array,
  81. * 1.get exist view file from mutil theme by theme protity.
  82. * 2.get content by yii view compontent function renderFile() ,
  83. */
  84. public function render($view, $params = [])
  85. {
  86. $viewFile = Yii::$service->page->theme->getViewFile($view);
  87. $content = Yii::$app->view->renderFile($viewFile, $params, $this);
  88. return $this->renderContent($content);
  89. }
  90. /**
  91. * Get current layoutFile absolute path from mutil theme dir by protity.
  92. */
  93. public function findLayoutFile($view)
  94. {
  95. $layoutFile = '';
  96. $relativeFile = 'layouts/'.Yii::$service->page->theme->layoutFile;
  97. $absoluteDir = Yii::$service->page->theme->getThemeDirArr();
  98. foreach ($absoluteDir as $dir) {
  99. if ($dir) {
  100. $file = $dir.'/'.$relativeFile;
  101. if (file_exists($file)) {
  102. $layoutFile = $file;
  103. return $layoutFile;
  104. }
  105. }
  106. }
  107. throw new InvalidValueException('layout file is not exist!');
  108. }
  109. public function getFecadminBlock($blockname=''){
  110. $_currentNameSpace = \fec\helpers\CModule::param("_currentNameSpace");
  111. //echo $_currentNameSpace;exit;
  112. if(empty($_currentNameSpace)){
  113. $message = "Modules Param '_currentNameSpace' is not set , you can set like fecadmin\\Module";
  114. throw new \yii\web\HttpException(406,$message);
  115. }
  116. $modulesDir = "\\".$_currentNameSpace."\\block\\";
  117. $url_key = \fec\helpers\CUrl::getUrlKey();
  118. $url_key = trim($url_key,"/");
  119. $url_key = substr($url_key,strpos($url_key,"/")+1 );
  120. $url_key_arr = explode("/",$url_key);
  121. if(!isset($url_key_arr[1])) $url_key_arr[1] = 'index';
  122. if($blockname){
  123. $url_key_arr[count($url_key_arr)-1] = ucfirst($blockname);
  124. }else{
  125. $url_key_arr[count($url_key_arr)-1] = ucfirst($url_key_arr[count($url_key_arr)-1]);
  126. }
  127. $block_space = implode("\\",$url_key_arr);
  128. $blockFile = $modulesDir.$block_space;
  129. //查找是否在rewriteMap中存在重写
  130. //$relativeFile = Yii::mapGetName($relativeFile);
  131. $blockFile = Yii::mapGetName($blockFile);
  132. //echo $blockFile;exit;
  133. return new $blockFile;
  134. }
  135. public function getBlock($blockName = ''){
  136. if (!$blockName) {
  137. $blockName = $this->action->id;
  138. }
  139. if (!$this->blockNamespace) {
  140. $this->blockNamespace = Yii::$app->controller->module->blockNamespace;
  141. }
  142. if (!$this->blockNamespace) {
  143. throw new \yii\web\HttpException(406, 'blockNamespace is empty , you should config it in module->blockNamespace or controller blockNamespace ');
  144. }
  145. $viewId = $this->id;
  146. $viewId = str_replace('/', '\\', $viewId);
  147. $relativeFile = '\\'.$this->blockNamespace;
  148. $relativeFile .= '\\'.$viewId.'\\'.ucfirst($blockName);
  149. //查找是否在rewriteMap中存在重写
  150. $relativeFile = Yii::mapGetName($relativeFile);
  151. return new $relativeFile();
  152. }
  153. }