Index.php 42 KB

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