Backend.php 6.9 KB

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