Ajax.php 8.3 KB

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