Gift.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 $giftModel;
  13. public $gifttypeModel;
  14. public function _initialize()
  15. {
  16. parent::_initialize();
  17. $this->giftModel = new \app\common\model\Gift();
  18. $this->gifttypeModel = new \app\common\model\GiftType();
  19. }
  20. /**
  21. * 获取礼物类型
  22. */
  23. public function getgifttype() {
  24. // 获取基本信息
  25. $where = [];
  26. $where["is_show"] = 1;
  27. $giftList = $this->gifttypeModel->field("id,name")->where($where)->order("weight","desc")->select();
  28. $this->success("获取成功!",$giftList);
  29. }
  30. //聊天送礼物
  31. public function givegift_typing() {
  32. // 接口防并发
  33. if (!$this->apiLimit(1, 1)) {
  34. $this->error(__('Operation frequently'));
  35. }
  36. $user_id = input('user_id');// 赠送对象
  37. $gift_id = input('gift_id');// 礼物ID
  38. $number = input('number',1,'intval');//数量
  39. if (!$user_id || !$gift_id || $number < 1)
  40. {
  41. $this->error();
  42. }
  43. // 不可以赠送给自己
  44. if($this->auth->id == $user_id)
  45. {
  46. $this->error("不可以赠送给自己");
  47. }
  48. // 获取礼物信息
  49. $giftinfo = Db::name('gift')->where('id',$gift_id)->find();
  50. if (!$giftinfo)
  51. {
  52. $this->error("请选择礼物");
  53. }
  54. $giftvalue = bcmul($giftinfo['value'],$number,2);
  55. //被赠送人信息
  56. $touserinfo = Db::name('user')->where('id',$user_id)->find();
  57. if (!$touserinfo)
  58. {
  59. $this->error("不存在的用户");
  60. }
  61. if($touserinfo['is_kefu'] == 1){
  62. $this->error('不可以给客服送礼物');
  63. }
  64. $money = 0.00;
  65. Db::startTrans();
  66. //需要走计费的规则:男的 || 女的 需要收费的
  67. if ($this->auth->gender == 1 || ($this->auth->gender == 0 && $this->auth->is_cost == 1)) {
  68. // 判断当前用户余额
  69. if($giftinfo['wallettype'] == 1){
  70. $user_gold = model('wallet')->getWallet($this->auth->id,'gold');
  71. if($user_gold < $giftvalue)
  72. {
  73. $this->error("您的金币不足");
  74. }
  75. $log_table = 'gift_user_typing';
  76. }else{
  77. $user_jewel = model('wallet')->getWallet($this->auth->id,'jewel');
  78. if($user_jewel < $giftvalue)
  79. {
  80. $this->error("您的钻石不足");
  81. }
  82. $log_table = 'gift_user_typing_jewel';
  83. }
  84. // 添加礼物赠送记录表
  85. $data = [
  86. 'user_id' => $this->auth->id,
  87. 'user_to_id' => $user_id,
  88. 'gift_id' => $giftinfo['id'],
  89. 'gift_name' => $giftinfo['name'],
  90. 'number' => $number,
  91. 'price' => $giftvalue,
  92. 'createtime' => time(),
  93. ];
  94. $log_id = Db::name($log_table)->insertGetId($data);
  95. if(!$log_id){
  96. Db::rollback();
  97. $this->error('赠送失败');
  98. }
  99. if($giftvalue > 0){
  100. // 扣除当前用户余额
  101. if($giftinfo['wallettype'] == 1){
  102. $wallet_rs = model('wallet')->lockChangeAccountRemain($this->auth->id,$user_id,'gold',-$giftvalue,53,'赠送礼物:'.$giftinfo["name"] . '*' . $number,'gift_user_typing',$log_id);
  103. if($wallet_rs['status'] === false){
  104. Db::rollback();
  105. $this->error($wallet_rs['msg']);
  106. }
  107. // 添加赠送用户余额
  108. $money_to_gold = config('site.money_to_gold');//送金币礼物得积分
  109. $gift_plat_scale = config('site.gift_plat_scale');
  110. $giftmoney = bcdiv($giftvalue,$money_to_gold,2);
  111. $money = bcdiv(bcmul($giftmoney,100 - $gift_plat_scale,2),100,2);
  112. $wallet_rs = model('wallet')->lockChangeAccountRemain($user_id,$this->auth->id,'money',$money,54,'获得礼物:'.$giftinfo["name"] . '*' . $number,'gift_user_typing',$log_id,2);
  113. if($wallet_rs['status'] === false){
  114. Db::rollback();
  115. $this->error($wallet_rs['msg']);
  116. }
  117. //增加赠送用户上级余额
  118. if ($touserinfo['intro_uid']) {
  119. //获取返利比率
  120. $is_agent = Db::name('user')->where(['id' => $touserinfo['intro_uid']])->value('is_agent');
  121. $intro_income_rebate_rate = $is_agent ? (int)config('site.h_intro_income_rebate_rate') : (int)config('site.intro_income_rebate_rate'); //邀请人收礼物返利比率
  122. if ($intro_income_rebate_rate > 0 && $intro_income_rebate_rate <= 100) {
  123. //上级获得金额
  124. $intro_uid_money = number_format($money * $intro_income_rebate_rate / 100, 2, '.', '');
  125. if ($intro_uid_money > 0) {
  126. $intro_result = model('Wallet')->lockChangeAccountRemain($touserinfo['intro_uid'],$user_id,'money',$intro_uid_money,68, '邀请人聊天礼物获赠奖励','gift_user_typing',$log_id);
  127. if($intro_result['status']===false)
  128. {
  129. Db::rollback();
  130. $this->error($intro_result['msg']);
  131. }
  132. }
  133. }
  134. }
  135. if ($this->auth->gender == 1 && $touserinfo['gender'] == 0) {
  136. //增加亲密度
  137. /*$user_intimacy_rs = addintimacy($this->auth->id, $user_id, $giftvalue);
  138. if (!$user_intimacy_rs['status']) {
  139. Db::rollback();
  140. $this->error('您的网络开小差啦~');
  141. }*/
  142. }
  143. }else{
  144. $wallet_rs = model('wallet')->lockChangeAccountRemain($this->auth->id,$user_id,'jewel',-$giftvalue,33,'赠送礼物:'.$giftinfo["name"] . '*' . $number,'gift_user_typing_jewel',$log_id);
  145. if($wallet_rs['status'] === false){
  146. Db::rollback();
  147. $this->error($wallet_rs['msg']);
  148. }
  149. }
  150. }
  151. }
  152. Db::commit();
  153. //发送消息
  154. /*if (isset($user_intimacy_rs) && $user_intimacy_rs['level_remark']) {
  155. $tenim = new \app\api\controller\Tenim;
  156. $tenim->sendMessageToUser($this->auth->id, $user_id, $user_intimacy_rs['level_remark'], 1);
  157. }*/
  158. $return_data['money'] = $money; //获得金额
  159. // $return_data['level_remark'] = isset($user_intimacy_rs) ? $user_intimacy_rs['level_remark'] : ''; //亲密度等级提示语
  160. $return_data['level_remark'] = ''; //亲密度等级提示语
  161. $this->success('赠送成功', $return_data);
  162. }
  163. //礼物列表
  164. public function giftlist() {
  165. $type = input('type',0);
  166. $where['status'] = 1;
  167. if($type){
  168. $where['type'] = $type;
  169. }
  170. $giftList = Db::name('gift')->field('id, name, value, image, special, wallettype')->where($where)->order("sort, value")->select();
  171. $giftList = list_domain_image($giftList,['image','special']);
  172. $this->success("获取成功!",$giftList);
  173. }
  174. }