Backend.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. <?php
  2. namespace app\common\controller;
  3. use app\admin\library\Auth;
  4. use app\admin\library\SystemLogin;
  5. use think\Config;
  6. use think\Controller;
  7. use think\Hook;
  8. use think\Lang;
  9. use think\Loader;
  10. use think\Model;
  11. use think\Session;
  12. use fast\Tree;
  13. use think\Validate;
  14. /**
  15. * 后台控制器基类
  16. */
  17. class Backend extends Controller
  18. {
  19. /**
  20. * 无需登录的方法,同时也就不需要鉴权了
  21. * @var array
  22. */
  23. protected $noNeedLogin = [];
  24. /**
  25. * 无需鉴权的方法,但需要登录
  26. * @var array
  27. */
  28. protected $noNeedRight = [];
  29. /**
  30. * 布局模板
  31. * @var string
  32. */
  33. protected $layout = 'default';
  34. /**
  35. * 权限控制类
  36. * @var Auth
  37. */
  38. protected $auth = null;
  39. /**
  40. * 模型对象
  41. * @var \think\Model
  42. */
  43. protected $model = null;
  44. /**
  45. * 快速搜索时执行查找的字段
  46. */
  47. protected $searchFields = 'id';
  48. /**
  49. * 是否是关联查询
  50. */
  51. protected $relationSearch = false;
  52. /**
  53. * 是否开启数据限制
  54. * 支持auth/personal
  55. * 表示按权限判断/仅限个人
  56. * 默认为禁用,若启用请务必保证表中存在admin_id字段
  57. */
  58. protected $dataLimit = false;
  59. /**
  60. * 数据限制字段
  61. */
  62. protected $dataLimitField = 'admin_id';
  63. /**
  64. * 数据限制开启时自动填充限制字段值
  65. */
  66. protected $dataLimitFieldAutoFill = true;
  67. /**
  68. * 是否开启Validate验证
  69. */
  70. protected $modelValidate = false;
  71. /**
  72. * 是否开启模型场景验证
  73. */
  74. protected $modelSceneValidate = false;
  75. /**
  76. * Multi方法可批量修改的字段
  77. */
  78. protected $multiFields = 'status';
  79. /**
  80. * Selectpage可显示的字段
  81. */
  82. protected $selectpageFields = '*';
  83. /**
  84. * 前台提交过来,需要排除的字段数据
  85. */
  86. protected $excludeFields = "";
  87. /**
  88. * 导入文件首行类型
  89. * 支持comment/name
  90. * 表示注释或字段名
  91. */
  92. protected $importHeadType = 'comment';
  93. /**
  94. * 引入后台控制器的traits
  95. */
  96. use \app\admin\library\traits\Backend;
  97. public function _initialize()
  98. {
  99. $modulename = $this->request->module();
  100. $controllername = Loader::parseName($this->request->controller());
  101. $actionname = strtolower($this->request->action());
  102. // 与 fastadmin.keeplogin_hours 一致(默认 72 小时)
  103. $keeploginSec = (int)Config::get('fastadmin.keeplogin_hours', 72);
  104. if ($keeploginSec <= 0) {
  105. $keeploginSec = 72;
  106. }
  107. $keeploginSec *= 3600;
  108. if (PHP_VERSION_ID >= 70300) {
  109. ini_set('session.gc_maxlifetime', (string)$keeploginSec);
  110. ini_set('session.cookie_lifetime', (string)$keeploginSec);
  111. }
  112. $path = str_replace('.', '/', $controllername) . '/' . $actionname;
  113. // 定义是否Addtabs请求
  114. !defined('IS_ADDTABS') && define('IS_ADDTABS', (bool)input("addtabs"));
  115. // 定义是否Dialog请求
  116. !defined('IS_DIALOG') && define('IS_DIALOG', (bool)input("dialog"));
  117. // 定义是否AJAX请求
  118. !defined('IS_AJAX') && define('IS_AJAX', $this->request->isAjax());
  119. // 检测IP是否允许
  120. check_ip_allowed();
  121. $this->auth = Auth::instance();
  122. // 设置当前请求的URI
  123. $this->auth->setRequestUri($path);
  124. // 检测是否需要验证登录
  125. if (!$this->auth->match($this->noNeedLogin)) {
  126. //检测是否登录
  127. if (!$this->auth->isLogin()) {
  128. Hook::listen('admin_nologin', $this);
  129. $url = Session::get('referer');
  130. $url = $url ? $url : $this->request->url();
  131. if (in_array($this->request->pathinfo(), ['/', 'index/index'])) {
  132. $this->redirect('index/login', [], 302, ['referer' => $url]);
  133. exit;
  134. }
  135. $this->error(__('Please login first'), url('index/login', ['url' => $url]));
  136. }
  137. // 判断是否需要验证权限
  138. if (!$this->auth->match($this->noNeedRight)) {
  139. // 判断控制器和方法是否有对应权限
  140. if (!$this->auth->check($path)) {
  141. Hook::listen('admin_nopermission', $this);
  142. $this->error(__('You have no permission'), '');
  143. }
  144. }
  145. }
  146. // 非选项卡时重定向
  147. if (!$this->request->isPost() && !IS_AJAX && !IS_ADDTABS && !IS_DIALOG && input("ref") == 'addtabs') {
  148. $url = preg_replace_callback("/([\?|&]+)ref=addtabs(&?)/i", function ($matches) {
  149. return $matches[2] == '&' ? $matches[1] : '';
  150. }, $this->request->url());
  151. if (Config::get('url_domain_deploy')) {
  152. if (stripos($url, $this->request->server('SCRIPT_NAME')) === 0) {
  153. $url = substr($url, strlen($this->request->server('SCRIPT_NAME')));
  154. }
  155. $url = url($url, '', false);
  156. }
  157. $this->redirect('index/index', [], 302, ['referer' => $url]);
  158. exit;
  159. }
  160. // 设置面包屑导航数据
  161. $breadcrumb = [];
  162. if (!IS_DIALOG && !config('fastadmin.multiplenav') && config('fastadmin.breadcrumb')) {
  163. $breadcrumb = $this->auth->getBreadCrumb($path);
  164. array_pop($breadcrumb);
  165. }
  166. $this->view->breadcrumb = $breadcrumb;
  167. // 如果有使用模板布局
  168. if ($this->layout) {
  169. $this->view->engine->layout('layout/' . $this->layout);
  170. }
  171. // 语言检测
  172. $lang = $this->request->langset();
  173. $lang = preg_match("/^([a-zA-Z\-_]{2,10})\$/i", $lang) ? $lang : 'zh-cn';
  174. $site = Config::get("site");
  175. $upload = \app\common\model\Config::upload();
  176. // 上传信息配置后
  177. Hook::listen("upload_config_init", $upload);
  178. // 配置信息
  179. $config = [
  180. 'site' => array_intersect_key($site, array_flip(['name', 'indexurl', 'cdnurl', 'version', 'timezone', 'languages'])),
  181. 'upload' => $upload,
  182. 'modulename' => $modulename,
  183. 'controllername' => $controllername,
  184. 'actionname' => $actionname,
  185. 'jsname' => 'backend/' . str_replace('.', '/', $controllername),
  186. 'moduleurl' => rtrim(url("/{$modulename}", '', false), '/'),
  187. 'language' => $lang,
  188. 'referer' => Session::get("referer")
  189. ];
  190. $config = array_merge($config, Config::get("view_replace_str"));
  191. Config::set('upload', array_merge(Config::get('upload'), $upload));
  192. // 配置信息后
  193. Hook::listen("config_init", $config);
  194. //加载当前控制器语言包
  195. $this->loadlang($controllername);
  196. //渲染站点配置
  197. $this->assign('site', $site);
  198. //渲染配置信息
  199. $this->assign('config', $config);
  200. //渲染权限对象
  201. $this->assign('auth', $this->auth);
  202. //渲染管理员对象(无自定义头像时使用站点品牌 Logo)
  203. $admin = Session::get('admin');
  204. if (is_array($admin)) {
  205. $admin['avatar_raw'] = $admin['avatar'] ?? '';
  206. $admin['avatar_is_default'] = is_default_admin_avatar($admin['avatar_raw']) ? 1 : 0;
  207. $admin['avatar'] = resolve_admin_avatar($admin['avatar_raw']);
  208. }
  209. $this->assign('admin', $admin);
  210. // 按登录所属系统套用后台风格(1白/2黑)
  211. if ($this->auth && $this->auth->isLogin()) {
  212. $this->view->assign(SystemLogin::applyTheme($this->auth));
  213. } else {
  214. $this->view->assign([
  215. 'login_system' => 'collab',
  216. 'system_style' => 1,
  217. 'adminskin' => Config::get('fastadmin.adminskin') ?: 'skin-blue-light',
  218. 'load_xinhua_theme' => true,
  219. 'system_brand' => '生产协同系统',
  220. ]);
  221. }
  222. }
  223. /**
  224. * 加载语言文件
  225. * @param string $name
  226. */
  227. protected function loadlang($name)
  228. {
  229. $name = Loader::parseName($name);
  230. $name = preg_match("/^([a-zA-Z0-9_\.\/]+)\$/i", $name) ? $name : 'index';
  231. $lang = $this->request->langset();
  232. $lang = preg_match("/^([a-zA-Z\-_]{2,10})\$/i", $lang) ? $lang : 'zh-cn';
  233. Lang::load(APP_PATH . $this->request->module() . '/lang/' . $lang . '/' . str_replace('.', '/', $name) . '.php');
  234. }
  235. /**
  236. * 渲染配置信息
  237. * @param mixed $name 键名或数组
  238. * @param mixed $value 值
  239. */
  240. protected function assignconfig($name, $value = '')
  241. {
  242. $this->view->config = array_merge($this->view->config ? $this->view->config : [], is_array($name) ? $name : [$name => $value]);
  243. }
  244. /**
  245. * 生成查询所需要的条件,排序方式
  246. * @param mixed $searchfields 快速查询的字段
  247. * @param boolean $relationSearch 是否关联查询
  248. * @return array
  249. */
  250. protected function buildparams($searchfields = null, $relationSearch = null)
  251. {
  252. $searchfields = is_null($searchfields) ? $this->searchFields : $searchfields;
  253. $relationSearch = is_null($relationSearch) ? $this->relationSearch : $relationSearch;
  254. $search = $this->request->get("search", '');
  255. $filter = $this->request->get("filter", '');
  256. $op = $this->request->get("op", '', 'trim');
  257. $sort = $this->request->get("sort", !empty($this->model) && $this->model->getPk() ? $this->model->getPk() : 'id');
  258. $order = $this->request->get("order", "DESC");
  259. $offset = $this->request->get("offset/d", 0);
  260. $limit = $this->request->get("limit/d", 999999);
  261. //新增自动计算页码
  262. $page = $limit ? intval($offset / $limit) + 1 : 1;
  263. if ($this->request->has("page")) {
  264. $page = $this->request->get("page/d", 1);
  265. }
  266. $this->request->get([config('paginate.var_page') => $page]);
  267. $filter = (array)json_decode($filter, true);
  268. $op = (array)json_decode($op, true);
  269. $filter = $filter ? $filter : [];
  270. $where = [];
  271. $alias = [];
  272. $bind = [];
  273. $name = '';
  274. $aliasName = '';
  275. if (!empty($this->model) && $relationSearch) {
  276. $name = $this->model->getTable();
  277. $alias[$name] = Loader::parseName(basename(str_replace('\\', '/', get_class($this->model))));
  278. $aliasName = $alias[$name] . '.';
  279. }
  280. $sortArr = explode(',', $sort);
  281. foreach ($sortArr as $index => & $item) {
  282. $item = stripos($item, ".") === false ? $aliasName . trim($item) : $item;
  283. }
  284. unset($item);
  285. $sort = implode(',', $sortArr);
  286. $adminIds = $this->getDataLimitAdminIds();
  287. if (is_array($adminIds)) {
  288. $where[] = [$aliasName . $this->dataLimitField, 'in', $adminIds];
  289. }
  290. if ($search) {
  291. $searcharr = is_array($searchfields) ? $searchfields : explode(',', $searchfields);
  292. foreach ($searcharr as $k => &$v) {
  293. $v = stripos($v, ".") === false ? $aliasName . $v : $v;
  294. }
  295. unset($v);
  296. $where[] = [implode("|", $searcharr), "LIKE", "%{$search}%"];
  297. }
  298. $index = 0;
  299. foreach ($filter as $k => $v) {
  300. if (!preg_match('/^[a-zA-Z0-9_\-\.]+$/', $k)) {
  301. continue;
  302. }
  303. $sym = $op[$k] ?? '=';
  304. if (stripos($k, ".") === false) {
  305. $k = $aliasName . $k;
  306. }
  307. $v = !is_array($v) ? trim($v) : $v;
  308. $sym = strtoupper($op[$k] ?? $sym);
  309. //null和空字符串特殊处理
  310. if (!is_array($v)) {
  311. if (in_array(strtoupper($v), ['NULL', 'NOT NULL'])) {
  312. $sym = strtoupper($v);
  313. }
  314. if (in_array($v, ['""', "''"])) {
  315. $v = '';
  316. $sym = '=';
  317. }
  318. }
  319. switch ($sym) {
  320. case '=':
  321. case '<>':
  322. $where[] = [$k, $sym, (string)$v];
  323. break;
  324. case 'LIKE':
  325. case 'NOT LIKE':
  326. case 'LIKE %...%':
  327. case 'NOT LIKE %...%':
  328. $where[] = [$k, trim(str_replace('%...%', '', $sym)), "%{$v}%"];
  329. break;
  330. case '>':
  331. case '>=':
  332. case '<':
  333. case '<=':
  334. $where[] = [$k, $sym, intval($v)];
  335. break;
  336. case 'FINDIN':
  337. case 'FINDINSET':
  338. case 'FIND_IN_SET':
  339. $v = is_array($v) ? $v : explode(',', str_replace(' ', ',', $v));
  340. $findArr = array_values($v);
  341. foreach ($findArr as $idx => $item) {
  342. $bindName = "item_" . $index . "_" . $idx;
  343. $bind[$bindName] = $item;
  344. $where[] = "FIND_IN_SET(:{$bindName}, `" . str_replace('.', '`.`', $k) . "`)";
  345. }
  346. break;
  347. case 'IN':
  348. case 'IN(...)':
  349. case 'NOT IN':
  350. case 'NOT IN(...)':
  351. $where[] = [$k, str_replace('(...)', '', $sym), is_array($v) ? $v : explode(',', $v)];
  352. break;
  353. case 'BETWEEN':
  354. case 'NOT BETWEEN':
  355. $arr = array_slice(explode(',', $v), 0, 2);
  356. if (stripos($v, ',') === false || !array_filter($arr, function ($v) {
  357. return $v != '' && $v !== false && $v !== null;
  358. })) {
  359. continue 2;
  360. }
  361. //当出现一边为空时改变操作符
  362. if ($arr[0] === '') {
  363. $sym = $sym == 'BETWEEN' ? '<=' : '>';
  364. $arr = $arr[1];
  365. } elseif ($arr[1] === '') {
  366. $sym = $sym == 'BETWEEN' ? '>=' : '<';
  367. $arr = $arr[0];
  368. }
  369. $where[] = [$k, $sym, $arr];
  370. break;
  371. case 'RANGE':
  372. case 'NOT RANGE':
  373. $v = str_replace(' - ', ',', $v);
  374. $arr = array_slice(explode(',', $v), 0, 2);
  375. if (stripos($v, ',') === false || !array_filter($arr)) {
  376. continue 2;
  377. }
  378. //当出现一边为空时改变操作符
  379. if ($arr[0] === '') {
  380. $sym = $sym == 'RANGE' ? '<=' : '>';
  381. $arr = $arr[1];
  382. } elseif ($arr[1] === '') {
  383. $sym = $sym == 'RANGE' ? '>=' : '<';
  384. $arr = $arr[0];
  385. }
  386. $tableArr = explode('.', $k);
  387. if (count($tableArr) > 1 && $tableArr[0] != $name && !in_array($tableArr[0], $alias)
  388. && !empty($this->model) && $this->relationSearch) {
  389. //修复关联模型下时间无法搜索的BUG
  390. $relation = Loader::parseName($tableArr[0], 1, false);
  391. $alias[$this->model->$relation()->getTable()] = $tableArr[0];
  392. }
  393. $where[] = [$k, str_replace('RANGE', 'BETWEEN', $sym) . ' TIME', $arr];
  394. break;
  395. case 'NULL':
  396. case 'IS NULL':
  397. case 'NOT NULL':
  398. case 'IS NOT NULL':
  399. $where[] = [$k, strtolower(str_replace('IS ', '', $sym))];
  400. break;
  401. default:
  402. break;
  403. }
  404. $index++;
  405. }
  406. if (!empty($this->model)) {
  407. $this->model->alias($alias);
  408. }
  409. $model = $this->model;
  410. $where = function ($query) use ($where, $alias, $bind, &$model) {
  411. if (!empty($model)) {
  412. $model->alias($alias);
  413. $model->bind($bind);
  414. }
  415. foreach ($where as $k => $v) {
  416. if (is_array($v)) {
  417. call_user_func_array([$query, 'where'], $v);
  418. } else {
  419. $query->where($v);
  420. }
  421. }
  422. };
  423. return [$where, $sort, $order, $offset, $limit, $page, $alias, $bind];
  424. }
  425. /**
  426. * 获取数据限制的管理员ID
  427. * 禁用数据限制时返回的是null
  428. * @return mixed
  429. */
  430. protected function getDataLimitAdminIds()
  431. {
  432. if (!$this->dataLimit) {
  433. return null;
  434. }
  435. if ($this->auth->isSuperAdmin()) {
  436. return null;
  437. }
  438. $adminIds = [];
  439. if (in_array($this->dataLimit, ['auth', 'personal'])) {
  440. $adminIds = $this->dataLimit == 'auth' ? $this->auth->getChildrenAdminIds(true) : [$this->auth->id];
  441. }
  442. return $adminIds;
  443. }
  444. /**
  445. * Selectpage的实现方法
  446. *
  447. * 当前方法只是一个比较通用的搜索匹配,请按需重载此方法来编写自己的搜索逻辑,$where按自己的需求写即可
  448. * 这里示例了所有的参数,所以比较复杂,实现上自己实现只需简单的几行即可
  449. *
  450. */
  451. protected function selectpage()
  452. {
  453. //设置过滤方法
  454. $this->request->filter(['trim', 'strip_tags', 'htmlspecialchars']);
  455. //搜索关键词,客户端输入以空格分开,这里接收为数组
  456. $word = (array)$this->request->request("q_word/a");
  457. //当前页
  458. $page = $this->request->request("pageNumber");
  459. //分页大小
  460. $pagesize = $this->request->request("pageSize");
  461. //搜索条件
  462. $andor = $this->request->request("andOr", "and", "strtoupper");
  463. //排序方式
  464. $orderby = (array)$this->request->request("orderBy/a");
  465. //显示的字段
  466. $field = $this->request->request("showField");
  467. //主键
  468. $primarykey = $this->request->request("keyField");
  469. //主键值
  470. $primaryvalue = $this->request->request("keyValue");
  471. //搜索字段
  472. $searchfield = (array)$this->request->request("searchField/a");
  473. //自定义搜索条件
  474. $custom = (array)$this->request->request("custom/a");
  475. //是否返回树形结构
  476. $istree = $this->request->request("isTree", 0);
  477. $ishtml = $this->request->request("isHtml", 0);
  478. if ($istree) {
  479. $word = [];
  480. $pagesize = 999999;
  481. }
  482. $order = [];
  483. foreach ($orderby as $k => $v) {
  484. $order[$v[0]] = $v[1];
  485. }
  486. $field = $field ? $field : 'name';
  487. //如果有primaryvalue,说明当前是初始化传值
  488. if ($primaryvalue !== null) {
  489. $where = [$primarykey => ['in', $primaryvalue]];
  490. $pagesize = 999999;
  491. } else {
  492. $where = function ($query) use ($word, $andor, $field, $searchfield, $custom) {
  493. $logic = $andor == 'AND' ? '&' : '|';
  494. $searchfield = is_array($searchfield) ? implode($logic, $searchfield) : $searchfield;
  495. $searchfield = str_replace(',', $logic, $searchfield);
  496. $word = array_filter(array_unique($word));
  497. if (count($word) == 1) {
  498. $query->where($searchfield, "like", "%" . reset($word) . "%");
  499. } else {
  500. $query->where(function ($query) use ($word, $searchfield) {
  501. foreach ($word as $index => $item) {
  502. $query->whereOr(function ($query) use ($item, $searchfield) {
  503. $query->where($searchfield, "like", "%{$item}%");
  504. });
  505. }
  506. });
  507. }
  508. if ($custom && is_array($custom)) {
  509. foreach ($custom as $k => $v) {
  510. if (is_array($v) && 2 == count($v)) {
  511. $query->where($k, trim($v[0]), $v[1]);
  512. } else {
  513. $query->where($k, '=', $v);
  514. }
  515. }
  516. }
  517. };
  518. }
  519. $adminIds = $this->getDataLimitAdminIds();
  520. if (is_array($adminIds)) {
  521. $this->model->where($this->dataLimitField, 'in', $adminIds);
  522. }
  523. $list = [];
  524. $total = $this->model->where($where)->count();
  525. if ($total > 0) {
  526. if (is_array($adminIds)) {
  527. $this->model->where($this->dataLimitField, 'in', $adminIds);
  528. }
  529. $fields = is_array($this->selectpageFields) ? $this->selectpageFields : ($this->selectpageFields && $this->selectpageFields != '*' ? explode(',', $this->selectpageFields) : []);
  530. //如果有primaryvalue,说明当前是初始化传值,按照选择顺序排序
  531. if ($primaryvalue !== null && preg_match("/^[a-z0-9_\-]+$/i", $primarykey)) {
  532. $primaryvalue = array_unique(is_array($primaryvalue) ? $primaryvalue : explode(',', $primaryvalue));
  533. //修复自定义data-primary-key为字符串内容时,给排序字段添加上引号
  534. $primaryvalue = array_map(function ($value) {
  535. return '\'' . $value . '\'';
  536. }, $primaryvalue);
  537. $primaryvalue = implode(',', $primaryvalue);
  538. $this->model->orderRaw("FIELD(`{$primarykey}`, {$primaryvalue})");
  539. } else {
  540. $this->model->order($order);
  541. }
  542. $datalist = $this->model->where($where)
  543. ->page($page, $pagesize)
  544. ->select();
  545. foreach ($datalist as $index => $item) {
  546. unset($item['password'], $item['salt']);
  547. if ($this->selectpageFields == '*') {
  548. $result = [
  549. $primarykey => $item[$primarykey] ?? '',
  550. $field => $item[$field] ?? '',
  551. ];
  552. } else {
  553. $result = array_intersect_key(($item instanceof Model ? $item->toArray() : (array)$item), array_flip($fields));
  554. }
  555. $result['pid'] = isset($item['pid']) ? $item['pid'] : (isset($item['parent_id']) ? $item['parent_id'] : 0);
  556. $list[] = $result;
  557. }
  558. if ($istree && !$primaryvalue) {
  559. $tree = Tree::instance();
  560. $tree->init(collection($list)->toArray(), 'pid');
  561. $list = $tree->getTreeList($tree->getTreeArray(0), $field);
  562. if (!$ishtml) {
  563. foreach ($list as &$item) {
  564. $item = str_replace('&nbsp;', ' ', $item);
  565. }
  566. unset($item);
  567. }
  568. }
  569. }
  570. //这里一定要返回有list这个字段,total是可选的,如果total<=list的数量,则会隐藏分页按钮
  571. return json(['list' => $list, 'total' => $total]);
  572. }
  573. /**
  574. * 刷新Token
  575. */
  576. protected function token()
  577. {
  578. $token = $this->request->param('__token__');
  579. //验证Token
  580. if (!Validate::make()->check(['__token__' => $token], ['__token__' => 'require|token'])) {
  581. $this->error(__('Token verification error'), '', ['__token__' => $this->request->token()]);
  582. }
  583. //刷新Token
  584. $this->request->token();
  585. }
  586. }