123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use think\Db;
- /**
- * 礼物接口
- */
- class Gift extends Api
- {
- protected $noNeedLogin = ['getGiftList','getGiftType'];
- protected $noNeedRight = '*';
- public function _initialize()
- {
- parent::_initialize();
- $this->giftModel = new \app\common\model\Gift();
- $this->gifttypeModel = new \app\common\model\GiftType();
- }
- public function test2(){
- $user_id = $this->auth->id;
- $gift_id = input('gift_id');
- $pay_total = input('pay_total');
- Db::startTrans();
- $rs = $this->test($user_id,$gift_id,$pay_total);
- if($rs !== true){
- Db::rollback();
- }
- Db::commit();
- dump($rs);
- }
- //gift_id必须是爆币礼物才可以
- public function test($user_id,$gift_id,$pay_total){
- //奖项参数
- $conf_arr = Db::name('gift_baobi_config')->where('gift_id',$gift_id)->select();
- if(empty($conf_arr)){
- return true;
- }
- //用户今天的爆币情况,与消费情况,不用区分具体哪个爆币礼物
- $today_start = strtotime(date('Y-m-d'));
- $today_end = $today_start + 86399;
- $map = [
- 'user_id' => $user_id,
- 'createtime' => ['BETWEEN',[$today_start,$today_end]],
- ];
- $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();
- //dump($today_data);
- if($today_data['today_baobi_total'] >= $today_data['today_pay_total']){
- //爆币比消费还高了,不用抽奖了,记录个日志,返回。这里的日志仅用来记录今日消费 sum(pay_total)
- $data = [];
- $data['user_id'] = $user_id;
- $data['gift_id'] = $gift_id;
- $data['baobi_id'] = 0;
- $data['beilv'] = 0;
- $data['rate'] = 0;
- $data['pay_total'] = $pay_total;
- $data['baobi_total'] = 0;
- $data['createtime'] = time();
- $log_id = Db::name('gift_baobi_log')->insertGetId($data);
- if(!$log_id){
- return false;
- }
- return true;
- }
- //概率新数组
- $gailv = [];
- $bei = 100; //小数变整数
- foreach ($conf_arr as $key=>$value)
- {
- $gailv[$value['id']] = $value['rate']*$bei;
- }
- //dump($gailv);
- //奖项新数组
- $conf_column = [];
- foreach ($conf_arr as $key=>$value)
- {
- $conf_column[$value['id']] = $value;
- }
- //dump($conf_column);
- //抽
- $rid = $this->getRand($gailv); //根据概率获取奖项id
- //dump($rid);
- //返回中奖结果
- $result = $conf_column[$rid];
- //爆币金额
- $baobi_total = bcdiv(bcmul($pay_total,$result['beilv'],0),100,0);
- //写入爆币记录,不论有没有钱。全都写入其实是为了方便查日志,比在money_log里找范围更小,而且可删除
- $data = [];
- $data['user_id'] = $user_id;
- $data['gift_id'] = $gift_id;
- $data['baobi_id'] = $result['id'];
- $data['beilv'] = $result['beilv'];
- $data['rate'] = $result['rate'];
- $data['pay_total'] = $pay_total;
- $data['baobi_total'] = $baobi_total;
- $data['createtime'] = time();
- $log_id = Db::name('gift_baobi_log')->insertGetId($data);
- if(!$log_id){
- return false;
- }
- //直接给用户钱
- if($baobi_total > 0){
- $rs_wallet = model('wallet')->lockChangeAccountRemain($user_id,$baobi_total,'+',0,'送礼物爆币'.$result['beilv'].'%',21,'jewel');
- if($rs_wallet['status'] === false){
- return false;
- }
- }
- //默认成功
- return true;
- }
- //概率获得算法
- private function getRand($proArr) {
- //概率数组的总概率精度
- $proSum = array_sum($proArr);
- $key = rand(1, $proSum);
- // echo $key;
- $result = 0;
- $now = 0;
- foreach ($proArr as $k=>$v)
- {
- $now = $now + $v;
- if($key<=$now)
- {
- $result = $k;
- break;
- }
- }
- unset ($proArr);
- return $result;
- }
- /**
- * 获取礼物列表
- */
- public function getGiftList() {
- $type = input('type',0);
- // 获取基本信息
- $where = ['is_show'=>1];
- if($type){
- $where['type'] = $type;
- }
- $giftList = Db::name('gift')->field('id,name,price,image,special')->where($where)->order("sort","asc")->select();
- $giftList = list_domain_image($giftList,['image','special']);
- $this->success("获取成功!",$giftList);
- }
- /**
- * 获取礼物类型
- */
- public function getGiftType() {
- // 获取基本信息
- $where = [];
- $where["is_show"] = 1;
- $giftList = $this->gifttypeModel->field("id,name")->where($where)->order("weight","desc")->select();
- $this->success("获取成功!",$giftList);
- }
- /**
- * 获取我的背包礼物
- */
- public function getMyBackGift() {
- $userid = input("user_id", $this->auth->id);
- // 分页搜索构建
- $list = \app\common\model\GiftBack::field("id,name,image,gif_image,value,sum(number) as number")
- ->where(["user_id"=>$userid,"is_use"=>0])
- ->group("name")
- ->select();
- $this->success("获取成功!",$list);
- }
- /**
- * 获取我的礼物墙
- */
- public function getMyGiftWall() {
- $user_id = input("user_id", 0);
- $userid = $user_id ? $user_id : $this->auth->id;
- $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")
- ->where(["user_to_id" => $userid,])
- ->group("gift_id")
- ->order('g.value desc')
- ->select();
- $this->success("获取成功!", $list);
- }
- /**
- * 获取我的礼物墙
- */
- public function getMyGiftWall_typing() {
- $user_id = input("user_id", 0);
- $userid = $user_id ? $user_id : $this->auth->id;
- $list = Db::name('gift_user_typing')->alias('log')
- ->join('gift', 'gift.id = log.gift_id', 'LEFT')->field('log.*,sum(number) as number,gift.name,gift.image,gift.special')
- ->where(['log.user_to_id' => $userid])
- ->group('log.gift_id')
- ->order('gift.price desc')
- ->select();
- $list = list_domain_image($list,['image','special']);
- $this->success("获取成功!", $list);
- }
- /**
- * 获取我的收送礼明细
- */
- public function my_gift_log(){
- $user_id = $this->auth->id;
- $type = input('type',1);
- $where = [];
- if($type == 1){
- $where['user_id'] = $user_id;//我送出
- $joinstr = 'gup.user_to_id = user.id';
- }else{
- $where['user_to_id'] = $user_id;//我收到
- $joinstr = 'gup.user_id = user.id';
- }
- $list = Db::name('gift_user_party')->alias('gup')
- ->join('user',$joinstr,'LEFT')->field('gup.*,user.nickname')
- ->where($where)->order('id desc')->autopage()->select();
- $list = list_domain_image($list,['gift_gif_image']);
- $rs = [];
- if(empty($list)){
- $this->success(1,$rs);
- }
- foreach($list as $key => $val){
- if($type == 1){
- $remark = '赠送'.$val['nickname'].','.$val['gift_name'].'*'.$val['number'].',价值'.$val['value'].'钻石';
- }else{
- $remark = $val['nickname'].'赠送,'.$val['gift_name'].'*'.$val['number'].',价值'.$val['value'].'钻石';
- }
- $rs[] = [
- 'id' => $val['id'],
- 'gift_image' => $val['gift_gif_image'],
- 'createtime' => $val['createtime'],
- 'remark' => $remark
- ];
- }
- $this->success(1,$rs);
- }
- //聊天送礼物
- public function givegift_typing() {
- // 接口防并发
- if (!$this->apiLimit(1, 1000)) {
- $this->error(__('Operation frequently'));
- }
- $user_id = input('user_id');// 赠送对象
- $gift_id = input('gift_id');// 礼物ID
- $number = input('number',1,'intval');//数量
- $is_back = input("is_back",0);// 是否背包赠送: 1=是,0=否
- if (!$user_id || !$gift_id || $number < 1 || !in_array($is_back,[0,1]) )
- {
- $this->error();
- }
- // 不可以赠送给自己
- if($this->auth->id == $user_id)
- {
- $this->error("不可以赠送给自己");
- }
- //被赠送人信息
- $touserinfo = Db::name('user')->where('id',$user_id)->find();
- if (!$touserinfo)
- {
- $this->error("不存在的用户");
- }
- $backGiftId = 0; //背包礼物表的 gift_id
- if($is_back == 1) {
- // 获取背包礼物信息
- $giftInfo = Db::name('gift_back')->field('gift_id')->where('id',$gift_id)->find();
- if(!$giftInfo){
- $this->error("背包礼物获取失败");
- }
- $backGiftId = $giftInfo['gift_id'];
- // 随机获取一个礼物
- $allCount = $number;
- $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();
- $giftinfo = isset($giftbackList[0]) ? $giftbackList[0] : [];
- $giftcount = 0;
- $giftList = [];
- if(!empty($giftbackList)) {
- foreach($giftbackList as $k => $v) {
- $giftList[$k] = $v;
- $giftcount += $v["number"];
- if($giftcount >= $allCount) {
- break;
- }
- }
- }
- if($giftcount < $allCount)
- {
- $this->error("背包数量不足");
- }
- $giftvalue = $giftinfo["value"] * $number;
- $getValue = $giftvalue;
- }else{
- // 获取礼物信息
- $giftinfo = Db::name('gift')->where('id',$gift_id)->find();
- if (!$giftinfo)
- {
- $this->error("请选择礼物");
- }
- $giftvalue = $giftinfo["value"] * $number;
- $getValue = $giftvalue;
- // 判断当前用户余额
- $user_jewel = model('wallet')->getWallet($this->auth->id,'jewel');
- if($user_jewel < $giftvalue)
- {
- $this->error("您的金币余额不足");
- }
- }
- Db::startTrans();
- if($is_back == 1) {
- $b=0;
- foreach($giftList as $k => $v) {
- for($a=1;$a<=$v["number"];$a++) {
- $b++;
- $num = $v["number"] - $a;
- if($num > 0) {
- $res1 = Db::name('gift_back')->where(["id"=>$v["id"]])->setDec("number");
- } else {
- // $res1 = \app\common\model\GiftBack::update(["is_use"=>1,"use_time"=>time()],["id"=>$v["id"]]);
- $res1 = Db::name('gift_back')->where(["id"=>$v["id"]])->delete();
- }
- if($b == $number) break;
- }
- }
- $res2 = true;
- }else{
- if($giftvalue > 0){
- // 扣除当前用户余额
- $wallet_rs = model('wallet')->lockChangeAccountRemain($this->auth->id,$giftvalue,'-',0,"赠送礼物:'" . $giftinfo["name"] . "',扣除" . $giftvalue . "金币!",3,'jewel');
- if($wallet_rs['status'] === false){
- Db::rollback();
- $this->error($wallet_rs['msg']);
- }
- // 添加赠送用户余额
- $money_to_jewel = config('site.money_to_jewel');
- $gift_plat_scale = config('site.gift_plat_scale');
- $giftmoney = bcdiv($giftvalue,$money_to_jewel,2);
- $money = bcdiv(bcmul($giftmoney,100 - $gift_plat_scale,2),100,2);
- $wallet_rs = model('wallet')->lockChangeAccountRemain($user_id,$money,'+',0,'获得礼物:'.$giftinfo["name"],103,'money');
- if($wallet_rs['status'] === false){
- Db::rollback();
- $this->error($wallet_rs['msg']);
- }
- }
- }
- // 添加礼物赠送记录表
- $data = [
- 'user_id' => $this->auth->id,
- 'user_to_id' => $user_id,
- 'gift_id' => $backGiftId > 0 ? $backGiftId : $gift_id,
- 'gift_give_type' => $is_back ? 1 : 2,
- 'gift_name' => $giftinfo['name'],
- 'gift_gif_image' => $giftinfo['image'],
- 'number' => $number,
- 'price' => $giftinfo['value'],
- 'value' => $giftvalue,
- 'task_status' => 1,
- 'createtime' => time(),
- ];
- //每个礼物都要计算平台抽成和房主抽成
- $platRate = 10;
- $data['platvalue'] = bcmul($platRate/100 ,$data["value"],2);//平台抽成
- $data['getvalue'] = bcsub($data["value"] ,$data['platvalue'],2);//减去抽成剩余价值
- $log_id = Db::name('gift_user_party')->insertGetId($data);
- if(!$log_id){
- Db::rollback();
- $this->error('赠送失败');
- }
- Db::commit();
- $this->success('赠送成功');
- }
- }
|