123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
- <?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()
- {
- check_cors_request();
- check_ip_allowed();
- 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'];
- /*if($info['status']==1003){
- $this->error($msg,'',-1);
- }else{
- $this->error($msg,'',-1);
- }*/
- $this->error($msg,'',-1);
- }
- $this->userInfo=$info['info'];
- parent::_initialize();
- }
- public function getRaffle()
- {
- $code = $this->request->post('code','USD');
- $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'],$code)['new'];
- $this->success('',$data);
- }
- public function getPrize($rid,$code='USD'){
- $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]['id']=$v['id'];
- $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'];*/
- $new[$k]['price']=$this->getCuPrice($code,$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 getCuPrice($code,$price){
- switch ($code)
- {
- case 'USD':
- $str='$';
- break;
- case 'AUD':
- $str='A$';
- $price =$price*1.5;
- break;
- case 'CAD':
- $str='CA$';
- $price =$price*1.35;
- break;
- case 'GBP':
- $str='£';
- $price =$price*0.81;
- break;
- case 'EUR':
- $str='€';
- $price =$price*0.95;
- break;
- case 'ZAR':
- $str='ZAR';
- $price =$price*19.5;
- break;
- default:
- $str='$';
- }
- $prices=$str.$price;
- return $prices;
- }
- //分享增加抽奖次数
- 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('You’ve shared today.');
- }
- }
- //获取积分,抽奖次数
- public function getPoints(){
- $where=$this->getWhere();
- $data['is_share'] =0;
- $list = Db::name('rashare')->where($where)->find();
- if($list){
- $data['is_share'] =1;
- }
- $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;
- $userinfo =$this->userInfo;
- if($userinfo['uid']==81012 || $userinfo['uid']==8524){
- $cnums=100;
- }
- 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不能为空');
- }
- //洛杉矶时间
- date_default_timezone_set("America/Los_Angeles");
- $where['create_day'] =date('Y-m-d');
- return $where;
- }
- //抽奖
- public function raffle(){
- $info = $this->getWhere();
- if(cache($info['uid'])){ //限制抽奖点击频率
- $this->error('Click too fast');
- }
- $code = $this->request->post('code','USD');
- $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'],$code)['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['id'] = $prize_arr[$rid]['id']; //中奖ID
- $res['result'] = $prize_arr[$rid]['prize']; //中奖项
- $res['code'] = $prize_arr[$rid]['description']; //中奖code
- $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();
- date_default_timezone_set("America/Los_Angeles");
- $data['create_time'] =date('Y-m-d H:i:s');
- $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){
- if($result['data']){
- $res['code']=$result['data']['code'];
- }
- 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;
- }
- //获取抽奖记录
- public function getRaffleList(){
- $info = $this->getWhere();
- $prize_arr = $this->getPrize($info['r_id'])['new'];
- $data=array();
- $alphabet = "abcdefghijklmnopqrstuvwxyz";
- $alphabet=strtoupper($alphabet);
- for($i=0;$i<20;$i++){
- $alphabet = str_shuffle($alphabet);
- $result = substr($alphabet, 0, 1);
- $nums = mt_rand(0,count($prize_arr)-1);
- $prize = $prize_arr[$nums];
- $data[$i]['name']=$result."*****";
- $data[$i]['prize']=$prize['price']." ".$prize['title'];
- }
- $this->success('success',$data);
- }
- }
|