Index.php 43 KB

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