Gold.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. $uiq=md5(time());
  49. $rid= isset($userinfo["data"]['id']) ? $userinfo["data"]['id'] : "";
  50. $rname= isset($userinfo["data"]['nickname']) ? $userinfo["data"]['nickname'] : "";
  51. $type = isset($this->post['type'])&&$this->post['type']!=''?intval($this->post['type']):"";
  52. if($type==""){
  53. return error_show(1004,"参数type不能为空");
  54. }
  55. $price = isset($this->post['price'])&&$this->post['price']!=''?floor($this->post['price']):"";
  56. if($price==''){
  57. return error_show(1004,"参数price不能为空");
  58. }
  59. $gold =[
  60. "action_name"=>$rname,
  61. "type"=>$type,
  62. "price"=>$price,
  63. "uiq"=>$uiq,
  64. "addtime"=>date("Y-m-d H:i:s")
  65. ];
  66. $int = Db::name("gold_price")->insert($gold);
  67. if($int){
  68. $orde = ["order_code"=>$type,"status"=>0,"action_remark"=>'',"action_type"=>"create"];
  69. ActionLog::logAdd($this->post['token'],$orde,'gold_price',0,$orde);
  70. return app_show(0,"新建成功");
  71. }else{
  72. return error_show(1004,"新建失败");
  73. }
  74. }
  75. /**
  76. * @return \think\response\Json|void
  77. * @throws \think\db\exception\DataNotFoundException
  78. * @throws \think\db\exception\DbException
  79. * @throws \think\db\exception\ModelNotFoundException
  80. */
  81. public function GetByType(){
  82. $type = isset($this->post['type'])&&$this->post['type']!=''?intval($this->post['type']):"";
  83. if($type==""){
  84. return error_show(1004,"参数type不能为空");
  85. }
  86. $price = Db::name("gold_price")->where(["type"=>$type])->order("addtime desc")->find();
  87. if(empty($price)){
  88. return error_show(1004,"未找到数据");
  89. }
  90. $price['gold'] = $price['type']==1?'18K':($price['type']==2?"24K":'白银');
  91. return app_show(0,"获取成功",$price);
  92. }
  93. }