CatalogController.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 catListName(){
  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")->getList($item_id,true);
  51. $ret = D("Catalog")->filteMemberCat($login_user['uid'] , $ret);
  52. }
  53. if ($ret) {
  54. $return = array() ;
  55. //匿名递归函数,准备递归改名
  56. // uee 指令后面引用参数转递。方便函数内部中使用。
  57. $rename = function ($catalog, $p_cat_name) use (&$return , &$rename) {
  58. if($catalog){
  59. foreach ($catalog as $key => $value) {
  60. $value['cat_name'] = $p_cat_name .'/'. $value['cat_name'] ;
  61. $sub = $value['sub'] ;
  62. unset($value['sub']);
  63. $return[] = $value ;
  64. if($sub){
  65. $rename($sub , $value['cat_name'] ) ;
  66. }
  67. }
  68. }
  69. };
  70. foreach ($ret as $key => $value) {
  71. $sub = $value['sub'] ;
  72. unset($value['sub']);
  73. $return[] = $value ;
  74. if($sub){
  75. $rename($sub , $value['cat_name'] ) ;
  76. }
  77. }
  78. $this->sendResult($return);
  79. }else{
  80. $this->sendResult(array());
  81. }
  82. }
  83. //获取二级目录列表
  84. public function secondCatList(){
  85. $login_user = $this->checkLogin();
  86. $item_id = I("item_id/d");
  87. if (!$this->checkItemVisit($login_user['uid'] , $item_id)) {
  88. $this->sendError(10103);
  89. return ;
  90. }
  91. if ($item_id > 0 ) {
  92. $ret = D("Catalog")->getListByLevel($item_id , 2);
  93. }
  94. if ($ret) {
  95. $this->sendResult($ret);
  96. }else{
  97. $this->sendResult(array());
  98. }
  99. }
  100. //获取二级目录的子目录列表,即三级目录列表(如果存在的话)
  101. public function childCatList(){
  102. $login_user = $this->checkLogin();
  103. $cat_id = I("cat_id/d");
  104. if ($cat_id > 0 ) {
  105. $row = D("Catalog")->where(" cat_id = '$cat_id' ")->find() ;
  106. $item_id = $row['item_id'] ;
  107. if (!$this->checkItemVisit($login_user['uid'] , $item_id)) {
  108. $this->sendError(10103);
  109. return ;
  110. }
  111. $ret = D("Catalog")->getChlid($item_id , $cat_id);
  112. }
  113. if ($ret) {
  114. $this->sendResult($ret);
  115. }else{
  116. $this->sendResult(array());
  117. }
  118. }
  119. //保存目录
  120. public function save(){
  121. $cat_name = I("cat_name");
  122. $s_number = I("s_number/d") ? I("s_number/d") : '' ;
  123. $cat_id = I("cat_id/d")? I("cat_id/d") : 0;
  124. $parent_cat_id = I("parent_cat_id/d")? I("parent_cat_id/d") : 0;
  125. $item_id = I("item_id/d");
  126. $login_user = $this->checkLogin();
  127. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  128. $this->sendError(10103);
  129. return;
  130. }
  131. //禁止空目录的生成
  132. if (!$cat_name) {
  133. return;
  134. }
  135. if ($parent_cat_id && $parent_cat_id == $cat_id) {
  136. $this->sendError(10101,"上级目录不能选择自身");
  137. return;
  138. }
  139. $data['cat_name'] = $cat_name ;
  140. if($s_number)$data['s_number'] = $s_number ;
  141. $data['item_id'] = $item_id ;
  142. $data['parent_cat_id'] = $parent_cat_id ;
  143. if ($parent_cat_id > 0 ) {
  144. $row = D("Catalog")->where(" cat_id = '$parent_cat_id' ")->find() ;
  145. $data['level'] = $row['level'] +1 ;
  146. }else{
  147. $data['level'] = 2;
  148. }
  149. if ($cat_id > 0 ) {
  150. $cat = D("Catalog")->where(" cat_id = '$cat_id' ")->find();
  151. $item_id = $cat['item_id'];
  152. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  153. $this->sendError(10103);
  154. return;
  155. }
  156. //如果一个目录已经是别的目录的父目录,那么它将无法再转为level4目录
  157. //if (D("Catalog")->where(" parent_cat_id = '$cat_id' ")->find() && $data['level'] == 4 ) {
  158. //$this->sendError(10101,"该目录含有子目录,不允许转为底层目录。");
  159. //return;
  160. //}
  161. $ret = D("Catalog")->where(" cat_id = '$cat_id' ")->save($data);
  162. $return = D("Catalog")->where(" cat_id = '$cat_id' ")->find();
  163. }else{
  164. $data['addtime'] = time();
  165. $cat_id = D("Catalog")->add($data);
  166. $return = D("Catalog")->where(" cat_id = '$cat_id' ")->find();
  167. }
  168. if (!$return) {
  169. $return['error_code'] = 10103 ;
  170. $return['error_message'] = 'request fail' ;
  171. }
  172. $this->sendResult($return);
  173. }
  174. //删除目录
  175. public function delete(){
  176. $cat_id = I("cat_id/d")? I("cat_id/d") : 0;
  177. $cat = D("Catalog")->where(" cat_id = '$cat_id' ")->find();
  178. $item_id = $cat['item_id'];
  179. $login_user = $this->checkLogin();
  180. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  181. $return['error_code'] = -1 ;
  182. $return['error_message'] = L('no_permissions');
  183. $this->sendResult($return);
  184. return;
  185. }
  186. if ($cat_id > 0 ) {
  187. $ret = D("Catalog")->deleteCat($cat_id);
  188. }
  189. if ($ret) {
  190. $this->sendResult($ret);
  191. }else{
  192. $return['error_code'] = -1 ;
  193. $return['error_message'] = 'request fail' ;
  194. $this->sendResult($return);
  195. }
  196. }
  197. //编辑页面时,自动帮助用户选中目录
  198. //选中的规则是:编辑页面则选中该页面目录,复制页面则选中目标页面目录;
  199. // 如果是恢复历史页面则使用历史页面的目录,如果都没有则选中用户上次使用的目录
  200. public function getDefaultCat(){
  201. $login_user = $this->checkLogin();
  202. $page_id = I("page_id/d");
  203. $item_id = I("item_id/d");
  204. $page_history_id = I("page_history_id/d");
  205. $copy_page_id = I("copy_page_id/d");
  206. if ($page_id > 0 ) {
  207. if ($page_history_id) {
  208. $page = D("PageHistory")->where(" page_history_id = '$page_history_id' ")->find();
  209. }else{
  210. $page = M("Page")->where(" page_id = '$page_id' ")->find();
  211. }
  212. $default_cat_id = $page['cat_id'];
  213. }
  214. //如果是复制接口
  215. elseif ($copy_page_id) {
  216. $copy_page = M("Page")->where(" page_id = '$copy_page_id' ")->find();
  217. $page['item_id'] = $copy_page['item_id'];
  218. $default_cat_id = $copy_page['cat_id'];
  219. }else{
  220. //查找用户上一次设置的目录
  221. $last_page = D("Page")->where(" author_uid ='$login_user[uid]' and item_id = '$item_id' ")->order(" addtime desc ")->limit(1)->find();
  222. $default_cat_id = $last_page['cat_id'];
  223. }
  224. $item_id = $page['item_id'] ?$page['item_id'] :$item_id;
  225. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  226. $this->sendError(10101,L('no_permissions'));
  227. return;
  228. }
  229. $this->sendResult(array("default_cat_id"=>$default_cat_id ));
  230. }
  231. //批量更新
  232. public function batUpdate(){
  233. $cats = I("cats");
  234. $item_id = I("item_id/d");
  235. $login_user = $this->checkLogin();
  236. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  237. $this->sendError(10103);
  238. return ;
  239. }
  240. $ret = '';
  241. $data_array = json_decode(htmlspecialchars_decode($cats) , true) ;
  242. if ($data_array) {
  243. foreach ($data_array as $key => $value) {
  244. if ($value['cat_name']) {
  245. $ret = D("Catalog")->where(" cat_id = '$value[cat_id]' and item_id = '$item_id' ")->save(array(
  246. "cat_name" => $value['cat_name'] ,
  247. "parent_cat_id" => $value['parent_cat_id'] ,
  248. "level" => $value['level'] ,
  249. "s_number" => $value['s_number'] ,
  250. ));
  251. }
  252. if ($value['page_id'] > 0) {
  253. $ret = D("Page")->where(" page_id = '$value[page_id]' and item_id = '$item_id' ")->save(array(
  254. "cat_id" => $value['parent_cat_id'] ,
  255. "s_number" => $value['s_number'] ,
  256. ));
  257. }
  258. }
  259. }
  260. $this->sendResult(array());
  261. }
  262. //获取某个目录下所有页面的标题
  263. public function getPagesBycat(){
  264. $cat_id = I("cat_id/d")? I("cat_id/d") : 0;
  265. $item_id = I("item_id/d");
  266. $login_user = $this->checkLogin();
  267. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  268. $this->sendError(10103);
  269. return ;
  270. }
  271. $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();
  272. $this->sendResult($return);
  273. }
  274. // 复制或移动目录
  275. public function copy(){
  276. // 参数new_p_cat_id 复制完目录后,挂在哪个父目录下。这里是父目录id。可为0
  277. // $to_item_id 要复制到的项目id。可以是同一个项目,可以是跨项目。默认是同一个项目
  278. $cat_id = I("cat_id/d");
  279. $new_p_cat_id = I("new_p_cat_id/d") ? I("new_p_cat_id/d") : 0;
  280. $to_item_id = I("to_item_id/d") ? I("to_item_id/d") : 0 ;
  281. $is_del = I("is_del/d") ? I("is_del/d") : 0 ; // 复制完是否删除原目录(相当于移动目录)
  282. $login_user = $this->checkLogin();
  283. if (!$this->checkItemPermn($login_user['uid'] , $to_item_id)) {
  284. $this->sendError(10103);
  285. return ;
  286. }
  287. $old_cat_ary = D("Catalog")->where("cat_id = '$cat_id' ")->find() ;
  288. if (!$this->checkItemPermn($login_user['uid'] , $old_cat_ary['item_id'])) {
  289. $this->sendError(10103);
  290. return ;
  291. }
  292. $res = D("Catalog")->copy($login_user['uid'] , $cat_id ,$new_p_cat_id , $to_item_id );
  293. if($is_del && $res){
  294. D("Catalog")->deleteCat($cat_id) ;
  295. }
  296. $this->sendResult($res);
  297. }
  298. }