123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- <?php
- 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;
- /**
- * 首页接口
- */
- class Raffle extends Api
- {
- protected $noNeedLogin = ['*'];
- protected $noNeedRight = ['*'];
- protected $userInfo;
- /**
- * 首页
- *
- */
- public function _initialize()
- {
- date_default_timezone_set('America/New_York');
- $token = $this->request->server('HTTP_TOKEN');
- if(empty($token)){
- $this->error('请输入Token');
- }
- $jwt = new Jwttoken();
- $info =$jwt->verifyJwt($token);
- if($info['status']!=0){
- $msg =$info['msg'];
- $this->error($msg);
- }
- $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'])['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'];
- if($v['t_type_name']=='CASH'||$v['t_type_name']=='Coupon'){
- $str='$';
- $new[$k]['price']=$str.$v['price'];
- }
- if($v['t_type_name']=='DiscountCoupon'){
- $str='%';
- $new[$k]['price']=$v['price'].$str;
- }
- $new[$k]['image']=$v['image'];
- $prize[$k]=$v;
- $prize[$k]['prize']= $new[$k]['price'].' '.$new[$k]['title'];
- }
- $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;
- }
- }
|