Index.php 42 KB

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