Ajax.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. namespace addons\cms\controller;
  3. use addons\cms\model\Collection;
  4. use addons\cms\model\Fields;
  5. use fast\Tree;
  6. use think\Config;
  7. use think\Db;
  8. /**
  9. * Ajax控制器
  10. * Class Ajax
  11. * @package addons\cms\controller
  12. */
  13. class Ajax extends Base
  14. {
  15. public function selectpage()
  16. {
  17. $id = $this->request->get("id/d", 0);
  18. $fieldInfo = Fields::get($id);
  19. if (!$fieldInfo) {
  20. $this->error("未找到指定字段");
  21. }
  22. $setting = $fieldInfo['setting'];
  23. if (!$setting || !isset($setting['table'])) {
  24. $this->error("字段配置不正确");
  25. }
  26. //搜索关键词,客户端输入以空格分开,这里接收为数组
  27. $word = (array)$this->request->request("q_word/a");
  28. //当前页
  29. $page = $this->request->request("pageNumber");
  30. //分页大小
  31. $pagesize = $this->request->request("pageSize");
  32. //搜索条件
  33. $andor = $this->request->request("andOr", "and", "strtoupper");
  34. //排序方式
  35. $orderby = (array)$this->request->request("orderBy/a");
  36. //显示的字段
  37. //$field = $this->request->request("showField");
  38. $field = $setting['field'];
  39. //主键
  40. //$primarykey = $this->request->request("keyField");
  41. $primarykey = $setting['primarykey'];
  42. //主键值
  43. $primaryvalue = $this->request->request("keyValue");
  44. //搜索字段
  45. $searchfield = (array)$this->request->request("searchField/a");
  46. $searchfield = [$field, $primarykey];
  47. //自定义搜索条件
  48. $custom = (array)$this->request->request("custom/a");
  49. $custom = isset($setting['conditions']) ? (array)json_decode($setting['conditions'], true) : [];
  50. $custom = array_filter($custom);
  51. $admin_id = session('admin.id') ?: 0;
  52. $user_id = $this->auth->id ?: 0;
  53. //如果是管理员需要移除user_id筛选,否则会导致管理员无法筛选列表信息
  54. $admin = $this->request->request("admin/d");
  55. if ($admin_id && $admin) {
  56. unset($custom['user_id']);
  57. } else {
  58. //如果不是管理员则需要判断是否开放相应的投稿字段
  59. if (!in_array($fieldInfo['source'], ['model', 'diyform'])) {
  60. $this->error("未开放栏目信息");
  61. }
  62. if (!$fieldInfo['iscontribute']) {
  63. $this->error("未开放字段信息");
  64. }
  65. }
  66. //是否返回树形结构
  67. $istree = $this->request->request("isTree", 0);
  68. $ishtml = $this->request->request("isHtml", 0);
  69. if ($istree) {
  70. $word = [];
  71. $pagesize = 99999;
  72. }
  73. $order = [];
  74. foreach ($orderby as $k => $v) {
  75. $order[$v[0]] = $v[1];
  76. }
  77. $field = $field ? $field : 'name';
  78. //如果有primaryvalue,说明当前是初始化传值
  79. if ($primaryvalue !== null) {
  80. $where = [$primarykey => ['in', $primaryvalue]];
  81. $where = function ($query) use ($primaryvalue, $custom, $admin_id, $user_id) {
  82. $query->where('id', 'in', $primaryvalue);
  83. if ($custom && is_array($custom)) {
  84. //替换暂位符
  85. $search = ["{admin_id}", "{user_id}"];
  86. $replace = [$admin_id, $user_id];
  87. foreach ($custom as $k => $v) {
  88. if (is_array($v) && 2 == count($v)) {
  89. $query->where($k, trim($v[0]), str_replace($search, $replace, $v[1]));
  90. } else {
  91. $query->where($k, '=', str_replace($search, $replace, $v));
  92. }
  93. }
  94. }
  95. };
  96. $pagesize = 99999;
  97. } else {
  98. $where = function ($query) use ($word, $andor, $field, $searchfield, $custom, $admin_id, $user_id) {
  99. $logic = $andor == 'AND' ? '&' : '|';
  100. $searchfield = is_array($searchfield) ? implode($logic, $searchfield) : $searchfield;
  101. $word = array_filter($word);
  102. if ($word) {
  103. foreach ($word as $k => $v) {
  104. $query->where(str_replace(',', $logic, $searchfield), "like", "%{$v}%");
  105. }
  106. }
  107. if ($custom && is_array($custom)) {
  108. //替换暂位符
  109. $search = ["{admin_id}", "{user_id}"];
  110. $replace = [$admin_id, $user_id];
  111. foreach ($custom as $k => $v) {
  112. if (is_array($v) && 2 == count($v)) {
  113. $query->where($k, trim($v[0]), str_replace($search, $replace, $v[1]));
  114. } else {
  115. $query->where($k, '=', str_replace($search, $replace, $v));
  116. }
  117. }
  118. }
  119. };
  120. }
  121. $list = [];
  122. $total = Db::table($setting['table'])->where($where)->count();
  123. if ($total > 0) {
  124. $datalist = Db::table($setting['table'])->where($where)
  125. ->order($order)
  126. ->page($page, $pagesize)
  127. ->field($primarykey . "," . $field . ($istree ? ",pid" : ""))
  128. ->select();
  129. foreach ($datalist as $index => &$item) {
  130. unset($item['password'], $item['salt']);
  131. $list[] = [
  132. $primarykey => isset($item[$primarykey]) ? $item[$primarykey] : '',
  133. $field => isset($item[$field]) ? $item[$field] : '',
  134. 'pid' => isset($item['pid']) ? $item['pid'] : 0
  135. ];
  136. }
  137. if ($istree && !$primaryvalue) {
  138. $tree = Tree::instance();
  139. $tree->init($list, 'pid');
  140. $list = $tree->getTreeList($tree->getTreeArray(0), $field);
  141. if (!$ishtml) {
  142. foreach ($list as &$item) {
  143. $item = str_replace('&nbsp;', ' ', $item);
  144. }
  145. unset($item);
  146. }
  147. }
  148. }
  149. //这里一定要返回有list这个字段,total是可选的,如果total<=list的数量,则会隐藏分页按钮
  150. return json(['list' => $list, 'total' => $total]);
  151. }
  152. /**
  153. * 添加收藏
  154. */
  155. public function collection()
  156. {
  157. if (!$this->auth->isLogin()) {
  158. $this->error("请登录后操作");
  159. }
  160. $type = $this->request->post("type");
  161. $aid = $this->request->post("aid/d");
  162. if (!in_array($type, ['archives', 'page', 'special', 'diyform'])) {
  163. $this->error("参数不正确");
  164. }
  165. $model = call_user_func_array(['\\addons\\cms\\model\\' . ucfirst($type), "get"], [$aid]);
  166. if (!$model) {
  167. $this->error("未找到指定数据");
  168. }
  169. Db::startTrans();
  170. try {
  171. $collection = Collection::lock(true)->where(['type' => $type, 'aid' => $aid,'user_id'=>$this->auth->id])->find();
  172. if ($collection) {
  173. throw new \think\Exception("请勿重复收藏");
  174. }
  175. $title = $model->title;
  176. $url = $model->fullurl;
  177. $image = $model->image;
  178. $data = [
  179. 'user_id' => $this->auth->id,
  180. 'type' => $type,
  181. 'aid' => $aid,
  182. 'title' => $title,
  183. 'url' => $url,
  184. 'image' => $image
  185. ];
  186. Collection::create($data);
  187. Db::commit();
  188. } catch (\think\Exception $e) {
  189. Db::rollback();
  190. $this->error($e->getMessage());
  191. } catch (\Exception $e) {
  192. Db::rollback();
  193. $this->error("收藏失败");
  194. }
  195. $this->success("收藏成功");
  196. }
  197. }