bianjunhui %!s(int64=2) %!d(string=hai) anos
pai
achega
e2b5b849cf

+ 1 - 0
application/admin/controller/Raprize.php

@@ -151,6 +151,7 @@ class Raprize extends Backend
         $params['p_name']=$prize->name;
         $params['price']=$prize->price;
         $params['money']=$prize->money;
+        $params['description']=$prize->code;
         $result = false;
         Db::startTrans();
         try {

+ 45 - 0
application/api/common.php

@@ -1 +1,46 @@
 <?php
+
+use think\Config;
+
+function make_curl($where,$url, $params=array()){
+    $str= 'site.'.$where['website'];
+    $url =Config::get($str).$url;
+    $ch = curl_init();
+    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
+    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded"));
+    curl_setopt($ch, CURLOPT_URL, $url);
+    curl_setopt($ch, CURLOPT_POST, true);
+    curl_setopt($ch, CURLOPT_TIMEOUT, 60);
+    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
+    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
+    $result = curl_exec($ch);
+    $result = json_decode($result,true);
+    return $result;
+}
+
+function get_rand($proArr)
+{
+    $result = '';
+    //概率数组的总概率精度
+    $proSum = array_sum($proArr);
+
+    //概率数组循环
+    foreach($proArr as $key => $proCur)
+    {
+        // 获取随机数
+        $randNum = mt_rand(1, $proSum);
+        if($randNum <= $proCur)
+        {
+            $result = $key;
+            break;
+        }
+        else
+        {
+            // 减掉当前中奖的概率
+            $proSum -= $proCur;
+        }
+    }
+    unset ($proArr);
+    return $result;
+}

+ 145 - 4
application/api/controller/Raffle.php

@@ -5,7 +5,9 @@ namespace app\api\controller;
 use app\common\controller\Api;
 use app\common\model\Jwttoken;
 use app\admin\model\Raffle as Raf;
+use think\Config;
 use think\Db;
+use think\Request;
 use function fast\e;
 
 /**
@@ -15,13 +17,15 @@ class Raffle extends Api
 {
     protected $noNeedLogin = ['*'];
     protected $noNeedRight = ['*'];
+    protected $userInfo;
 
     /**
      * 首页
      *
      */
-    public function getRaffle()
+    public function _initialize()
     {
+        date_default_timezone_set('America/New_York');
         $token = $this->request->server('HTTP_TOKEN');
         if(empty($token)){
             $this->error('请输入Token');
@@ -32,20 +36,27 @@ class Raffle extends Api
             $msg =$info['msg'];
             $this->error($msg);
         }
-        $userinfo =$info['info'];
+        $this->userInfo=$info['info'];
+        parent::_initialize();
+    }
+
+    public function getRaffle()
+    {
+        $userinfo =$this->userInfo;
         $web=$userinfo['website'];
         $list =  Db::name('raffle')->where("website='{$web}' and status ='normal' ")->order('id desc')->find();
         $data['id']=$list['id'];
         $data['website']=$list['website'];
         $data['name']=$list['name'];
         $data['image']=$list['image'];
-        $data['prize']=$this->getPrize($list['id']);
+        $data['prize']=$this->getPrize($list['id'])['new'];
         $this->success('',$data);
     }
 
     public function getPrize($rid){
         $list =  Db::name('raprize')->where("r_id='$rid' and status ='normal' ")->order('id desc')->select();
         $new=array();
+        $prize =array();
         foreach ($list as $k=>$v){
             $new[$k]['title']=$v['t_type_name'];
             $new[$k]['price']=$v['price'];
@@ -58,12 +69,142 @@ class Raffle extends Api
                 $new[$k]['price']=$v['price'].$str;
             }
             $new[$k]['image']=$v['image'];
+            $prize[$k]=$v;
+            $prize[$k]['prize']= $new[$k]['price'].' '.$new[$k]['title'];
         }
-        return $new;
+        $data['new']=$new;
+        $data['prize']=$prize;
+        return $data;
     }
 
