Gift.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. /**
  6. * 礼物接口
  7. */
  8. class Gift extends Api
  9. {
  10. protected $noNeedLogin = ['getGiftList','getGiftType'];
  11. protected $noNeedRight = '*';
  12. public function _initialize()
  13. {
  14. parent::_initialize();
  15. $this->giftModel = new \app\common\model\Gift();
  16. $this->gifttypeModel = new \app\common\model\GiftType();
  17. }
  18. public function test2(){
  19. $user_id = $this->auth->id;
  20. $gift_id = input('gift_id');
  21. $pay_total = input('pay_total');
  22. Db::startTrans();
  23. $rs = $this->test($user_id,$gift_id,$pay_total);
  24. if($rs !== true){
  25. Db::rollback();
  26. }
  27. Db::commit();
  28. dump($rs);
  29. }
  30. //gift_id必须是爆币礼物才可以
  31. public function test($user_id,$gift_id,$pay_total){
  32. //奖项参数
  33. $conf_arr = Db::name('gift_baobi_config')->where('gift_id',$gift_id)->select();
  34. if(empty($conf_arr)){
  35. return true;
  36. }
  37. //用户今天的爆币情况,与消费情况,不用区分具体哪个爆币礼物
  38. $today_start = strtotime(date('Y-m-d'));
  39. $today_end = $today_start + 86399;
  40. $map = [
  41. 'user_id' => $user_id,
  42. 'createtime' => ['BETWEEN',[$today_start,$today_end]],
  43. ];
  44. $today_data = Db::name('gift_baobi_log')->field('IFNULL(sum(pay_total),0) as today_pay_total,IFNULL(sum(baobi_total),0) as today_baobi_total')->where($map)->find();
  45. //dump($today_data);
  46. if($today_data['today_baobi_total'] >= $today_data['today_pay_total']){
  47. //爆币比消费还高了,不用抽奖了,记录个日志,返回。这里的日志仅用来记录今日消费 sum(pay_total)
  48. $data = [];
  49. $data['user_id'] = $user_id;
  50. $data['gift_id'] = $gift_id;
  51. $data['baobi_id'] = 0;
  52. $data['beilv'] = 0;
  53. $data['rate'] = 0;
  54. $data['pay_total'] = $pay_total;
  55. $data['baobi_total'] = 0;
  56. $data['createtime'] = time();
  57. $log_id = Db::name('gift_baobi_log')->insertGetId($data);
  58. if(!$log_id){
  59. return false;
  60. }
  61. return true;
  62. }
  63. //概率新数组
  64. $gailv = [];
  65. $bei = 100; //小数变整数
  66. foreach ($conf_arr as $key=>$value)
  67. {
  68. $gailv[$value['id']] = $value['rate']*$bei;
  69. }
  70. //dump($gailv);
  71. //奖项新数组
  72. $conf_column = [];
  73. foreach ($conf_arr as $key=>$value)
  74. {
  75. $conf_column[$value['id']] = $value;
  76. }
  77. //dump($conf_column);
  78. //抽
  79. $rid = $this->getRand($gailv); //根据概率获取奖项id
  80. //dump($rid);
  81. //返回中奖结果
  82. $result = $conf_column[$rid];
  83. //爆币金额
  84. $baobi_total = bcdiv(bcmul($pay_total,$result['beilv'],0),100,0);
  85. //写入爆币记录,不论有没有钱。全都写入其实是为了方便查日志,比在money_log里找范围更小,而且可删除
  86. $data = [];
  87. $data['user_id'] = $user_id;
  88. $data['gift_id'] = $gift_id;
  89. $data['baobi_id'] = $result['id'];
  90. $data['beilv'] = $result['beilv'];
  91. $data['rate'] = $result['rate'];
  92. $data['pay_total'] = $pay_total;
  93. $data['baobi_total'] = $baobi_total;
  94. $data['createtime'] = time();
  95. $log_id = Db::name('gift_baobi_log')->insertGetId($data);
  96. if(!$log_id){
  97. return false;
  98. }
  99. //直接给用户钱
  100. if($baobi_total > 0){
  101. $rs_wallet = model('wallet')->lockChangeAccountRemain($user_id,$baobi_total,'+',0,'送礼物爆币'.$result['beilv'].'%',21,'jewel');
  102. if($rs_wallet['status'] === false){
  103. return false;
  104. }
  105. }
  106. //默认成功
  107. return true;
  108. }
  109. //概率获得算法
  110. private function getRand($proArr) {
  111. //概率数组的总概率精度
  112. $proSum = array_sum($proArr);
  113. $key = rand(1, $proSum);
  114. // echo $key;
  115. $result = 0;
  116. $now = 0;
  117. foreach ($proArr as $k=>$v)
  118. {
  119. $now = $now + $v;
  120. if($key<=$now)
  121. {
  122. $result = $k;
  123. break;
  124. }
  125. }
  126. unset ($proArr);
  127. return $result;
  128. }
  129. /**
  130. * 获取礼物列表
  131. */
  132. public function getGiftList() {
  133. $type = input('type',0);
  134. // 获取基本信息
  135. $where = ['is_show'=>1];
  136. if($type){
  137. $where['type'] = $type;
  138. }
  139. $giftList = Db::name('gift')->field('id,name,price,image,special')->where($where)->order("sort","asc")->select();
  140. $giftList = list_domain_image($giftList,['image','special']);
  141. $this->success("获取成功!",$giftList);
  142. }
  143. /**
  144. * 获取礼物类型
  145. */
  146. public function getGiftType() {
  147. // 获取基本信息
  148. $where = [];
  149. $where["is_show"] = 1;
  150. $giftList = $this->gifttypeModel->field("id,name")->where($where)->order("weight","desc")->select();
  151. $this->success("获取成功!",$giftList);
  152. }
  153. /**
  154. * 获取我的背包礼物
  155. */
  156. public function getMyBackGift() {
  157. $userid = input("user_id", $this->auth->id);
  158. // 分页搜索构建
  159. $list = \app\common\model\GiftBack::field("id,name,image,gif_image,value,sum(number) as number")
  160. ->where(["user_id"=>$userid,"is_use"=>0])
  161. ->group("name")
  162. ->select();
  163. $this->success("获取成功!",$list);
  164. }
  165. /**
  166. * 获取我的礼物墙
  167. */
  168. public function getMyGiftWall() {
  169. $user_id = input("user_id", 0);
  170. $userid = $user_id ? $user_id : $this->auth->id;
  171. $list = \app\common\model\GiftUserParty::alias('a')->join("hx_gift g", "g.id = a.gift_id", "inner")->field("gift_id,g.name,g.image,sum(number) as number")
  172. ->where(["user_to_id" => $userid,])
  173. ->group("gift_id")
  174. ->order('g.value desc')
  175. ->select();
  176. $this->success("获取成功!", $list);
  177. }
  178. /**
  179. * 获取我的礼物墙
  180. */
  181. public function getMyGiftWall_typing() {
  182. $user_id = input("user_id", 0);
  183. $userid = $user_id ? $user_id : $this->auth->id;
  184. $list = Db::name('gift_user_typing')->alias('log')
  185. ->join('gift', 'gift.id = log.gift_id', 'LEFT')->field('log.*,sum(number) as number,gift.name,gift.image,gift.special')
  186. ->where(['log.user_to_id' => $userid])
  187. ->group('log.gift_id')
  188. ->order('gift.price desc')
  189. ->select();
  190. $list = list_domain_image($list,['image','special']);
  191. $this->success("获取成功!", $list);
  192. }
  193. /**
  194. * 获取我的收送礼明细
  195. */
  196. public function my_gift_log(){
  197. $user_id = $this->auth->id;
  198. $type = input('type',1);
  199. $where = [];
  200. if($type == 1){
  201. $where['user_id'] = $user_id;//我送出
  202. $joinstr = 'gup.user_to_id = user.id';
  203. }else{
  204. $where['user_to_id'] = $user_id;//我收到
  205. $joinstr = 'gup.user_id = user.id';
  206. }
  207. $list = Db::name('gift_user_party')->alias('gup')
  208. ->join('user',$joinstr,'LEFT')->field('gup.*,user.nickname')
  209. ->where($where)->order('id desc')->autopage()->select();
  210. $list = list_domain_image($list,['gift_gif_image']);
  211. $rs = [];
  212. if(empty($list)){
  213. $this->success(1,$rs);
  214. }
  215. foreach($list as $key => $val){
  216. if($type == 1){
  217. $remark = '赠送'.$val['nickname'].','.$val['gift_name'].'*'.$val['number'].',价值'.$val['value'].'钻石';
  218. }else{
  219. $remark = $val['nickname'].'赠送,'.$val['gift_name'].'*'.$val['number'].',价值'.$val['value'].'钻石';
  220. }
  221. $rs[] = [
  222. 'id' => $val['id'],
  223. 'gift_image' => $val['gift_gif_image'],
  224. 'createtime' => $val['createtime'],
  225. 'remark' => $remark
  226. ];
  227. }
  228. $this->success(1,$rs);
  229. }
  230. //聊天送礼物
  231. public function givegift_typing() {
  232. // 接口防并发
  233. if (!$this->apiLimit(1, 1000)) {
  234. $this->error(__('Operation frequently'));
  235. }
  236. $user_id = input('user_id');// 赠送对象
  237. $gift_id = input('gift_id');// 礼物ID
  238. $number = input('number',1,'intval');//数量
  239. $is_back = input("is_back",0);// 是否背包赠送: 1=是,0=否
  240. if (!$user_id || !$gift_id || $number < 1 || !in_array($is_back,[0,1]) )
  241. {
  242. $this->error();
  243. }
  244. // 不可以赠送给自己
  245. if($this->auth->id == $user_id)
  246. {
  247. $this->error("不可以赠送给自己");
  248. }
  249. //被赠送人信息
  250. $touserinfo = Db::name('user')->where('id',$user_id)->find();
  251. if (!$touserinfo)
  252. {
  253. $this->error("不存在的用户");
  254. }
  255. $backGiftId = 0; //背包礼物表的 gift_id
  256. if($is_back == 1) {
  257. // 获取背包礼物信息
  258. $giftInfo = Db::name('gift_back')->field('gift_id')->where('id',$gift_id)->find();
  259. if(!$giftInfo){
  260. $this->error("背包礼物获取失败");
  261. }
  262. $backGiftId = $giftInfo['gift_id'];
  263. // 随机获取一个礼物
  264. $allCount = $number;
  265. $giftbackList = Db::name('gift_back')->field('id,value,name,image,gif_image,number')->where(["gift_id"=>$backGiftId,"user_id"=>$this->auth->id,'is_use'=>0])->limit($allCount)->order('id asc')->select();
  266. $giftinfo = isset($giftbackList[0]) ? $giftbackList[0] : [];
  267. $giftcount = 0;
  268. $giftList = [];
  269. if(!empty($giftbackList)) {
  270. foreach($giftbackList as $k => $v) {
  271. $giftList[$k] = $v;
  272. $giftcount += $v["number"];
  273. if($giftcount >= $allCount) {
  274. break;
  275. }
  276. }
  277. }
  278. if($giftcount < $allCount)
  279. {
  280. $this->error("背包数量不足");
  281. }
  282. $giftvalue = $giftinfo["value"] * $number;
  283. $getValue = $giftvalue;
  284. }else{
  285. // 获取礼物信息
  286. $giftinfo = Db::name('gift')->where('id',$gift_id)->find();
  287. if (!$giftinfo)
  288. {
  289. $this->error("请选择礼物");
  290. }
  291. $giftvalue = $giftinfo["value"] * $number;
  292. $getValue = $giftvalue;
  293. // 判断当前用户余额
  294. $user_jewel = model('wallet')->getWallet($this->auth->id,'jewel');
  295. if($user_jewel < $giftvalue)
  296. {
  297. $this->error("您的金币余额不足");
  298. }
  299. }
  300. Db::startTrans();
  301. if($is_back == 1) {
  302. $b=0;
  303. foreach($giftList as $k => $v) {
  304. for($a=1;$a<=$v["number"];$a++) {
  305. $b++;
  306. $num = $v["number"] - $a;
  307. if($num > 0) {
  308. $res1 = Db::name('gift_back')->where(["id"=>$v["id"]])->setDec("number");
  309. } else {
  310. // $res1 = \app\common\model\GiftBack::update(["is_use"=>1,"use_time"=>time()],["id"=>$v["id"]]);
  311. $res1 = Db::name('gift_back')->where(["id"=>$v["id"]])->delete();
  312. }
  313. if($b == $number) break;
  314. }
  315. }
  316. $res2 = true;
  317. }else{
  318. if($giftvalue > 0){
  319. // 扣除当前用户余额
  320. $wallet_rs = model('wallet')->lockChangeAccountRemain($this->auth->id,$giftvalue,'-',0,"赠送礼物:'" . $giftinfo["name"] . "',扣除" . $giftvalue . "金币!",3,'jewel');
  321. if($wallet_rs['status'] === false){
  322. Db::rollback();
  323. $this->error($wallet_rs['msg']);
  324. }
  325. // 添加赠送用户余额
  326. $money_to_jewel = config('site.money_to_jewel');
  327. $gift_plat_scale = config('site.gift_plat_scale');
  328. $giftmoney = bcdiv($giftvalue,$money_to_jewel,2);
  329. $money = bcdiv(bcmul($giftmoney,100 - $gift_plat_scale,2),100,2);
  330. $wallet_rs = model('wallet')->lockChangeAccountRemain($user_id,$money,'+',0,'获得礼物:'.$giftinfo["name"],103,'money');
  331. if($wallet_rs['status'] === false){
  332. Db::rollback();
  333. $this->error($wallet_rs['msg']);
  334. }
  335. }
  336. }
  337. // 添加礼物赠送记录表
  338. $data = [
  339. 'user_id' => $this->auth->id,
  340. 'user_to_id' => $user_id,
  341. 'gift_id' => $backGiftId > 0 ? $backGiftId : $gift_id,
  342. 'gift_give_type' => $is_back ? 1 : 2,
  343. 'gift_name' => $giftinfo['name'],
  344. 'gift_gif_image' => $giftinfo['image'],
  345. 'number' => $number,
  346. 'price' => $giftinfo['value'],
  347. 'value' => $giftvalue,
  348. 'task_status' => 1,
  349. 'createtime' => time(),
  350. ];
  351. //每个礼物都要计算平台抽成和房主抽成
  352. $platRate = 10;
  353. $data['platvalue'] = bcmul($platRate/100 ,$data["value"],2);//平台抽成
  354. $data['getvalue'] = bcsub($data["value"] ,$data['platvalue'],2);//减去抽成剩余价值
  355. $log_id = Db::name('gift_user_party')->insertGetId($data);
  356. if(!$log_id){
  357. Db::rollback();
  358. $this->error('赠送失败');
  359. }
  360. Db::commit();
  361. $this->success('赠送成功');
  362. }
  363. }