Goldprice.php 6.8 KB

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