Gold.php 3.7 KB

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