Raffle.php 6.3 KB

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