CatalogController.class.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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")->where(" item_id = '$item_id' ")->order(" 's_number', addtime asc ")->select();
  15. }
  16. if ($ret) {
  17. $this->sendResult($ret);
  18. }else{
  19. $return['error_code'] = 10103 ;
  20. $return['error_message'] = 'request fail' ;
  21. $this->sendResult($return);
  22. }
  23. }
  24. //获取二级目录列表
  25. public function secondCatList(){
  26. $login_user = $this->checkLogin();
  27. $item_id = I("item_id/d");
  28. if (!$this->checkItemVisit($login_user['uid'] , $item_id)) {
  29. $this->sendError(10103);
  30. return ;
  31. }
  32. if ($item_id > 0 ) {
  33. $ret = D("Catalog")->where(" item_id = '$item_id' and level =2 ")->order(" 's_number', addtime asc ")->select();
  34. }
  35. if ($ret) {
  36. $this->sendResult($ret);
  37. }else{
  38. $return['error_code'] = 10103 ;
  39. $return['error_message'] = 'request fail' ;
  40. $this->sendResult($return);
  41. }
  42. }
  43. //获取二级目录的子目录列表,即三级目录列表(如果存在的话)
  44. public function childCatList(){
  45. $cat_id = I("cat_id/d");
  46. if ($cat_id > 0 ) {
  47. $ret = D("Catalog")->where(" parent_cat_id = '$cat_id' ")->order(" 's_number', addtime asc ")->select();
  48. }
  49. if ($ret) {
  50. $this->sendResult($ret);
  51. }else{
  52. $return['error_code'] = 10103 ;
  53. $return['error_message'] = 'request fail' ;
  54. $this->sendResult($return);
  55. }
  56. }
  57. //保存目录
  58. public function save(){
  59. $cat_name = I("cat_name");
  60. $s_number = I("s_number/d") ? I("s_number/d") : 99 ;
  61. $cat_id = I("cat_id/d")? I("cat_id/d") : 0;
  62. $parent_cat_id = I("parent_cat_id/d")? I("parent_cat_id/d") : 0;
  63. $item_id = I("item_id/d");
  64. $login_user = $this->checkLogin();
  65. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  66. $this->sendError(10103);
  67. return;
  68. }
  69. //禁止空目录的生成
  70. if (!$cat_name) {
  71. return;
  72. }
  73. $data['cat_name'] = $cat_name ;
  74. $data['s_number'] = $s_number ;
  75. $data['item_id'] = $item_id ;
  76. $data['parent_cat_id'] = $parent_cat_id ;
  77. if ($parent_cat_id > 0 ) {
  78. $data['level'] = 3;
  79. }else{
  80. $data['level'] = 2;
  81. }
  82. if ($cat_id > 0 ) {
  83. $ret = D("Catalog")->where(" cat_id = '$cat_id' ")->save($data);
  84. $return = D("Catalog")->where(" cat_id = '$cat_id' ")->find();
  85. }else{
  86. $data['addtime'] = time();
  87. $cat_id = D("Catalog")->add($data);
  88. $return = D("Catalog")->where(" cat_id = '$cat_id' ")->find();
  89. }
  90. if (!$return) {
  91. $return['error_code'] = 10103 ;
  92. $return['error_message'] = 'request fail' ;
  93. }
  94. $this->sendResult($return);
  95. }
  96. //删除目录
  97. public function delete(){
  98. $cat_id = I("cat_id/d")? I("cat_id/d") : 0;
  99. $cat = D("Catalog")->where(" cat_id = '$cat_id' ")->find();
  100. $item_id = $cat['item_id'];
  101. $login_user = $this->checkLogin();
  102. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  103. $return['error_code'] = -1 ;
  104. $return['error_message'] = L('no_permissions');
  105. $this->sendResult($return);
  106. return;
  107. }
  108. if (D("Page")->where(" cat_id = '$cat_id' ")->find() || D("Catalog")->where(" parent_cat_id = '$cat_id' ")->find()) {
  109. $return['error_code'] = -1 ;
  110. $return['error_message'] = L('no_delete_empty_catalog') ;
  111. $this->sendResult($return);
  112. return;
  113. }
  114. if ($cat_id > 0 ) {
  115. $ret = D("Catalog")->where(" cat_id = '$cat_id' ")->delete();
  116. }
  117. if ($ret) {
  118. $this->sendResult($ret);
  119. }else{
  120. $return['error_code'] = -1 ;
  121. $return['error_message'] = 'request fail' ;
  122. $this->sendResult($return);
  123. }
  124. }
  125. }