Index.php 43 KB

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