فهرست منبع

大转盘,来自icool,未改动

lizhen_gitee 1 سال پیش
والد
کامیت
2b9a96584b
1فایلهای تغییر یافته به همراه223 افزوده شده و 0 حذف شده
  1. 223 0
      application/api/controller/Active.php

+ 223 - 0
application/api/controller/Active.php

@@ -0,0 +1,223 @@
+<?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,msg')->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();
+
+        //插入抽奖记录
+        $data = [];
+        $data['user_id']   = $this->auth->id;
+        $data['active_id'] = $active['id'];
+        $data['active_info_id'] = $result['id'];
+
+        $data['name'] = $result['name'];
+        $data['status'] = 0;
+        $data['remark'] = $result['info'];
+        $data['createtime'] = $time;
+        $log_id = Db::name('actives_user_log')->insertGetId($data);
+        if(!$log_id){
+            $this->error('抽奖记录更新有误');
+        }
+
+        //无论是否中奖。抽奖消耗
+        $rs_wallet = model('wallet')->lockChangeAccountRemain($this->auth->id,'active',-3,11,'抽奖一次','actives_user_log',$log_id);
+        if ($rs_wallet['status'] === false) {
+            Db::rollback();
+            $this->error($rs_wallet['msg']);
+        }
+
+        Db::commit();
+        unset($result['name']);
+        unset($result['info']);
+        $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['times'] = model('wallet')->getWallet($this->auth->id,'active');
+        $rt['times'] = intval($rt['times']/3);   //3变1取整
+        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')->page($this->page,$this->pagenum)->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);
+    }
+
+
+
+
+}