+    //分享增加抽奖次数
+    public function shareAdd(){
+        $where=$this->getWhere();
+        $list =  Db::name('rashare')->where($where)->find();
+        if(empty($list)){
+            $where['createtime'] =time();
+            Db::name('rashare')->insertGetId($where);
+            $this->success('success');
+        }else{
+            $this->error('今天已分享');
+        }
+    }
 
 
+    //获取积分,抽奖次数
+    public function getPoints(){
+        $where=$this->getWhere();
+        $data['points'] =$this->getUserPoints($where);
+        $data['nums']=$this->getRanums($where);
+        $this->success('success',$data);
 
+    }
+    //获取抽奖次数
+    public function getRanums($where){
+        $num=5;//抽奖次数 初始5次
+        $list =  Db::name('rashare')->where($where)->find();
+        if($list){
+            $num=$num+1;// 分享加1
+        }
+        $nums = Db::name('rafflelog')->where($where)->count();
+        $cnums =$num-$nums;
+        return $cnums;
+    }
+    //获取积分
+    public function getUserPoints($where){
+        $params['token']=$this->request->server('HTTP_TOKEN');
+        $re= make_curl($where,'app-api/user/getUserPoints',$params);
+
+        if($re['code']==200){
+            $point =$re['data']['points'];
+        }else{
+            $this->error('error');
+            return;
+        }
+        return $point;
+    }
+    public function getWhere(){
+        $userinfo =$this->userInfo;
+        $where['uid'] = $userinfo['uid'];
+        $where['website'] = $userinfo['website'];
+        $where['r_id'] = input('rid');
+        if(empty($where['r_id'])){
+            $this->error('活动ID不能为空');
+        }
+        $where['create_day'] =date('Y-m-d');
+        return $where;
+    }
+    //抽奖
+    public function raffle(){
+        $info = $this->getWhere();
+        if(cache($info['uid'])){ //限制抽奖点击频率
+            $this->error('Click too fast');
+        }
+        $ponits = $this->getUserPoints($info);
+        if($ponits<10){//检测抽奖积分是否足够
+            $this->error('Insufficient points!');
+        }
+        $nums = $this->getRanums($info);
+        if($nums<1){//查询抽奖次数
+            $this->error('No lucky draw times!');
+        }
+        cache($info['uid'], 1, 3);
+        $prize_arr = $this->getPrize($info['r_id'])['prize'];
+        // 把奖品id 设置为键名
+        $prize_arr = array_combine(array_column($prize_arr, 'id'), $prize_arr);
+        $arr=array();
+        foreach($prize_arr as $key => $val)
+        {
+            $arr[$val['id']] = $val['probability'];
+        }
+        $rid = get_rand($arr); //根据概率获取奖项id
+        $res['result'] = $prize_arr[$rid]['prize']; //中奖项
+        $data=$prize_arr[$rid];
+        $data['ra_id']=$data['id'];
+        unset($data['id']);
+        unset($data['image']);
+        unset($data['createtime']);
+        unset($data['updatetime']);
+        unset($data['status']);
+        unset($data['prize']);
+        $data['createtime']=time();
+        $data=array_merge($data,$info);
+        $re=false;
+        Db::startTrans();
+        try {
+            $re = Db::name('rafflelog')->insertGetId($data);
+            if($re){//向网站传递抽奖数据,并扣除积分
+                $result = $this->addRafflePrize($info,$data);
+                if($result['code']==200){
+                    Db::commit();
+                    $this->success('success',$res);
+                }else{
+                    $this->error();
+                }
+            }
+        } catch (ValidateException $e) {
+            Db::rollback();
+            $this->error($e->getMessage());
+        } catch (PDOException $e) {
+            Db::rollback();
+            $this->error($e->getMessage());
+        } catch (Exception $e) {
+            Db::rollback();
+            $this->error($e->getMessage());
+        }
+        if ($re) {
+            $this->success('success',$res);
+        } else {
+            $this->error(__('No rows were inserted'));
+        }
+
+    }
+    //传递数据,并扣除积分
+    public function addRafflePrize($info,$data){
+        $url ='app-api/user/addUserPrize';
+        $data['token']=$this->request->server('HTTP_TOKEN');
+        $re= make_curl($info,$url,$data);
+        return $re;
+    }
 
 }

+ 3 - 2
application/extra/site.php

@@ -22,8 +22,7 @@ return array (
     'basic' => '基础配置',
     'email' => '邮件配置',
     'dictionary' => '字典配置',
-    'user' => '会员配置',
-    'example' => '示例分组',
+    'website' => '网站设置',
   ),
   'mail_type' => '1',
   'mail_smtp_host' => 'smtp.qq.com',
@@ -38,4 +37,6 @@ return array (
     'category2' => '分类二',
     'custom' => '自定义',
   ),
+  'westkiss' => 'https://westkissapp.snjon.com/',
+  'alipearlhair' => 'https://alipearlapp.snjon.com/',
 );