Gift.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. if(!$this->is_vip($this->auth->id)){
  23. $where['is_vip'] = 0;
  24. }
  25. $giftList = Db::name('gift')->where($where)->order("weigh","desc")->select();
  26. $giftList = list_domain_image($giftList,['image','special']);
  27. $this->success("获取成功!",$giftList);
  28. }
  29. /**
  30. * 获取我的礼物墙
  31. */
  32. public function getMyGiftWall() {
  33. $user_id = $this->request->request("user_id", 0);
  34. $userid = $user_id ? $user_id : $this->auth->id;
  35. $list = Db::name('gift_user_typing')->alias('log')
  36. ->join('gift', 'gift.id = log.gift_id', 'LEFT')->field('log.*,sum(number) as number,gift.name,gift.image,gift.special')
  37. ->where(['log.user_to_id' => $userid])
  38. ->group('log.gift_id')
  39. ->order('gift.price desc')
  40. ->select();
  41. $list = list_domain_image($list,['image','special']);
  42. $this->success("获取成功!", $list);
  43. }
  44. //聊天送礼物
  45. public function givegift_typing() {
  46. // 接口防并发
  47. if (!$this->apiLimit(1, 1000)) {
  48. $this->error(__('Operation frequently'));
  49. }
  50. $user_id = input('user_id');// 赠送对象
  51. $gift_id = input('gift_id');// 礼物ID
  52. $number = input('number',1,'intval');//数量
  53. if (!$user_id || !$gift_id || $number < 1)
  54. {
  55. $this->error();
  56. }
  57. // 不可以赠送给自己
  58. if($this->auth->id == $user_id)
  59. {
  60. $this->error("不可以赠送给自己");
  61. }
  62. // 获取礼物信息
  63. $giftinfo = Db::name('gift')->where('id',$gift_id)->find();
  64. if (!$giftinfo)
  65. {
  66. $this->error("请选择礼物");
  67. }
  68. $giftvalue = bcmul($giftinfo['price'],$number);
  69. //被赠送人信息
  70. $touserinfo = Db::name('user')->where('id',$user_id)->find();
  71. if (!$touserinfo)
  72. {
  73. $this->error("不存在的用户");
  74. }
  75. // 判断当前用户余额
  76. $user_gold = model('wallet')->getWallet($this->auth->id,'gold');
  77. if($user_gold < $giftvalue)
  78. {
  79. $this->error("您的金币余额不足");
  80. }
  81. Db::startTrans();
  82. // 添加礼物赠送记录表
  83. $data = [
  84. 'user_id' => $this->auth->id,
  85. 'user_to_id' => $user_id,
  86. 'gift_id' => $giftinfo['id'],
  87. 'gift_name' => $giftinfo['name'],
  88. 'number' => $number,
  89. 'price' => $giftinfo['price'],
  90. 'total_price' => $giftvalue,
  91. 'createtime' => time(),
  92. ];
  93. $log_id = Db::name('gift_user_typing')->insertGetId($data);
  94. if(!$log_id){
  95. Db::rollback();
  96. $this->error('赠送失败');
  97. }
  98. if($giftvalue > 0){
  99. // 扣除当前用户余额
  100. $wallet_rs = model('wallet')->lockChangeAccountRemain($this->auth->id,'gold',-$giftvalue,53,'赠送礼物:'.$giftinfo["name"],'gift_user_typing',$log_id);
  101. if($wallet_rs['status'] === false){
  102. Db::rollback();
  103. $this->error($wallet_rs['msg']);
  104. }
  105. // 添加赠送用户余额
  106. $money_to_gold = config('site.money_to_gold');
  107. $gift_plat_scale = config('site.gift_plat_scale');
  108. $giftmoney = bcdiv($giftvalue,$money_to_gold,2);
  109. $money = bcdiv(bcmul($giftmoney,100 - $gift_plat_scale,2),100,2);
  110. $wallet_rs = model('wallet')->lockChangeAccountRemain($user_id,'money',$money,54,'获得礼物:'.$giftinfo["name"],'gift_user_typing',$log_id);
  111. if($wallet_rs['status'] === false){
  112. Db::rollback();
  113. $this->error($wallet_rs['msg']);
  114. }
  115. }
  116. Db::commit();
  117. $this->success('赠送成功');
  118. }
  119. }