Specs.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\ChangeLog;
  4. use app\BaseController;
  5. use think\App;
  6. use think\facade\Db;
  7. //商品规格
  8. class Specs extends BaseController
  9. {
  10. public $post="";
  11. public function __construct(App $app)
  12. {
  13. parent::__construct($app);
  14. $this->post=$this->request->post();
  15. }
  16. public function create(){
  17. $spec_name = isset($this->post['spec_name']) && $this->post['spec_name'] !==""? trim($this->post['spec_name']):"";
  18. if($spec_name==""){
  19. return error_show(1002,"参数spec_name不能为空");
  20. }
  21. $token = isset($this->post['token'])&&$this->post['token']!='' ? trim($this->post['token']):"";
  22. if($token==''){
  23. return error_show(105,"参数token不能为空");
  24. }
  25. $user =GetUserInfo($token);
  26. if(empty($user)||$user['code']!=0){
  27. return error_show(1002,"创建人数据不存在");
  28. }
  29. $createrid= isset($user["data"]['id']) ? $user["data"]['id'] : "";
  30. $creater= isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  31. $exam_status = isset($this->post['exam_status']) &&$this->post['exam_status'] !==""? intval($this->post['exam_status']):"0";
  32. $status = isset($this->post['status']) &&$this->post['status'] !==""? intval($this->post['status']):"1";
  33. $data = [
  34. "spec_name"=>$spec_name,
  35. "creater"=>$creater,
  36. "createrid"=>$createrid,
  37. "exam_status"=>$exam_status,
  38. "status"=>$status,
  39. "is_del"=>0,
  40. "addtime"=>date("Y-m-d H:i:s"),
  41. "updatetime"=>date("Y-m-d H:i:s")
  42. ];
  43. $datainfo = Db::name('specs')->insert($data);
  44. if($datainfo){
  45. return error_show(0,"新建成功");
  46. }else{
  47. return error_show(1002,"创建失败");
  48. }
  49. }
  50. public function list(){
  51. $page = isset($this->post['page']) && $this->post['page'] !==""? intval($this->post['page']):"1";
  52. $size = isset($this->post['size']) && $this->post['size'] !=="" ? intval($this->post['size']):"10";
  53. $where=[["is_del","=",0]];
  54. $spec_name = isset($this->post['spec_name']) && $this->post['spec_name'] !=="" ? trim($this->post['spec_name']):"";
  55. if($spec_name!=""){
  56. $where[]=['spec_name',"like","%$spec_name%"];
  57. }
  58. $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status'])
  59. :"";
  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('specs') ->where($where)->count();
  76. $total = ceil($count / $size);
  77. $page = $page >= $total ? $total : $page;
  78. $list = Db::name('specs')->where($where)->page($page,$size)->order("addtime desc")->select();
  79. return app_show(0,"获取成功",['list'=>$list,'count'=>$count]);
  80. }
  81. public function status(){
  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("specs")->where([["id","=",$id],["is_del","=",0]])->find();
  87. if(!$info){
  88. return error_show(1002,"未找到对应数据");
  89. }
  90. $status = isset($this->post['status']) && $this->post['status']!==""? intval($this->post['status']):"";
  91. if($status===""){
  92. return error_show(1002,"参数status不能为空");
  93. }
  94. if(!in_array($status,[0,1])){
  95. return error_show(1002,"参数status无效");
  96. }
  97. $info['status']=$status;
  98. $info['updatetime']=date("Y-m-d H:i:s");
  99. $msg = $status==1?"启用":"禁用";
  100. $update = Db::name("specs")->save($info);
  101. return $update? error_show(0,"{$msg}成功"):error_show(1004,"{$msg}失败");
  102. }
  103. public function title(){
  104. $where=[["is_del","=",0]];
  105. $spec_name = isset($this->post['spec_name']) && $this->post['spec_name'] !=="" ? trim($this->post['spec_name']):"";
  106. if($spec_name!=""){
  107. $where[]=['spec_name',"like","%$spec_name%"];
  108. }
  109. $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']):"";
  110. if($status!=""){
  111. $where[]=['status',"=",$status];
  112. }
  113. $creater = isset($this->post['creater']) && $this->post['creater'] !=="" ? intval($this->post['creater']):"";
  114. if($creater!=""){
  115. $where[]=['creater',"like","%$creater%"];
  116. }
  117. $start = isset($this->post['start']) && $this->post['start']!=="" ? $this->post['start']:"";
  118. if($start!==""){
  119. $where[]=['addtime',">=",date('Y-m-d H:i:s',strtotime($start))];
  120. }
  121. $end = isset($this->post['end']) && $this->post['end']!=="" ? $this->post['end']:"";
  122. if($end!==""){
  123. $where[]=['addtime',"<",date('Y-m-d H:i:s',strtotime($end)+24*3600)];
  124. }
  125. $list = Db::name('specs')->where($where)->order("addtime desc")->select();
  126. return app_show(0,"获取成功",$list);
  127. }
  128. public function edit(){
  129. $id = isset($this->post['id']) && $this->post['id']!==""?intval($this->post['id']):"";
  130. if($id==""){
  131. return error_show(1002,"参数id不能为空");
  132. }
  133. $info = Db::name("specs")->where(['id'=>$id,"is_del"=>0])->find();
  134. if($info==""){
  135. return error_show(1002,"未找到数据");
  136. }
  137. if($info['status']==1){
  138. return error_show(1002,"状态是启用状态,无法编辑");
  139. }
  140. $spec_name = isset($this->post['spec_name']) && $this->post['spec_name'] !==""? trim($this->post['spec_name']):"";
  141. if($spec_name==""){
  142. return error_show(1002,"参数spec_name不能为空");
  143. }
  144. $exam_status = isset($this->post['exam_status']) &&$this->post['exam_status'] !==""? intval($this->post['exam_status']):"0";
  145. $status = isset($this->post['status']) &&$this->post['status'] !==""? intval($this->post['status']):"0";
  146. $data = [
  147. "id"=>$id,
  148. "spec_name"=>$spec_name,
  149. "exam_status"=>$exam_status,
  150. "status"=>$status,
  151. "is_del"=>0,
  152. "updatetime"=>date("Y-m-d H:i:s")
  153. ];
  154. $datainfo=Db::name('specs')->save($data);
  155. $item = array_diff($data,$info);
  156. $json = json_encode($item,JSON_UNESCAPED_UNICODE);
  157. $jsp = json_encode($info,JSON_UNESCAPED_UNICODE);
  158. if($datainfo){
  159. ChangeLog::logAdd(5,$info['id'],$jsp,$json,$this->post['token'],$this->post);
  160. return error_show(0,"更新成功");
  161. }else{
  162. return error_show(1002,"更新失败");
  163. }
  164. }
  165. public function del(){
  166. $id = $this->post['id'] && $this->post['id'] !=="" ? intval($this->post['id']) :"";
  167. if($id===""){
  168. return error_show(1004,"参数id不能为空");
  169. }
  170. $str= Db::name('specs')->where(['id'=>$id,'is_del'=>0])->find();
  171. if(empty($str)){
  172. return error_show(1002,"未找到数据");
  173. }
  174. $end = Db::name('specs')->update(['id'=>$id,'is_del'=>1]);
  175. if($end){
  176. return error_show(0,"删除成功");
  177. }else{
  178. return error_show(1002,"删除失败");
  179. }
  180. }
  181. }