123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- <?php
- namespace App\Http\Controllers\Api\Kx;
- use App\Models\KxSignUpRecords;
- use App\Models\KxThree9Record;
- use App\Models\KxThree9LuckyRecord;
- use App\Models\KxThree9LuckyGifts;
- use App\Http\Controllers\Controller;
- use Illuminate\Http\Request;
- use Illuminate\Validation\Rule;
- use Maatwebsite\Excel\Facades\Excel;
- use App\Exports\UserExport;
- use App\Support\Curl;
- use App\Support\Log;
- use App\Support\RedisModel;
- class DataController extends Controller
- {
- /**
- * 报名记录
- */
- public function signRecords(Request $request)
- {
- $validator = \Validator::make($request->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');
-
- }
- }
|