123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use think\Db;
- /**
- * 大转盘
- */
- class Active extends Api
- {
- protected $noNeedLogin = [];
- protected $noNeedRight = ['*'];
- public function index(){
- echo '123';
- }
- public function choujiang_fake(){
- //信息
- $active_id = input('active_id',0);
- $active = Db::name('actives')->where(['id'=>$active_id,'is_show'=>1])->find();
- if(!$active){
- $this->error('不存在的活动');
- }
- //时间判断
- $time= time();
- if($time > $active['end_time']){
- $this->error('抽奖活动已结束');
- }
- if($time < $active['start_time']){
- $this->error('抽奖活动未开始,请耐心等待');
- }
- //奖项参数
- $money_arr = Db::name('actives_info')->field('id,name,info')->where(['active_id'=>$active['id'],'is_show'=>1])->order('sort asc,id asc')->limit(6)->select();
- $key = rand(0,5);
- $this->success('success',$money_arr[$key]);
- }
- /**
- * @description 开始抽奖
- * @return array
- */
- public function choujiang()
- {
- //信息
- $active_id = input('active_id',0);
- $active = Db::name('actives')->where(['id'=>$active_id,'is_show'=>1])->find();
- if(!$active){
- $this->error('不存在的活动');
- }
- //时间判断
- $time= time();
- if($time > $active['end_time']){
- $this->error('抽奖活动已结束');
- }
- if($time < $active['start_time']){
- $this->error('抽奖活动未开始,请耐心等待');
- }
- //奖项参数
- $conf_arr = Db::name('actives_info')->field('id,name,info,rate,number,gift_ids')->where(['active_id'=>$active['id'],'is_show'=>1])->order('sort asc,id asc')->limit(6)->select();
- //目前已有奖项
- $user_log = Db::name('actives_user_log')->field('active_info_id,count(id) as num')->where(['active_id'=>$active['id']])->group('active_info_id')->select();
- $user_log_arr = [];
- foreach($user_log as $ulk => $ulv){
- $user_log_arr[$ulv['active_info_id']] = $ulv['num'];
- }
- // dump($user_log_arr);
- //概率新数组
- $gailv = [];
- $bei = 100; //小数变整数
- foreach ($conf_arr as $key=>$value)
- {
- //受限
- if($value['number'] > 0){
- //已获此奖 && 已获次数 >= 计划数量
- if(isset($user_log_arr[$value['id']]) && $user_log_arr[$value['id']] >= $value['number']){
- //忽略此次
- // dump($user_log_arr[$value['id']]);
- // dump($value['number']);
- continue;
- }
- }
- $gailv[$value['id']] = $value['rate']*$bei;
- }
- // dump($gailv);
- //奖项新数组
- $conf_column = [];
- foreach ($conf_arr as $key=>$value)
- {
- unset($value['rate']);
- unset($value['number']);
- $conf_column[$value['id']] = $value;
- }
- // dump($conf_column);
- //抽
- $rid = $this->getRand($gailv); //根据概率获取奖项id
- // dump($rid);exit;
- //返回结果
- $result = $conf_column[$rid];
- Db::startTrans();
- //礼物id
- $gift_id = 0;
- $remark = $result['info'];
- if(!empty($result['gift_ids'])){
- $gift_ids = explode(',',$result['gift_ids']);
- $gift_id = $gift_ids[rand(0,count($gift_ids)-1)];
- $gift_info = Db::name('gift')->where('id',$gift_id)->find();
- if(!empty($gift_info)){
- //加入到背包
- $back = [
- 'user_id' => $this->auth->id,
- 'gift_id' => $gift_id,
- 'name' => $gift_info['name'],
- 'image' => $gift_info['image'],
- 'gif_image' => $gift_info['special'],
- 'value' => $gift_info['value'],
- 'number' => 1,
- 'is_use' => 0,
- 'use_time' => 0,
- 'get_way' => 1,
- 'createtime' => time(),
- ];
- $gift_back = Db::name('gift_back')->insertGetId($back);
- if(!$gift_back){
- Db::rollback();
- $this->error('加入背包失败');
- }
- $remark .= ':'.$gift_info['name'];
- }else{
- $gift_id = 0;
- }
- }
- //插入抽奖记录
- $data = [];
- $data['user_id'] = $this->auth->id;
- $data['active_id'] = $active['id'];
- $data['active_info_id'] = $result['id'];
- $data['gift_id'] = $gift_id;
- $data['name'] = $result['name'];
- $data['status'] = 0;
- $data['remark'] = $remark;
- $data['createtime'] = $time;
- $log_id = Db::name('actives_user_log')->insertGetId($data);
- if(!$log_id){
- Db::rollback();
- $this->error('抽奖记录更新有误');
- }
- //无论是否中奖。抽奖消耗
- $rs_wallet = model('wallet')->lockChangeAccountRemain($this->auth->id,10,'-',0,'大转盘抽奖一次',13,'jewel');
- if ($rs_wallet['status'] === false) {
- Db::rollback();
- $this->error($rs_wallet['msg']);
- }
- Db::commit();
- unset($result['name']);
- unset($result['info']);
- unset($result['gift_ids']);
- $result['remark'] = $remark;
- $this->success('success',$result);
- }
- //概率获得算法
- 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;
- }
- /**
- * @description 获取大转盘信息
- * @return array
- * @return_format array info 大转盘基本信息
- * @return_format array conf 奖项
- */
- public function getactive()
- {
- //信息
- $map = [
- 'is_show' => 1,
- ];
- $info = Db::name('actives')->field('id,name,image,start_time,end_time,content')->where($map)->find();
- $info = info_domain_image($info,['image']);
- //获取奖项
- $conf = Db::name('actives_info')->field('id,name,info')->where('active_id',$info['id'])->where('is_show',1)->order('sort asc,id asc')->limit(6)->select();
- $rt = [];
- $rt['info'] = $info;
- $rt['conf'] = $conf;
- $rt['jewel'] = model('wallet')->getWallet($this->auth->id,'jewel');
- $rt['jewel_price'] = 10;
- return $this->success('success',$rt);
- }
- //本次活动中奖纪录
- public function activeuserlog(){
- $active_id = input('active_id',0);
- $where = [
- 'log.active_id' => $active_id,
- ];
- $my = input('my',0);
- if($my){
- $where['log.user_id'] = $this->auth->id;
- }
- $log = Db::name('actives_user_log log')
- ->field('log.user_id,log.active_id,log.name,log.status,log.createtime,user.nickname,user.mobile,user.avatar')
- ->join('user','user.id = log.user_id','LEFT')
- ->where($where)->order('log.id desc')->autopage()->select();
- foreach($log as $key => &$val){
- $val['avatar'] = cdnurl($val['avatar']);
- $val['mobile'] = str_replace(substr($val['mobile'],3,5),'****',$val['mobile']);
- }
- return $this->success('success',$log);
- }
- }
|