Catdesc.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace app\admin\controller;
  3. use app\BaseController;
  4. use think\App;
  5. use think\facade\Db;
  6. //分类描述信息
  7. class Catdesc extends Base
  8. {
  9. //public $post="";
  10. public function __construct(App $app)
  11. {
  12. parent::__construct($app);
  13. // $this->post=$this->request->post();
  14. }
  15. public function create(){
  16. $cat_id = isset($this->post['cat_id']) && $this->post['cat_id'] !==""? intval($this->post['cat_id']):"";
  17. if($cat_id===""){
  18. return error_show(1002,"参数cat_id不能为空");
  19. }
  20. $desc = isset($this->post['desc']) && $this->post['desc'] !==""? trim($this->post['desc']):"";
  21. if($desc==""){
  22. return error_show(1002,"参数desc不能为空");
  23. }
  24. $token = isset($this->post['token'])&& $this->post['token']!='' ? trim($this->post['token']):"";
  25. if($token==''){
  26. return error_show(105,"参数token不能为空");
  27. }
  28. $user =GetUserInfo($token);
  29. if(empty($user)||$user['code']!=0){
  30. return error_show(102,"创建人数据不存在");
  31. }
  32. $createrid= isset($user["data"]['id']) ? $user["data"]['id'] : "";
  33. $creater= isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  34. $status = isset($this->post['status']) &&$this->post['status'] !==""? intval($this->post['status']):"0";
  35. $data =[
  36. "cat_id"=>$cat_id,
  37. "desc"=>$desc,
  38. "status"=>$status,
  39. "createrid"=>$createrid,
  40. "creater"=>$creater,
  41. "addtime"=>date("Y-m-d H:i:s"),
  42. "updatetime"=>date("Y-m-d H:i:s")
  43. ];
  44. $datainfo = Db::name('cat_desc')->insert($data);
  45. if($datainfo){
  46. return error_show(0,"新建成功");
  47. }else{
  48. return error_show(1002,"新建失败");
  49. }
  50. }
  51. public function list(){
  52. $page = isset($this->post['page']) && $this->post['page'] !=="" ? intval($this->post['page']):"1";
  53. $size = isset($this->post['size']) && $this->post['size'] !=="" ? intval($this->post['size']):"10";
  54. $where=[];
  55. $cat_id = isset($this->post['cat_id']) && $this->post['cat_id'] !=="" ? trim($this->post['cat_id']):"";
  56. if($cat_id!=""){
  57. $where[]=['a.cat_id',"=",$cat_id];
  58. }
  59. $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']):"";
  60. if($status!=""){
  61. $where[]=['a.status',"=",$status];
  62. }
  63. $stat = isset($this->post['stat']) && $this->post['stat'] !=="" ? intval($this->post['stat']):"";
  64. if($stat!=""){
  65. $where[]=['b.status',"=",$stat];
  66. }
  67. $creater = isset($this->post['creater']) && $this->post['creater'] !=="" ? intval($this->post['creater']):"";
  68. if($creater!=""){
  69. $where[]=['a.creater',"like","%$creater%"];
  70. }
  71. $start = isset($this->post['start']) && $this->post['start']!=="" ? $this->post['start']:"";
  72. if($start!==""){
  73. $where[]=['a.addtime',">=",date('Y-m-d H:i:s',strtotime($start))];
  74. }
  75. $end = isset($this->post['end']) && $this->post['end']!=="" ? $this->post['end']:"";
  76. if($end!==""){
  77. $where[]=['a.addtime',"<",date('Y-m-d H:i:s',strtotime($end)+24*3600)];
  78. }
  79. $count = Db::name('cat_desc')->alias('a')->join('cat b',"b.id=a.cat_id","left") ->where($where)->count();
  80. $total = ceil($count / $size);
  81. $page = $page >= $total ? $total : $page;
  82. $list = Db::name('cat_desc')->alias('a')->join('wsm_cat b',"b.id=a.cat_id","left")
  83. ->where($where)->page($page,$size)->order("addtime desc")->field("a.*,b.status as stat")->select();
  84. return app_show(0,"获取成功",['list'=>$list,'count'=>$count]);
  85. }
  86. public function status(){
  87. $id = isset($this->post['id']) && $this->post['id'] !==""? intval($this->post['id']):"";
  88. if($id==""){
  89. return error_show(1002,"参数id不能为空");
  90. }
  91. $info = Db::name("cat_desc")->where([["id","=",$id]])->find();
  92. if(!$info){
  93. return error_show(1002,"未找到对应数据");
  94. }
  95. $status = isset($this->post['status']) && $this->post['status']!==""? intval($this->post['status']):"";
  96. if($status===""){
  97. return error_show(1002,"参数status不能为空");
  98. }
  99. if(!in_array($status,[0,1])){
  100. return error_show(1002,"参数status无效");
  101. }
  102. $info['status']=$status;
  103. $info['updatetime']=date("Y-m-d H:i:s");
  104. $msg = $status==1?"启用":"禁用";
  105. $update = Db::name("cat_desc")->save($info);
  106. return $update? error_show(0,"{$msg}成功"):error_show(1004,"{$msg}失败");
  107. }
  108. public function edit(){
  109. $id = isset($this->post['id']) && $this->post['id'] !==""? intval($this->post['id']):"";
  110. if($id==""){
  111. return error_show(1002,"参数id不能为空");
  112. }
  113. $info = Db::name("cat_desc")->where(['id'=>$id,"is_del"=>0])->find();
  114. if($info==""){
  115. return error_show(1002,"未找到数据");
  116. }
  117. $cat_id = isset($this->post['cat_id']) && $this->post['cat_id'] !==""? intval($this->post['cat_id']):"";
  118. if($cat_id===""){
  119. return error_show(1002,"参数cat_id不能为空");
  120. }
  121. $desc = isset($this->post['desc']) && $this->post['desc'] !==""? trim($this->post['desc']):"";
  122. if($desc==""){
  123. return error_show(1002,"参数desc不能为空");
  124. }
  125. $token = isset($this->post['token'])&& $this->post['token']!='' ? trim($this->post['token']):"";
  126. if($token==''){
  127. return error_show(105,"参数token不能为空");
  128. }
  129. $user =GetUserInfo($token);
  130. if(empty($user)||$user['code']!=0){
  131. return error_show(102,"创建人数据不存在");
  132. }
  133. $createrid= isset($user["data"]['id']) ? $user["data"]['id'] : "";
  134. $creater= isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  135. $status = isset($this->post['status']) &&$this->post['status'] !==""? intval($this->post['status']):"0";
  136. $data =[
  137. "id"=>$id,
  138. "cat_id"=>$cat_id,
  139. "desc"=>$desc,
  140. "status"=>$status,
  141. "createrid"=>$createrid,
  142. "creater"=>$creater,
  143. "updatetime"=>date("Y-m-d H:i:s")
  144. ];
  145. $datatinfo = Db::name("cat_desc")->save($data);
  146. if($datatinfo){
  147. return error_show(0,"更新成功");
  148. }else{
  149. return error_show(1002,"更新失败");
  150. }
  151. }
  152. public function del(){
  153. $id = $this->post['id'] && $this->post['id'] !=="" ? intval($this->post['id']) :"";
  154. if($id===""){
  155. return error_show(1004,"参数id不能为空");
  156. }
  157. $str= Db::name('cat_desc')->where(['id'=>$id,'is_del'=>0])->find();
  158. if(empty($str)){
  159. return error_show(1002,"未找到数据");
  160. }
  161. $end = Db::name('cat_desc')->update(['id'=>$id,'is_del'=>1]);
  162. if($end){
  163. return error_show(0,"删除成功");
  164. }else{
  165. return error_show(1002,"删除失败");
  166. }
  167. }
  168. }