You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

266 lines
9.0 KiB

3 years ago
  1. <?php
  2. namespace Api\Controller;
  3. use Think\Controller;
  4. class CatalogController extends BaseController {
  5. //获取目录列表
  6. public function catList(){
  7. $login_user = $this->checkLogin();
  8. $item_id = I("item_id/d");
  9. if (!$this->checkItemVisit($login_user['uid'] , $item_id)) {
  10. $this->sendError(10103);
  11. return ;
  12. }
  13. if ($item_id > 0 ) {
  14. $ret = D("Catalog")->getList($item_id);
  15. $ret = D("Catalog")->filteMemberCat($login_user['uid'] , $ret);
  16. }
  17. if ($ret) {
  18. $this->sendResult($ret);
  19. }else{
  20. $this->sendResult(array());
  21. }
  22. }
  23. //获取目录列表
  24. public function catListGroup(){
  25. $login_user = $this->checkLogin();
  26. $item_id = I("item_id/d");
  27. if (!$this->checkItemVisit($login_user['uid'] , $item_id)) {
  28. $this->sendError(10103);
  29. return ;
  30. }
  31. if ($item_id > 0 ) {
  32. $ret = D("Catalog")->getList($item_id,true);
  33. $ret = D("Catalog")->filteMemberCat($login_user['uid'] , $ret);
  34. }
  35. if ($ret) {
  36. $this->sendResult($ret);
  37. }else{
  38. $this->sendResult(array());
  39. }
  40. }
  41. //获取二级目录列表
  42. public function secondCatList(){
  43. $login_user = $this->checkLogin();
  44. $item_id = I("item_id/d");
  45. if (!$this->checkItemVisit($login_user['uid'] , $item_id)) {
  46. $this->sendError(10103);
  47. return ;
  48. }
  49. if ($item_id > 0 ) {
  50. $ret = D("Catalog")->getListByLevel($item_id , 2);
  51. }
  52. if ($ret) {
  53. $this->sendResult($ret);
  54. }else{
  55. $this->sendResult(array());
  56. }
  57. }
  58. //获取二级目录的子目录列表,即三级目录列表(如果存在的话)
  59. public function childCatList(){
  60. $login_user = $this->checkLogin();
  61. $cat_id = I("cat_id/d");
  62. if ($cat_id > 0 ) {
  63. $row = D("Catalog")->where(" cat_id = '$cat_id' ")->find() ;
  64. $item_id = $row['item_id'] ;
  65. if (!$this->checkItemVisit($login_user['uid'] , $item_id)) {
  66. $this->sendError(10103);
  67. return ;
  68. }
  69. $ret = D("Catalog")->getChlid($item_id , $cat_id);
  70. }
  71. if ($ret) {
  72. $this->sendResult($ret);
  73. }else{
  74. $this->sendResult(array());
  75. }
  76. }
  77. //保存目录
  78. public function save(){
  79. $cat_name = I("cat_name");
  80. $s_number = I("s_number/d") ? I("s_number/d") : '' ;
  81. $cat_id = I("cat_id/d")? I("cat_id/d") : 0;
  82. $parent_cat_id = I("parent_cat_id/d")? I("parent_cat_id/d") : 0;
  83. $item_id = I("item_id/d");
  84. $login_user = $this->checkLogin();
  85. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  86. $this->sendError(10103);
  87. return;
  88. }
  89. //禁止空目录的生成
  90. if (!$cat_name) {
  91. return;
  92. }
  93. if ($parent_cat_id && $parent_cat_id == $cat_id) {
  94. $this->sendError(10101,"上级目录不能选择自身");
  95. return;
  96. }
  97. $data['cat_name'] = $cat_name ;
  98. if($s_number)$data['s_number'] = $s_number ;
  99. $data['item_id'] = $item_id ;
  100. $data['parent_cat_id'] = $parent_cat_id ;
  101. if ($parent_cat_id > 0 ) {
  102. $row = D("Catalog")->where(" cat_id = '$parent_cat_id' ")->find() ;
  103. $data['level'] = $row['level'] +1 ;
  104. }else{
  105. $data['level'] = 2;
  106. }
  107. if ($cat_id > 0 ) {
  108. $cat = D("Catalog")->where(" cat_id = '$cat_id' ")->find();
  109. $item_id = $cat['item_id'];
  110. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  111. $this->sendError(10103);
  112. return;
  113. }
  114. //如果一个目录已经是别的目录的父目录,那么它将无法再转为level4目录
  115. //if (D("Catalog")->where(" parent_cat_id = '$cat_id' ")->find() && $data['level'] == 4 ) {
  116. //$this->sendError(10101,"该目录含有子目录,不允许转为底层目录。");
  117. //return;
  118. //}
  119. $ret = D("Catalog")->where(" cat_id = '$cat_id' ")->save($data);
  120. $return = D("Catalog")->where(" cat_id = '$cat_id' ")->find();
  121. }else{
  122. $data['addtime'] = time();
  123. $cat_id = D("Catalog")->add($data);
  124. $return = D("Catalog")->where(" cat_id = '$cat_id' ")->find();
  125. }
  126. if (!$return) {
  127. $return['error_code'] = 10103 ;
  128. $return['error_message'] = 'request fail' ;
  129. }
  130. $this->sendResult($return);
  131. }
  132. //删除目录
  133. public function delete(){
  134. $cat_id = I("cat_id/d")? I("cat_id/d") : 0;
  135. $cat = D("Catalog")->where(" cat_id = '$cat_id' ")->find();
  136. $item_id = $cat['item_id'];
  137. $login_user = $this->checkLogin();
  138. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  139. $return['error_code'] = -1 ;
  140. $return['error_message'] = L('no_permissions');
  141. $this->sendResult($return);
  142. return;
  143. }
  144. if ($cat_id > 0 ) {
  145. $ret = D("Catalog")->deleteCat($cat_id);
  146. }
  147. if ($ret) {
  148. $this->sendResult($ret);
  149. }else{
  150. $return['error_code'] = -1 ;
  151. $return['error_message'] = 'request fail' ;
  152. $this->sendResult($return);
  153. }
  154. }
  155. //编辑页面时,自动帮助用户选中目录
  156. //选中的规则是:编辑页面则选中该页面目录,复制页面则选中目标页面目录;
  157. // 如果是恢复历史页面则使用历史页面的目录,如果都没有则选中用户上次使用的目录
  158. public function getDefaultCat(){
  159. $login_user = $this->checkLogin();
  160. $page_id = I("page_id/d");
  161. $item_id = I("item_id/d");
  162. $page_history_id = I("page_history_id/d");
  163. $copy_page_id = I("copy_page_id/d");
  164. if ($page_id > 0 ) {
  165. if ($page_history_id) {
  166. $page = D("PageHistory")->where(" page_history_id = '$page_history_id' ")->find();
  167. }else{
  168. $page = M("Page")->where(" page_id = '$page_id' ")->find();
  169. }
  170. $default_cat_id = $page['cat_id'];
  171. }
  172. //如果是复制接口
  173. elseif ($copy_page_id) {
  174. $copy_page = M("Page")->where(" page_id = '$copy_page_id' ")->find();
  175. $page['item_id'] = $copy_page['item_id'];
  176. $default_cat_id = $copy_page['cat_id'];
  177. }else{
  178. //查找用户上一次设置的目录
  179. $last_page = D("Page")->where(" author_uid ='$login_user[uid]' and item_id = '$item_id' ")->order(" addtime desc ")->limit(1)->find();
  180. $default_cat_id = $last_page['cat_id'];
  181. }
  182. $item_id = $page['item_id'] ?$page['item_id'] :$item_id;
  183. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  184. $this->sendError(10101,L('no_permissions'));
  185. return;
  186. }
  187. $this->sendResult(array("default_cat_id"=>$default_cat_id ));
  188. }
  189. //批量更新
  190. public function batUpdate(){
  191. $cats = I("cats");
  192. $item_id = I("item_id/d");
  193. $login_user = $this->checkLogin();
  194. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  195. $this->sendError(10103);
  196. return ;
  197. }
  198. $ret = '';
  199. $data_array = json_decode(htmlspecialchars_decode($cats) , true) ;
  200. if ($data_array) {
  201. foreach ($data_array as $key => $value) {
  202. if ($value['cat_name']) {
  203. $ret = D("Catalog")->where(" cat_id = '$value[cat_id]' and item_id = '$item_id' ")->save(array(
  204. "cat_name" => $value['cat_name'] ,
  205. "parent_cat_id" => $value['parent_cat_id'] ,
  206. "level" => $value['level'] ,
  207. "s_number" => $value['s_number'] ,
  208. ));
  209. }
  210. if ($value['page_id'] > 0) {
  211. $ret = D("Page")->where(" page_id = '$value[page_id]' and item_id = '$item_id' ")->save(array(
  212. "cat_id" => $value['parent_cat_id'] ,
  213. "s_number" => $value['s_number'] ,
  214. ));
  215. }
  216. }
  217. }
  218. $this->sendResult(array());
  219. }
  220. //获取某个目录下所有页面的标题
  221. public function getPagesBycat(){
  222. $cat_id = I("cat_id/d")? I("cat_id/d") : 0;
  223. $item_id = I("item_id/d");
  224. $login_user = $this->checkLogin();
  225. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  226. $this->sendError(10103);
  227. return ;
  228. }
  229. $return = D("Page")->where("cat_id = '$cat_id' and item_id = '$item_id' and is_del = 0 ")->field("page_id , page_title,s_number")->order("s_number asc , page_id asc")->select();
  230. $this->sendResult($return);
  231. }
  232. }