CatalogController.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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->checkItemEdit($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->checkItemEdit($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->checkItemEdit($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. //选中的规则是:编辑页面则选中该页面目录,复制页面则选中目标页面目录;
  201. // 如果是恢复历史页面则使用历史页面的目录,如果都没有则选中用户上次使用的目录
  202. public function getDefaultCat(){
  203. $login_user = $this->checkLogin();
  204. $page_id = I("page_id/d");
  205. $item_id = I("item_id/d");
  206. $page_history_id = I("page_history_id/d");
  207. $copy_page_id = I("copy_page_id/d");
  208. if ($page_id > 0 ) {
  209. if ($page_history_id) {
  210. $page = D("PageHistory")->where(" page_history_id = '$page_history_id' ")->find();
  211. }else{
  212. $page = M("Page")->where(" page_id = '$page_id' ")->find();
  213. }
  214. $default_cat_id = $page['cat_id'];
  215. }
  216. //如果是复制接口
  217. elseif ($copy_page_id) {
  218. $copy_page = M("Page")->where(" page_id = '$copy_page_id' ")->find();
  219. $page['item_id'] = $copy_page['item_id'];
  220. $default_cat_id = $copy_page['cat_id'];
  221. }else{
  222. //查找用户上一次设置的目录
  223. $last_page = D("Page")->where(" author_uid ='$login_user[uid]' and item_id = '$item_id' ")->order(" addtime desc ")->limit(1)->find();
  224. $default_cat_id = $last_page['cat_id'];
  225. }
  226. $item_id = $page['item_id'] ?$page['item_id'] :$item_id;
  227. if (!$this->checkItemEdit($login_user['uid'] , $item_id)) {
  228. $this->sendError(10101,L('no_permissions'));
  229. return;
  230. }
  231. $this->sendResult(array("default_cat_id"=>$default_cat_id ));
  232. }
  233. //批量更新
  234. public function batUpdate(){
  235. $cats = I("cats");
  236. $item_id = I("item_id/d");
  237. $login_user = $this->checkLogin();
  238. if (!$this->checkItemEdit($login_user['uid'] , $item_id)) {
  239. $this->sendError(10103);
  240. return ;
  241. }
  242. $ret = '';
  243. $data_array = json_decode(htmlspecialchars_decode($cats) , true) ;
  244. if ($data_array) {
  245. foreach ($data_array as $key => $value) {
  246. if ($value['cat_name']) {
  247. $ret = D("Catalog")->where(" cat_id = '%d' and item_id = '%d' ",array($value['cat_id'],$item_id) )->save(array(
  248. "cat_name" => $value['cat_name'] ,
  249. "parent_cat_id" => $value['parent_cat_id'] ,
  250. "level" => $value['level'] ,
  251. "s_number" => $value['s_number'] ,
  252. ));
  253. }
  254. if ($value['page_id'] > 0) {
  255. $ret = D("Page")->where(" page_id = '%d' and item_id = '%d' " ,array($value['page_id'],$item_id) )->save(array(
  256. "cat_id" => $value['parent_cat_id'] ,
  257. "s_number" => $value['s_number'] ,
  258. ));
  259. }
  260. }
  261. }
  262. $this->sendResult(array());
  263. }
  264. //获取某个目录下所有页面的标题
  265. public function getPagesBycat(){
  266. $cat_id = I("cat_id/d")? I("cat_id/d") : 0;
  267. $item_id = I("item_id/d");
  268. $login_user = $this->checkLogin();
  269. if (!$this->checkItemEdit($login_user['uid'] , $item_id)) {
  270. $this->sendError(10103);
  271. return ;
  272. }
  273. $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();
  274. $this->sendResult($return);
  275. }
  276. // 复制或移动目录
  277. public function copy(){
  278. // 参数new_p_cat_id 复制完目录后,挂在哪个父目录下。这里是父目录id。可为0
  279. // $to_item_id 要复制到的项目id。可以是同一个项目,可以是跨项目。默认是同一个项目
  280. $cat_id = I("cat_id/d");
  281. $new_p_cat_id = I("new_p_cat_id/d") ? I("new_p_cat_id/d") : 0;
  282. $to_item_id = I("to_item_id/d") ? I("to_item_id/d") : 0 ;
  283. $is_del = I("is_del/d") ? I("is_del/d") : 0 ; // 复制完是否删除原目录(相当于移动目录)
  284. $login_user = $this->checkLogin();
  285. if (!$this->checkItemEdit($login_user['uid'] , $to_item_id)) {
  286. $this->sendError(10103);
  287. return ;
  288. }
  289. $old_cat_ary = D("Catalog")->where("cat_id = '$cat_id' ")->find() ;
  290. if (!$this->checkItemEdit($login_user['uid'] , $old_cat_ary['item_id'])) {
  291. $this->sendError(10103);
  292. return ;
  293. }
  294. $res = D("Catalog")->copy($login_user['uid'] , $cat_id ,$new_p_cat_id , $to_item_id );
  295. if($is_del && $res){
  296. D("Catalog")->deleteCat($cat_id) ;
  297. }
  298. $this->sendResult($res);
  299. }
  300. }