Gift.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 = [];
  11. protected $noNeedRight = '*';
  12. //聊天送礼物
  13. public function givegift_typing() {
  14. // 接口防并发
  15. if (!$this->apiLimit(1, 1)) {
  16. $this->error(__('Operation frequently'));
  17. }
  18. $user_id = input('user_id');// 赠送对象
  19. $gift_id = input('gift_id');// 礼物ID
  20. $number = input('number',1,'intval');//数量
  21. if (!$user_id || !$gift_id || $number < 1)
  22. {
  23. $this->error();
  24. }
  25. // 不可以赠送给自己
  26. if($this->auth->id == $user_id)
  27. {
  28. $this->error("不可以赠送给自己");
  29. }
  30. // 获取礼物信息
  31. $giftinfo = Db::name('gift')->where('id',$gift_id)->find();
  32. if (!$giftinfo)
  33. {
  34. $this->error("请选择礼物");
  35. }
  36. $giftvalue = bcmul($giftinfo['price'],$number);
  37. //被赠送人信息
  38. $touserinfo = Db::name('user')->where('id',$user_id)->find();
  39. if (!$touserinfo)
  40. {
  41. $this->error("不存在的用户");
  42. }
  43. if($touserinfo['is_kefu'] == 1){
  44. $this->error('不可以给客服送礼物');
  45. }
  46. Db::startTrans();
  47. // 判断当前用户余额
  48. $user_gold = model('wallet')->getWallet($this->auth->id,'gold');
  49. if($user_gold < $giftvalue)
  50. {
  51. Db::rollback();
  52. $this->error("您的金币不足");
  53. }
  54. // 添加礼物赠送记录表
  55. $data = [
  56. 'user_id' => $this->auth->id,
  57. 'user_to_id' => $user_id,
  58. 'gift_id' => $giftinfo['id'],
  59. 'gift_name' => $giftinfo['name'],
  60. 'number' => $number,
  61. 'price' => $giftinfo['price'],
  62. 'total_price' => $giftvalue,
  63. 'createtime' => time(),
  64. ];
  65. //每个礼物都要计算平台抽成和房主抽成
  66. $gift_plat_scale = config('site.gift_plat_scale');
  67. $data['platvalue'] = bcmul($gift_plat_scale/100 ,$data['total_price'],1);//平台抽成
  68. $data['getvalue'] = bcsub($data['total_price'] ,$data['platvalue'],1);//减去抽成剩余价值
  69. $money_to_gold = config('site.money_to_gold');
  70. $data['getmoney'] = bcdiv($data['getvalue'] ,$money_to_gold,2);//收益
  71. $log_id = Db::name('gift_user_typing')->insertGetId($data);
  72. if(!$log_id){
  73. Db::rollback();
  74. $this->error('赠送失败');
  75. }
  76. if($giftvalue > 0){
  77. // 扣除当前用户金币
  78. $wallet_rs = model('wallet')->lockChangeAccountRemain($this->auth->id,$user_id,'gold',-$giftvalue,53,'赠送礼物:'.$giftinfo["name"] . '*' . $number,'gift_user_typing',$log_id);
  79. if($wallet_rs['status'] === false){
  80. Db::rollback();
  81. $this->error($wallet_rs['msg']);
  82. }
  83. }
  84. if($data['getmoney'] > 0){
  85. // 添加获赠用户收益
  86. $wallet_rs = model('wallet')->lockChangeAccountRemain($user_id,$this->auth->id,'money',$data['getmoney'],54,'获得礼物:'.$giftinfo["name"] . '*' . $number,'gift_user_typing',$log_id,2);
  87. if($wallet_rs['status'] === false){
  88. Db::rollback();
  89. $this->error($wallet_rs['msg']);
  90. }
  91. }
  92. //增加赠送用户上级余额
  93. if ($touserinfo['intro_uid']) {
  94. //获取返利比率
  95. $agent_info = Db::name('user')->where(['id' => $touserinfo['intro_uid']])->field('is_agent,h_intro_income_rebate_rate')->find();
  96. $intro_income_rebate_rate = ($agent_info['is_agent'] == 1) ? $agent_info['h_intro_income_rebate_rate'] : (int)config('site.intro_income_rebate_rate'); //邀请人收礼物返利比率
  97. if ($intro_income_rebate_rate > 0 && $intro_income_rebate_rate <= 100) {
  98. //上级获得金额
  99. $intro_uid_money = bcdiv(bcmul($data['getmoney'],$intro_income_rebate_rate,2),100,2);
  100. if ($intro_uid_money > 0) {
  101. $intro_result = model('Wallet')->lockChangeAccountRemain($touserinfo['intro_uid'],$user_id,'money',$intro_uid_money,68, '邀请人聊天礼物获赠奖励','gift_user_typing',$log_id);
  102. if($intro_result['status']===false)
  103. {
  104. Db::rollback();
  105. $this->error($intro_result['msg']);
  106. }
  107. }
  108. }
  109. }
  110. if ($this->auth->gender == 1 && $touserinfo['gender'] == 0) {
  111. //增加亲密度
  112. $user_intimacy_rs = addintimacy($this->auth->id, $user_id, $giftvalue);
  113. if (!$user_intimacy_rs['status']) {
  114. Db::rollback();
  115. $this->error('您的网络开小差啦~');
  116. }
  117. }
  118. Db::commit();
  119. //发送消息
  120. if (isset($user_intimacy_rs) && $user_intimacy_rs['level_remark']) {
  121. $tenim = new \app\api\controller\Tenim;
  122. $tenim->sendMessageToUser($this->auth->id, $user_id, $user_intimacy_rs['level_remark'], 1);
  123. }
  124. $return_data['money'] = $data['getmoney']; //获得金额
  125. $return_data['level_remark'] = isset($user_intimacy_rs) ? $user_intimacy_rs['level_remark'] : ''; //亲密度等级提示语
  126. $this->success('赠送成功', $return_data);
  127. }
  128. //礼物列表
  129. public function giftlist() {
  130. $where = [
  131. 'is_show' => 1,
  132. ];
  133. $giftList = Db::name('gift')->where($where)->order('weigh','desc')->select();
  134. $giftList = list_domain_image($giftList,['image','special']);
  135. $this->success("获取成功!",$giftList);
  136. }
  137. }