Gift.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. public function test2(){
  20. $user_id = $this->auth->id;
  21. $gift_id = input('gift_id');
  22. $pay_total = input('pay_total');
  23. Db::startTrans();
  24. $rs = $this->baobi($user_id,$gift_id,$pay_total);
  25. if($rs !== true){
  26. Db::rollback();
  27. }
  28. Db::commit();
  29. dump($rs);
  30. }
  31. //gift_id必须是爆币礼物才可以
  32. //仅用来测试
  33. public function baobi($user_id,$gift_id,$pay_total){
  34. //奖项参数
  35. $conf_arr = Db::name('gift_baobi_config')->where('gift_id',$gift_id)->select();
  36. if(empty($conf_arr)){
  37. return true;
  38. }
  39. //用户今天的爆币情况,与消费情况,不用区分具体哪个爆币礼物
  40. $today_start = strtotime(date('Y-m-d'));
  41. $today_end = $today_start + 86399;
  42. $map = [
  43. 'user_id' => $user_id,
  44. 'createtime' => ['BETWEEN',[$today_start,$today_end]],
  45. ];
  46. $today_data = Db::name('gift_baobi_log')->field('IFNULL(sum(pay_total),0) as today_pay_total,IFNULL(sum(baobi_total),0) as today_baobi_total')->where($map)->find();
  47. //dump($today_data);
  48. if($today_data['today_baobi_total'] >= $today_data['today_pay_total']){
  49. //爆币比消费还高了,不用抽奖了,记录个日志,返回。这里的日志仅用来记录今日消费 sum(pay_total)
  50. $data = [];
  51. $data['user_id'] = $user_id;
  52. $data['gift_id'] = $gift_id;
  53. $data['baobi_id'] = 0;
  54. $data['beilv'] = 0;
  55. $data['rate'] = 0;
  56. $data['pay_total'] = $pay_total;
  57. $data['baobi_total'] = 0;
  58. $data['createtime'] = time();
  59. $log_id = Db::name('gift_baobi_log')->insertGetId($data);
  60. if(!$log_id){
  61. return false;
  62. }
  63. return true;
  64. }
  65. //概率新数组
  66. $gailv = [];
  67. $bei = 100; //小数变整数
  68. foreach ($conf_arr as $key=>$value)
  69. {
  70. $gailv[$value['id']] = $value['rate']*$bei;
  71. }
  72. //dump($gailv);
  73. //奖项新数组
  74. $conf_column = [];
  75. foreach ($conf_arr as $key=>$value)
  76. {
  77. $conf_column[$value['id']] = $value;
  78. }
  79. //dump($conf_column);
  80. //抽
  81. $rid = $this->getRand($gailv); //根据概率获取奖项id
  82. //dump($rid);
  83. //返回中奖结果
  84. $result = $conf_column[$rid];
  85. //爆币金额
  86. $baobi_total = bcmul($pay_total,$result['beilv'],0);
  87. //写入爆币记录,不论有没有钱。全都写入其实是为了方便查日志,比在money_log里找范围更小,而且可删除
  88. $data = [];
  89. $data['user_id'] = $user_id;
  90. $data['gift_id'] = $gift_id;
  91. $data['baobi_id'] = $result['id'];
  92. $data['beilv'] = $result['beilv'];
  93. $data['rate'] = $result['rate'];
  94. $data['pay_total'] = $pay_total;
  95. $data['baobi_total'] = $baobi_total;
  96. $data['createtime'] = time();
  97. $log_id = Db::name('gift_baobi_log')->insertGetId($data);
  98. if(!$log_id){
  99. return false;
  100. }
  101. //直接给用户钱
  102. if($baobi_total > 0){
  103. $rs_wallet = model('wallet')->lockChangeAccountRemain($user_id,$baobi_total,'+',0,'送礼物爆币'.$result['beilv'].'倍',21,'jewel');
  104. if($rs_wallet['status'] === false){
  105. return false;
  106. }
  107. //返回爆币金额
  108. return [
  109. 'baobi_total' => $baobi_total,
  110. 'baobi_beilv' => $result['beilv'],
  111. ];
  112. }
  113. //默认成功
  114. return true;
  115. }
  116. //概率获得算法
  117. private function getRand($proArr) {
  118. //概率数组的总概率精度
  119. $proSum = array_sum($proArr);
  120. $key = rand(1, $proSum);
  121. // echo $key;
  122. $result = 0;
  123. $now = 0;
  124. foreach ($proArr as $k=>$v)
  125. {
  126. $now = $now + $v;
  127. if($key<=$now)
  128. {
  129. $result = $k;
  130. break;
  131. }
  132. }
  133. unset ($proArr);
  134. return $result;
  135. }
  136. /**
  137. * 获取礼物列表
  138. */
  139. public function getGiftList() {
  140. $type = input('type',0);
  141. // 获取基本信息
  142. $where = ['is_show'=>1];
  143. if($type){
  144. $where['type'] = $type;
  145. }
  146. $giftList = Db::name('gift')->field('id,name,price,image,special')->where($where)->order("sort","asc")->select();
  147. $giftList = list_domain_image($giftList,['image','special']);
  148. $this->success("获取成功!",$giftList);
  149. }
  150. /**
  151. * 获取礼物类型
  152. */
  153. public function getGiftType() {
  154. // 获取基本信息
  155. $where = [];
  156. $where["is_show"] = 1;
  157. $giftList = $this->gifttypeModel->field("id,name")->where($where)->order("weight","desc")->select();
  158. $this->success("获取成功!",$giftList);
  159. }
  160. /**
  161. * 获取我的背包礼物
  162. */
  163. public function getMyBackGift() {
  164. $userid = input("user_id", $this->auth->id);
  165. // 分页搜索构建
  166. $list = \app\common\model\GiftBack::field("id,name,image,gif_image,value,sum(number) as number")
  167. ->where(["user_id"=>$userid,"is_use"=>0])
  168. ->group("gift_id")
  169. ->autopage()
  170. ->select();
  171. $this->success("获取成功!",$list);
  172. }
  173. /**
  174. * 获取我的礼物墙
  175. */
  176. public function getMyGiftWall() {
  177. $user_id = input("user_id", 0);
  178. $userid = $user_id ? $user_id : $this->auth->id;
  179. $list = Db::name('gift_user_party')->alias('a')
  180. ->join("gift g", "g.id = a.gift_id", "LEFT")
  181. ->field("a.gift_id,g.name,g.image,sum(number) as number")
  182. ->where(["a.user_to_id" => $userid,])
  183. ->group("a.gift_id")
  184. ->order('g.value desc')
  185. ->select();
  186. $list = list_domain_image($list,['image']);
  187. $this->success("获取成功!", $list);
  188. }
  189. /**
  190. * 获取我的收送礼明细
  191. */
  192. public function my_gift_log(){
  193. $user_id = $this->auth->id;
  194. $type = input('type',1);
  195. $where = [];
  196. if($type == 1){
  197. $where['user_id'] = $user_id;//我送出
  198. $joinstr = 'gup.user_to_id = user.id';
  199. }else{
  200. $where['user_to_id'] = $user_id;//我收到
  201. $joinstr = 'gup.user_id = user.id';
  202. }
  203. $list = Db::name('gift_user_party')->alias('gup')
  204. ->join('user',$joinstr,'LEFT')->field('gup.*,user.nickname,user.avatar,user.gender')
  205. ->where($where)->order('id desc')->autopage()->select();
  206. $list = list_domain_image($list,['gift_gif_image','avatar']);
  207. $rs = [];
  208. if(empty($list)){
  209. $this->success(1,$rs);
  210. }
  211. foreach($list as $key => $val){
  212. if($type == 1){
  213. $remark = '送给Ta '.$val['gift_name'].'*'.$val['number'];
  214. }else{
  215. $remark = '送给你 '.$val['gift_name'].'*'.$val['number'];
  216. }
  217. $rs[] = [
  218. 'id' => $val['id'],
  219. 'gift_image' => $val['gift_gif_image'],
  220. 'createtime' => $val['createtime'],
  221. 'remark' => $remark,
  222. 'nickname' => $val['nickname'],
  223. 'avatar' => $val['avatar'],
  224. 'gender' => $val['gender'],
  225. 'user_id' => $type == 1 ? $val['user_to_id'] : $val['user_id'],
  226. ];
  227. }
  228. $this->success(1,$rs);
  229. }
  230. //聊天送礼物
  231. public function givegift_typing() {
  232. // 接口防并发
  233. if (!$this->apiLimit(1, 1000)) {
  234. $this->error(__('Operation frequently'));
  235. }
  236. $user_id = input('user_id');// 赠送对象
  237. $gift_id = input('gift_id');// 礼物ID
  238. $number = input('number',1,'intval');//数量
  239. $is_back = input("is_back",0);// 是否背包赠送: 1=是,0=否
  240. if (!$user_id || !$gift_id || $number < 1 || !in_array($is_back,[0,1]) )
  241. {
  242. $this->error();
  243. }
  244. // 不可以赠送给自己
  245. if($this->auth->id == $user_id)
  246. {
  247. $this->error("不可以赠送给自己");
  248. }
  249. //被赠送人信息
  250. $touserinfo = Db::name('user')->where('id',$user_id)->find();
  251. if (!$touserinfo)
  252. {
  253. $this->error("不存在的用户");
  254. }
  255. $backGiftId = 0; //背包礼物表的 gift_id
  256. if($is_back == 1) {
  257. // 获取背包礼物信息
  258. $giftInfo = Db::name('gift_back')->field('gift_id')->where('id',$gift_id)->find();
  259. if(!$giftInfo){
  260. $this->error("背包礼物获取失败");
  261. }
  262. $backGiftId = $giftInfo['gift_id'];
  263. // 随机获取一个礼物
  264. $allCount = $number;
  265. $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();
  266. $giftinfo = isset($giftbackList[0]) ? $giftbackList[0] : [];
  267. $giftcount = 0;
  268. $giftList = [];
  269. if(!empty($giftbackList)) {
  270. foreach($giftbackList as $k => $v) {
  271. $giftList[$k] = $v;
  272. $giftcount += $v["number"];
  273. if($giftcount >= $allCount) {
  274. break;
  275. }
  276. }
  277. }
  278. if($giftcount < $allCount)
  279. {
  280. $this->error("背包数量不足");
  281. }
  282. $giftvalue = $giftinfo["value"] * $number;
  283. $getValue = $giftvalue;
  284. }else{
  285. // 获取礼物信息
  286. $giftinfo = Db::name('gift')->where('id',$gift_id)->find();
  287. if (!$giftinfo)
  288. {
  289. $this->error("请选择礼物");
  290. }
  291. $giftvalue = $giftinfo["value"] * $number;
  292. $getValue = $giftvalue;
  293. // 判断当前用户余额
  294. $user_jewel = model('wallet')->getWallet($this->auth->id,'jewel');
  295. if($user_jewel < $giftvalue)
  296. {
  297. $this->error("您的金币余额不足");
  298. }
  299. }
  300. Db::startTrans();
  301. if($is_back == 1) {
  302. $b=0;
  303. foreach($giftList as $k => $v) {
  304. for($a=1;$a<=$v["number"];$a++) {
  305. $b++;
  306. $num = $v["number"] - $a;
  307. if($num > 0) {
  308. $res1 = Db::name('gift_back')->where(["id"=>$v["id"]])->setDec("number");
  309. } else {
  310. // $res1 = \app\common\model\GiftBack::update(["is_use"=>1,"use_time"=>time()],["id"=>$v["id"]]);
  311. $res1 = Db::name('gift_back')->where(["id"=>$v["id"]])->delete();
  312. }
  313. if($b == $number) break;
  314. }
  315. }
  316. $res2 = true;
  317. }else{
  318. if($giftvalue > 0){
  319. // 扣除当前用户余额
  320. $wallet_rs = model('wallet')->lockChangeAccountRemain($this->auth->id,$giftvalue,'-',0,"赠送礼物:'" . $giftinfo["name"] . "',扣除" . $giftvalue . "金币!",3,'jewel');
  321. if($wallet_rs['status'] === false){
  322. Db::rollback();
  323. $this->error($wallet_rs['msg']);
  324. }
  325. // 添加赠送用户余额,放到计划任务里了
  326. /*$money_to_jewel = config('site.money_to_jewel');
  327. $gift_plat_scale = config('site.gift_plat_scale');
  328. $giftmoney = bcdiv($giftvalue,$money_to_jewel,2);
  329. $money = bcdiv(bcmul($giftmoney,100 - $gift_plat_scale,2),100,2);
  330. $wallet_rs = model('wallet')->lockChangeAccountRemain($user_id,$money,'+',0,'获得礼物:'.$giftinfo["name"],101,'money');
  331. if($wallet_rs['status'] === false){
  332. Db::rollback();
  333. $this->error($wallet_rs['msg']);
  334. }*/
  335. }
  336. }
  337. // 添加礼物赠送记录表
  338. $data = [
  339. 'user_id' => $this->auth->id,
  340. 'user_to_id' => $user_id,
  341. 'gift_id' => $backGiftId > 0 ? $backGiftId : $gift_id,
  342. 'gift_give_type' => $is_back ? 1 : 2,
  343. 'gift_name' => $giftinfo['name'],
  344. 'gift_gif_image' => $giftinfo['image'],
  345. 'number' => $number,
  346. 'price' => $giftinfo['value'],
  347. 'value' => $giftvalue,
  348. 'task_status' => 0,
  349. 'createtime' => time(),
  350. ];
  351. //每个礼物都要计算平台抽成和房主抽成
  352. $gift_plat_scale = config('site.gift_plat_scale');
  353. $data['platvalue'] = bcmul($gift_plat_scale/100 ,$data["value"],2);//平台抽成
  354. $data['getvalue'] = bcsub($data["value"] ,$data['platvalue'],2);//减去抽成剩余价值
  355. $log_id = Db::name('gift_user_party')->insertGetId($data);
  356. if(!$log_id){
  357. Db::rollback();
  358. $this->error('赠送失败');
  359. }
  360. Db::commit();
  361. $this->success('赠送成功');
  362. }
  363. }