Gift.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 function _initialize()
  13. {
  14. parent::_initialize();
  15. $this->giftModel = new \app\common\model\Gift();
  16. $this->gifttypeModel = new \app\common\model\GiftType();
  17. }
  18. /**
  19. * 获取礼物列表
  20. */
  21. public function getGiftList() {
  22. // 获取基本信息
  23. $where = ['is_show'=>1];
  24. if(!$this->is_vip($this->auth->id)){
  25. $where['is_vip'] = 0;
  26. }
  27. $giftList = Db::name('gift')->where($where)->order("weigh","desc")->select();
  28. $giftList = list_domain_image($giftList,['image','special']);
  29. $this->success("获取成功!",$giftList);
  30. }
  31. /**
  32. * 获取礼物类型
  33. */
  34. public function getGiftType() {
  35. // 获取基本信息
  36. $where = [];
  37. $where["is_show"] = 1;
  38. $giftList = $this->gifttypeModel->field("id,name")->where($where)->order("weight","desc")->select();
  39. $this->success("获取成功!",$giftList);
  40. }
  41. /**
  42. * 获取我的背包礼物
  43. */
  44. public function getMyBackGift() {
  45. $userid = $this->request->request("user_id", $this->auth->id);
  46. //$page = $this->request->request('page',1); // 分页
  47. //$pageNum = $this->request->request('pageNum',10); // 分页
  48. // 分页搜索构建
  49. //$pageStart = ($page-1)*$pageNum;
  50. $list = \app\common\model\GiftBack::field("id,name,image,gif_image,value,sum(number) as number")
  51. ->where(["user_id"=>$userid,"is_use"=>0])
  52. // ->limit($pageStart,$pageNum)
  53. ->group("name")
  54. ->select();
  55. $this->success("获取成功!",$list);
  56. }
  57. /**
  58. * 获取我的礼物墙
  59. */
  60. public function getMyGiftWall() {
  61. $user_id = input("user_id", 0);
  62. $userid = $user_id ? $user_id : $this->auth->id;
  63. $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")
  64. ->where(["user_to_id" => $userid,])
  65. ->group("gift_id")
  66. ->order('g.value desc')
  67. ->select();
  68. $this->success("获取成功!", $list);
  69. }
  70. /**
  71. * 获取我的礼物墙
  72. */
  73. public function getMyGiftWall_typing() {
  74. $user_id = $this->request->request("user_id", 0);
  75. $userid = $user_id ? $user_id : $this->auth->id;
  76. $list = Db::name('gift_user_typing')->alias('log')
  77. ->join('gift', 'gift.id = log.gift_id', 'LEFT')->field('log.*,sum(number) as number,gift.name,gift.image,gift.special')
  78. ->where(['log.user_to_id' => $userid])
  79. ->group('log.gift_id')
  80. ->order('gift.price desc')
  81. ->select();
  82. $list = list_domain_image($list,['image','special']);
  83. $this->success("获取成功!", $list);
  84. }
  85. /**
  86. * 获取我的收送礼明细
  87. */
  88. public function my_gift_log(){
  89. $user_id = $this->auth->id;
  90. $type = input('type',1);
  91. $where = [];
  92. if($type == 1){
  93. $where['user_id'] = $user_id;//我送出
  94. $joinstr = 'gup.user_to_id = user.id';
  95. }else{
  96. $where['user_to_id'] = $user_id;//我收到
  97. $joinstr = 'gup.user_id = user.id';
  98. }
  99. $list = Db::name('gift_user_party')->alias('gup')
  100. ->join('user',$joinstr,'LEFT')->field('gup.*,user.nickname')
  101. ->where($where)->order('id desc')->autopage()->select();
  102. $list = list_domain_image($list,['gift_gif_image']);
  103. $rs = [];
  104. if(empty($list)){
  105. $this->success(1,$rs);
  106. }
  107. foreach($list as $key => $val){
  108. if($type == 1){
  109. $remark = '赠送'.$val['nickname'].','.$val['gift_name'].'*'.$val['number'].',价值'.$val['value'].'钻石';
  110. }else{
  111. $remark = $val['nickname'].'赠送,'.$val['gift_name'].'*'.$val['number'].',价值'.$val['value'].'钻石';
  112. }
  113. $rs[] = [
  114. 'id' => $val['id'],
  115. 'gift_image' => $val['gift_gif_image'],
  116. 'createtime' => $val['createtime'],
  117. 'remark' => $remark
  118. ];
  119. }
  120. $this->success(1,$rs);
  121. }
  122. //聊天送礼物
  123. public function givegift_typing() {
  124. // 接口防并发
  125. if (!$this->apiLimit(1, 1000)) {
  126. $this->error(__('Operation frequently'));
  127. }
  128. $user_id = input('user_id');// 赠送对象
  129. $gift_id = input('gift_id');// 礼物ID
  130. $number = input('number',1,'intval');//数量
  131. if (!$user_id || !$gift_id || $number < 1)
  132. {
  133. $this->error();
  134. }
  135. // 不可以赠送给自己
  136. if($this->auth->id == $user_id)
  137. {
  138. $this->error("不可以赠送给自己");
  139. }
  140. // 获取礼物信息
  141. $giftinfo = Db::name('gift')->where('id',$gift_id)->find();
  142. if (!$giftinfo)
  143. {
  144. $this->error("请选择礼物");
  145. }
  146. $giftvalue = bcmul($giftinfo['price'],$number);
  147. //被赠送人信息
  148. $touserinfo = Db::name('user')->where('id',$user_id)->find();
  149. if (!$touserinfo)
  150. {
  151. $this->error("不存在的用户");
  152. }
  153. // 判断当前用户余额
  154. $user_gold = model('wallet')->getWallet($this->auth->id,'gold');
  155. if($user_gold < $giftvalue)
  156. {
  157. $this->error("您的金币余额不足");
  158. }
  159. Db::startTrans();
  160. // 添加礼物赠送记录表
  161. $data = [
  162. 'user_id' => $this->auth->id,
  163. 'user_to_id' => $user_id,
  164. 'gift_id' => $giftinfo['id'],
  165. 'gift_name' => $giftinfo['name'],
  166. 'number' => $number,
  167. 'price' => $giftinfo['price'],
  168. 'total_price' => $giftvalue,
  169. 'createtime' => time(),
  170. ];
  171. $log_id = Db::name('gift_user_typing')->insertGetId($data);
  172. if(!$log_id){
  173. Db::rollback();
  174. $this->error('赠送失败');
  175. }
  176. if($giftvalue > 0){
  177. // 扣除当前用户余额
  178. $wallet_rs = model('wallet')->lockChangeAccountRemain($this->auth->id,'gold',-$giftvalue,53,'赠送礼物:'.$giftinfo["name"],'gift_user_typing',$log_id);
  179. if($wallet_rs['status'] === false){
  180. Db::rollback();
  181. $this->error($wallet_rs['msg']);
  182. }
  183. // 添加赠送用户余额
  184. $money_to_gold = config('site.money_to_gold');
  185. $gift_plat_scale = config('site.gift_plat_scale');
  186. $giftmoney = bcdiv($giftvalue,$money_to_gold,2);
  187. $money = bcdiv(bcmul($giftmoney,100 - $gift_plat_scale,2),100,2);
  188. $wallet_rs = model('wallet')->lockChangeAccountRemain($user_id,'money',$money,54,'获得礼物:'.$giftinfo["name"],'gift_user_typing',$log_id);
  189. if($wallet_rs['status'] === false){
  190. Db::rollback();
  191. $this->error($wallet_rs['msg']);
  192. }
  193. }
  194. Db::commit();
  195. $this->success('赠送成功');
  196. }
  197. }