Brand.php 6.8 KB

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