Backend.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <?php
  2. namespace app\common\controller;
  3. use app\admin\library\Auth;
  4. use app\common\model\Configvalue;
  5. use think\Config;
  6. use think\Controller;
  7. use think\Lang;
  8. use think\Session;
  9. load_trait('library/traits/Backend');
  10. /**
  11. * 后台控制器基类
  12. */
  13. class Backend extends Controller
  14. {
  15. /**
  16. * 返回码,默认为null,当设置了该值后将输出json数据
  17. * @var int
  18. */
  19. protected $code = null;
  20. /**
  21. * 返回内容,默认为null,当设置了该值后将输出json数据
  22. * @var mixed
  23. */
  24. protected $data = null;
  25. /**
  26. * 返回文本,默认为空
  27. * @var mixed
  28. */
  29. protected $msg = '';
  30. /**
  31. * 无需登录的方法,同时也就不需要鉴权了
  32. * @var array
  33. */
  34. protected $noNeedLogin = [];
  35. /**
  36. * 无需鉴权的方法,但需要登录
  37. * @var array
  38. */
  39. protected $noNeedRight = [];
  40. /**
  41. * 布局模板
  42. * @var string
  43. */
  44. protected $layout = 'default';
  45. /**
  46. * 权限控制类
  47. * @var Auth
  48. */
  49. protected $auth = null;
  50. /**
  51. * 快速搜索时执行查找的字段
  52. */
  53. protected $searchFields = 'id';
  54. /**
  55. * 是否是关联查询
  56. */
  57. protected $relationSearch = false;
  58. /**
  59. * 是否开启Validate验证
  60. */
  61. protected $modelValidate = false;
  62. /**
  63. * 是否开启模型场景验证
  64. */
  65. protected $modelSceneValidate = false;
  66. /**
  67. * Multi方法可批量修改的字段
  68. */
  69. protected $multiFields = 'status';
  70. /**
  71. * 引入后台控制器的traits
  72. */
  73. use \app\admin\library\traits\Backend;
  74. public function _initialize()
  75. {
  76. $modulename = $this->request->module();
  77. $controllername = strtolower($this->request->controller());
  78. $actionname = strtolower($this->request->action());
  79. $path = '/' . $modulename . '/' . str_replace('.', '/', $controllername) . '/' . $actionname;
  80. // 定义是否Addtabs请求
  81. !defined('IS_ADDTABS') && define('IS_ADDTABS', input("addtabs") ? TRUE : FALSE);
  82. // 定义是否Dialog请求
  83. !defined('IS_DIALOG') && define('IS_DIALOG', input("dialog") ? TRUE : FALSE);
  84. // 定义是否AJAX请求
  85. !defined('IS_AJAX') && define('IS_AJAX', $this->request->isAjax());
  86. $this->auth = Auth::instance();
  87. // 设置当前请求的URI
  88. $this->auth->setRequestUri($path);
  89. // 检测是否需要验证登录
  90. if (!$this->auth->match($this->noNeedLogin))
  91. {
  92. //检测是否登录
  93. if (!$this->auth->isLogin())
  94. {
  95. $url = Session::get('referer');
  96. $url = $url ? $url : $this->request->url();
  97. $this->error(__('Please login first'), url('index/login', ['url' => $url]));
  98. }
  99. // 判断是否需要验证权限
  100. if (!$this->auth->match($this->noNeedRight))
  101. {
  102. // 判断控制器和方法判断是否有对应权限
  103. if (!$this->auth->check($path))
  104. {
  105. $this->error(__('You have no permission'), NULL);
  106. }
  107. }
  108. }
  109. // 非选项卡时重定向
  110. if (!$this->request->isPost() && !IS_AJAX && !IS_ADDTABS && !IS_DIALOG && input("ref") == 'addtabs')
  111. {
  112. $url = preg_replace_callback("/([\?|&]+)ref=addtabs(&?)/i", function($matches) {
  113. return $matches[2] == '&' ? $matches[1] : '';
  114. }, $this->request->url());
  115. $this->redirect('index/index', [], 302, ['referer' => $url]);
  116. exit;
  117. }
  118. // 设置面包屑导航数据
  119. $breadcrumb = $this->auth->getBreadCrumb($path);
  120. array_pop($breadcrumb);
  121. $this->view->breadcrumb = $breadcrumb;
  122. // 如果有使用模板布局
  123. if ($this->layout)
  124. {
  125. $this->view->engine->layout('layout/' . $this->layout);
  126. }
  127. // 语言检测
  128. $lang = Lang::detect();
  129. $site = Config::get("site");
  130. // 配置信息
  131. $config = [
  132. 'site' => array_intersect_key($site, array_flip(['name', 'cdnurl', 'version', 'timezone', 'languages'])),
  133. 'upload' => Configvalue::upload(),
  134. 'modulename' => $modulename,
  135. 'controllername' => $controllername,
  136. 'actionname' => $actionname,
  137. 'jsname' => 'backend/' . str_replace('.', '/', $controllername),
  138. 'moduleurl' => url("/{$modulename}", '', false),
  139. 'language' => $lang,
  140. 'referer' => Session::get("referer")
  141. ];
  142. $this->loadlang($controllername);
  143. $this->assign('site', $site);
  144. $this->assign('config', $config);
  145. $this->assign('admin', Session::get('admin'));
  146. }
  147. /**
  148. * 加载语言文件
  149. * @param string $name
  150. */
  151. protected function loadlang($name)
  152. {
  153. Lang::load(APP_PATH . $this->request->module() . '/lang/' . Lang::detect() . '/' . str_replace('.', '/', $name) . '.php');
  154. }
  155. /**
  156. * 生成查询所需要的条件,排序方式
  157. * @param mixed $searchfields 查询条件
  158. * @param boolean $relationSearch 是否关联查询
  159. * @return array
  160. */
  161. protected function buildparams($searchfields = null, $relationSearch = null)
  162. {
  163. $searchfields = is_null($searchfields) ? $this->searchFields : $searchfields;
  164. $relationSearch = is_null($relationSearch) ? $this->relationSearch : $relationSearch;
  165. $search = $this->request->get("search", '');
  166. $filter = $this->request->get("filter", '');
  167. $op = $this->request->get("op", '');
  168. $sort = $this->request->get("sort", "id");
  169. $order = $this->request->get("order", "DESC");
  170. $offset = $this->request->get("offset", 0);
  171. $limit = $this->request->get("limit", 0);
  172. $filter = json_decode($filter, TRUE);
  173. $op = json_decode($op, TRUE);
  174. $filter = $filter ? $filter : [];
  175. $where = [];
  176. $modelName = '';
  177. if ($relationSearch)
  178. {
  179. if (!empty($this->model))
  180. {
  181. $class = get_class($this->model);
  182. $name = basename(str_replace('\\', '/', $class));
  183. $name = strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $name));
  184. $modelName = $name . ".";
  185. }
  186. if (stripos($sort, ".") === false)
  187. {
  188. $sort = $modelName . $sort;
  189. }
  190. }
  191. if ($search)
  192. {
  193. $searcharr = is_array($searchfields) ? $searchfields : explode(',', $searchfields);
  194. $searchlist = [];
  195. foreach ($searcharr as $k => $v)
  196. {
  197. $searchlist[] = (stripos($v, ".") !== false ? $v : "{$modelName}`{$v}`") . " LIKE '%{$search}%'";
  198. }
  199. $where[] = "(" . implode(' OR ', $searchlist) . ")";
  200. }
  201. foreach ($filter as $k => $v)
  202. {
  203. $sym = isset($op[$k]) ? $op[$k] : '=';
  204. if (stripos($k, ".") === false)
  205. {
  206. $k = $modelName . $k;
  207. }
  208. $sym = isset($op[$k]) ? $op[$k] : $sym;
  209. switch ($sym)
  210. {
  211. case '=':
  212. case '!=':
  213. case 'LIKE':
  214. case 'NOT LIKE':
  215. $where[] = [$k, $sym, $v];
  216. break;
  217. case '>':
  218. case '>=':
  219. case '<':
  220. case '<=':
  221. $where[] = [$k, $sym, intval($v)];
  222. break;
  223. case 'IN(...)':
  224. case 'NOT IN(...)':
  225. $where[] = [$k, str_replace('(...)', '', $sym), explode(',', $v)];
  226. break;
  227. case 'BETWEEN':
  228. case 'NOT BETWEEN':
  229. $where[] = [$k, $sym, array_slice(explode(',', $v), 0, 2)];
  230. break;
  231. case 'LIKE %...%':
  232. $where[] = [$k, 'LIKE', "%{$v}%"];
  233. break;
  234. case 'IS NULL':
  235. case 'IS NOT NULL':
  236. $where[] = [$k, strtolower(str_replace('IS ', '', $sym))];
  237. break;
  238. default:
  239. break;
  240. }
  241. }
  242. $where = function($query) use ($where) {
  243. foreach ($where as $k => $v)
  244. {
  245. if (is_array($v))
  246. {
  247. call_user_func_array([$query, 'where'], $v);
  248. }
  249. else
  250. {
  251. $query->where($v);
  252. }
  253. }
  254. };
  255. return [$where, $sort, $order, $offset, $limit];
  256. }
  257. /**
  258. * 析构方法
  259. *
  260. */
  261. public function __destruct()
  262. {
  263. //判断是否设置code值,如果有则变动response对象的正文
  264. if (!is_null($this->code))
  265. {
  266. $this->result($this->data, $this->code, $this->msg, 'json');
  267. }
  268. }
  269. }