Backend.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. $uploadcfg = Configvalue::upload();
  100. $upload = [
  101. 'uploadurl' => $uploadcfg['uploadurl'],
  102. 'cdnurl' => $uploadcfg['cdnurl'],
  103. 'multipart' => [
  104. 'policy' => $uploadcfg['policy'],
  105. 'signature' => $uploadcfg['signature']
  106. ],
  107. 'maxsize' => $uploadcfg['maxsize'],
  108. 'mimetype' => $uploadcfg['mimetype'],
  109. ];
  110. $lang = Lang::detect();
  111. // 配置信息
  112. $config = [
  113. 'site' => Config::get("site"),
  114. 'upload' => $upload,
  115. 'modulename' => $modulename,
  116. 'controllername' => $controllername,
  117. 'actionname' => $actionname,
  118. 'jsname' => 'backend/' . str_replace('.', '/', $controllername),
  119. 'subdomain' => 0,
  120. 'language' => $lang
  121. ];
  122. Lang::load(APP_PATH . $modulename . '/lang/' . $lang . '/' . str_replace('.', '/', $controllername) . '.php');
  123. $this->assign('site', Config::get("site"));
  124. $this->assign('config', $config);
  125. $this->assign('admin', Session::get('admin'));
  126. }
  127. /**
  128. * 生成查询所需要的条件,排序方式
  129. * @param mixed $searchfields 查询条件
  130. * @return array
  131. */
  132. protected function buildparams($searchfields = NULL)
  133. {
  134. $searchfields = is_null($searchfields) ? 'id' : $searchfields;
  135. $search = $this->request->get("search", '');
  136. $filter = $this->request->get("filter", '');
  137. $op = $this->request->get("op", '');
  138. $sort = $this->request->get("sort", "id");
  139. $order = $this->request->get("order", "DESC");
  140. $offset = $this->request->get("offset", 0);
  141. $limit = $this->request->get("limit", 10);
  142. $filter = json_decode($filter, TRUE);
  143. $op = json_decode($op, TRUE);
  144. $filter = $filter ? $filter : [];
  145. $where = [];
  146. if ($search)
  147. {
  148. $searcharr = is_array($searchfields) ? $searchfields : explode(',', $searchfields);
  149. $searchlist = [];
  150. foreach ($searcharr as $k => $v)
  151. {
  152. $searchlist[] = "`{$v}` LIKE '%{$search}%'";
  153. }
  154. $where[] = "(" . implode(' OR ', $searchlist) . ")";
  155. }
  156. foreach ($filter as $k => $v)
  157. {
  158. $sym = isset($op[$k]) ? $op[$k] : '=';
  159. switch ($sym)
  160. {
  161. case '=':
  162. case '!=':
  163. case 'LIKE':
  164. case 'NOT LIKE':
  165. $where[] = [$k, $sym, $v];
  166. break;
  167. case '>':
  168. case '>=':
  169. case '<':
  170. case '<=':
  171. $where[] = [$k, $sym, intval($v)];
  172. break;
  173. case 'IN(...)':
  174. case 'NOT IN(...)':
  175. $where[] = [$k, str_replace('(...)', '', $sym), explode(',', $v)];
  176. break;
  177. case 'BETWEEN':
  178. case 'NOT BETWEEN':
  179. $where[] = [$k, $sym, array_slice(explode(',', $v), 0, 2)];
  180. break;
  181. case 'LIKE %...%':
  182. $where[] = [$k, 'LIKE', "%{$v}%"];
  183. break;
  184. case 'IS NULL':
  185. case 'IS NOT NULL':
  186. $where[] = [$k, str_replace(' NULL', '', $sym), NULL];
  187. break;
  188. default:
  189. break;
  190. }
  191. }
  192. return [$where, $sort, $order, $offset, $limit];
  193. }
  194. /**
  195. * 析构方法
  196. *
  197. */
  198. public function __destruct()
  199. {
  200. //判断是否设置code值,如果有则变动response对象的正文
  201. if (!is_null($this->code))
  202. {
  203. $this->result($this->data, $this->code, $this->msg);
  204. }
  205. }
  206. }