Gift.php 7.1 KB

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