Backend.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. * 引入后台控制器的traits
  60. */
  61. use \app\admin\library\traits\Backend;
  62. public function _initialize()
  63. {
  64. $modulename = $this->request->module();
  65. $controllername = strtolower($this->request->controller());
  66. $actionname = strtolower($this->request->action());
  67. $path = '/' . $modulename . '/' . str_replace('.', '/', $controllername) . '/' . $actionname;
  68. // 定义是否Addtabs请求
  69. !defined('IS_ADDTABS') && define('IS_ADDTABS', input("addtabs") ? TRUE : FALSE);
  70. // 定义是否Dialog请求
  71. !defined('IS_DIALOG') && define('IS_DIALOG', input("dialog") ? TRUE : FALSE);
  72. // 定义是否AJAX请求
  73. !defined('IS_AJAX') && define('IS_AJAX', $this->request->isAjax());
  74. // 非选项卡时重定向
  75. if (!$this->request->isPost() && !IS_AJAX && !IS_ADDTABS && !IS_DIALOG && input("ref") == 'addtabs')
  76. {
  77. $url = preg_replace_callback("/([\?|&]+)ref=addtabs(&?)/i", function($matches) {
  78. return $matches[2] == '&' ? $matches[1] : '';
  79. }, $this->request->url());
  80. $this->redirect('index/index', [], 302, ['referer' => $url]);
  81. exit;
  82. }
  83. $this->auth = Auth::instance();
  84. // 设置当前请求的URI
  85. $this->auth->setRequestUri($path);
  86. // 检测是否需要验证登录
  87. if (!$this->auth->match($this->noNeedLogin))
  88. {
  89. //检测是否登录
  90. if (!$this->auth->isLogin())
  91. {
  92. $url = Session::get('referer');
  93. $url = $url ? $url : $this->request->url();
  94. $this->error(__('Please login first'), url('index/login', ['url' => $url]));
  95. }
  96. // 判断是否需要验证权限
  97. if (!$this->auth->match($this->noNeedRight))
  98. {
  99. // 判断控制器和方法判断是否有对应权限
  100. if (!$this->auth->check($path))
  101. {
  102. $this->error(__('You have no permission'), NULL);
  103. }
  104. }
  105. }
  106. // 设置面包屑导航数据
  107. $breadcrumb = $this->auth->getBreadCrumb($path);
  108. array_pop($breadcrumb);
  109. $this->view->breadcrumb = $breadcrumb;
  110. // 如果有使用模板布局
  111. if ($this->layout)
  112. {
  113. $this->view->engine->layout('layout/' . $this->layout);
  114. }
  115. // 语言检测
  116. $lang = Lang::detect();
  117. // 配置信息
  118. $config = [
  119. 'site' => Config::get("site"),
  120. 'upload' => Configvalue::upload(),
  121. 'modulename' => $modulename,
  122. 'controllername' => $controllername,
  123. 'actionname' => $actionname,
  124. 'jsname' => 'backend/' . str_replace('.', '/', $controllername),
  125. 'moduleurl' => url("/{$modulename}", '', false),
  126. 'language' => $lang,
  127. 'referer' => Session::get("referer")
  128. ];
  129. Lang::load(APP_PATH . $modulename . '/lang/' . $lang . '/' . str_replace('.', '/', $controllername) . '.php');
  130. $this->assign('site', Config::get("site"));
  131. $this->assign('config', $config);
  132. $this->assign('admin', Session::get('admin'));
  133. }
  134. /**
  135. * 生成查询所需要的条件,排序方式
  136. * @param mixed $searchfields 查询条件
  137. * @param boolean $relationSearch 是否关联查询
  138. * @return array
  139. */
  140. protected function buildparams($searchfields = null, $relationSearch = null)
  141. {
  142. $searchfields = is_null($searchfields) ? $this->searchFields : $searchfields;
  143. $relationSearch = is_null($relationSearch) ? $this->relationSearch : $relationSearch;
  144. $search = $this->request->get("search", '');
  145. $filter = $this->request->get("filter", '');
  146. $op = $this->request->get("op", '');
  147. $sort = $this->request->get("sort", "id");
  148. $order = $this->request->get("order", "DESC");
  149. $offset = $this->request->get("offset", 0);
  150. $limit = $this->request->get("limit", 0);
  151. $filter = json_decode($filter, TRUE);
  152. $op = json_decode($op, TRUE);
  153. $filter = $filter ? $filter : [];
  154. $where = [];
  155. if ($search)
  156. {
  157. $searcharr = is_array($searchfields) ? $searchfields : explode(',', $searchfields);
  158. $searchlist = [];
  159. foreach ($searcharr as $k => $v)
  160. {
  161. $searchlist[] = "`{$v}` LIKE '%{$search}%'";
  162. }
  163. $where[] = "(" . implode(' OR ', $searchlist) . ")";
  164. }
  165. $modelName = '';
  166. if ($relationSearch)
  167. {
  168. if (!empty($this->model))
  169. {
  170. $class = get_class($this->model);
  171. $name = basename(str_replace('\\', '/', $class));
  172. $name = strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $name));
  173. $modelName = $name . ".";
  174. }
  175. if (stripos($sort, ".") === false)
  176. {
  177. $sort = $modelName . $sort;
  178. }
  179. }
  180. foreach ($filter as $k => $v)
  181. {
  182. $sym = isset($op[$k]) ? $op[$k] : '=';
  183. if (stripos($k, ".") === false)
  184. {
  185. $k = $modelName . $k;
  186. }
  187. $sym = isset($op[$k]) ? $op[$k] : $sym;
  188. switch ($sym)
  189. {
  190. case '=':
  191. case '!=':
  192. case 'LIKE':
  193. case 'NOT LIKE':
  194. $where[] = [$k, $sym, $v];
  195. break;
  196. case '>':
  197. case '>=':
  198. case '<':
  199. case '<=':
  200. $where[] = [$k, $sym, intval($v)];
  201. break;
  202. case 'IN(...)':
  203. case 'NOT IN(...)':
  204. $where[] = [$k, str_replace('(...)', '', $sym), explode(',', $v)];
  205. break;
  206. case 'BETWEEN':
  207. case 'NOT BETWEEN':
  208. $where[] = [$k, $sym, array_slice(explode(',', $v), 0, 2)];
  209. break;
  210. case 'LIKE %...%':
  211. $where[] = [$k, 'LIKE', "%{$v}%"];
  212. break;
  213. case 'IS NULL':
  214. case 'IS NOT NULL':
  215. $where[] = [$k, strtolower(str_replace('IS ', '', $sym))];
  216. break;
  217. default:
  218. break;
  219. }
  220. }
  221. $where = function($query) use ($where) {
  222. foreach ($where as $k => $v)
  223. {
  224. if (is_array($v))
  225. {
  226. call_user_func_array([$query, 'where'], $v);
  227. }
  228. else
  229. {
  230. $query->where($v);
  231. }
  232. }
  233. };
  234. return [$where, $sort, $order, $offset, $limit];
  235. }
  236. /**
  237. * 析构方法
  238. *
  239. */
  240. public function __destruct()
  241. {
  242. //判断是否设置code值,如果有则变动response对象的正文
  243. if (!is_null($this->code))
  244. {
  245. $this->result($this->data, $this->code, $this->msg, 'json');
  246. }
  247. }
  248. }