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