Backend.php 7.5 KB

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