Gift.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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,is_baobi')->where($where)->order("sort","asc")->select();
  29. $giftList = list_domain_image($giftList,['image','special']);
  30. //追加赠送数量
  31. $gift_num = Db::name('gift_number')->order('number asc')->select();
  32. foreach($giftList as $key => &$val){
  33. $child = [];
  34. foreach($gift_num as $k => $num){
  35. if($val['id'] == $num['gift_id']){
  36. unset($num['id']);
  37. unset($num['gift_id']);
  38. $child[] = $num;
  39. }
  40. }
  41. if(empty($child)){
  42. $child = [
  43. ['number'=>1,'name'=>'一心一意'],
  44. ['number'=>10,'name'=>'十全十美'],
  45. ['number'=>50,'name'=>'长相厮守'],
  46. ['number'=>100,'name'=>'生生世世'],
  47. ];
  48. }
  49. $val['num_child'] = $child;
  50. }
  51. $this->success("获取成功!",$giftList);
  52. }
  53. /**
  54. * 获取礼物类型
  55. */
  56. public function getGiftType() {
  57. // 获取基本信息
  58. $where = [];
  59. $where["is_show"] = 1;
  60. $giftList = $this->gifttypeModel->field("id,name")->where($where)->order("weight","desc")->select();
  61. $this->success("获取成功!",$giftList);
  62. }
  63. /**
  64. * 获取我的背包礼物
  65. */
  66. public function getMyBackGift() {
  67. $userid = input("user_id", $this->auth->id);
  68. // 分页搜索构建
  69. $list = \app\common\model\GiftBack::field("id,name,image,gif_image,value,sum(number) as number")
  70. ->where(["user_id"=>$userid,"is_use"=>0])
  71. ->group("gift_id")
  72. ->autopage()
  73. ->select();
  74. //追加赠送数量
  75. foreach($list as $key => &$val){
  76. $child = [
  77. ['number'=>1,'name'=>'一心一意'],
  78. ['number'=>10,'name'=>'十全十美'],
  79. ['number'=>50,'name'=>'长相厮守'],
  80. ['number'=>100,'name'=>'生生世世'],
  81. ];
  82. $val['num_child'] = $child;
  83. }
  84. $this->success("获取成功!",$list);
  85. }
  86. /**
  87. * 获取我的礼物墙
  88. */
  89. public function getMyGiftWall() {
  90. $user_id = input("user_id", 0);
  91. $userid = $user_id ? $user_id : $this->auth->id;
  92. $list = Db::name('gift_user_party')->alias('a')
  93. ->join("gift g", "g.id = a.gift_id", "LEFT")
  94. ->field("a.gift_id,g.name,g.image,sum(number) as number")
  95. ->where(["a.user_to_id" => $userid,])
  96. ->group("a.gift_id")
  97. ->order('g.value desc')
  98. ->select();
  99. $list = list_domain_image($list,['image']);
  100. $this->success("获取成功!", $list);
  101. }
  102. /**
  103. * 获取我的收送礼明细
  104. */
  105. public function my_gift_log(){
  106. $user_id = $this->auth->id;
  107. $type = input('type',1);
  108. //普通礼物 还是幸运礼物
  109. $where_baobi = [];
  110. $is_baobi = input('is_baobi','all');
  111. if($is_baobi == 0){
  112. $where_baobi['gup.is_baobi'] = 0;
  113. }
  114. $where = [];
  115. if($type == 1){
  116. $where['user_id'] = $user_id;//我送出
  117. $joinstr = 'gup.user_to_id = user.id';
  118. }else{
  119. $where['user_to_id'] = $user_id;//我收到
  120. $joinstr = 'gup.user_id = user.id';
  121. }
  122. $list = Db::name('gift_user_party')->alias('gup')
  123. ->join('user',$joinstr,'LEFT')->field('gup.*,user.nickname,user.avatar,user.gender')
  124. ->where($where)->where($where_baobi)->order('id desc')->autopage()->select();
  125. $list = list_domain_image($list,['gift_gif_image','avatar']);
  126. $rs = [];
  127. if(empty($list)){
  128. $this->success(1,$rs);
  129. }
  130. foreach($list as $key => $val){
  131. if($type == 1){
  132. $remark = '送给Ta '.$val['gift_name'].'*'.$val['number'];
  133. }else{
  134. $remark = '送给你 '.$val['gift_name'].'*'.$val['number'];
  135. }
  136. $rs[] = [
  137. 'id' => $val['id'],
  138. 'gift_image' => $val['gift_gif_image'],
  139. 'createtime' => $val['createtime'],
  140. 'remark' => $remark,
  141. 'nickname' => $val['nickname'],
  142. 'avatar' => $val['avatar'],
  143. 'gender' => $val['gender'],
  144. 'user_id' => $type == 1 ? $val['user_to_id'] : $val['user_id'],
  145. ];
  146. }
  147. $this->success(1,$rs);
  148. }
  149. //聊天送礼物
  150. public function givegift_typing() {
  151. // 接口防并发
  152. if (!$this->apiLimit(1, 1000)) {
  153. $this->error('休息一下吧');
  154. }
  155. $user_id = input('user_id');// 赠送对象
  156. $gift_id = input('gift_id');// 礼物ID
  157. $number = input('number',1,'intval');//数量
  158. $is_back = input("is_back",0);// 是否背包赠送: 1=是,0=否
  159. if (!$user_id || !$gift_id || $number < 1 || !in_array($is_back,[0,1]) )
  160. {
  161. $this->error();
  162. }
  163. // 不可以赠送给自己
  164. if($this->auth->id == $user_id)
  165. {
  166. $this->error("不可以赠送给自己");
  167. }
  168. //被赠送人信息
  169. $touserinfo = Db::name('user')->where('id',$user_id)->find();
  170. if (!$touserinfo)
  171. {
  172. $this->error("不存在的用户");
  173. }
  174. $backGiftId = 0; //背包礼物表的 gift_id
  175. if($is_back == 1) {
  176. // 获取背包礼物信息
  177. $giftInfo = Db::name('gift_back')->field('gift_id')->where('id',$gift_id)->find();
  178. if(!$giftInfo){
  179. $this->error("背包礼物获取失败");
  180. }
  181. $backGiftId = $giftInfo['gift_id'];
  182. // 随机获取一个礼物
  183. $allCount = $number;
  184. $giftbackList = Db::name('gift_back')->field('id,value,name,image,gif_image,number')->where(["gift_id"=>$backGiftId,"user_id"=>$this->auth->id,'is_use'=>0])->limit($allCount)->order('id asc')->select();
  185. $giftinfo = isset($giftbackList[0]) ? $giftbackList[0] : [];
  186. $giftcount = 0;
  187. $giftList = [];
  188. if(!empty($giftbackList)) {
  189. foreach($giftbackList as $k => $v) {
  190. $giftList[$k] = $v;
  191. $giftcount += $v["number"];
  192. if($giftcount >= $allCount) {
  193. break;
  194. }
  195. }
  196. }
  197. if($giftcount < $allCount)
  198. {
  199. $this->error("背包数量不足");
  200. }
  201. $giftvalue = $giftinfo["value"] * $number;
  202. $getValue = $giftvalue;
  203. }else{
  204. // 获取礼物信息
  205. $giftinfo = Db::name('gift')->where('id',$gift_id)->find();
  206. if (!$giftinfo)
  207. {
  208. $this->error("请选择礼物");
  209. }
  210. $giftvalue = $giftinfo["value"] * $number;
  211. $getValue = $giftvalue;
  212. // 判断当前用户余额
  213. $user_jewel = model('wallet')->getWallet($this->auth->id,'jewel');
  214. if($user_jewel < $giftvalue)
  215. {
  216. $this->error("您的金币余额不足");
  217. }
  218. }
  219. Db::startTrans();
  220. if($is_back == 1) {
  221. $b=0;
  222. foreach($giftList as $k => $v) {
  223. for($a=1;$a<=$v["number"];$a++) {
  224. $b++;
  225. $num = $v["number"] - $a;
  226. if($num > 0) {
  227. $res1 = Db::name('gift_back')->where(["id"=>$v["id"]])->setDec("number");
  228. } else {
  229. // $res1 = \app\common\model\GiftBack::update(["is_use"=>1,"use_time"=>time()],["id"=>$v["id"]]);
  230. $res1 = Db::name('gift_back')->where(["id"=>$v["id"]])->delete();
  231. }
  232. if($b == $number) break;
  233. }
  234. }
  235. $res2 = true;
  236. }else{
  237. if($giftvalue > 0){
  238. // 扣除当前用户余额
  239. $wallet_rs = model('wallet')->lockChangeAccountRemain($this->auth->id,$giftvalue,'-',0,"赠送礼物:'" . $giftinfo["name"] . "',扣除" . $giftvalue . "金币!",3,'jewel');
  240. if($wallet_rs['status'] === false){
  241. Db::rollback();
  242. $this->error($wallet_rs['msg']);
  243. }
  244. // 添加赠送用户余额,放到计划任务里了
  245. /*$jewel_to_money = config('jewel_to_money');
  246. $gift_plat_scale = config('site.gift_plat_scale');
  247. $giftmoney = bcdiv($giftvalue,$jewel_to_money,2);
  248. $money = bcdiv(bcmul($giftmoney,100 - $gift_plat_scale,2),100,2);
  249. $wallet_rs = model('wallet')->lockChangeAccountRemain($user_id,$money,'+',0,'获得礼物:'.$giftinfo["name"],101,'money');
  250. if($wallet_rs['status'] === false){
  251. Db::rollback();
  252. $this->error($wallet_rs['msg']);
  253. }*/
  254. }
  255. }
  256. // 添加礼物赠送记录表
  257. $data = [
  258. 'user_id' => $this->auth->id,
  259. 'user_to_id' => $user_id,
  260. 'gift_id' => $backGiftId > 0 ? $backGiftId : $gift_id,
  261. 'gift_give_type' => $is_back ? 1 : 2,
  262. 'gift_name' => $giftinfo['name'],
  263. 'gift_gif_image' => $giftinfo['image'],
  264. 'number' => $number,
  265. 'price' => $giftinfo['value'],
  266. 'value' => $giftvalue,
  267. 'task_status' => 0,
  268. 'createtime' => time(),
  269. ];
  270. //每个礼物都要计算平台抽成和房主抽成
  271. $gift_plat_scale = config('site.gift_plat_scale');
  272. $data['platvalue'] = bcmul($gift_plat_scale/100 ,$data["value"],2);//平台抽成
  273. $data['getvalue'] = bcsub($data["value"] ,$data['platvalue'],2);//减去抽成剩余价值
  274. $log_id = Db::name('gift_user_party')->insertGetId($data);
  275. if(!$log_id){
  276. Db::rollback();
  277. $this->error('赠送失败');
  278. }
  279. Db::commit();
  280. $this->success('赠送成功');
  281. }
  282. }