Index.php 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271
  1. <?php
  2. namespace app\index\controller;
  3. use think\facade\Db;
  4. use app\middleware\Log;
  5. use think\facade\Cache;
  6. use ReflectionException;
  7. use app\middleware\Safe;
  8. use think\file\UploadedFile;
  9. use \think\response\Json;
  10. use think\facade\Request;
  11. use think\admin\Controller;
  12. use InvalidArgumentException;
  13. use app\service\WeiboService;
  14. use app\admin\model\SystemNotice;
  15. use think\facade\Log as FacadeLog;
  16. use think\db\exception\DbException;
  17. use think\admin\service\SystemService;
  18. use think\exception\FuncNotFoundException;
  19. use think\exception\ClassNotFoundException;
  20. use think\db\exception\DataNotFoundException;
  21. use think\db\exception\ModelNotFoundException;
  22. /**
  23. * Class Index
  24. * @package app\index\controller
  25. */
  26. class Index extends Controller
  27. {
  28. /**
  29. * 获取品牌任务相关配置
  30. *
  31. * @throws FuncNotFoundException
  32. * @throws ClassNotFoundException
  33. * @throws ReflectionException
  34. * @throws DbException
  35. * @throws ModelNotFoundException
  36. * @throws DataNotFoundException
  37. * @return mixed
  38. */
  39. public function brand()
  40. {
  41. $conf = $this->getBrandConfigAndState();
  42. if ($conf instanceof Json) {
  43. return $conf;
  44. }
  45. return $this->successResponse($conf);
  46. }
  47. /**
  48. * 检测登录 通过cookie中的SUB字段的内容,调用用户接口检测登录状态
  49. * @throws ClassNotFoundException
  50. * @throws ReflectionException
  51. * @return mixed
  52. */
  53. public function checkLogin()
  54. {
  55. FacadeLog::info("checkLogin cookies:" . json_encode(Request::post()));
  56. $sub = Request::post('cookie');
  57. $uid = 0;
  58. $userInfoRes = (new WeiboService($uid))->userinfo($sub);
  59. if (empty($userInfoRes) || $userInfoRes['ok'] != 1) {
  60. return $this->response(403, $userInfoRes['msg'] ?? '没有登录');
  61. }
  62. // 使用客户端信息生成token
  63. $token = md5($_SERVER['HTTP_USER_AGENT'] . $_SERVER['HTTP_ACCEPT_ENCODING'] . $_SERVER['HTTP_ACCEPT_LANGUAGE'] . get_client_ip(0) . $userInfoRes['data']['uid']);
  64. $user = $userInfoRes['data'];
  65. $userInfo = Db::table('awards_user_info')->where('uid', $user['uid'])->find();
  66. $count = 0;
  67. $isShare = 0;
  68. if (empty($userInfo)) {
  69. $userAttr = [
  70. 'uid' => $user['uid'],
  71. 'portrait' => $user['profile_image_url'],
  72. 'nickname' => $user['name'],
  73. 'is_share' => 0,
  74. 'count' => 1,
  75. 'create_at' => time()
  76. ];
  77. if (0 == Db::table('awards_user_info')->insert($userAttr)) {
  78. return $this->response(5001, '系统错误,请稍后再试~');
  79. }
  80. } else {
  81. $count = Db::table('awards_user_task_log')->where('uid', $user['uid'])->count('id');
  82. $isShare = $userInfo['is_share'];
  83. }
  84. // 生成加密用的密钥和向量
  85. $cipher = "aes-256-gcm";
  86. $ivlen = openssl_cipher_iv_length($cipher);
  87. $iv = bin2hex(openssl_random_pseudo_bytes($ivlen));
  88. $aesKey = bin2hex(openssl_random_pseudo_bytes(32));
  89. // $user = array_merge($user, [
  90. // 'aes_key' => $aesKey,
  91. // 'ase_iv' => $iv,
  92. // ]);
  93. $cacheUser = [
  94. 'aes_key' => $aesKey,
  95. 'ase_iv' => $iv,
  96. 'uid' => $user['uid'],
  97. ];
  98. $activity = Db::table('awards_activity')->where('begin_at', '<=', time())
  99. ->where('end_at', '>=', time())->find();
  100. $isBeginActivity = 1;
  101. if (empty($activity)) {
  102. $isBeginActivity = 0;
  103. }
  104. $userInfo = [];
  105. if ($count > 0) {
  106. $userInfo = Db::table('awards_user_task_log')->alias('l')
  107. ->leftJoin('awards_user_info u', 'l.uid = u.uid')
  108. ->field(['u.nickname', 'u.portrait', 'u.uid', 'l.duration', 'l.number'])
  109. ->order('l.number', 'desc')
  110. ->order('l.duration', 'asc')
  111. ->find();
  112. }
  113. // 缓存用户信息1天
  114. Cache::set('u:' . $token, json_encode($cacheUser), 86400);
  115. return $this->successResponse([
  116. 'user' => $user,
  117. 'token' => $token,
  118. 'isShare' => $isShare,
  119. 'count' => $count,
  120. 'isBeginActivity' => $isBeginActivity,
  121. 'task' => $userInfo
  122. ]);
  123. }
  124. /**
  125. * 是否可以参与游戏
  126. * @throws ClassNotFoundException
  127. * @throws ReflectionException
  128. * @return mixed
  129. */
  130. public function checkRole()
  131. {
  132. $activity = Db::table('awards_activity')->where('begin_at', '<=', time())
  133. ->where('end_at', '>=', time())->find();
  134. if (empty($activity)) {
  135. return $this->response(5002, '活动末开始');
  136. }
  137. $userInfo = Db::table('awards_user_info')->where('uid', Safe::$user['uid'])->find();
  138. $count = Db::table('awards_user_task_log')->where('uid', Safe::$user['uid'])->count('id');
  139. return $this->successResponse([
  140. 'isShare' => $userInfo['is_share'],
  141. 'count' => $count
  142. ]);
  143. }
  144. /**
  145. * 提交任务接口
  146. * @throws FuncNotFoundException
  147. * @throws ClassNotFoundException
  148. * @throws DbException
  149. * @throws ModelNotFoundException
  150. * @throws DataNotFoundException
  151. * @return mixed
  152. */
  153. public function submitTask()
  154. {
  155. $activity = Db::table('awards_activity')->where('begin_at', '<=', time())
  156. ->where('end_at', '>=', time())->find();
  157. if (empty($activity)) {
  158. return $this->response(5002, '活动末开始');
  159. }
  160. // if (empty(Safe::$body)) {
  161. // return $this->response(5003, '参数有误');
  162. // }
  163. //
  164. // if (!isset(Safe::$body['duration']) || Safe::$body['duration'] < 1 || Safe::$body['duration'] > 1000) {
  165. // return $this->response(5003, '时长参数有误');
  166. // }
  167. //
  168. // if (!isset(Safe::$body['number']) || Safe::$body['number'] < 0 || Safe::$body['number'] > 1000) {
  169. // return $this->response(5003, '时长参数有误');
  170. // }
  171. // $duration = Safe::$body['duration'];
  172. // $number = Safe::$body['number'];
  173. // $duration = Request::post("duration");
  174. // $number = Request::post("number");
  175. //
  176. // $date = date('Y-m-d');
  177. //
  178. // $userInfo = Db::table('awards_user_info')->where('uid', Safe::$user['uid'])->find();
  179. // if (empty($userInfo)) {
  180. // return $this->response(403, '没有登录');
  181. // }
  182. //
  183. // $count = Db::table('awards_user_task_log')->where('uid', Safe::$user['uid'])->count('id');
  184. // if ($count >= $userInfo['count']) {
  185. // return $this->response(5002, '没有参与次数');
  186. // }
  187. //
  188. // $log = [
  189. // 'uid' => Safe::$user['uid'],
  190. // 'date' => $date,
  191. // 'duration' => $duration,
  192. // 'number' => $number,
  193. // 'create_at' => time()
  194. // ];
  195. // if (0 == Db::table('awards_user_task_log')->insert($log)) {
  196. // return $this->response(5001, '系统错误,请稍后再试~');
  197. // }
  198. return $this->successResponse(null, '提交成功');
  199. }
  200. /**
  201. * 提交发布微博接口
  202. * @throws FuncNotFoundException
  203. * @throws ClassNotFoundException
  204. * @throws DbException
  205. * @throws ModelNotFoundException
  206. * @throws DataNotFoundException
  207. * @return mixed
  208. */
  209. public function submitShare()
  210. {
  211. $activity = Db::table('awards_activity')->where('begin_at', '<=', time())
  212. ->where('end_at', '>=', time())->find();
  213. if (empty($activity)) {
  214. return $this->response(5002, '活动末开始');
  215. }
  216. $userInfo = Db::table('awards_user_info')->where('uid', Safe::$user['uid'])->find();
  217. if (empty($userInfo)) {
  218. return $this->response(403, '没有登录');
  219. }
  220. if ($userInfo['is_share'] > 0) {
  221. return $this->response(601, '已发布');
  222. }
  223. // 更新任务状态
  224. $nums = Db::table('awards_user_info')->where('uid', Safe::$user['uid'])->update([
  225. 'is_share' => 1,
  226. 'count' => 2,
  227. 'share_at' => time()
  228. ]);
  229. if ($nums) {
  230. return $this->successResponse(null, '提交成功');
  231. } else {
  232. return $this->response(601, '发布失败');
  233. }
  234. }
  235. public function startGame()
  236. {
  237. $activity = Db::table('awards_activity')->where('begin_at', '<=', time())
  238. ->where('end_at', '>=', time())->find();
  239. if (empty($activity)) {
  240. return $this->response(5002, '活动末开始');
  241. }
  242. $userInfo = Db::table('awards_user_info')->where('uid', Safe::$user['uid'])->find();
  243. if (empty($userInfo)) {
  244. return $this->response(403, '没有登录');
  245. }
  246. $count = Db::table('awards_user_task_log')->where('uid', Safe::$user['uid'])->count('id');
  247. if ($count >= $userInfo['count']) {
  248. return $this->response(5002, '没有参与次数');
  249. }
  250. $game = Db::table('awards_user_game')
  251. ->where('uid', '=', Safe::$user['uid'])
  252. ->where('state', '=', 1)
  253. ->find();
  254. if (!empty($game)) {
  255. Db::table('awards_user_game')->where('id', '=', $game['id'])->delete(true);
  256. }
  257. $gameId = $this->uuid();
  258. $attr = [
  259. 'game_id' => $gameId,
  260. 'uid' => Safe::$user['uid'],
  261. 'begin_at' => time(),
  262. 'end_at' => 0,
  263. 'state' => 1,
  264. 'create_at' => time()
  265. ];
  266. Db::table('awards_user_game')->insert($attr);
  267. return $this->successResponse(['gameId' => $gameId], '开始游戏成功');
  268. }
  269. public function endGame()
  270. {
  271. $activity = Db::table('awards_activity')->where('begin_at', '<=', time())
  272. ->where('end_at', '>=', time())->find();
  273. if (empty($activity)) {
  274. return $this->response(5002, '活动末开始');
  275. }
  276. $userInfo = Db::table('awards_user_info')->where('uid', Safe::$user['uid'])->find();
  277. if (empty($userInfo)) {
  278. return $this->response(403, '没有登录');
  279. }
  280. $gameId = Request::post("gameId");
  281. $game = Db::table('awards_user_game')
  282. ->where('uid', '=', Safe::$user['uid'])
  283. ->where('game_id', '=', $gameId)
  284. ->where('state', '=', 1)
  285. ->find();
  286. if (empty($game)) {
  287. return $this->response(6001, '游戏不存在');
  288. }
  289. if((time() - $game['begin_at']) > 122 || (time() - $game['begin_at']) < 13) {
  290. return $this->response(6003, '游戏参数有误');
  291. }
  292. $endAt = time();
  293. $duration = $endAt - $game['begin_at'];
  294. if($duration < 14) {
  295. return $this->response(6001, '游戏有误');
  296. }
  297. Db::table('awards_user_game')->where('game_id', '=', $gameId)->update([
  298. 'end_at' => $endAt,
  299. 'state' => 2,
  300. 'update_at' => time()
  301. ]);
  302. $count = Db::table('awards_user_task_log')->where('uid', Safe::$user['uid'])->count('id');
  303. if ($count >= $userInfo['count']) {
  304. return $this->response(5002, '没有参与次数');
  305. }
  306. $number = Db::table('awards_user_game_log')
  307. ->where('uid', '=', Safe::$user['uid'])
  308. ->where('game_id', '=', $gameId)
  309. ->count('id');
  310. $date = date('Y-m-d');
  311. $log = [
  312. 'uid' => Safe::$user['uid'],
  313. 'date' => $date,
  314. 'duration' => $duration,
  315. 'number' => $number,
  316. 'create_at' => time()
  317. ];
  318. Db::table('awards_user_task_log')->insert($log);
  319. return $this->successResponse(null, '提交成功');
  320. }
  321. public function submitEasterEgg()
  322. {
  323. $easterEgg = Request::post("easterEgg");
  324. if (!in_array($easterEgg, [1, 2, 3,4 ,5 ,6,7, 8, 9, 10, 11, 12, 13, 14, 15])) {
  325. return $this->response(6003, '游戏参数有误');
  326. }
  327. $activity = Db::table('awards_activity')->where('begin_at', '<=', time())
  328. ->where('end_at', '>=', time())->find();
  329. if (empty($activity)) {
  330. return $this->response(5002, '活动末开始');
  331. }
  332. $userInfo = Db::table('awards_user_info')->where('uid', Safe::$user['uid'])->find();
  333. if (empty($userInfo)) {
  334. return $this->response(403, '没有登录');
  335. }
  336. $gameId = Request::post("gameId");
  337. $game = Db::table('awards_user_game')
  338. ->where('uid', '=', Safe::$user['uid'])
  339. ->where('game_id', '=', $gameId)
  340. ->where('state', '=', 1)
  341. ->find();
  342. if (empty($game)) {
  343. return $this->response(6001, '游戏不存在');
  344. }
  345. if((time() - $game['begin_at']) > 120) {
  346. return $this->response(6003, '游戏已结束');
  347. }
  348. $log = Db::table('awards_user_game_log')
  349. ->where('uid', '=', Safe::$user['uid'])
  350. ->where('game_id', '=', $gameId)
  351. ->where('draw', '=', $easterEgg)
  352. ->find();
  353. if (!empty($log)) {
  354. return $this->successResponse(null, '提交成功');
  355. }
  356. $log = Db::table('awards_user_game_log')
  357. ->where('uid', '=', Safe::$user['uid'])
  358. ->where('game_id', '=', $gameId)
  359. ->order('create_at', 'desc')
  360. ->find();
  361. if (empty($log)) {
  362. $startTime = $game['begin_at'];
  363. } else {
  364. $startTime = $log['create_at'];
  365. }
  366. $duration = time() - $startTime;
  367. if ($duration < 0) {
  368. return $this->response(6002, '游戏参数有误');
  369. }
  370. $attr = [
  371. 'uid' => Safe::$user['uid'],
  372. 'game_id' => $gameId,
  373. 'draw' => $easterEgg,
  374. 'duration' => $duration,
  375. 'create_at' => time()
  376. ];
  377. Db::table('awards_user_game_log')->insert($attr);
  378. return $this->successResponse(null, '提交成功');
  379. }
  380. function uuid()
  381. {
  382. $chars = md5(uniqid(mt_rand(), true));
  383. $uuid = substr ( $chars, 0, 8 )
  384. . substr ( $chars, 8, 4 )
  385. . substr ( $chars, 12, 4 )
  386. . substr ( $chars, 16, 4 )
  387. . substr ( $chars, 20, 12 );
  388. return $uuid ;
  389. }
  390. /**
  391. * 头号排行接口
  392. * @throws FuncNotFoundException
  393. * @throws ClassNotFoundException
  394. * @throws DbException
  395. * @throws ModelNotFoundException
  396. * @throws DataNotFoundException
  397. * @return mixed
  398. */
  399. public function topRanking()
  400. {
  401. $ranking = Db::table('awards_user_task_log')->alias("l")
  402. ->leftJoin('awards_user_info u', 'l.uid = u.uid')
  403. ->field(['u.nickname', 'u.portrait', 'l.uid', 'min(l.duration) as duration', 'max(l.number) as number'])
  404. ->where('u.portrait', '<>', '')
  405. ->whereNotNull('u.portrait')
  406. ->group('l.uid')
  407. ->order('l.number', 'desc')
  408. ->order('l.duration', 'asc')
  409. ->limit(50)
  410. ->select();
  411. $userInfo = Db::table('awards_user_task_log')->alias('l')
  412. ->leftJoin('awards_user_info u', 'l.uid = u.uid')
  413. ->field(['u.nickname', 'u.portrait', 'l.uid', 'l.duration', 'l.number'])
  414. ->where('l.uid', '=', Safe::$user['uid'])
  415. ->order('l.number', 'desc')
  416. ->order('l.duration', 'asc')
  417. ->find();
  418. $res = [
  419. 'self' => $userInfo,
  420. 'ranking' => $ranking
  421. ];
  422. return $this->successResponse($res);
  423. }
  424. /**
  425. * 最新排行接口
  426. * @throws FuncNotFoundException
  427. * @throws ClassNotFoundException
  428. * @throws DbException
  429. * @throws ModelNotFoundException
  430. * @throws DataNotFoundException
  431. * @return mixed
  432. */
  433. public function newRanking()
  434. {
  435. $ranking = Db::table('awards_user_task_log')->alias("l")
  436. ->leftJoin('awards_user_info u', 'l.uid = u.uid')
  437. ->field(['u.nickname', 'u.portrait', 'l.uid', 'min(l.duration) as duration', 'max(l.number) as number', 'l.create_at'])
  438. ->where('u.portrait', '<>', '')
  439. ->whereNotNull('u.portrait')
  440. ->group('l.uid')
  441. ->order('l.create_at', 'desc')
  442. ->order('l.number', 'desc')
  443. ->order('l.duration', 'asc')
  444. ->limit(50)
  445. ->select();
  446. $userInfo = Db::table('awards_user_task_log')->alias('l')
  447. ->leftJoin('awards_user_info u', 'l.uid = u.uid')
  448. ->field(['u.nickname', 'u.portrait', 'l.uid', 'l.duration', 'l.number'])
  449. ->where('l.uid', '=', Safe::$user['uid'])
  450. ->order('l.number', 'desc')
  451. ->order('l.duration', 'asc')
  452. ->find();
  453. $res = [
  454. 'self' => $userInfo,
  455. 'ranking' => $ranking
  456. ];
  457. return $this->successResponse($res);
  458. }
  459. /**
  460. * 提交领取信息接口
  461. * @throws FuncNotFoundException
  462. * @throws ClassNotFoundException
  463. * @throws DbException
  464. * @throws ModelNotFoundException
  465. * @throws DataNotFoundException
  466. * @return mixed
  467. */
  468. public function submitReceive()
  469. {
  470. $name = trim(Request::post("name"));
  471. $mobile = trim(Request::post("mobile"));
  472. $address = trim(Request::post("address"));
  473. $giftId = trim(Request::post("giftId"));
  474. $type = trim(Request::post("type"));
  475. $orders = Db::table('awards_order')->where('uid', Safe::$user['uid'])->where('type', $type)->find();
  476. if (empty($orders)) {
  477. return $this->response(5001, '参数有误');
  478. }
  479. $userInfo = Db::table('awards_user_info')->where('uid', Safe::$user['uid'])->find();
  480. if (empty($userInfo)) {
  481. return $this->response(403, '没有登录');
  482. }
  483. if (strlen($name) < 1 || strlen($name) > 50) {
  484. return $this->response(5001, '姓名有误');
  485. }
  486. if (strlen($mobile) != 11) {
  487. return $this->response(5001, '手机号有误');
  488. }
  489. if (strlen($address) < 1 || strlen($address) > 1000) {
  490. return $this->response(5001, '地址有误');
  491. }
  492. // 更新任务状态
  493. $nums = Db::table('awards_order')->where('uid', Safe::$user['uid'])->where('type', $type)->update([
  494. 'name' => $name,
  495. 'mobile' => $mobile,
  496. 'address' => $address,
  497. ]);
  498. if ($nums < 1) {
  499. return $this->response(5001, '系统错误,请稍后再试~');
  500. }
  501. return $this->successResponse(null, '提交成功');
  502. }
  503. /**
  504. * 获取轮播中奖接口
  505. * @throws FuncNotFoundException
  506. * @throws ClassNotFoundException
  507. * @throws DbException
  508. * @throws ModelNotFoundException
  509. * @throws DataNotFoundException
  510. * @return mixed
  511. */
  512. public function getRotationAward() {
  513. $activity = Db::table('awards_activity')->where('begin_at', '<=', time())
  514. ->where('end_at', '>=', time())->find();
  515. if (empty($activity)) {
  516. return $this->response(5002, '活动末开始');
  517. }
  518. $orders = Db::table('awards_order')->alias('o')
  519. ->leftJoin('awards_user_info u', 'o.uid = u.uid')
  520. ->leftJoin('awards_gift g', 'o.gift_id = g.id')
  521. ->field(['u.nickname', 'u.portrait', 'u.uid', 'g.name as giftName'])
  522. ->order('o.id', 'desc')
  523. ->select();
  524. return $this->successResponse($orders);
  525. }
  526. /**
  527. * 排行中奖接口
  528. * @throws FuncNotFoundException
  529. * @throws ClassNotFoundException
  530. * @throws DbException
  531. * @throws ModelNotFoundException
  532. * @throws DataNotFoundException
  533. * @return mixed
  534. */
  535. public function getRankingWinAward() {
  536. $activity = Db::table('awards_activity')->where('begin_at', '<=', time())
  537. ->where('end_at', '>=', time())->find();
  538. if (!empty($activity)) {
  539. return $this->response(5002, '活动末结束');
  540. }
  541. $ranking = Db::table('awards_user_task_log')->alias("l")
  542. ->leftJoin('awards_user_info u', 'l.uid = u.uid')
  543. ->field(['u.nickname', 'u.portrait', 'u.uid', 'l.duration', 'l.number'])
  544. ->distinct('l.uid')
  545. ->order('l.number', 'desc')
  546. ->order('l.duration', 'asc')
  547. ->limit(50)
  548. ->select();
  549. $isWinAward = 0;
  550. $rank = 0;
  551. foreach ($ranking as $key => $val) {
  552. if ($val['uid'] == Safe::$user['uid']) {
  553. $isWinAward = 1;
  554. $rank = $key +1;
  555. break;
  556. }
  557. }
  558. $giftId = 0;
  559. // 如果中奖 插入用户中奖信息
  560. if ($isWinAward == 1) {
  561. $gifts = Db::table('awards_gift')->where('type', 2)->select();
  562. foreach ($gifts as $val) {
  563. if ($rank >= $val['min_rank'] && $rank <= $val['max_rank']) {
  564. $giftId = $val['id'];
  565. $orders = Db::table('awards_order')->where('uid', Safe::$user['uid'])->where('type', 2)->find();
  566. if (empty($orders)) {
  567. $order = [
  568. 'uid' => Safe::$user['uid'],
  569. 'gift_id' => $giftId,
  570. 'type' => 2,
  571. 'create_at' => time(),
  572. ];
  573. Db::table('awards_order')->insert($order);
  574. }
  575. break;
  576. }
  577. }
  578. }
  579. return $this->successResponse([
  580. 'isWinAward' => $isWinAward,
  581. 'giftId' => $giftId
  582. ]);
  583. }
  584. /**
  585. * 抽奖接口
  586. * @throws FuncNotFoundException
  587. * @throws ClassNotFoundException
  588. * @throws DbException
  589. * @throws ModelNotFoundException
  590. * @throws DataNotFoundException
  591. * @return mixed
  592. */
  593. public function getLuckDraw()
  594. {
  595. $userInfo = Db::table('awards_user_info')->where('uid', Safe::$user['uid'])->find();
  596. if ($userInfo['draw_count'] >= 30) {
  597. return $this->response(5002, '抽奖次数已达上线~');
  598. }
  599. Db::table('awards_user_info')->where('uid', Safe::$user['uid'])->update([
  600. 'draw_count' => $userInfo['draw_count'] + 1
  601. ]);
  602. $orders = Db::table('awards_order')->where('uid', Safe::$user['uid'])->where('type', 1)->find();
  603. $isWinAward = 0;
  604. if ($orders) {
  605. return $this->successResponse([
  606. 'isWinAward' => $isWinAward,
  607. 'giftId' => 0
  608. ]);
  609. }
  610. $gifts = Db::table('awards_gift')->where('type', 1)->select();
  611. $logCount = Db::table('awards_user_task_log')->distinct('uid')->count('id');
  612. $limit = 5000;
  613. $userRate = rand(0, $limit);
  614. $giftId = 0;
  615. foreach ($gifts as $gift) {
  616. $orderCount = Db::table('awards_order')->where('type', 1)->where('gift_id', $gift['id'])->count('id');
  617. if ($orderCount >= $gift['count']) {
  618. continue;
  619. }
  620. // 如果参与活动人数 5000人内 有人中奖过,那么不在计算。
  621. $total = $logCount + 1;
  622. if ($orderCount == floor($total/$limit)) {
  623. $isWinAward = 0;
  624. $giftId = 0;
  625. break;
  626. }
  627. for ($i=0;$i<$gift['count'];$i++) {
  628. $giftRage = rand(0, $limit);
  629. if ($userRate == $giftRage) {
  630. $isWinAward = 1;
  631. $giftId = $gift['id'];
  632. break;
  633. }
  634. }
  635. // 如果参与活动人数 5000人内 还没有人中奖那最后一人必中。
  636. if ($orderCount < floor($total/$limit) && $isWinAward ==0 && ($total%$limit) == 0) {
  637. $isWinAward = 1;
  638. $giftId = $gift['id'];
  639. break;
  640. }
  641. }
  642. // 如果中奖 插入用户中奖信息
  643. if ($isWinAward == 1) {
  644. $order = [
  645. 'uid' => Safe::$user['uid'],
  646. 'gift_id' => $giftId,
  647. 'type' => 1,
  648. 'create_at' => time(),
  649. ];
  650. if (0 == Db::table('awards_order')->insert($order)) {
  651. return $this->response(5001, '系统错误,请稍后再试~');
  652. }
  653. }
  654. return $this->successResponse([
  655. 'isWinAward' => $isWinAward,
  656. 'giftId' => $giftId
  657. ]);
  658. }
  659. /**
  660. * 奖品记录接口
  661. * @throws FuncNotFoundException
  662. * @throws ClassNotFoundException
  663. * @throws DbException
  664. * @throws ModelNotFoundException
  665. * @throws DataNotFoundException
  666. * @return mixed
  667. */
  668. public function getAwardLog()
  669. {
  670. $awards = Db::table('awards_order')->alias('o')
  671. ->leftJoin('awards_gift g', 'o.gift_id = g.id')
  672. ->field(['g.name as giftName', 'o.*'])
  673. ->where('o.uid', Safe::$user['uid'])
  674. ->select();
  675. $res = [];
  676. foreach ($awards as $val) {
  677. $type = '抽奖';
  678. if ($val['type'] == 2) {
  679. $type = '排名奖';
  680. }
  681. $isReceive = 1;
  682. if (empty($val['name']) && empty($val['address']) && empty($val['mobile'])) {
  683. $isReceive = 0;
  684. }
  685. $res[] = [
  686. 'giftId' => $val['gift_id'],
  687. 'giftName' => $val['giftName'],
  688. 'typeLabel' => $type,
  689. 'type' => $val['type'],
  690. 'createAt' => date('y-m-d h:i:s', $val['create_at']),
  691. 'address' => $val['address'],
  692. 'name' => $val['name'],
  693. 'mobile' => $val['mobile'],
  694. 'isReceive' => $isReceive
  695. ];
  696. }
  697. return $this->successResponse($res);
  698. }
  699. public function clearGameData()
  700. {
  701. // 更新用户参与状态
  702. Db::table('awards_user_info')->where('uid', Safe::$user['uid'])->update([
  703. 'is_share' => 0,
  704. 'share_at' => 0,
  705. 'count' => 1,
  706. ]);
  707. Db::table('awards_user_task_log')->where('uid', Safe::$user['uid'])->delete();
  708. Db::table('awards_order')->where('uid', Safe::$user['uid'])->delete();
  709. return $this->successResponse(null, '清除成功');
  710. }
  711. /**
  712. * 品牌任务关注接口
  713. * @throws FuncNotFoundException
  714. * @throws ClassNotFoundException
  715. * @throws ReflectionException
  716. * @throws DbException
  717. * @throws ModelNotFoundException
  718. * @throws DataNotFoundException
  719. * @return mixed
  720. */
  721. public function follow()
  722. {
  723. // 先获取品牌任务配置及状态
  724. $conf = $this->getBrandConfigAndState();
  725. if ($conf instanceof Json) {
  726. return $conf;
  727. }
  728. // 已经关注过了
  729. if ($conf['state']['follow_state'] == 1) {
  730. return $this->successResponse([
  731. 'result' => 2,
  732. 'brand' => $conf,
  733. ], '您已经关注过了');
  734. }
  735. // 调用接口关注
  736. $weiboRes = (new WeiboService(Safe::$user['uid']))->add($conf['config']['follow']['id']);
  737. if (empty($weiboRes) || $weiboRes['code'] != 10000) {
  738. return $this->response(600, $weiboRes['msg'] ?? '关注失败~');
  739. }
  740. $date = date('Y-m-d');
  741. $redisKey = $this->getTaskStateRedisKey(intval(Safe::$user['uid']));
  742. // 如果已经完成了另外两项,则标记品牌任务已经完成
  743. $finishState = $conf['state']['finish_state'];
  744. if ($conf['state']['view_state'] == 1 && $conf['state']['forward_state'] == 1) {
  745. $finishState = 1;
  746. }
  747. // 更新任务状态
  748. $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update([
  749. 'follow_state' => 1,
  750. 'finish_state' => $finishState,
  751. ]);
  752. if ($nums) {
  753. $conf['state']['follow_state'] = 1;
  754. $conf['state']['finish_state'] = $finishState;
  755. Cache::set($redisKey, json_encode($conf['state']), 86400);
  756. return $this->successResponse([
  757. 'result' => 1,
  758. 'brand' => $conf,
  759. ], '关注成功');
  760. } else {
  761. return $this->response(601, '更新任务状态失败');
  762. }
  763. }
  764. /**
  765. * 品牌任务转发博文接口
  766. * @throws FuncNotFoundException
  767. * @throws ClassNotFoundException
  768. * @throws ReflectionException
  769. * @throws DbException
  770. * @throws ModelNotFoundException
  771. * @throws DataNotFoundException
  772. * @return mixed
  773. */
  774. public function forward()
  775. {
  776. // 先获取品牌任务配置及状态
  777. $conf = $this->getBrandConfigAndState();
  778. if ($conf instanceof Json) {
  779. return $conf;
  780. }
  781. // 已经转发过了
  782. if ($conf['state']['forward_state'] == 1) {
  783. return $this->successResponse([
  784. 'result' => 2,
  785. 'brand' => $conf,
  786. ], '您已经转发过了');
  787. }
  788. // 调用微博接口转发
  789. $weiboRes = (new WeiboService(Safe::$user['uid']))->repost($conf['config']['forward']['id'], $conf['config']['forward']['content'] ?? '');
  790. if (empty($weiboRes) || $weiboRes['code'] != 10000) {
  791. return $this->response(600, $weiboRes['msg'] ?? '转发失败~');
  792. }
  793. $date = date('Y-m-d');
  794. $redisKey = $this->getTaskStateRedisKey(intval(Safe::$user['uid']));
  795. // 如果已经完成了另外两项,则标记品牌任务已经完成
  796. $finishState = $conf['state']['finish_state'];
  797. if ($conf['state']['view_state'] == 1 && $conf['state']['follow_state'] == 1) {
  798. $finishState = 1;
  799. }
  800. // 更新任务状态
  801. $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update([
  802. 'forward_state' => 1,
  803. 'finish_state' => $finishState,
  804. ]);
  805. if ($nums) {
  806. $conf['state']['forward_state'] = 1;
  807. $conf['state']['finish_state'] = $finishState;
  808. Cache::set($redisKey, json_encode($conf['state']), 86400);
  809. return $this->successResponse([
  810. 'result' => 1,
  811. 'brand' => $conf,
  812. ], '转发成功');
  813. } else {
  814. return $this->response(601, '更新任务状态失败');
  815. }
  816. }
  817. /**
  818. * 获取活动规则
  819. * @throws FuncNotFoundException
  820. * @throws ReflectionException
  821. * @throws InvalidArgumentException
  822. * @throws ClassNotFoundException
  823. * @return mixed
  824. */
  825. public function getRule()
  826. {
  827. $config = SystemService::instance()->getData('activity:rule');
  828. return $this->successResponse($config);
  829. }
  830. /**
  831. * 聚合页配置信息
  832. * @throws FuncNotFoundException
  833. * @throws ReflectionException
  834. * @throws InvalidArgumentException
  835. * @throws ClassNotFoundException
  836. * @return mixed
  837. */
  838. public function groupPageConfig()
  839. {
  840. return $this->successResponse(['config' => SystemService::instance()->getData('group:page:config')]);
  841. }
  842. // public function index()
  843. // {
  844. // $this->redirect('video');
  845. // }
  846. /**
  847. * 聚合页通知获取
  848. * 查询最近20条通知
  849. * @throws DbException
  850. * @throws ModelNotFoundException
  851. * @throws DataNotFoundException
  852. * @return mixed
  853. */
  854. public function notices()
  855. {
  856. $rows = SystemNotice::limit(20)->order('id', 'desc')->select();
  857. return $this->successResponse([
  858. "lists" => $rows,
  859. ]);
  860. }
  861. /**
  862. * 首次弹窗
  863. * 发放邀请函微博
  864. * @return Json
  865. */
  866. public function sendInviteWeibo()
  867. {
  868. $config = SystemService::instance()->getData('group:page:config');
  869. $sendRes = (new WeiboService(Safe::$user['uid']))->status($config['firstDialog']['content']);
  870. if (empty($sendRes) || $sendRes['code'] != 10000) {
  871. return $this->response(403, $sendRes['msg'] ?? '发布失败');
  872. }
  873. Cache::set('u:f:' . Safe::$user['uid'], 1, 15552000);
  874. return $this->successResponse(null, '发布成功!');
  875. }
  876. /**
  877. * 首次弹窗用户未选择发送邀请函
  878. * 标记已经首次弹窗过了
  879. * @return mixed
  880. */
  881. public function setFirst()
  882. {
  883. Cache::set('u:f:' . Safe::$user['uid'], 1, 15552000);
  884. return $this->successResponse(null, '操作成功!');
  885. }
  886. /**
  887. * 标记用户已经浏览过主页了,用户点了去浏览时调用
  888. * @throws FuncNotFoundException
  889. * @throws ClassNotFoundException
  890. * @throws ReflectionException
  891. * @throws DbException
  892. * @throws ModelNotFoundException
  893. * @throws DataNotFoundException
  894. * @return mixed
  895. */
  896. public function setViewed()
  897. {
  898. // 先获取品牌任务配置及状态
  899. $conf = $this->getBrandConfigAndState();
  900. if ($conf instanceof Json) {
  901. return $conf;
  902. }
  903. if ($conf['state']['view_state'] == 1) {
  904. return $this->successResponse([
  905. 'result' => 2,
  906. 'brand' => $conf,
  907. ], '您已经浏览过了');
  908. }
  909. $date = date('Y-m-d');
  910. $redisKey = $this->getTaskStateRedisKey(intval(Safe::$user['uid']));
  911. // 如果已经完成了另外两项,则标记品牌任务已经完成
  912. $finishState = $conf['state']['finish_state'];
  913. if ($conf['state']['forward_state'] == 1 && $conf['state']['follow_state'] == 1) {
  914. $finishState = 1;
  915. }
  916. $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update([
  917. 'view_state' => 1,
  918. 'finish_state' => $finishState,
  919. ]);
  920. if ($nums) {
  921. $conf['state']['view_state'] = 1;
  922. $conf['state']['finish_state'] = $finishState;
  923. Cache::set($redisKey, json_encode($conf['state']), 86400);
  924. return $this->successResponse([
  925. 'result' => 1,
  926. 'brand' => $conf,
  927. ], '浏览成功');
  928. } else {
  929. return $this->response(601, '更新任务状态失败');
  930. }
  931. }
  932. /**
  933. * 用户完成端外分享时调用接口加票
  934. * @throws FuncNotFoundException
  935. * @throws ClassNotFoundException
  936. * @throws ReflectionException
  937. * @throws DbException
  938. * @throws ModelNotFoundException
  939. * @throws DataNotFoundException
  940. * @return mixed
  941. */
  942. public function shareFinishAddVotes()
  943. {
  944. // 先获取品牌任务配置及状态
  945. $conf = $this->getBrandConfigAndState();
  946. if ($conf instanceof Json) {
  947. return $conf;
  948. }
  949. if ($conf['state']['share_add_votes'] == 1) {
  950. return $this->successResponse([
  951. 'result' => 2,
  952. 'brand' => $conf,
  953. ], '您今日已经领取过投票机会了~');
  954. }
  955. // 调用接口给用户发券(加票)
  956. $weiboRes = (new WeiboService(Safe::$user['uid']))->setcoupons($conf['config']['share']['votes']);
  957. if (empty($weiboRes) || $weiboRes['code'] != 10000) {
  958. return $this->response(600, $weiboRes['msg'] ?? '加票失败~');
  959. }
  960. // 标记状态
  961. $date = date('Y-m-d');
  962. $redisKey = $this->getTaskStateRedisKey(intval(Safe::$user['uid']));
  963. $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update([
  964. 'share_add_votes' => 1,
  965. ]);
  966. if ($nums) {
  967. $conf['state']['share_add_votes'] = 1;
  968. Cache::set($redisKey, json_encode($conf['state']), 86400);
  969. return $this->successResponse([
  970. 'result' => 1,
  971. 'brand' => $conf,
  972. ], '加票成功');
  973. } else {
  974. return $this->response(601, '更新任务状态失败');
  975. }
  976. }
  977. /**
  978. * 完成品牌任务后领取加票机会接口
  979. * @throws FuncNotFoundException
  980. * @throws ClassNotFoundException
  981. * @throws ReflectionException
  982. * @throws DbException
  983. * @throws ModelNotFoundException
  984. * @throws DataNotFoundException
  985. * @return mixed
  986. */
  987. public function taskFinishAddVotes()
  988. {
  989. // 先获取品牌任务配置及状态
  990. $conf = $this->getBrandConfigAndState();
  991. if ($conf instanceof Json) {
  992. return $conf;
  993. }
  994. if ($conf['state']['finish_add_votes'] == 1) {
  995. return $this->successResponse([
  996. 'result' => 2,
  997. 'brand' => $conf,
  998. ], '您今日已经领取过投票机会了~');
  999. }
  1000. // 未完成品牌任务 领取失败
  1001. if ($conf['state']['finish_state'] == 0) {
  1002. return $this->response(603, '请先完成品牌任务,再来领取加票', [
  1003. 'result' => 0,
  1004. 'brand' => $conf,
  1005. ]);
  1006. }
  1007. // 调用微博接口 给用户发券(加票)
  1008. $weiboRes = (new WeiboService(Safe::$user['uid']))->setcoupons($conf['config']['task']['votes']);
  1009. if (empty($weiboRes) || $weiboRes['code'] != 10000) {
  1010. return $this->response(600, $weiboRes['msg'] ?? '加票失败~');
  1011. }
  1012. // 标记加票成功
  1013. $date = date('Y-m-d');
  1014. $redisKey = $this->getTaskStateRedisKey(intval(Safe::$user['uid']));
  1015. $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update([
  1016. 'finish_add_votes' => 1,
  1017. ]);
  1018. if ($nums) {
  1019. $conf['state']['finish_add_votes'] = 1;
  1020. Cache::set($redisKey, json_encode($conf['state']), 86400);
  1021. return $this->successResponse([
  1022. 'result' => 1,
  1023. 'brand' => $conf,
  1024. ], '加票成功');
  1025. } else {
  1026. return $this->response(601, '更新任务状态失败');
  1027. }
  1028. }
  1029. /**
  1030. * 获取品牌任务配置及状态
  1031. * @throws FuncNotFoundException
  1032. * @throws ReflectionException
  1033. * @throws InvalidArgumentException
  1034. * @throws ClassNotFoundException
  1035. * @throws DbException
  1036. * @throws ModelNotFoundException
  1037. * @throws DataNotFoundException
  1038. * @return \think\response\Json|array
  1039. */
  1040. protected function getBrandConfigAndState()
  1041. {
  1042. $config = SystemService::instance()->getData('brand:task:config');
  1043. $date = date('Y-m-d');
  1044. $dateForRedis = date('Ymd');
  1045. if (empty(Safe::$user['uid'])) {
  1046. $state = [
  1047. // 品牌任务完成状态
  1048. 'finish_state' => 0,
  1049. // 关注子任务状态
  1050. 'follow_state' => 0,
  1051. // 转发子任务状态
  1052. 'forward_state' => 0,
  1053. // 查看浏览主页任务状态
  1054. 'view_state' => 0,
  1055. // 品牌任务完成后是否加票
  1056. 'finish_add_votes' => 0,
  1057. // 分享完成后是否加票
  1058. 'share_add_votes' => 0,
  1059. 'uid' => 0,
  1060. 'date' => $date,
  1061. ];
  1062. } else {
  1063. $redisKey = "t:{$dateForRedis}:" . Safe::$user['uid'];
  1064. $state = Cache::get($redisKey);
  1065. if (empty($state)) {
  1066. // 缓存失败 查数据库
  1067. $state = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->find();
  1068. if (empty($state)) {
  1069. // 当天第一次,初始化数据
  1070. $state = [
  1071. // 品牌任务完成状态
  1072. 'finish_state' => 0,
  1073. // 关注子任务状态
  1074. 'follow_state' => 0,
  1075. // 转发子任务状态
  1076. 'forward_state' => 0,
  1077. // 查看浏览主页任务状态
  1078. 'view_state' => 0,
  1079. // 品牌任务完成后是否加票
  1080. 'finish_add_votes' => 0,
  1081. // 分享完成后是否加票
  1082. 'share_add_votes' => 0,
  1083. 'uid' => Safe::$user['uid'],
  1084. 'date' => $date,
  1085. ];
  1086. if (0 == Db::table('awards_user_task')->insert($state)) {
  1087. return $this->response(5001, '系统错误,请稍后再试~');
  1088. }
  1089. }
  1090. Cache::set($redisKey, json_encode($state), 86400);
  1091. } else {
  1092. $state = json_decode($state, true);
  1093. }
  1094. // 如果未关注状态,查询下已经实际已经关注了
  1095. if ($state['follow_state'] == 0) {
  1096. $weiboRes = (new WeiboService(Safe::$user['uid']))->friends($config['follow']['id']);
  1097. if (isset($weiboRes['data'][$config['follow']['id']]) && $weiboRes['data'][$config['follow']['id']] == 1) {
  1098. $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update(['follow_state' => 1]);
  1099. if ($nums) {
  1100. $state['follow_state'] = 1;
  1101. Cache::set($redisKey, json_encode($state), 86400);
  1102. }
  1103. }
  1104. }
  1105. }
  1106. // 校准状态,可能存在异常情况关注,转发,浏览都完成了,但是总的品牌任务状态没标记成已完成
  1107. if ($state['follow_state'] == 1 && $state['forward_state'] == 1 && $state['view_state'] == 1 && $state['finish_state'] != 1) {
  1108. $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update(['finish_state' => 1]);
  1109. if ($nums) {
  1110. $state['finish_state'] = 1;
  1111. Cache::set($redisKey, json_encode($state), 86400);
  1112. }
  1113. }
  1114. return [
  1115. 'config' => $config,
  1116. 'state' => $state,
  1117. ];
  1118. }
  1119. /**
  1120. * 获取品牌任务redis 缓存键
  1121. * @param int $uid
  1122. * @return string
  1123. */
  1124. protected function getTaskStateRedisKey(int $uid): string
  1125. {
  1126. $dateForRedis = date('Ymd');
  1127. return "t:{$dateForRedis}:" . $uid;
  1128. }
  1129. /**
  1130. * @param $code
  1131. * @param $msg
  1132. * @param $data
  1133. * @return Json
  1134. */
  1135. protected function response($code, $msg, $data = null)
  1136. {
  1137. return json(
  1138. [
  1139. 'code' => $code,
  1140. 'message' => $msg,
  1141. 'data' => $data,
  1142. 'trackID' => Log::$logID,
  1143. ],
  1144. 200
  1145. );
  1146. }
  1147. /**
  1148. * @param $data
  1149. * @param $msg
  1150. * @return mixed
  1151. */
  1152. protected function successResponse($data, $msg = '操作成功')
  1153. {
  1154. return $this->response(0, $msg, $data);
  1155. }
  1156. }