Gift.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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->baobi($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 baobi($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. return $baobi_total;
  107. }
  108. //默认成功
  109. return true;
  110. }
  111. //概率获得算法
  112. private function getRand($proArr) {
  113. //概率数组的总概率精度
  114. $proSum = array_sum($proArr);
  115. $key = rand(1, $proSum);
  116. // echo $key;
  117. $result = 0;
  118. $now = 0;
  119. foreach ($proArr as $k=>$v)
  120. {
  121. $now = $now + $v;
  122. if($key<=$now)
  123. {
  124. $result = $k;
  125. break;
  126. }
  127. }
  128. unset ($proArr);
  129. return $result;
  130. }
  131. /**
  132. * 获取礼物列表
  133. */
  134. public function getGiftList() {
  135. $type = input('type',0);
  136. // 获取基本信息
  137. $where = ['is_show'=>1];
  138. if($type){
  139. $where['type'] = $type;
  140. }
  141. $giftList = Db::name('gift')->field('id,name,price,image,special')->where($where)->order("sort","asc")->select();
  142. $giftList = list_domain_image($giftList,['image','special']);
  143. $this->success("获取成功!",$giftList);
  144. }
  145. /**
  146. * 获取礼物类型
  147. */
  148. public function getGiftType() {
  149. // 获取基本信息
  150. $where = [];
  151. $where["is_show"] = 1;
  152. $giftList = $this->gifttypeModel->field("id,name")->where($where)->order("weight","desc")->select();
  153. $this->success("获取成功!",$giftList);
  154. }
  155. /**
  156. * 获取我的背包礼物
  157. */
  158. public function getMyBackGift() {
  159. $userid = input("user_id", $this->auth->id);
  160. // 分页搜索构建
  161. $list = \app\common\model\GiftBack::field("id,name,image,gif_image,value,sum(number) as number")
  162. ->where(["user_id"=>$userid,"is_use"=>0])
  163. ->group("gift_id")
  164. ->autopage()
  165. ->select();
  166. $this->success("获取成功!",$list);
  167. }
  168. /**
  169. * 获取我的礼物墙
  170. */
  171. public function getMyGiftWall() {
  172. $user_id = input("user_id", 0);
  173. $userid = $user_id ? $user_id : $this->auth->id;
  174. $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")
  175. ->where(["user_to_id" => $userid,])
  176. ->group("gift_id")
  177. ->order('g.value desc')
  178. ->select();
  179. $this->success("获取成功!", $list);
  180. }
  181. /**
  182. * 获取我的礼物墙
  183. */
  184. public function getMyGiftWall_typing() {
  185. $user_id = input("user_id", 0);
  186. $userid = $user_id ? $user_id : $this->auth->id;
  187. $list = Db::name('gift_user_typing')->alias('log')
  188. ->join('gift', 'gift.id = log.gift_id', 'LEFT')->field('log.*,sum(number) as number,gift.name,gift.image,gift.special')
  189. ->where(['log.user_to_id' => $userid])
  190. ->group('log.gift_id')
  191. ->order('gift.price desc')
  192. ->select();
  193. $list = list_domain_image($list,['image','special']);
  194. $this->success("获取成功!", $list);
  195. }
  196. /**
  197. * 获取我的收送礼明细
  198. */
  199. public function my_gift_log(){
  200. $user_id = $this->auth->id;
  201. $type = input('type',1);
  202. $where = [];
  203. if($type == 1){
  204. $where['user_id'] = $user_id;//我送出
  205. $joinstr = 'gup.user_to_id = user.id';
  206. }else{
  207. $where['user_to_id'] = $user_id;//我收到
  208. $joinstr = 'gup.user_id = user.id';
  209. }
  210. $list = Db::name('gift_user_party')->alias('gup')
  211. ->join('user',$joinstr,'LEFT')->field('gup.*,user.nickname')
  212. ->where($where)->order('id desc')->autopage()->select();
  213. $list = list_domain_image($list,['gift_gif_image']);
  214. $rs = [];
  215. if(empty($list)){
  216. $this->success(1,$rs);
  217. }
  218. foreach($list as $key => $val){
  219. if($type == 1){
  220. $remark = '赠送'.$val['nickname'].','.$val['gift_name'].'*'.$val['number'].',价值'.$val['value'].'钻石';
  221. }else{
  222. $remark = $val['nickname'].'赠送,'.$val['gift_name'].'*'.$val['number'].',价值'.$val['value'].'钻石';
  223. }
  224. $rs[] = [
  225. 'id' => $val['id'],
  226. 'gift_image' => $val['gift_gif_image'],
  227. 'createtime' => $val['createtime'],
  228. 'remark' => $remark
  229. ];
  230. }
  231. $this->success(1,$rs);
  232. }
  233. //聊天送礼物
  234. public function givegift_typing() {
  235. // 接口防并发
  236. if (!$this->apiLimit(1, 1000)) {
  237. $this->error(__('Operation frequently'));
  238. }
  239. $user_id = input('user_id');// 赠送对象
  240. $gift_id = input('gift_id');// 礼物ID
  241. $number = input('number',1,'intval');//数量
  242. $is_back = input("is_back",0);// 是否背包赠送: 1=是,0=否
  243. if (!$user_id || !$gift_id || $number < 1 || !in_array($is_back,[0,1]) )
  244. {
  245. $this->error();
  246. }
  247. // 不可以赠送给自己
  248. if($this->auth->id == $user_id)
  249. {
  250. $this->error("不可以赠送给自己");
  251. }
  252. //被赠送人信息
  253. $touserinfo = Db::name('user')->where('id',$user_id)->find();
  254. if (!$touserinfo)
  255. {
  256. $this->error("不存在的用户");
  257. }
  258. $backGiftId = 0; //背包礼物表的 gift_id
  259. if($is_back == 1) {
  260. // 获取背包礼物信息
  261. $giftInfo = Db::name('gift_back')->field('gift_id')->where('id',$gift_id)->find();
  262. if(!$giftInfo){
  263. $this->error("背包礼物获取失败");
  264. }
  265. $backGiftId = $giftInfo['gift_id'];
  266. // 随机获取一个礼物
  267. $allCount = $number;
  268. $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();
  269. $giftinfo = isset($giftbackList[0]) ? $giftbackList[0] : [];
  270. $giftcount = 0;
  271. $giftList = [];
  272. if(!empty($giftbackList)) {
  273. foreach($giftbackList as $k => $v) {
  274. $giftList[$k] = $v;
  275. $giftcount += $v["number"];
  276. if($giftcount >= $allCount) {
  277. break;
  278. }
  279. }
  280. }
  281. if($giftcount < $allCount)
  282. {
  283. $this->error("背包数量不足");
  284. }
  285. $giftvalue = $giftinfo["value"] * $number;
  286. $getValue = $giftvalue;
  287. }else{
  288. // 获取礼物信息
  289. $giftinfo = Db::name('gift')->where('id',$gift_id)->find();
  290. if (!$giftinfo)
  291. {
  292. $this->error("请选择礼物");
  293. }
  294. $giftvalue = $giftinfo["value"] * $number;
  295. $getValue = $giftvalue;
  296. // 判断当前用户余额
  297. $user_jewel = model('wallet')->getWallet($this->auth->id,'jewel');
  298. if($user_jewel < $giftvalue)
  299. {
  300. $this->error("您的金币余额不足");
  301. }
  302. }
  303. Db::startTrans();
  304. if($is_back == 1) {
  305. $b=0;
  306. foreach($giftList as $k => $v) {
  307. for($a=1;$a<=$v["number"];$a++) {
  308. $b++;
  309. $num = $v["number"] - $a;
  310. if($num > 0) {
  311. $res1 = Db::name('gift_back')->where(["id"=>$v["id"]])->setDec("number");
  312. } else {
  313. // $res1 = \app\common\model\GiftBack::update(["is_use"=>1,"use_time"=>time()],["id"=>$v["id"]]);
  314. $res1 = Db::name('gift_back')->where(["id"=>$v["id"]])->delete();
  315. }
  316. if($b == $number) break;
  317. }
  318. }
  319. $res2 = true;
  320. }else{
  321. if($giftvalue > 0){
  322. // 扣除当前用户余额
  323. $wallet_rs = model('wallet')->lockChangeAccountRemain($this->auth->id,$giftvalue,'-',0,"赠送礼物:'" . $giftinfo["name"] . "',扣除" . $giftvalue . "金币!",3,'jewel');
  324. if($wallet_rs['status'] === false){
  325. Db::rollback();
  326. $this->error($wallet_rs['msg']);
  327. }
  328. // 添加赠送用户余额,放到计划任务里了
  329. /*$money_to_jewel = config('site.money_to_jewel');
  330. $gift_plat_scale = config('site.gift_plat_scale');
  331. $giftmoney = bcdiv($giftvalue,$money_to_jewel,2);
  332. $money = bcdiv(bcmul($giftmoney,100 - $gift_plat_scale,2),100,2);
  333. $wallet_rs = model('wallet')->lockChangeAccountRemain($user_id,$money,'+',0,'获得礼物:'.$giftinfo["name"],101,'money');
  334. if($wallet_rs['status'] === false){
  335. Db::rollback();
  336. $this->error($wallet_rs['msg']);
  337. }*/
  338. }
  339. }
  340. // 添加礼物赠送记录表
  341. $data = [
  342. 'user_id' => $this->auth->id,
  343. 'user_to_id' => $user_id,
  344. 'gift_id' => $backGiftId > 0 ? $backGiftId : $gift_id,
  345. 'gift_give_type' => $is_back ? 1 : 2,
  346. 'gift_name' => $giftinfo['name'],
  347. 'gift_gif_image' => $giftinfo['image'],
  348. 'number' => $number,
  349. 'price' => $giftinfo['value'],
  350. 'value' => $giftvalue,
  351. 'task_status' => 0,
  352. 'createtime' => time(),
  353. ];
  354. //每个礼物都要计算平台抽成和房主抽成
  355. $gift_plat_scale = config('site.gift_plat_scale');
  356. $data['platvalue'] = bcmul($gift_plat_scale/100 ,$data["value"],2);//平台抽成
  357. $data['getvalue'] = bcsub($data["value"] ,$data['platvalue'],2);//减去抽成剩余价值
  358. $log_id = Db::name('gift_user_party')->insertGetId($data);
  359. if(!$log_id){
  360. Db::rollback();
  361. $this->error('赠送失败');
  362. }
  363. Db::commit();
  364. $this->success('赠送成功');
  365. }
  366. }