Gift.php 4.3 KB

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