Gift.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. $type = $this->request->request("type");
  25. // 获取基本信息
  26. $where = [];
  27. $type != '' && $where["type"] = $type;
  28. $giftList = Db::name('gift')->where($where)->order("value","asc")->select();
  29. $giftList = list_domain_image($giftList,['image','special']);
  30. $this->success("获取成功!",$giftList);
  31. }
  32. /**
  33. * 获取礼物类型
  34. */
  35. public function getGiftType() {
  36. // 获取基本信息
  37. $where = [];
  38. $where["is_show"] = 1;
  39. $giftList = $this->gifttypeModel->field("id,name")->where($where)->order("weight","desc")->select();
  40. $this->success("获取成功!",$giftList);
  41. }
  42. /**
  43. * 获取我的背包礼物
  44. */
  45. public function getMyBackGift() {
  46. $userid = $this->auth->id;
  47. $page = $this->request->request('page',1); // 分页
  48. $pageNum = $this->request->request('pageNum',10); // 分页
  49. // 分页搜索构建
  50. $pageStart = ($page-1)*$pageNum;
  51. $list = \app\common\model\GiftBack::field("id,name,image,gif_image,value,sum(number) as number")
  52. ->where(["user_id"=>$userid,"is_use"=>0])
  53. ->limit($pageStart,$pageNum)
  54. ->order('value', 'asc')
  55. ->group("name")
  56. ->select();
  57. $this->success("获取成功!",$list);
  58. }
  59. /**
  60. * 获取我的礼物墙
  61. */
  62. public function getMyGiftWall() {
  63. $user_id = $this->request->request("user_id", 0);
  64. $userid = $user_id ? $user_id : $this->auth->id;
  65. $list = \app\common\model\GiftUserParty::alias('a')->join("hx_gift g", "g.id = a.gift_id", "inner")->field("gift_id,g.name,g.image,sum(number) as number")
  66. ->where(["user_to_id" => $userid, 'g.type' => ['<>', 6]])
  67. ->group("gift_id")
  68. ->order('g.value desc')
  69. ->select();
  70. $this->success("获取成功!", $list);
  71. }
  72. //聊天送礼物
  73. public function givegift_typing() {
  74. // 接口防并发
  75. if (!$this->apiLimit(1, 1000)) {
  76. $this->error(__('Operation frequently'));
  77. }
  78. $user_id = input('user_id');// 赠送对象
  79. $gift_id = input('gift_id');// 礼物ID
  80. $number = input('number',1,'intval');//数量
  81. if (!$user_id || !$gift_id || $number < 1)
  82. {
  83. $this->error();
  84. }
  85. // 不可以赠送给自己
  86. if($this->auth->id == $user_id)
  87. {
  88. $this->error("不可以赠送给自己");
  89. }
  90. // 获取礼物信息
  91. $giftinfo = Db::name('gift')->where('id',$gift_id)->find();
  92. if (!$giftinfo)
  93. {
  94. $this->error("请选择礼物");
  95. }
  96. $giftvalue = bcmul($giftinfo['value'],$number);
  97. //被赠送人信息
  98. $touserinfo = Db::name('user')->where('id',$user_id)->find();
  99. if (!$touserinfo)
  100. {
  101. $this->error("不存在的用户");
  102. }
  103. // 判断当前用户余额
  104. $user_gold = model('wallet')->getWallet($this->auth->id,'gold');
  105. if($user_gold < $giftvalue)
  106. {
  107. $this->error("您的金币余额不足");
  108. }
  109. Db::startTrans();
  110. // 添加礼物赠送记录表
  111. $data = [
  112. 'user_id' => $this->auth->id,
  113. 'user_to_id' => $user_id,
  114. 'gift_id' => $giftinfo['id'],
  115. 'gift_name' => $giftinfo['name'],
  116. 'number' => $number,
  117. 'price' => $giftvalue,
  118. 'createtime' => time(),
  119. ];
  120. $log_id = Db::name('gift_user_typing')->insertGetId($data);
  121. if(!$log_id){
  122. Db::rollback();
  123. $this->error('赠送失败');
  124. }
  125. if($giftvalue > 0){
  126. // 扣除当前用户余额
  127. $wallet_rs = model('wallet')->lockChangeAccountRemain($this->auth->id,'gold',-$giftvalue,53,'赠送礼物:'.$giftinfo["name"],'gift_user_typing',$log_id);
  128. if($wallet_rs['status'] === false){
  129. Db::rollback();
  130. $this->error($wallet_rs['msg']);
  131. }
  132. // 添加赠送用户余额
  133. $money_to_gold = config('site.money_to_gold');
  134. $gift_plat_scale = config('site.gift_plat_scale');
  135. $giftmoney = bcdiv($giftvalue,$money_to_gold,2);
  136. $money = bcdiv(bcmul($giftmoney,100 - $gift_plat_scale,2),100,2);
  137. $wallet_rs = model('wallet')->lockChangeAccountRemain($user_id,'money',$money,54,'获得礼物:'.$giftinfo["name"],'gift_user_typing',$log_id);
  138. if($wallet_rs['status'] === false){
  139. Db::rollback();
  140. $this->error($wallet_rs['msg']);
  141. }
  142. }
  143. //tag任务赠送金币
  144. //搭讪奖励
  145. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,15);
  146. if($task_rs === false){
  147. Db::rollback();
  148. $this->error('完成任务赠送奖励失败');
  149. }
  150. Db::commit();
  151. $this->success('赠送成功');
  152. }
  153. }