Gift.php 5.7 KB

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