Backend.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. header("location:" . url('index/index#!' . urlencode($this->request->url()), '', false));
  70. exit;
  71. }
  72. $this->auth = Auth::instance();
  73. // 设置当前请求的URI
  74. $this->auth->setRequestUri($path);
  75. // 检测是否需要验证登录
  76. if (!$this->auth->match($this->noNeedLogin))
  77. {
  78. //检测是否登录
  79. if (!$this->auth->isLogin())
  80. {
  81. $this->error(__('Please login first'), url('index/login', ['url' => $this->request->url()]));
  82. }
  83. // 判断是否需要验证权限
  84. if (!$this->auth->match($this->noNeedRight))
  85. {
  86. // 判断控制器和方法判断是否有对应权限
  87. if (!$this->auth->check($path))
  88. {
  89. $this->error(__('You have no permission'), NULL);
  90. }
  91. }
  92. }
  93. // 如果有使用模板布局
  94. if ($this->layout)
  95. {
  96. $this->view->engine->layout('layout/' . $this->layout);
  97. }
  98. // 语言检测
  99. $lang = Lang::detect();
  100. // 配置信息
  101. $config = [
  102. 'site' => Config::get("site"),
  103. 'upload' => Configvalue::upload(),
  104. 'modulename' => $modulename,
  105. 'controllername' => $controllername,
  106. 'actionname' => $actionname,
  107. 'jsname' => 'backend/' . str_replace('.', '/', $controllername),
  108. 'subdomain' => 0,
  109. 'language' => $lang
  110. ];
  111. Lang::load(APP_PATH . $modulename . '/lang/' . $lang . '/' . str_replace('.', '/', $controllername) . '.php');
  112. $this->assign('site', Config::get("site"));
  113. $this->assign('config', $config);
  114. $this->assign('admin', Session::get('admin'));
  115. }
  116. /**
  117. * 生成查询所需要的条件,排序方式
  118. * @param mixed $searchfields 查询条件
  119. * @return array
  120. */
  121. protected function buildparams($searchfields = NULL)
  122. {
  123. $searchfields = is_null($searchfields) ? 'id' : $searchfields;
  124. $search = $this->request->get("search", '');
  125. $filter = $this->request->get("filter", '');
  126. $op = $this->request->get("op", '');
  127. $sort = $this->request->get("sort", "id");
  128. $order = $this->request->get("order", "DESC");
  129. $offset = $this->request->get("offset", 0);
  130. $limit = $this->request->get("limit", 10);
  131. $filter = json_decode($filter, TRUE);
  132. $op = json_decode($op, TRUE);
  133. $filter = $filter ? $filter : [];
  134. $where = [];
  135. if ($search)
  136. {
  137. $searcharr = is_array($searchfields) ? $searchfields : explode(',', $searchfields);
  138. $searchlist = [];
  139. foreach ($searcharr as $k => $v)
  140. {
  141. $searchlist[] = "`{$v}` LIKE '%{$search}%'";
  142. }
  143. $where[] = "(" . implode(' OR ', $searchlist) . ")";
  144. }
  145. foreach ($filter as $k => $v)
  146. {
  147. $sym = isset($op[$k]) ? $op[$k] : '=';
  148. switch ($sym)
  149. {
  150. case '=':
  151. case '!=':
  152. case 'LIKE':
  153. case 'NOT LIKE':
  154. $where[] = [$k, $sym, $v];
  155. break;
  156. case '>':
  157. case '>=':
  158. case '<':
  159. case '<=':
  160. $where[] = [$k, $sym, intval($v)];
  161. break;
  162. case 'IN(...)':
  163. case 'NOT IN(...)':
  164. $where[] = [$k, str_replace('(...)', '', $sym), explode(',', $v)];
  165. break;
  166. case 'BETWEEN':
  167. case 'NOT BETWEEN':
  168. $where[] = [$k, $sym, array_slice(explode(',', $v), 0, 2)];
  169. break;
  170. case 'LIKE %...%':
  171. $where[] = [$k, 'LIKE', "%{$v}%"];
  172. break;
  173. case 'IS NULL':
  174. case 'IS NOT NULL':
  175. $where[] = [$k, str_replace(' NULL', '', $sym), NULL];
  176. break;
  177. default:
  178. break;
  179. }
  180. }
  181. return [$where, $sort, $order, $offset, $limit];
  182. }
  183. /**
  184. * 析构方法
  185. *
  186. */
  187. public function __destruct()
  188. {
  189. //判断是否设置code值,如果有则变动response对象的正文
  190. if (!is_null($this->code))
  191. {
  192. $this->result($this->data, $this->code, $this->msg, 'json');
  193. }
  194. }
  195. }