Gift.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. public function _initialize()
  13. {
  14. parent::_initialize();
  15. }
  16. /**
  17. * 获取礼物列表
  18. */
  19. public function getGiftList() {
  20. // 获取基本信息
  21. $where = ['is_show'=>1];
  22. $giftList = Db::name('gift')->where($where)->order("weigh","desc")->select();
  23. $giftList = list_domain_image($giftList,['image','special']);
  24. $this->success("获取成功!",$giftList);
  25. }
  26. /**
  27. * 获取我的礼物墙
  28. */
  29. public function getMyGiftWall() {
  30. $user_id = input("user_id", 0);
  31. $userid = $user_id ? $user_id : $this->auth->id;
  32. $list = Db::name('gift_user_typing')->alias('log')
  33. ->join('gift', 'gift.id = log.gift_id', 'LEFT')
  34. ->field('log.id,log.gift_name,sum(log.number) as number,gift.image')
  35. ->where(['log.user_to_id' => $userid])
  36. ->group('log.gift_id')
  37. ->order('gift.price desc')
  38. ->select();
  39. $list = list_domain_image($list,['image']);
  40. $this->success("获取成功!", $list);
  41. }
  42. //聊天送礼物
  43. public function givegift_typing() {
  44. // 接口防并发
  45. if (!$this->apiLimit(1, 1000)) {
  46. $this->error('休息一下吧');
  47. }
  48. $user_id = input('user_id');// 赠送对象
  49. $gift_id = input('gift_id');// 礼物ID
  50. $number = input('number',1,'intval');//数量
  51. if (!$user_id || !$gift_id || $number < 1)
  52. {
  53. $this->error();
  54. }
  55. // 不可以赠送给自己
  56. if($this->auth->id == $user_id)
  57. {
  58. $this->error("不可以赠送给自己");
  59. }
  60. // 获取礼物信息
  61. $giftinfo = Db::name('gift')->where('id',$gift_id)->find();
  62. if (!$giftinfo)
  63. {
  64. $this->error("请选择礼物");
  65. }
  66. $giftvalue = bcmul($giftinfo['price'],$number);
  67. //被赠送人信息
  68. $touserinfo = Db::name('user')->where('id',$user_id)->find();
  69. if (!$touserinfo)
  70. {
  71. $this->error("不存在的用户");
  72. }
  73. Db::startTrans();
  74. // 判断当前用户余额
  75. $user_gold = model('wallet')->getWallettotal($this->auth->id);
  76. if($user_gold < $giftvalue)
  77. {
  78. Db::rollback();
  79. $this->error("您的金币余额不足");
  80. }
  81. // 添加礼物赠送记录表
  82. $data = [
  83. 'user_id' => $this->auth->id,
  84. 'user_to_id' => $user_id,
  85. 'gift_id' => $giftinfo['id'],
  86. 'gift_name' => $giftinfo['name'],
  87. 'number' => $number,
  88. 'price' => $giftinfo['price'],
  89. 'total_price' => $giftvalue,
  90. 'createtime' => time(),
  91. ];
  92. //每个礼物都要计算平台抽成和房主抽成
  93. $gift_plat_scale = config('site.gift_plat_scale');
  94. $data['platvalue'] = bcmul($gift_plat_scale/100 ,$data['total_price'],1);//平台抽成
  95. $data['getvalue'] = bcsub($data['total_price'] ,$data['platvalue'],1);//减去抽成剩余价值
  96. $log_id = Db::name('gift_user_typing')->insertGetId($data);
  97. if(!$log_id){
  98. Db::rollback();
  99. $this->error('赠送失败');
  100. }
  101. if($giftvalue > 0){
  102. // 扣除当前用户余额
  103. $wallet_rs = model('wallet')->lockChangeAccountRemain($this->auth->id,'gold',-$giftvalue,14,'赠送给'.$touserinfo['username'].'礼物:'.$giftinfo['name'].'X'.$number,'gift_user_typing',$log_id);
  104. if($wallet_rs['status'] === false){
  105. Db::rollback();
  106. $this->error($wallet_rs['msg']);
  107. }
  108. }
  109. if($data['getvalue'] > 0){
  110. $wallet_rs = model('wallet')->lockChangeAccountRemain($user_id,'jewel',$data['getvalue'],24,$this->auth->username.'送给我礼物:'.$giftinfo['name'].'X'.$number,'gift_user_typing',$log_id);
  111. if($wallet_rs['status'] === false){
  112. Db::rollback();
  113. $this->error($wallet_rs['msg']);
  114. }
  115. }
  116. //增加送礼用户的财富等级
  117. $res_wealth = \app\common\model\User::add_wealth_level($this->auth->id,$giftvalue);
  118. //魅力等级
  119. $res_wealth = \app\common\model\User::add_charm_level($user_id,$giftvalue);
  120. Db::commit();
  121. $this->success('赠送成功');
  122. }
  123. }