Gift.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 getGiftList() {
  24. // 获取基本信息
  25. $where = ['is_show'=>1];
  26. $giftList = Db::name('gift')->where($where)->order("weigh","desc")->select();
  27. $giftList = list_domain_image($giftList,['image','special']);
  28. $this->success("获取成功!",$giftList);
  29. }
  30. /**
  31. * 获取我的礼物墙
  32. */
  33. public function getMyGiftWall() {
  34. $user_id = $this->request->request("user_id", 0);
  35. $userid = $user_id ? $user_id : $this->auth->id;
  36. $list = Db::name('gift_user_typing')->alias('log')
  37. ->join('gift', 'gift.id = log.gift_id', 'LEFT')->field('log.*,sum(number) as number,gift.name,gift.image,gift.special')
  38. ->where(['log.user_to_id' => $userid])
  39. ->group('log.gift_id')
  40. ->order('gift.price desc')
  41. ->select();
  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['value'],$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' => $giftvalue,
  90. 'createtime' => time(),
  91. ];
  92. $log_id = Db::name('gift_user_typing')->insertGetId($data);
  93. if(!$log_id){
  94. Db::rollback();
  95. $this->error('赠送失败');
  96. }
  97. if($giftvalue > 0){
  98. // 扣除当前用户余额
  99. $wallet_rs = model('wallet')->lockChangeAccountRemain($this->auth->id,'gold',-$giftvalue,53,'赠送礼物:'.$giftinfo["name"],'gift_user_typing',$log_id);
  100. if($wallet_rs['status'] === false){
  101. Db::rollback();
  102. $this->error($wallet_rs['msg']);
  103. }
  104. // 添加赠送用户余额
  105. $money_to_gold = config('site.money_to_gold');
  106. $gift_plat_scale = config('site.gift_plat_scale');
  107. $giftmoney = bcdiv($giftvalue,$money_to_gold,2);
  108. $money = bcdiv(bcmul($giftmoney,100 - $gift_plat_scale,2),100,2);
  109. $wallet_rs = model('wallet')->lockChangeAccountRemain($user_id,'money',$money,54,'获得礼物:'.$giftinfo["name"],'gift_user_typing',$log_id);
  110. if($wallet_rs['status'] === false){
  111. Db::rollback();
  112. $this->error($wallet_rs['msg']);
  113. }
  114. }
  115. //tag任务赠送金币
  116. //搭讪奖励
  117. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,15);
  118. if($task_rs === false){
  119. Db::rollback();
  120. $this->error('完成任务赠送奖励失败');
  121. }
  122. Db::commit();
  123. $this->success('赠送成功');
  124. }
  125. }