Brak opisu

DataController.php 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <?php
  2. namespace App\Http\Controllers\Api\Kx;
  3. use App\Models\KxSignUpRecords;
  4. use App\Models\KxThree9Record;
  5. use App\Models\KxThree9LuckyRecord;
  6. use App\Models\KxThree9LuckyGifts;
  7. use App\Http\Controllers\Controller;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Validation\Rule;
  10. use Maatwebsite\Excel\Facades\Excel;
  11. use App\Exports\UserExport;
  12. use App\Support\Curl;
  13. use App\Support\Log;
  14. use App\Support\RedisModel;
  15. class DataController extends Controller
  16. {
  17. /**
  18. * 报名记录
  19. */
  20. public function signRecords(Request $request)
  21. {
  22. $validator = \Validator::make($request->all(),[
  23. 'name' => 'required|string',
  24. 'phone' => 'required|string',
  25. 'arrive_at' => 'required|date_format:Y-m-d H:i:s',
  26. 'leave_at' => 'required|date_format:Y-m-d H:i:s',
  27. 'arrive_flight' => 'nullable|string',
  28. 'leave_flight' => 'nullable|string',
  29. 'cloth_size' => 'nullable|string',
  30. ]);
  31. if ($validator->fails()) {
  32. return self::returnValue($validator->getMessageBag(), 100);
  33. }
  34. $record = new KxSignUpRecords();
  35. $record->name = trim($request->input('name'));
  36. $record->phone = trim($request->input('phone'));
  37. $record->arrive_at = $request->input('arrive_at');
  38. $record->leave_at = $request->input('leave_at');
  39. //$record->arrive_type = $request->input('arrive_type');
  40. //$record->leave_type = $request->input('leave_type');
  41. $record->arrive_flight = trim($request->input('arrive_flight'));
  42. $record->leave_flight = trim($request->input('leave_flight'));
  43. $record->cloth_size = trim($request->input('cloth_size'));
  44. //$record->note = trim($request->input('note'));
  45. $res = $record->save();
  46. return self::returnValue($res);
  47. }
  48. /**
  49. * 报名导出
  50. */
  51. public function exportRecords()
  52. {
  53. $list = KxSignUpRecords::where('enable', 1)
  54. ->orderBy('id', 'desc')
  55. ->get()
  56. ->toArray();
  57. $data = array();
  58. $data[] = ['姓名', '手机号', '到京时间', '到京航班/高铁', '离京时间', '离京航班/高铁', '服装尺寸', '填写时间'];
  59. foreach($list as $val)
  60. {
  61. $data[] = [
  62. $val['name'],
  63. $val['phone'],
  64. $val['arrive_at'],
  65. $val['arrive_flight'],
  66. $val['leave_at'],
  67. $val['leave_flight'],
  68. $val['cloth_size'],
  69. $val['created_at']
  70. ];
  71. }
  72. return Excel::download(new UserExport($data), '报名信息'. date('Y-m-d') .'.xlsx');
  73. }
  74. /**
  75. * 模拟点击url,记录uvpv
  76. */
  77. public function urlSimulateClick(Request $request)
  78. {
  79. $validator = \Validator::make($request->all(),[
  80. 'url' => 'required|string',
  81. 'type' => 'string',
  82. ]);
  83. if ($validator->fails()) {
  84. return self::returnValue($validator->getMessageBag(), 100);
  85. }
  86. $url = trim($request->input('url'));
  87. $type = trim($request->input('type'));
  88. KxThree9Record::setRecord($url, $type);
  89. if(!$url){
  90. return self::returnValue('异常链接');
  91. }
  92. //$res = $this->curlGet($url);
  93. return self::returnValue(['url' => $url]);
  94. }
  95. public function curlGet($url, array $headers = [], $timeout = 10)
  96. {
  97. try{
  98. $opt = [
  99. CURLOPT_RETURNTRANSFER => true, //要求结果为字符串且输出到屏幕上
  100. CURLOPT_SSL_VERIFYPEER => false,
  101. CURLOPT_SSL_VERIFYHOST => false,
  102. CURLOPT_HEADER => false, // 不输出响应头信息
  103. CURLOPT_HTTPHEADER => $headers,
  104. CURLOPT_TIMEOUT => $timeout,
  105. ];
  106. $curl = curl_init($url);
  107. curl_setopt_array($curl, $opt);
  108. $output = curl_exec($curl); // 执行
  109. $httpCode = curl_getinfo($curl,CURLINFO_HTTP_CODE);
  110. curl_close($curl);
  111. } catch(\Exception $e) {
  112. Log::error('出现异常', [
  113. 'url' => $url,
  114. 'headers' => $headers,
  115. ], 'curlget_exception');
  116. return false;
  117. }
  118. return $httpCode;
  119. }
  120. /**
  121. * 39记录中奖人信息
  122. */
  123. public function addLuckyInfo(Request $request)
  124. {
  125. $validator = \Validator::make($request->all(),[
  126. 'uid' => 'required|string',
  127. 'phone' => 'required|string',
  128. 'name' => 'required|string',
  129. 'adress' => 'required|string'
  130. ]);
  131. if ($validator->fails()) {
  132. return self::returnValue($validator->getMessageBag(), 100);
  133. }
  134. $uid = trim($request->input('uid'));
  135. $phone = trim($request->input('phone'));
  136. $name = trim($request->input('name'));
  137. $adress = trim($request->input('adress'));
  138. /*if(KxThree9LuckyRecord::where('phone', $phone)->exists()){
  139. return self::returnValue('手机号已存在', 1301);
  140. }*/
  141. $res = KxThree9LuckyRecord::setRecord($uid, $phone, $name, $adress);
  142. return self::returnValue($res);
  143. }
  144. /**
  145. * 39抽奖接口
  146. */
  147. public function luckyDraw(Request $request)
  148. {
  149. $validator = \Validator::make($request->all(),[
  150. 'uid' => 'required|string',
  151. 'web_type' => 'nullable|string',
  152. ]);
  153. if ($validator->fails()) {
  154. return self::returnValue($validator->getMessageBag(), 100);
  155. }
  156. $today = date('Y-m-d');
  157. $uid = trim($request->input('uid'));
  158. $web_type = trim($request->input('web_type', 'initial'));
  159. if(KxThree9LuckyRecord::where('uid', $uid)->where('created_at', '>', $today)->exists()){
  160. return self::returnValue('今天已参与过抽奖', 1300);
  161. }
  162. $gifts = KxThree9LuckyGifts::select('id', 'name', 'price', 'inventory', 'img', 'note')->get()->keyBy('id')->toArray();
  163. $rkey = 'KxThree9LuckyGiftsRdsList';
  164. if(!RedisModel::lLen($rkey)){
  165. #计算真实库存
  166. KxThree9LuckyRecord::where('prize', '>', 0)
  167. ->where('enable', 1)
  168. ->whereNull('phone')
  169. ->where('created_at', '<', date('Y-m-d H:i:s', strtotime('-2 day')))
  170. ->update([
  171. 'enable' => 0
  172. ]); //超过两天未填手机号的返还奖品次数
  173. $alLuckys = KxThree9LuckyRecord::selectRaw('prize, count(1) as num')
  174. ->where('prize', '>', 0)
  175. ->where('enable', 1)
  176. ->groupBy('prize')
  177. ->get()
  178. ->keyBy('prize')
  179. ->toArray();
  180. $giftArr = array(); //奖池
  181. foreach($gifts as $gift){
  182. $al_n = $alLuckys[$gift['id']]['num'] ?? 0;
  183. $gift['leave'] = $gift['inventory'] - $al_n;
  184. if($gift['leave']>0){
  185. $giftArr = $this->array_repeat($gift, $gift['leave'], $giftArr);
  186. }
  187. }
  188. if(empty($giftArr)){
  189. self::returnValue([
  190. 'prize'=>null,
  191. 'lucky'=>0
  192. ]);
  193. }
  194. shuffle($giftArr);
  195. foreach ($giftArr as $val) {
  196. RedisModel::lPush($rkey, json_encode($val));
  197. }
  198. }
  199. $lucky = 1;
  200. $kv = mt_rand(1, 1000);
  201. if($kv==1){
  202. $gid = RedisModel::rPop($rkey);
  203. $prize = json_decode($gid, true);
  204. } else {
  205. $prize = null;
  206. $lucky = 0;
  207. }
  208. KxThree9LuckyRecord::insert([
  209. 'uid' => $uid,
  210. 'web_type' => $web_type,
  211. 'prize' => $prize['id'] ?? null,
  212. ]);
  213. return self::returnValue([
  214. 'prize'=>$prize,
  215. 'lucky'=>$lucky
  216. ]);
  217. }
  218. public function array_repeat($str, $n, $data=[])
  219. {
  220. for($i=0; $i<$n; $i++){
  221. $data[] = $str;
  222. }
  223. return $data;
  224. }
  225. /**
  226. * 中奖导出
  227. */
  228. public function exportLuckyRecords()
  229. {
  230. $list = KxThree9LuckyRecord::where('enable', 1)
  231. ->orderBy('id', 'asc')
  232. ->get()
  233. ->toArray();
  234. $data = array();
  235. $data[] = ['设备号', '手机号', '姓名', '地址', '奖品', '抽奖时间'];
  236. foreach($list as $val)
  237. {
  238. if($val['prize']){
  239. $val['prize'] = KxThree9LuckyGifts::where('id', $val['prize'])->value('name');
  240. }
  241. $data[] = [
  242. $val['uid'],
  243. $val['phone'],
  244. $val['name'],
  245. $val['adress'],
  246. $val['prize'],
  247. $val['created_at']
  248. ];
  249. }
  250. return Excel::download(new UserExport($data), '抽奖记录_'. date('Y-m-d') .'.xlsx');
  251. }
  252. }