LotteryController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 老猫 <thinkcmf@126.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\love\controller;
  12. use app\common\Wechat;
  13. use app\love\model\LotteryLogModel;
  14. use app\love\model\LotteryPrizeModel;
  15. use app\love\model\UserModel;
  16. use cmf\controller\HomeBaseController;
  17. use think\facade\Request;
  18. class LotteryController extends HomeBaseController
  19. {
  20. public function initialize()
  21. {
  22. parent::initialize();
  23. //分享页面
  24. $wx_api_sign = Wechat::instance()->getJsSign(Request::url(true));
  25. $this->assign('domain', $this->request->domain());
  26. $this->assign('wx_api_sign', json_encode($wx_api_sign));
  27. }
  28. //抽奖页
  29. public function index()
  30. {
  31. //验证
  32. $user_id = cmf_get_current_user_id();
  33. $user = UserModel::get($user_id);
  34. if ($user['is_marry'] == 2 || $user['sex'] == 1) {
  35. $this->redirect('/');
  36. }
  37. $prize = LotteryPrizeModel::all();
  38. $ids = [];
  39. $restaraunts = [];
  40. $colors = [];
  41. $images = [];
  42. foreach ($prize as $v) {
  43. $ids[] = $v->id;
  44. $restaraunts[] = $v->name;
  45. $colors[] = $v->bg;
  46. $images[] = cmf_get_image_url($v->image);
  47. }
  48. $this->assign('prize', $prize);
  49. $this->assign('ids', $ids);
  50. $this->assign('restaraunts', $restaraunts);
  51. $this->assign('colors', $colors);
  52. $this->assign('images', $images);
  53. return $this->fetch();
  54. }
  55. /**
  56. * 抽奖
  57. */
  58. public function lotteryPost()
  59. {
  60. //抽奖次数
  61. $user_id = cmf_get_current_user_id();
  62. $user = UserModel::get($user_id);
  63. //已婚
  64. if ($user['is_marry'] == 2) {
  65. $this->error('已结婚的才能抽奖');
  66. }
  67. if ($user['sex'] == 1) {
  68. $this->error('女生才能抽奖');
  69. }
  70. $lottery = LotteryLogModel::where('user_id', $user_id)->find();
  71. if (!empty($lottery)) {
  72. $this->error('一个人只能抽一次奖',url('log'));
  73. }
  74. //奖品列表
  75. $prize_arr = LotteryPrizeModel::where('number', '>', 0)->select();
  76. $arr = [];
  77. foreach ($prize_arr as $key => $val) {
  78. $arr[$val['id']] = $val['odds'] * 100;
  79. }
  80. $rid = $this->_get_rand($arr); //根据概率获取奖项id
  81. //增加奖品
  82. $prize = LotteryPrizeModel::where('id', $rid)->find();
  83. $prize->number--;
  84. $prize->save();
  85. LotteryLogModel::create([
  86. 'user_id' => $user_id,
  87. 'prize_id' => $rid,
  88. 'create_time' => time(),
  89. 'prize_name' => $prize['name'],
  90. ]);
  91. $this->success('恭喜你,获得' . $prize['name'],'',['id' => $rid]);
  92. }
  93. /**
  94. * 中奖记录
  95. */
  96. public function log()
  97. {
  98. //奖品
  99. $user_id = cmf_get_current_user_id();
  100. $lottery = LotteryLogModel::with('lottery')->where('user_id',$user_id)->find();
  101. if (empty($lottery) || $lottery['status'] == 1) {
  102. $this->redirect('/');
  103. }
  104. $lottery['lottery']['image'] = cmf_get_image_url($lottery['lottery']['image']);
  105. $this->assign('lottery',$lottery);
  106. return $this->fetch();
  107. }
  108. /**
  109. * 获取奖品
  110. */
  111. private function _get_rand($proArr)
  112. {
  113. $result = '';
  114. //概率数组的总概率精度
  115. $proSum = array_sum($proArr);
  116. //概率数组循环
  117. foreach ($proArr as $key => $proCur) {
  118. $randNum = mt_rand(1, $proSum);
  119. if ($randNum <= $proCur) {
  120. $result = $key;
  121. break;
  122. } else {
  123. $proSum -= $proCur;
  124. }
  125. }
  126. return $result;
  127. }
  128. }