Raffle.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. $this->error($msg);
  36. }
  37. $this->userInfo=$info['info'];
  38. parent::_initialize();
  39. }
  40. public function getRaffle()
  41. {
  42. $userinfo =$this->userInfo;
  43. $web=$userinfo['website'];
  44. $list = Db::name('raffle')->where("website='{$web}' and status ='normal' ")->order('id desc')->find();
  45. $data['id']=$list['id'];
  46. $data['website']=$list['website'];
  47. $data['name']=$list['name'];
  48. $data['image']=$list['image'];
  49. $data['prize']=$this->getPrize($list['id'])['new'];
  50. $this->success('',$data);
  51. }
  52. public function getPrize($rid){
  53. $list = Db::name('raprize')->where("r_id='$rid' and status ='normal' ")->order('id desc')->select();
  54. $new=array();
  55. $prize =array();
  56. foreach ($list as $k=>$v){
  57. $new[$k]['id']=$v['id'];
  58. $new[$k]['title']=$v['t_type_name'];
  59. $new[$k]['price']=$v['price'];
  60. if($v['t_type_name']=='CASH'||$v['t_type_name']=='Coupon'){
  61. $str='$';
  62. $new[$k]['price']=$str.$v['price'];
  63. }
  64. if($v['t_type_name']=='DiscountCoupon'){
  65. $str='%';
  66. $new[$k]['price']=$v['price'].$str;
  67. }
  68. $new[$k]['image']=$v['image'];
  69. $prize[$k]=$v;
  70. $prize[$k]['prize']= $new[$k]['price'].' '.$new[$k]['title'];
  71. }
  72. $data['new']=$new;
  73. $data['prize']=$prize;
  74. return $data;
  75. }
  76. //分享增加抽奖次数
  77. public function shareAdd(){
  78. $where=$this->getWhere();
  79. $list = Db::name('rashare')->where($where)->find();
  80. if(empty($list)){
  81. $where['createtime'] =time();
  82. Db::name('rashare')->insertGetId($where);
  83. $this->success('success');
  84. }else{
  85. $this->error('You’ve shared today.');
  86. }
  87. }
  88. //获取积分,抽奖次数
  89. public function getPoints(){
  90. $where=$this->getWhere();
  91. $data['is_share'] =0;
  92. $list = Db::name('rashare')->where($where)->find();
  93. if($list){
  94. $data['is_share'] =1;
  95. }
  96. $data['points'] =$this->getUserPoints($where);
  97. $data['nums']=$this->getRanums($where);
  98. $this->success('success',$data);
  99. }
  100. //获取抽奖次数
  101. public function getRanums($where){
  102. $num=5;//抽奖次数 初始5次
  103. $list = Db::name('rashare')->where($where)->find();
  104. if($list){
  105. $num=$num+1;// 分享加1
  106. }
  107. $nums = Db::name('rafflelog')->where($where)->count();
  108. $cnums =$num-$nums;
  109. $userinfo =$this->userInfo;
  110. if($userinfo['uid']==81012 || $userinfo['uid']==8524){
  111. $cnums=100;
  112. }
  113. return $cnums;
  114. }
  115. //获取积分
  116. public function getUserPoints($where){
  117. $params['token']=$this->request->server('HTTP_TOKEN');
  118. $re= make_curl($where,'app-api/user/getUserPoints',$params);
  119. if($re['code']==200){
  120. $point =$re['data']['points'];
  121. }else{
  122. $this->error('error');
  123. return;
  124. }
  125. return $point;
  126. }
  127. public function getWhere(){
  128. $userinfo =$this->userInfo;
  129. $where['uid'] = $userinfo['uid'];
  130. $where['website'] = $userinfo['website'];
  131. $where['r_id'] = input('rid');
  132. if(empty($where['r_id'])){
  133. $this->error('活动ID不能为空');
  134. }
  135. $where['create_day'] =date('Y-m-d');
  136. return $where;
  137. }
  138. //抽奖
  139. public function raffle(){
  140. $info = $this->getWhere();
  141. if(cache($info['uid'])){ //限制抽奖点击频率
  142. $this->error('Click too fast');
  143. }
  144. $ponits = $this->getUserPoints($info);
  145. if($ponits<10){//检测抽奖积分是否足够
  146. $this->error('Insufficient points!');
  147. }
  148. $nums = $this->getRanums($info);
  149. if($nums<1){//查询抽奖次数
  150. $this->error('No lucky draw times!');
  151. }
  152. cache($info['uid'], 1, 3);
  153. $prize_arr = $this->getPrize($info['r_id'])['prize'];
  154. // 把奖品id 设置为键名
  155. $prize_arr = array_combine(array_column($prize_arr, 'id'), $prize_arr);
  156. $arr=array();
  157. foreach($prize_arr as $key => $val)
  158. {
  159. $arr[$val['id']] = $val['probability'];
  160. }
  161. $rid = get_rand($arr); //根据概率获取奖项id
  162. $res['id'] = $prize_arr[$rid]['id']; //中奖ID
  163. $res['result'] = $prize_arr[$rid]['prize']; //中奖项
  164. $res['code'] = $prize_arr[$rid]['description']; //中奖code
  165. $data=$prize_arr[$rid];
  166. $data['ra_id']=$data['id'];
  167. unset($data['id']);
  168. unset($data['image']);
  169. unset($data['createtime']);
  170. unset($data['updatetime']);
  171. unset($data['status']);
  172. unset($data['prize']);
  173. $data['createtime']=time();
  174. $data=array_merge($data,$info);
  175. $re=false;
  176. Db::startTrans();
  177. try {
  178. $re = Db::name('rafflelog')->insertGetId($data);
  179. if($re){//向网站传递抽奖数据,并扣除积分
  180. $result = $this->addRafflePrize($info,$data);
  181. if($result['code']==200){
  182. Db::commit();
  183. $this->success('success',$res);
  184. }else{
  185. $this->error();
  186. }
  187. }
  188. } catch (ValidateException $e) {
  189. Db::rollback();
  190. $this->error($e->getMessage());
  191. } catch (PDOException $e) {
  192. Db::rollback();
  193. $this->error($e->getMessage());
  194. } catch (Exception $e) {
  195. Db::rollback();
  196. $this->error($e->getMessage());
  197. }
  198. if ($re) {
  199. $this->success('success',$res);
  200. } else {
  201. $this->error(__('No rows were inserted'));
  202. }
  203. }
  204. //传递数据,并扣除积分
  205. public function addRafflePrize($info,$data){
  206. $url ='app-api/user/addUserPrize';
  207. $data['token']=$this->request->server('HTTP_TOKEN');
  208. $re= make_curl($info,$url,$data);
  209. return $re;
  210. }
  211. //获取抽奖记录
  212. public function getRaffleList(){
  213. $info = $this->getWhere();
  214. $prize_arr = $this->getPrize($info['r_id'])['new'];
  215. $data=array();
  216. $alphabet = "abcdefghijklmnopqrstuvwxyz";
  217. for($i=0;$i<20;$i++){
  218. $alphabet = str_shuffle($alphabet);
  219. $result = substr($alphabet, 0, 1);
  220. $nums = mt_rand(0,count($prize_arr)-1);
  221. $prize = $prize_arr[$nums];
  222. $data[$i]['name']=$result."*****";
  223. $data[$i]['prize']=$prize['price']." ".$prize['title'];
  224. }
  225. $this->success('success',$data);
  226. }
  227. }