all(),[ 'name' => 'required|string', 'phone' => 'required|string', 'arrive_at' => 'required|date_format:Y-m-d H:i:s', 'leave_at' => 'required|date_format:Y-m-d H:i:s', 'arrive_flight' => 'nullable|string', 'leave_flight' => 'nullable|string', 'cloth_size' => 'nullable|string', ]); if ($validator->fails()) { return self::returnValue($validator->getMessageBag(), 100); } $record = new KxSignUpRecords(); $record->name = trim($request->input('name')); $record->phone = trim($request->input('phone')); $record->arrive_at = $request->input('arrive_at'); $record->leave_at = $request->input('leave_at'); //$record->arrive_type = $request->input('arrive_type'); //$record->leave_type = $request->input('leave_type'); $record->arrive_flight = trim($request->input('arrive_flight')); $record->leave_flight = trim($request->input('leave_flight')); $record->cloth_size = trim($request->input('cloth_size')); //$record->note = trim($request->input('note')); $res = $record->save(); return self::returnValue($res); } /** * 报名导出 */ public function exportRecords() { $list = KxSignUpRecords::where('enable', 1) ->orderBy('id', 'desc') ->get() ->toArray(); $data = array(); $data[] = ['姓名', '手机号', '到京时间', '到京航班/高铁', '离京时间', '离京航班/高铁', '服装尺寸', '填写时间']; foreach($list as $val) { $data[] = [ $val['name'], $val['phone'], $val['arrive_at'], $val['arrive_flight'], $val['leave_at'], $val['leave_flight'], $val['cloth_size'], $val['created_at'] ]; } return Excel::download(new UserExport($data), '报名信息'. date('Y-m-d') .'.xlsx'); } /** * 模拟点击url,记录uvpv */ public function urlSimulateClick(Request $request) { $validator = \Validator::make($request->all(),[ 'url' => 'required|string', 'type' => 'string', ]); if ($validator->fails()) { return self::returnValue($validator->getMessageBag(), 100); } $url = trim($request->input('url')); $type = trim($request->input('type')); KxThree9Record::setRecord($url, $type); if(!$url){ return self::returnValue('异常链接'); } //$res = $this->curlGet($url); return self::returnValue(['url' => $url]); } public function curlGet($url, array $headers = [], $timeout = 10) { try{ $opt = [ CURLOPT_RETURNTRANSFER => true, //要求结果为字符串且输出到屏幕上 CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false, CURLOPT_HEADER => false, // 不输出响应头信息 CURLOPT_HTTPHEADER => $headers, CURLOPT_TIMEOUT => $timeout, ]; $curl = curl_init($url); curl_setopt_array($curl, $opt); $output = curl_exec($curl); // 执行 $httpCode = curl_getinfo($curl,CURLINFO_HTTP_CODE); curl_close($curl); } catch(\Exception $e) { Log::error('出现异常', [ 'url' => $url, 'headers' => $headers, ], 'curlget_exception'); return false; } return $httpCode; } /** * 39记录中奖人信息 */ public function addLuckyInfo(Request $request) { $validator = \Validator::make($request->all(),[ 'uid' => 'required|string', 'phone' => 'required|string', 'name' => 'required|string', 'adress' => 'required|string' ]); if ($validator->fails()) { return self::returnValue($validator->getMessageBag(), 100); } $uid = trim($request->input('uid')); $phone = trim($request->input('phone')); $name = trim($request->input('name')); $adress = trim($request->input('adress')); /*if(KxThree9LuckyRecord::where('phone', $phone)->exists()){ return self::returnValue('手机号已存在', 1301); }*/ $res = KxThree9LuckyRecord::setRecord($uid, $phone, $name, $adress); return self::returnValue($res); } /** * 39抽奖接口 */ public function luckyDraw(Request $request) { $validator = \Validator::make($request->all(),[ 'uid' => 'required|string', 'web_type' => 'nullable|string', ]); if ($validator->fails()) { return self::returnValue($validator->getMessageBag(), 100); } $today = date('Y-m-d'); $uid = trim($request->input('uid')); $web_type = trim($request->input('web_type', 'initial')); if(KxThree9LuckyRecord::where('uid', $uid)->where('created_at', '>', $today)->exists()){ return self::returnValue('今天已参与过抽奖', 1300); } $gifts = KxThree9LuckyGifts::select('id', 'name', 'price', 'inventory', 'img', 'note')->get()->keyBy('id')->toArray(); $rkey = 'KxThree9LuckyGiftsRdsList'; if(!RedisModel::lLen($rkey)){ #计算真实库存 KxThree9LuckyRecord::where('prize', '>', 0) ->where('enable', 1) ->whereNull('phone') ->where('created_at', '<', date('Y-m-d H:i:s', strtotime('-2 day'))) ->update([ 'enable' => 0 ]); //超过两天未填手机号的返还奖品次数 $alLuckys = KxThree9LuckyRecord::selectRaw('prize, count(1) as num') ->where('prize', '>', 0) ->where('enable', 1) ->groupBy('prize') ->get() ->keyBy('prize') ->toArray(); $giftArr = array(); //奖池 foreach($gifts as $gift){ $al_n = $alLuckys[$gift['id']]['num'] ?? 0; $gift['leave'] = $gift['inventory'] - $al_n; if($gift['leave']>0){ $giftArr = $this->array_repeat($gift, $gift['leave'], $giftArr); } } if(empty($giftArr)){ self::returnValue([ 'prize'=>null, 'lucky'=>0 ]); } shuffle($giftArr); foreach ($giftArr as $val) { RedisModel::lPush($rkey, json_encode($val)); } } $lucky = 1; $kv = mt_rand(1, 1000); if($kv==1){ $gid = RedisModel::rPop($rkey); $prize = json_decode($gid, true); } else { $prize = null; $lucky = 0; } KxThree9LuckyRecord::insert([ 'uid' => $uid, 'web_type' => $web_type, 'prize' => $prize['id'] ?? null, ]); return self::returnValue([ 'prize'=>$prize, 'lucky'=>$lucky ]); } public function array_repeat($str, $n, $data=[]) { for($i=0; $i<$n; $i++){ $data[] = $str; } return $data; } /** * 中奖导出 */ public function exportLuckyRecords() { $list = KxThree9LuckyRecord::where('enable', 1) ->orderBy('id', 'asc') ->get() ->toArray(); $data = array(); $data[] = ['设备号', '手机号', '姓名', '地址', '奖品', '抽奖时间']; foreach($list as $val) { if($val['prize']){ $val['prize'] = KxThree9LuckyGifts::where('id', $val['prize'])->value('name'); } $data[] = [ $val['uid'], $val['phone'], $val['name'], $val['adress'], $val['prize'], $val['created_at'] ]; } return Excel::download(new UserExport($data), '抽奖记录_'. date('Y-m-d') .'.xlsx'); } }