Gift.php 7.3 KB

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