Template.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. /**
  5. *
  6. *
  7. * @icon fa fa-circle-o
  8. */
  9. class Template extends Backend
  10. {
  11. /**
  12. * Template模型对象
  13. * @var \app\admin\model\Template
  14. */
  15. protected $model = null;
  16. public function _initialize()
  17. {
  18. parent::_initialize();
  19. $this->model = new \app\admin\model\Template;
  20. $this->view->assign("statusList", $this->model->getStatusList());
  21. }
  22. /**
  23. * 查看
  24. *
  25. * @return string|Json
  26. * @throws \think\Exception
  27. * @throws DbException
  28. */
  29. public function indexs()
  30. {
  31. //设置过滤方法
  32. $this->request->filter(['strip_tags', 'trim']);
  33. if (false === $this->request->isAjax()) {
  34. return $this->view->fetch();
  35. }
  36. //如果发送的来源是 Selectpage,则转发到 Selectpage
  37. if ($this->request->request('keyField')) {
  38. return $this->selectpages();
  39. }
  40. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  41. $list = $this->model
  42. ->where($where)
  43. ->order($sort, $order)
  44. ->paginate($limit);
  45. $result = ['total' => $list->total(), 'rows' => $list->items()];
  46. return json($result);
  47. }
  48. protected function selectpages()
  49. {
  50. //设置过滤方法
  51. $this->request->filter(['trim', 'strip_tags', 'htmlspecialchars']);
  52. //搜索关键词,客户端输入以空格分开,这里接收为数组
  53. $word = [1];
  54. //当前页
  55. $page = $this->request->request("pageNumber");
  56. //分页大小
  57. $pagesize = $this->request->request("pageSize");
  58. //搜索条件
  59. $andor = $this->request->request("andOr", "and", "strtoupper");
  60. //排序方式
  61. $orderby = (array)$this->request->request("orderBy/a");
  62. //显示的字段
  63. $field = $this->request->request("showField");
  64. //主键
  65. $primarykey = $this->request->request("keyField");
  66. //主键值
  67. $primaryvalue = $this->request->request("keyValue");
  68. //搜索字段
  69. $searchfield = ['status'];
  70. //自定义搜索条件
  71. $custom = (array)$this->request->request("custom/a");
  72. //是否返回树形结构
  73. $istree = $this->request->request("isTree", 0);
  74. $ishtml = $this->request->request("isHtml", 0);
  75. if ($istree) {
  76. $word = [];
  77. $pagesize = 999999;
  78. }
  79. $order = [];
  80. foreach ($orderby as $k => $v) {
  81. $order[$v[0]] = $v[1];
  82. }
  83. $field = $field ? $field : 'name';
  84. //如果有primaryvalue,说明当前是初始化传值
  85. if ($primaryvalue !== null) {
  86. $where = [$primarykey => ['in', $primaryvalue]];
  87. $pagesize = 999999;
  88. } else {
  89. $where = function ($query) use ($word, $andor, $field, $searchfield, $custom) {
  90. $logic = $andor == 'AND' ? '&' : '|';
  91. $searchfield = is_array($searchfield) ? implode($logic, $searchfield) : $searchfield;
  92. $searchfield = str_replace(',', $logic, $searchfield);
  93. $word = array_filter(array_unique($word));
  94. if (count($word) == 1) {
  95. $query->where($searchfield, "like", "%" . reset($word) . "%");
  96. } else {
  97. $query->where(function ($query) use ($word, $searchfield) {
  98. foreach ($word as $index => $item) {
  99. $query->whereOr(function ($query) use ($item, $searchfield) {
  100. $query->where($searchfield, "like", "%{$item}%");
  101. });
  102. }
  103. });
  104. }
  105. if ($custom && is_array($custom)) {
  106. foreach ($custom as $k => $v) {
  107. if (is_array($v) && 2 == count($v)) {
  108. $query->where($k, trim($v[0]), $v[1]);
  109. } else {
  110. $query->where($k, '=', $v);
  111. }
  112. }
  113. }
  114. };
  115. }
  116. $adminIds = $this->getDataLimitAdminIds();
  117. if (is_array($adminIds)) {
  118. $this->model->where($this->dataLimitField, 'in', $adminIds);
  119. }
  120. $list = [];
  121. $total = $this->model->where($where)->count();
  122. if ($total > 0) {
  123. if (is_array($adminIds)) {
  124. $this->model->where($this->dataLimitField, 'in', $adminIds);
  125. }
  126. $fields = is_array($this->selectpageFields) ? $this->selectpageFields : ($this->selectpageFields && $this->selectpageFields != '*' ? explode(',', $this->selectpageFields) : []);
  127. //如果有primaryvalue,说明当前是初始化传值,按照选择顺序排序
  128. if ($primaryvalue !== null && preg_match("/^[a-z0-9_\-]+$/i", $primarykey)) {
  129. $primaryvalue = array_unique(is_array($primaryvalue) ? $primaryvalue : explode(',', $primaryvalue));
  130. //修复自定义data-primary-key为字符串内容时,给排序字段添加上引号
  131. $primaryvalue = array_map(function ($value) {
  132. return '\'' . $value . '\'';
  133. }, $primaryvalue);
  134. $primaryvalue = implode(',', $primaryvalue);
  135. $this->model->orderRaw("FIELD(`{$primarykey}`, {$primaryvalue})");
  136. } else {
  137. $this->model->order($order);
  138. }
  139. $datalist = $this->model->where($where)
  140. ->page($page, $pagesize)
  141. ->select();
  142. foreach ($datalist as $index => $item) {
  143. unset($item['password'], $item['salt']);
  144. if ($this->selectpageFields == '*') {
  145. $result = [
  146. $primarykey => isset($item[$primarykey]) ? $item[$primarykey] : '',
  147. $field => isset($item[$field]) ? $item[$field] : '',
  148. ];
  149. } else {
  150. $result = array_intersect_key(($item instanceof Model ? $item->toArray() : (array)$item), array_flip($fields));
  151. }
  152. $result['pid'] = isset($item['pid']) ? $item['pid'] : (isset($item['parent_id']) ? $item['parent_id'] : 0);
  153. $list[] = $result;
  154. }
  155. if ($istree && !$primaryvalue) {
  156. $tree = Tree::instance();
  157. $tree->init(collection($list)->toArray(), 'pid');
  158. $list = $tree->getTreeList($tree->getTreeArray(0), $field);
  159. if (!$ishtml) {
  160. foreach ($list as &$item) {
  161. $item = str_replace('&nbsp;', ' ', $item);
  162. }
  163. unset($item);
  164. }
  165. }
  166. }
  167. //这里一定要返回有list这个字段,total是可选的,如果total<=list的数量,则会隐藏分页按钮
  168. return json(['list' => $list, 'total' => $total]);
  169. }
  170. /**
  171. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  172. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  173. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  174. */
  175. }