AdminController.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | EasyAdmin
  4. // +----------------------------------------------------------------------
  5. // | PHP交流群: 763822524
  6. // +----------------------------------------------------------------------
  7. // | 开源协议 https://mit-license.org
  8. // +----------------------------------------------------------------------
  9. // | github开源项目:https://github.com/zhongshaofa/EasyAdmin
  10. // +----------------------------------------------------------------------
  11. namespace app\common\controller;
  12. use app\admin\service\ConfigService;
  13. use app\BaseController;
  14. use app\common\constants\AdminConstant;
  15. use app\common\service\AuthService;
  16. use EasyAdmin\tool\CommonTool;
  17. use think\facade\Env;
  18. use think\facade\View;
  19. use think\Model;
  20. /**
  21. * Class AdminController
  22. * @package app\common\controller
  23. */
  24. class AdminController extends BaseController
  25. {
  26. use \app\common\traits\JumpTrait;
  27. /**
  28. * 当前模型
  29. * @Model
  30. * @var object
  31. */
  32. protected $model;
  33. /**
  34. * 字段排序
  35. * @var array
  36. */
  37. protected $sort = [
  38. 'id' => 'desc',
  39. ];
  40. /**
  41. * 允许修改的字段
  42. * @var array
  43. */
  44. protected $allowModifyFields = [
  45. 'status',
  46. 'sort',
  47. 'remark',
  48. 'is_delete',
  49. 'is_auth',
  50. 'title',
  51. 'on_sale'
  52. ];
  53. /**
  54. * 不导出的字段信息
  55. * @var array
  56. */
  57. protected $noExportFields = ['delete_time', 'update_time'];
  58. /**
  59. * 下拉选择条件
  60. * @var array
  61. */
  62. protected $selectWhere = [];
  63. /**
  64. * 是否关联查询
  65. * @var bool
  66. */
  67. protected $relationSearch = false;
  68. /**
  69. * 模板布局, false取消
  70. * @var string|bool
  71. */
  72. protected $layout = 'layout/default';
  73. /**
  74. * 是否为演示环境
  75. * @var bool
  76. */
  77. protected $isDemo = false;
  78. /**
  79. * 会员查询条件
  80. * @var array
  81. */
  82. protected $user_map = [];
  83. /**
  84. * 初始化方法
  85. */
  86. protected function initialize()
  87. {
  88. parent::initialize();
  89. $this->layout && $this->app->view->engine()->layout($this->layout);
  90. $this->isDemo = Env::get('easyadmin.is_demo', false);
  91. $this->viewInit();
  92. $this->checkAuth();
  93. }
  94. /**
  95. * 模板变量赋值
  96. * @param string|array $name 模板变量
  97. * @param mixed $value 变量值
  98. * @return mixed
  99. */
  100. public function assign($name, $value = null)
  101. {
  102. return $this->app->view->assign($name, $value);
  103. }
  104. /**
  105. * 解析和获取模板内容 用于输出
  106. * @param string $template
  107. * @param array $vars
  108. * @return mixed
  109. */
  110. public function fetch($template = '', $vars = [])
  111. {
  112. return $this->app->view->fetch($template, $vars);
  113. }
  114. /**
  115. * 重写验证规则
  116. * @param array $data
  117. * @param array|string $validate
  118. * @param array $message
  119. * @param bool $batch
  120. * @return array|bool|string|true
  121. */
  122. public function validate(array $data, $validate, array $message = [], bool $batch = false)
  123. {
  124. try {
  125. parent::validate($data, $validate, $message, $batch);
  126. } catch (\Exception $e) {
  127. $this->error($e->getMessage());
  128. }
  129. return true;
  130. }
  131. /**
  132. * 构建请求参数
  133. * @param array $excludeFields 忽略构建搜索的字段
  134. * @return array
  135. */
  136. protected function buildTableParames($excludeFields = [])
  137. {
  138. $get = $this->request->get('', null, null);
  139. $page = isset($get['page']) && !empty($get['page']) ? $get['page'] : 1;
  140. $limit = isset($get['limit']) && !empty($get['limit']) ? $get['limit'] : 15;
  141. $filters = isset($get['filter']) && !empty($get['filter']) ? $get['filter'] : '{}';
  142. $ops = isset($get['op']) && !empty($get['op']) ? $get['op'] : '{}';
  143. // json转数组
  144. $filters = json_decode($filters, true);
  145. $ops = json_decode($ops, true);
  146. $where = [];
  147. $excludes = [];
  148. // 判断是否关联查询
  149. $tableName = CommonTool::humpToLine(lcfirst($this->model->getName()));
  150. foreach ($filters as $key => $val) {
  151. if (in_array($key, $excludeFields)) {
  152. $excludes[$key] = $val;
  153. continue;
  154. }
  155. $op = isset($ops[$key]) && !empty($ops[$key]) ? $ops[$key] : '%*%';
  156. if ($this->relationSearch && count(explode('.', $key)) == 1) {
  157. $key = "{$tableName}.{$key}";
  158. }
  159. switch (strtolower($op)) {
  160. case '=':
  161. $where[] = [$key, '=', $val];
  162. break;
  163. case '%*%':
  164. $where[] = [$key, 'LIKE', "%{$val}%"];
  165. break;
  166. case '*%':
  167. $where[] = [$key, 'LIKE', "{$val}%"];
  168. break;
  169. case '%*':
  170. $where[] = [$key, 'LIKE', "%{$val}"];
  171. break;
  172. case 'range':
  173. [$beginTime, $endTime] = explode(' - ', $val);
  174. $where[] = [$key, '>=', strtotime($beginTime)];
  175. $where[] = [$key, '<=', strtotime($endTime)];
  176. break;
  177. default:
  178. $where[] = [$key, $op, "%{$val}"];
  179. }
  180. }
  181. return [$page, $limit, $where, $excludes];
  182. }
  183. /**
  184. * 下拉选择列表
  185. * @return \think\response\Json
  186. */
  187. public function selectList()
  188. {
  189. $fields = input('selectFields');
  190. $data = $this->model
  191. ->where($this->selectWhere)
  192. ->field($fields)
  193. ->select();
  194. $this->success(null, $data);
  195. }
  196. /**
  197. * 初始化视图参数
  198. */
  199. private function viewInit(){
  200. $request = app()->request;
  201. list($thisModule, $thisController, $thisAction) = [app('http')->getName(), app()->request->controller(), $request->action()];
  202. list($thisControllerArr, $jsPath) = [explode('.', $thisController), null];
  203. foreach ($thisControllerArr as $vo) {
  204. empty($jsPath) ? $jsPath = parse_name($vo) : $jsPath .= '/' . parse_name($vo);
  205. }
  206. $autoloadJs = file_exists(root_path('public') . "static/{$thisModule}/js/{$jsPath}.js") ? true : false;
  207. $thisControllerJsPath = "{$thisModule}/js/{$jsPath}.js";
  208. $adminModuleName = config('app.admin_alias_name');
  209. $isSuperAdmin = session('admin.id') == AdminConstant::SUPER_ADMIN_ID ? true : false;
  210. $data = [
  211. 'adminModuleName' => $adminModuleName,
  212. 'thisController' => parse_name($thisController),
  213. 'thisAction' => $thisAction,
  214. 'thisRequest' => parse_name("{$thisModule}/{$thisController}/{$thisAction}"),
  215. 'thisControllerJsPath' => "{$thisControllerJsPath}",
  216. 'autoloadJs' => $autoloadJs,
  217. 'isSuperAdmin' => $isSuperAdmin,
  218. 'version' => env('app_debug') ? time() : ConfigService::getVersion(),
  219. ];
  220. View::assign($data);
  221. }
  222. /**
  223. * 检测权限
  224. * @throws \think\db\exception\DataNotFoundException
  225. * @throws \think\db\exception\DbException
  226. * @throws \think\db\exception\ModelNotFoundException
  227. */
  228. private function checkAuth(){
  229. $adminConfig = config('admin');
  230. $adminId = session('admin.id');
  231. $expireTime = session('admin.expire_time');
  232. /** @var AuthService $authService */
  233. $authService = app(AuthService::class, ['adminId' => $adminId]);
  234. $currentNode = $authService->getCurrentNode();
  235. $currentController = parse_name(app()->request->controller());
  236. // 验证登录
  237. if (!in_array($currentController, $adminConfig['no_login_controller']) &&
  238. !in_array($currentNode, $adminConfig['no_login_node'])) {
  239. empty($adminId) && $this->error('请先登录后台', [], __url(Env::get('easyadmin.admin', '').'/login/index'));
  240. // 判断是否登录过期
  241. if ($expireTime !== true && time() > $expireTime) {
  242. session('admin', null);
  243. $this->error('登录已过期,请重新登录', [], __url(Env::get('easyadmin.admin', '').'/login/index'));
  244. }
  245. }
  246. // 验证权限
  247. if (!in_array($currentController, $adminConfig['no_auth_controller']) &&
  248. !in_array($currentNode, $adminConfig['no_auth_node'])) {
  249. $check = $authService->checkNode($currentNode);
  250. !$check && $this->error('无权限访问');
  251. // 判断是否为演示环境
  252. if(env('easyadmin.is_demo', false) && app()->request->isPost()){
  253. $this->error('演示环境下不允许修改');
  254. }
  255. }
  256. }
  257. /**
  258. * 严格校验接口是否为POST请求
  259. */
  260. protected function checkPostRequest(){
  261. // if (!$this->request->isPost()) {
  262. // $this->error("当前请求不合法!");
  263. // }
  264. }
  265. }