Raffle.php 7.6 KB

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