ApiController.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: guanxl
  5. * Date: 2018/3/25
  6. * Time: 15:30
  7. */
  8. namespace App\Http\Controllers;
  9. use DB;
  10. use App\Models\Lottery;
  11. use App\Models\LotteryBoxs;
  12. use App\Models\Prize;
  13. use App\Services\BrainstormingService;
  14. use App\Services\GameTimeService;
  15. use App\Services\UserService;
  16. use Illuminate\Http\Request;
  17. use Illuminate\Support\Facades\Log;
  18. use Illuminate\Support\Facades\Redis;
  19. use Qiniu\Auth;
  20. use Qiniu\Storage\UploadManager;
  21. class ApiController extends Controller
  22. {
  23. public function lottery($id)
  24. {
  25. $userInfo = UserService::getInstance()->getUser($this->userId);
  26. $brainstorming = BrainstormingService::getInstance()->get($this->activityId);
  27. if($brainstorming["is_temp_stop"]==1){
  28. return response()->json([
  29. "error" => 1000,
  30. "message" => "活动已暂停!"
  31. ]);
  32. }
  33. if($brainstorming["is_open_prize"]==0){
  34. return response()->json([
  35. "error" => 1002,
  36. "message" => "本次活动未设置抽奖!"
  37. ]);
  38. }
  39. $lottery_level = intval($id);
  40. if(GameTimeService::getInstance()->isReachEndTime($this->activityId)){
  41. return response()->json([
  42. "error" => 1000,
  43. "message" => "活动已经结束!"
  44. ]);
  45. }
  46. if (empty($userInfo)) {
  47. return response()->json([
  48. "error" => 1000,
  49. "message" => config("errMsg.permission_denied")
  50. ]);
  51. }
  52. $knowledge_money = $userInfo["knowledge_money"];
  53. $boxs = LotteryBoxs::where("activity_id",$this->activityId)->where("status",1)->orderBy("lottery_level")->get();
  54. $lottery = [];
  55. $curHasChance = false;
  56. foreach ($boxs as $box){
  57. $has_chance = $knowledge_money>=$box["open_rule"]? true:false;
  58. if($box["lottery_level"]==$lottery_level&&$has_chance){
  59. $curHasChance = true;
  60. }
  61. array_push($lottery,[
  62. "lottery_level" => $box["lottery_level"],
  63. "lottery_name" => $box["lottery_name"],
  64. "lottery_icon" => $box["lottery_icon"],
  65. "has_chance" => $has_chance,
  66. 'is_lottery' => false,
  67. "need_km" => $box["open_rule"]
  68. ]);
  69. }
  70. if (!$curHasChance) {
  71. return response()->json([
  72. "error" => 1000,
  73. "message" => "没有本等级开箱的权限"
  74. ]);
  75. }
  76. /*$lottery_level_1 = $brainstorming["copper_box_km"]==0? 100:$brainstorming["copper_box_km"];
  77. $lottery_level_2 = $brainstorming["silver_box_km"]==0? 500:$brainstorming["silver_box_km"];
  78. $lottery_level_3 = $brainstorming["gold_box_km"]==0? 1000:$brainstorming["gold_box_km"];
  79. $has_chance_1 = $knowledge_money >= $lottery_level_1 ? true : false;
  80. $has_chance_2 = $knowledge_money >= $lottery_level_2 ? true : false;
  81. $has_chance_3 = $knowledge_money >= $lottery_level_3 ? true : false;*/
  82. $lotterys = Lottery::where('user_id', $this->userId)
  83. ->orderBy('lottery_time', 'desc')
  84. ->get();
  85. if ($lotterys) {
  86. foreach ($lotterys as $lot) {
  87. if ($lot->lottery_level == $lottery_level) {
  88. return response()->json([
  89. "error" => 1000,
  90. "message" => "该等级宝箱你已经开启过"
  91. ]);
  92. }
  93. }
  94. }
  95. $prize = Prize::where("status", 1) ->where("activity_id",$this->activityId)-> where("lottery_level",$lottery_level)->get();
  96. if (empty($prize)) {
  97. return response()->json([
  98. "error" => 1000,
  99. "message" => "该级别没有设置任何奖项"
  100. ]);
  101. }
  102. $date = date("Ymd");
  103. $lottery_data = new Lottery;
  104. $lottery_data->user_id = $this->userId;
  105. $lottery_data->lottery_level = $lottery_level;
  106. $lottery_data->prize_id = 0;
  107. $lottery_data->prize_name = "谢谢参与";
  108. $lottery_data->prize_image_url = env("NO_PRIZE_IMG");
  109. $lottery_data->corp_id = $this->corpId;
  110. $lottery_data->activity_id = $this->activityId;
  111. $lottery_data->lottery_time = date("Y-m-d H:i:s");
  112. $data['prize_id'] = 0;
  113. $data['prize_name'] = "谢谢参与";
  114. $data['prize_image_url'] = env("NO_PRIZE_IMG");
  115. $min = 0;
  116. $probability = array();
  117. foreach ($prize as $p){
  118. $max = $p->prize_weights;
  119. $value = array( 'min' => $min,
  120. 'max' => $max,
  121. 'prize_id' => $p->prize_id,
  122. 'prize_name' => $p->prize_name,
  123. 'prize_image_url' => $p->prize_image_url,
  124. 'prize_day_rule' => $p->prize_day_rule,
  125. 'prize_count' => $p->prize_count,
  126. 'prize_grant_count' => $p->prize_grant_count);
  127. $probability[] = $value;
  128. }
  129. shuffle($probability);
  130. $rand = rand(1, 1000);
  131. foreach ($probability as $pr){
  132. $redisKey = 'bs_prize_count:'.$pr['prize_id'].':'.$date;
  133. if(!Redis::exists($redisKey)){
  134. $count = $pr["prize_day_rule"];
  135. Redis::set($redisKey,$count);
  136. }else{
  137. $count = Redis::get($redisKey);
  138. }
  139. if ($rand > $pr['min'] && $rand <= $pr['max'] && $pr['prize_grant_count'] < $pr['prize_count'] && $count > 0){
  140. Redis::incrby($redisKey,-1);
  141. Prize::where('prize_id',$pr['prize_id'])->update(["prize_grant_count" => $pr['prize_grant_count']+1]);
  142. $lottery_data->prize_id = $pr['prize_id'];
  143. $lottery_data->prize_name = $pr['prize_name'];
  144. $lottery_data->prize_image_url = $pr['prize_image_url'];
  145. $data['prize_id'] = $pr['prize_id'];
  146. $data['prize_name'] = $pr['prize_name'];
  147. $data['prize_image_url'] = $pr['prize_image_url'];
  148. break;
  149. }
  150. }
  151. $lottery_data->save();
  152. return response()->json([
  153. "error" => "0",
  154. "data" => $data
  155. ]);
  156. }
  157. public function lotteryResult(Request $request)
  158. {
  159. $userInfo = UserService::getInstance()->getUser($this->userId);
  160. //$brainstorming = BrainstormingService::getInstance()->get($this->activityId);
  161. if ($userInfo) {
  162. $knowledge_money = $userInfo["knowledge_money"];
  163. //获取宝箱参数
  164. $boxs = LotteryBoxs::where("activity_id",$this->activityId)->where("status",1)->orderBy("lottery_level")->get();
  165. /*$lottery_level_1 = $brainstorming["copper_box_km"]==0? 100:$brainstorming["copper_box_km"];
  166. $lottery_level_2 = $brainstorming["silver_box_km"]==0? 500:$brainstorming["silver_box_km"];
  167. $lottery_level_3 = $brainstorming["gold_box_km"]==0? 1000:$brainstorming["gold_box_km"];
  168. $has_chance_1 = $knowledge_money >= $lottery_level_1 ? true:false;
  169. $has_chance_2 = $knowledge_money >= $lottery_level_2 ? true:false;
  170. $has_chance_3 = $knowledge_money >= $lottery_level_3 ? true:false;*/
  171. $lottery = [];
  172. foreach ($boxs as $box){
  173. $has_chance = $knowledge_money>=$box["open_rule"]? true:false;
  174. array_push($lottery,[
  175. "lottery_level" => $box["lottery_level"],
  176. "lottery_name" => $box["lottery_name"],
  177. "lottery_icon" => $box["lottery_icon"],
  178. "has_chance" => $has_chance,
  179. 'is_lottery' => false,
  180. "need_km" => $box["open_rule"]
  181. ]);
  182. }
  183. $guid = $userInfo["guid"];
  184. $lotterys = DB::table("bs_user_lotterys")
  185. -> leftJoin("bs_users","bs_user_lotterys.user_id","=","bs_users.user_id")
  186. -> where("bs_users.guid","=",$guid)
  187. -> where("bs_user_lotterys.activity_id",">","28")
  188. -> orderBy("bs_user_lotterys.lottery_time","desc")
  189. -> get();
  190. $cur_lotterys = Lottery::where('user_id',$this->userId)
  191. ->orderBy('lottery_time','desc')
  192. ->get();
  193. if ($lotterys){
  194. /*$lottery = array(
  195. array("lottery_level" => '1',"has_chance" => $has_chance_1,'is_lottery' => false,"need_km" => $lottery_level_1),
  196. array("lottery_level" => '2',"has_chance" => $has_chance_2,'is_lottery' => false,"need_km" => $lottery_level_2),
  197. array("lottery_level" => '3',"has_chance" => $has_chance_3,'is_lottery' => false,"need_km" => $lottery_level_3)
  198. );*/
  199. $prize = array();
  200. foreach ($lotterys as $lot){
  201. $lotteryName = "";
  202. foreach ($lottery as $lot_array){
  203. if($lot->lottery_level == $lot_array['lottery_level'])
  204. {
  205. $lotteryName = $lot_array["lottery_name"];
  206. }
  207. }
  208. $prize[] = [
  209. 'prize_id'=>$lot->prize_id,
  210. 'prize_name' => $lot->prize_name,
  211. 'prize_image_url' => $lot->prize_image_url,
  212. 'lottery_level' => $lot->lottery_level,
  213. 'lottery_name' => $lotteryName];
  214. }
  215. foreach ($cur_lotterys as $clot) {
  216. foreach ($lottery as &$clot_array){
  217. if($clot->lottery_level == $clot_array['lottery_level'])
  218. {
  219. $clot_array['has_chance'] = false;
  220. $clot_array['is_lottery'] = true;
  221. }
  222. }
  223. }
  224. $resule = array('error' => 0, 'data' =>array( 'lottery' => $lottery,'prize' => $prize));
  225. return \response()->json($resule);
  226. }else{
  227. return \response()->json([
  228. "error" => 0,
  229. "data" => [
  230. "lottery" => $lottery,
  231. "prize" => [
  232. "status" => 0
  233. ]
  234. ]
  235. ]);
  236. }
  237. } else {
  238. return response()->json([
  239. "error" => 401,
  240. "message" => config("errMsg.permission_denied")
  241. ]);
  242. }
  243. }
  244. }