Active.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. /**
  6. * 大转盘
  7. */
  8. class Active extends Api
  9. {
  10. protected $noNeedLogin = [];
  11. protected $noNeedRight = ['*'];
  12. public function index(){
  13. echo '123';
  14. }
  15. public function choujiang_fake(){
  16. //信息
  17. $active_id = input('active_id',0);
  18. $active = Db::name('actives')->where(['id'=>$active_id,'is_show'=>1])->find();
  19. if(!$active){
  20. $this->error('不存在的活动');
  21. }
  22. //时间判断
  23. $time= time();
  24. if($time > $active['end_time']){
  25. $this->error('抽奖活动已结束');
  26. }
  27. if($time < $active['start_time']){
  28. $this->error('抽奖活动未开始,请耐心等待');
  29. }
  30. //奖项参数
  31. $money_arr = Db::name('actives_info')->field('id,name,info')->where(['active_id'=>$active['id'],'is_show'=>1])->order('sort asc,id asc')->limit(6)->select();
  32. $key = rand(0,5);
  33. $this->success('success',$money_arr[$key]);
  34. }
  35. /**
  36. * @description 开始抽奖
  37. * @return array
  38. */
  39. public function choujiang()
  40. {
  41. //信息
  42. $active_id = input('active_id',0);
  43. $active = Db::name('actives')->where(['id'=>$active_id,'is_show'=>1])->find();
  44. if(!$active){
  45. $this->error('不存在的活动');
  46. }
  47. //时间判断
  48. $time= time();
  49. if($time > $active['end_time']){
  50. $this->error('抽奖活动已结束');
  51. }
  52. if($time < $active['start_time']){
  53. $this->error('抽奖活动未开始,请耐心等待');
  54. }
  55. //奖项参数
  56. $conf_arr = Db::name('actives_info')->field('id,name,info,rate,number,gift_ids')->where(['active_id'=>$active['id'],'is_show'=>1])->order('sort asc,id asc')->limit(6)->select();
  57. //目前已有奖项
  58. $user_log = Db::name('actives_user_log')->field('active_info_id,count(id) as num')->where(['active_id'=>$active['id']])->group('active_info_id')->select();
  59. $user_log_arr = [];
  60. foreach($user_log as $ulk => $ulv){
  61. $user_log_arr[$ulv['active_info_id']] = $ulv['num'];
  62. }
  63. // dump($user_log_arr);
  64. //概率新数组
  65. $gailv = [];
  66. $bei = 100; //小数变整数
  67. foreach ($conf_arr as $key=>$value)
  68. {
  69. //受限
  70. if($value['number'] > 0){
  71. //已获此奖 && 已获次数 >= 计划数量
  72. if(isset($user_log_arr[$value['id']]) && $user_log_arr[$value['id']] >= $value['number']){
  73. //忽略此次
  74. // dump($user_log_arr[$value['id']]);
  75. // dump($value['number']);
  76. continue;
  77. }
  78. }
  79. $gailv[$value['id']] = $value['rate']*$bei;
  80. }
  81. // dump($gailv);
  82. //奖项新数组
  83. $conf_column = [];
  84. foreach ($conf_arr as $key=>$value)
  85. {
  86. unset($value['rate']);
  87. unset($value['number']);
  88. $conf_column[$value['id']] = $value;
  89. }
  90. // dump($conf_column);
  91. //抽
  92. $rid = $this->getRand($gailv); //根据概率获取奖项id
  93. // dump($rid);exit;
  94. //返回结果
  95. $result = $conf_column[$rid];
  96. Db::startTrans();
  97. //礼物id
  98. $gift_id = 0;
  99. $remark = $result['info'];
  100. if(!empty($result['gift_ids'])){
  101. $gift_ids = explode(',',$result['gift_ids']);
  102. $gift_id = $gift_ids[rand(0,count($gift_ids)-1)];
  103. $gift_info = Db::name('gift')->where('id',$gift_id)->find();
  104. if(!empty($gift_info)){
  105. //加入到背包
  106. $back = [
  107. 'user_id' => $this->auth->id,
  108. 'gift_id' => $gift_id,
  109. 'name' => $gift_info['name'],
  110. 'image' => $gift_info['image'],
  111. 'gif_image' => $gift_info['special'],
  112. 'value' => $gift_info['value'],
  113. 'number' => 1,
  114. 'is_use' => 0,
  115. 'use_time' => 0,
  116. 'get_way' => 1,
  117. 'createtime' => time(),
  118. ];
  119. $gift_back = Db::name('gift_back')->insertGetId($back);
  120. if(!$gift_back){
  121. Db::rollback();
  122. $this->error('加入背包失败');
  123. }
  124. $remark .= ':'.$gift_info['name'];
  125. }else{
  126. $gift_id = 0;
  127. }
  128. }
  129. //插入抽奖记录
  130. $data = [];
  131. $data['user_id'] = $this->auth->id;
  132. $data['active_id'] = $active['id'];
  133. $data['active_info_id'] = $result['id'];
  134. $data['gift_id'] = $gift_id;
  135. $data['name'] = $result['name'];
  136. $data['remark'] = $remark;
  137. $data['createtime'] = $time;
  138. $log_id = Db::name('actives_user_log')->insertGetId($data);
  139. if(!$log_id){
  140. Db::rollback();
  141. $this->error('抽奖记录更新有误');
  142. }
  143. //无论是否中奖。抽奖消耗
  144. $rs_wallet = model('wallet')->lockChangeAccountRemain($this->auth->id,10,'-',0,'大转盘抽奖一次',13,'jewel');
  145. if ($rs_wallet['status'] === false) {
  146. Db::rollback();
  147. $this->error($rs_wallet['msg']);
  148. }
  149. Db::commit();
  150. unset($result['name']);
  151. unset($result['info']);
  152. unset($result['gift_ids']);
  153. $result['remark'] = $remark;
  154. $this->success('success',$result);
  155. }
  156. //概率获得算法
  157. function getRand($proArr) {
  158. //概率数组的总概率精度
  159. $proSum = array_sum($proArr);
  160. $key = rand(1, $proSum);
  161. // echo $key;
  162. $result = 0;
  163. $now = 0;
  164. foreach ($proArr as $k=>$v)
  165. {
  166. $now = $now + $v;
  167. if($key<=$now)
  168. {
  169. $result = $k;
  170. break;
  171. }
  172. }
  173. unset ($proArr);
  174. return $result;
  175. }
  176. /**
  177. * @description 获取大转盘信息
  178. * @return array
  179. * @return_format array info 大转盘基本信息
  180. * @return_format array conf 奖项
  181. */
  182. public function getactive()
  183. {
  184. //信息
  185. $map = [
  186. 'is_show' => 1,
  187. ];
  188. $info = Db::name('actives')->field('id,name,image,start_time,end_time,content')->where($map)->find();
  189. $info = info_domain_image($info,['image']);
  190. //获取奖项
  191. $conf = Db::name('actives_info')->field('id,name,info')->where('active_id',$info['id'])->where('is_show',1)->order('sort asc,id asc')->limit(6)->select();
  192. $rt = [];
  193. $rt['info'] = $info;
  194. $rt['conf'] = $conf;
  195. $rt['jewel'] = model('wallet')->getWallet($this->auth->id,'jewel');
  196. $rt['jewel_price'] = 10;
  197. return $this->success('success',$rt);
  198. }
  199. //本次活动中奖纪录
  200. public function activeuserlog(){
  201. $active_id = input('active_id',0);
  202. $where = [
  203. 'log.active_id' => $active_id,
  204. ];
  205. $my = input('my',0);
  206. if($my){
  207. $where['log.user_id'] = $this->auth->id;
  208. }
  209. $log = Db::name('actives_user_log log')
  210. ->field('log.user_id,log.name,log.remark,log.createtime,user.nickname,user.avatar')
  211. ->join('user','user.id = log.user_id','LEFT')
  212. ->where($where)->order('log.id desc')->autopage()->select();
  213. foreach($log as $key => &$val){
  214. $val['avatar'] = cdnurl($val['avatar']);
  215. // $val['mobile'] = str_replace(substr($val['mobile'],3,5),'****',$val['mobile']);
  216. }
  217. return $this->success('success',$log);
  218. }
  219. }