Attr.php 6.0 KB

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