Raffle.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\model\Jwttoken;
  5. use app\admin\model\Raffle as Raf;
  6. use think\Config;
  7. use think\Db;
  8. use think\Request;
  9. use function fast\e;
  10. /**
  11. * 首页接口
  12. */
  13. class Raffle extends Api
  14. {
  15. protected $noNeedLogin = ['*'];
  16. protected $noNeedRight = ['*'];
  17. protected $userInfo;
  18. /**
  19. * 首页
  20. *
  21. */
  22. public function _initialize()
  23. {
  24. check_cors_request();
  25. check_ip_allowed();
  26. date_default_timezone_set('America/New_York');
  27. $token = $this->request->server('HTTP_TOKEN');
  28. if(empty($token)){
  29. $this->error('请输入Token');
  30. }
  31. $jwt = new Jwttoken();
  32. $info =$jwt->verifyJwt($token);
  33. if($info['status']!=0){
  34. $msg =$info['msg'];
  35. /*if($info['status']==1003){
  36. $this->error($msg,'',-1);
  37. }else{
  38. $this->error($msg,'',-1);
  39. }*/
  40. $this->error($msg,'',-1);
  41. }
  42. $this->userInfo=$info['info'];
  43. parent::_initialize();
  44. }
  45. public function getRaffle()
  46. {
  47. $code = $this->request->post('code','USD');
  48. $userinfo =$this->userInfo;
  49. $web=$userinfo['website'];
  50. $list = Db::name('raffle')->where("website='{$web}' and status ='normal' ")->order('id desc')->find();
  51. $data['id']=$list['id'];
  52. $data['website']=$list['website'];
  53. $data['name']=$list['name'];
  54. $data['image']=$list['image'];
  55. $data['prize']=$this->getPrize($list['id'],$code)['new'];
  56. $this->success('',$data);
  57. }
  58. public function getPrize($rid,$code='USD'){
  59. $list = Db::name('raprize')->where("r_id='$rid' and status ='normal' ")->order('id desc')->select();
  60. $new=array();
  61. $prize =array();
  62. foreach ($list as $k=>$v){
  63. $new[$k]['id']=$v['id'];
  64. $new[$k]['title']=$v['t_type_name'];
  65. $new[$k]['price']=$v['price'];
  66. if($v['t_type_name']=='CASH'||$v['t_type_name']=='Coupon'){
  67. /*$str='$';
  68. $new[$k]['price']=$str.$v['price'];*/
  69. $new[$k]['price']=$this->getCuPrice($code,$v['price']);
  70. }
  71. if($v['t_type_name']=='DiscountCoupon'){
  72. $str='%';
  73. $new[$k]['price']=$v['price'].$str;
  74. }
  75. $new[$k]['image']=$v['image'];
  76. $prize[$k]=$v;
  77. $prize[$k]['prize']= $new[$k]['price'].' '.$new[$k]['title'];
  78. }
  79. $data['new']=$new;
  80. $data['prize']=$prize;
  81. return $data;
  82. }
  83. public function getCuPrice($code,$price){
  84. switch ($code)
  85. {
  86. case 'USD':
  87. $str='$';
  88. break;
  89. case 'AUD':
  90. $str='A$';
  91. $price =$price*1.5;
  92. break;
  93. case 'CAD':
  94. $str='CA$';
  95. $price =$price*1.35;
  96. break;
  97. case 'GBP':
  98. $str='£';
  99. $price =$price*0.81;
  100. break;
  101. case 'EUR':
  102. $str='€';
  103. $price =$price*0.95;
  104. break;
  105. case 'ZAR':
  106. $str='ZAR';
  107. $price =$price*19.5;
  108. break;
  109. default:
  110. $str='$';
  111. }
  112. $prices=$str.$price;
  113. return $prices;
  114. }
  115. //分享增加抽奖次数
  116. public function shareAdd(){
  117. $where=$this->getWhere();
  118. $list = Db::name('rashare')->where($where)->find();
  119. if(empty($list)){
  120. $where['createtime'] =time();
  121. Db::name('rashare')->insertGetId($where);
  122. $this->success('success');
  123. }else{
  124. $this->error('You’ve shared today.');
  125. }
  126. }
  127. //获取积分,抽奖次数
  128. public function getPoints(){
  129. $where=$this->getWhere();
  130. $data['is_share'] =0;
  131. $list = Db::name('rashare')->where($where)->find();
  132. if($list){
  133. $data['is_share'] =1;
  134. }
  135. $data['points'] =$this->getUserPoints($where);
  136. $data['nums']=$this->getRanums($where);
  137. $this->success('success',$data);
  138. }
  139. //获取抽奖次数
  140. public function getRanums($where){
  141. $num=5;//抽奖次数 初始5次
  142. $list = Db::name('rashare')->where($where)->find();
  143. if($list){
  144. $num=$num+1;// 分享加1
  145. }
  146. $nums = Db::name('rafflelog')->where($where)->count();
  147. $cnums =$num-$nums;
  148. $userinfo =$this->userInfo;
  149. if($userinfo['uid']==81012 || $userinfo['uid']==8524){
  150. $cnums=100;
  151. }
  152. return $cnums;
  153. }
  154. //获取积分
  155. public function getUserPoints($where){
  156. $params['token']=$this->request->server('HTTP_TOKEN');
  157. $re= make_curl($where,'app-api/user/getUserPoints',$params);
  158. if($re['code']==200){
  159. $point =$re['data']['points'];
  160. }else{
  161. $this->error('error');
  162. return;
  163. }
  164. return $point;
  165. }
  166. public function getWhere(){
  167. $userinfo =$this->userInfo;
  168. $where['uid'] = $userinfo['uid'];
  169. $where['website'] = $userinfo['website'];
  170. $where['r_id'] = input('rid');
  171. if(empty($where['r_id'])){
  172. $this->error('活动ID不能为空');
  173. }
  174. //洛杉矶时间
  175. date_default_timezone_set("America/Los_Angeles");
  176. $where['create_day'] =date('Y-m-d');
  177. return $where;
  178. }
  179. //抽奖
  180. public function raffle(){
  181. $info = $this->getWhere();
  182. if(cache($info['uid'])){ //限制抽奖点击频率
  183. $this->error('Click too fast');
  184. }
  185. $code = $this->request->post('code','USD');
  186. $ponits = $this->getUserPoints($info);
  187. if($ponits<10){//检测抽奖积分是否足够
  188. $this->error('Insufficient points!');
  189. }
  190. $nums = $this->getRanums($info);
  191. if($nums<1){//查询抽奖次数
  192. $this->error('No lucky draw times!');
  193. }
  194. cache($info['uid'], 1, 3);
  195. $prize_arr = $this->getPrize($info['r_id'],$code)['prize'];
  196. // 把奖品id 设置为键名
  197. $prize_arr = array_combine(array_column($prize_arr, 'id'), $prize_arr);
  198. $arr=array();
  199. foreach($prize_arr as $key => $val)
  200. {
  201. $arr[$val['id']] = $val['probability'];
  202. }
  203. $rid = get_rand($arr); //根据概率获取奖项id
  204. $res['id'] = $prize_arr[$rid]['id']; //中奖ID
  205. $res['result'] = $prize_arr[$rid]['prize']; //中奖项
  206. $res['code'] = $prize_arr[$rid]['description']; //中奖code
  207. $data=$prize_arr[$rid];
  208. $data['ra_id']=$data['id'];
  209. unset($data['id']);
  210. unset($data['image']);
  211. unset($data['createtime']);
  212. unset($data['updatetime']);
  213. unset($data['status']);
  214. unset($data['prize']);
  215. $data['createtime']=time();
  216. date_default_timezone_set("America/Los_Angeles");
  217. $data['create_time'] =date('Y-m-d H:i:s');
  218. $data=array_merge($data,$info);
  219. $re=false;
  220. Db::startTrans();
  221. try {
  222. $re = Db::name('rafflelog')->insertGetId($data);
  223. if($re){//向网站传递抽奖数据,并扣除积分
  224. $result = $this->addRafflePrize($info,$data);
  225. if($result['code']==200){
  226. if($result['data']){
  227. $res['code']=$result['data']['code'];
  228. }
  229. Db::commit();
  230. $this->success('success',$res);
  231. }else{
  232. $this->error();
  233. }
  234. }
  235. } catch (ValidateException $e) {
  236. Db::rollback();
  237. $this->error($e->getMessage());
  238. } catch (PDOException $e) {
  239. Db::rollback();
  240. $this->error($e->getMessage());
  241. } catch (Exception $e) {
  242. Db::rollback();
  243. $this->error($e->getMessage());
  244. }
  245. if ($re) {
  246. $this->success('success',$res);
  247. } else {
  248. $this->error(__('No rows were inserted'));
  249. }
  250. }
  251. //传递数据,并扣除积分
  252. public function addRafflePrize($info,$data){
  253. $url ='app-api/user/addUserPrize';
  254. $data['token']=$this->request->server('HTTP_TOKEN');
  255. $re= make_curl($info,$url,$data);
  256. return $re;
  257. }
  258. //获取抽奖记录
  259. public function getRaffleList(){
  260. $info = $this->getWhere();
  261. $prize_arr = $this->getPrize($info['r_id'])['new'];
  262. $data=array();
  263. $alphabet = "abcdefghijklmnopqrstuvwxyz";
  264. $alphabet=strtoupper($alphabet);
  265. for($i=0;$i<20;$i++){
  266. $alphabet = str_shuffle($alphabet);
  267. $result = substr($alphabet, 0, 1);
  268. $nums = mt_rand(0,count($prize_arr)-1);
  269. $prize = $prize_arr[$nums];
  270. $data[$i]['name']=$result."*****";
  271. $data[$i]['prize']=$prize['price']." ".$prize['title'];
  272. }
  273. $this->success('success',$data);
  274. }
  275. }