Gold.php 3.8 KB

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