|
@@ -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;
|
|
|
+ }
|
|
|
|
|
|
}
|