Active.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\library\Tenim;
  5. use think\Db;
  6. /**
  7. * 活动
  8. */
  9. class Active extends Api
  10. {
  11. // 无需登录的接口,*表示全部
  12. protected $noNeedLogin = [''];
  13. // 无需鉴权的接口,*表示全部
  14. protected $noNeedRight = ['*'];
  15. //首页
  16. public function index(){
  17. $result = [];
  18. //开奖时间
  19. $result['kaijiang_time'] = date('H:i',strtotime(config('site.active_kaijiang_time')));
  20. //奖池金额
  21. $result['jiangchi'] = $this->jiangchi();
  22. //我今天已经领取的气泡,数字
  23. $result['my_number'] = $this->get_my_number();
  24. //我今天的应得气泡数量,扣除已经领掉的,最大4个
  25. $qipao_count = $this->get_qipao_count() - count($result['my_number']);
  26. $qipao_count = $qipao_count > 4 ? 4 : $qipao_count ;
  27. $result['qipao_count'] = $qipao_count;
  28. $this->success('success',$result);
  29. }
  30. //收集一个气泡
  31. public function open_qipao(){
  32. //我今天的应得气泡数量
  33. $qipao_count = $this->get_qipao_count();
  34. //我今天打开的气泡数量
  35. $my_count = Db::name('active_user_number')->whereTime('createtime','today')->where('user_id',$this->auth->id)->count();
  36. if($my_count + 1 > $qipao_count){
  37. $this->error('可收集气泡不足');
  38. }
  39. $week = [
  40. 1 => 4,
  41. 2 => 4,
  42. 3 => 4,
  43. 4 => 4,
  44. 5 => 4,
  45. 6 => 4,
  46. 7 => 10,
  47. ];
  48. $today_max = $week[date('N')];
  49. if($my_count + 1 > $today_max){
  50. $this->error('今天最多能收集'.$today_max.'个气泡,已到上限');
  51. }
  52. $data = [
  53. 'user_id' => $this->auth->id,
  54. 'number' => $this->get_rand_number(),
  55. 'createtime' => time(),
  56. ];
  57. Db::name('active_user_number')->insertGetId($data);
  58. $this->success('已收集',$this->get_my_number());
  59. }
  60. //今日开奖结果
  61. public function active_result(){}
  62. //我今天已经领取的气泡,数字
  63. private function get_my_number(){
  64. $list = Db::name('active_user_number')->whereTime('createtime','today')->where('user_id',$this->auth->id)->order('id asc')->select();
  65. return $list;
  66. }
  67. //我今天的应得气泡数量
  68. private function get_qipao_count(){
  69. $xiaofei_log_type = '11,12,13,53,59,71,81';//消费金币的log
  70. $xiaofei_total = Db::name('user_gold_log')->whereTime('createtime','today')->where('user_id',$this->auth->id)->where('log_type','IN',$xiaofei_log_type)
  71. ->sum('change_value');
  72. $xiaofei_total = abs($xiaofei_total);
  73. $qipao_value = config('site.active_qipao_value'); //气泡价格
  74. if($qipao_value <= 0){
  75. return 0; //0不能做除数
  76. }
  77. $qipao = bcdiv($xiaofei_total,$qipao_value); //金币没有小数点
  78. return intval($qipao); //退一法
  79. }
  80. //随机一个中奖号码
  81. private function get_rand_number(){
  82. $min = config('site.active_number_min');
  83. $max = config('site.active_number_max');
  84. return rand($min,$max);
  85. }
  86. //奖池金额
  87. private function jiangchi(){
  88. $xiaofei_log_type = '11,12,13,53,59,71,81';//消费金币的log
  89. //奖池为昨日平台消费总金额百分比
  90. $starttime = strtotime(date('Y-m-d')) - 86400;
  91. $endtime = $starttime + 86399;
  92. $xiaofei_total = Db::name('user_gold_log')->where('createtime','BETWEEN',[$starttime,$endtime])->where('log_type','IN',$xiaofei_log_type)->sum('change_value');
  93. $xiaofei_total = abs($xiaofei_total);
  94. $jiangchi_bili = config('site.active_jiangchi_bili');
  95. $jiangchi = bcdiv(bcmul($xiaofei_total,$jiangchi_bili,0),100,0); //金币没有小数点
  96. return $jiangchi;
  97. }
  98. }