Gold.php 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace app\admin\controller;
  3. use think\App;
  4. use think\facade\Db;
  5. class Gold extends \app\BaseController
  6. {
  7. public $post=[];
  8. public function __construct(App $app)
  9. {
  10. parent::__construct($app);
  11. $this->post=$this->request->post();
  12. }
  13. /**
  14. * @return \think\response\Json|void
  15. * @throws \think\db\exception\DataNotFoundException
  16. * @throws \think\db\exception\DbException
  17. * @throws \think\db\exception\ModelNotFoundException
  18. */
  19. public function list(){
  20. $page = isset($this->post['page']) && $this->post['page'] !== "" ? intval($this->post['page']) : "1";
  21. $size = isset($this->post['size']) && $this->post['size'] !== "" ? intval($this->post['size']) : "10";
  22. $type = isset($this->post['type']) && $this->post['type'] !== "" ? intval($this->post['type']) : "";
  23. $condition=[];
  24. if($type!=''){
  25. $condition = [['type',"=",$type]];
  26. }
  27. $count = Db::name("gold_price")->where($condition)->count();
  28. $total = ceil($count/$size);
  29. $page = $page >= $total ? $total : $page;
  30. $list =Db::name("gold_price")->where($condition)->page($page,$size)->order("addtime desc")->select();
  31. $data=[];
  32. foreach ($list as $value){
  33. $value['gold'] = $value['type']==1?'18K':($value['type']==2?"24K":'白银');
  34. $data[]=$value;
  35. }
  36. return app_show(0,"获取成功",['list'=>$data,"count"=>$count]);
  37. }
  38. public function add(){
  39. $token=isset($this->post['token'])&&$this->post['token']!=''?trim($this->post['token']):"";
  40. if($token==""){
  41. return error_show(1004,"参数token不能为空");
  42. }
  43. $userinfo = GetUserInfo($token);
  44. if(empty($userinfo)||$userinfo['code']!=0){
  45. return error_show(1002,"申请人数据不存在");
  46. }
  47. $rid= isset($userinfo["data"]['id']) ? $userinfo["data"]['id'] : "";
  48. $rname= isset($userinfo["data"]['nickname']) ? $userinfo["data"]['nickname'] : "";
  49. $type = isset($this->post['type'])&&$this->post['type']!=''?intval($this->post['type']):"";
  50. if($type==""){
  51. return error_show(1004,"参数type不能为空");
  52. }
  53. $price = isset($this->post['price'])&&$this->post['price']!=''?floor($this->post['price']):"";
  54. if($price==''){
  55. return error_show(1004,"参数price不能为空");
  56. }
  57. $gold =[
  58. "action_name"=>$rname,
  59. "type"=>$type,
  60. "price"=>$price,
  61. "addtime"=>date("Y-m-d H:i:s")
  62. ];
  63. $int = Db::name("gold_price")->insert($gold);
  64. if($int){
  65. return app_show(0,"新建成功");
  66. }else{
  67. return error_show(1004,"新建失败");
  68. }
  69. }
  70. /**
  71. * @return \think\response\Json|void
  72. * @throws \think\db\exception\DataNotFoundException
  73. * @throws \think\db\exception\DbException
  74. * @throws \think\db\exception\ModelNotFoundException
  75. */
  76. public function GetByType(){
  77. $type = isset($this->post['type'])&&$this->post['type']!=''?intval($this->post['type']):"";
  78. if($type==""){
  79. return error_show(1004,"参数type不能为空");
  80. }
  81. $price = Db::name("gold_price")->where(["type"=>$type])->order("addtime desc")->find();
  82. if(empty($price)){
  83. return error_show(1004,"未找到数据");
  84. }
  85. $price['gold'] = $price['type']==1?'18K':($price['type']==2?"24K":'白银');
  86. return app_show(0,"获取成功",$price);
  87. }
  88. }