Index.php 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285
  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' => 4,
  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.number >= 15 group by l.uid order by l.id desc limit 100");
  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. /**
  592. * 活动专属周边礼包 id 4:中奖概率30%,每天中出去1个
  593. * 电影票代金券 id 5:中奖概率40%,每天中出去2个
  594. */
  595. $rate = [4 => 30, 5 => 40];
  596. /**
  597. * 活动专属周边礼包 id 4:每天中出去1个
  598. * 电影票代金券 id 5:每天中出去2个
  599. */
  600. $giftDayCount = [4 => 1, 5 => 2];
  601. $limit = 100;
  602. $userRate = rand(1, $limit);
  603. $giftId = 0;
  604. $beginAt = strtotime(date('Y-m-d',time()));
  605. $endAt = time();
  606. foreach ($gifts as $gift) {
  607. // 判断奖品库存
  608. $orderCount = Db::table('awards_order')
  609. ->where('type', 1)
  610. ->where('gift_id', $gift['id'])
  611. ->count('id');
  612. if ($orderCount >= $gift['count']) {
  613. continue;
  614. }
  615. // 判断奖品当天是否有中奖记录
  616. $orderCount = Db::table('awards_order')
  617. ->where('type', 1)
  618. ->where('gift_id', $gift['id'])
  619. ->where('create_at', '>=', $beginAt)
  620. ->where('create_at', '<', $endAt)
  621. ->count('id');
  622. if ($orderCount >= $giftDayCount[$gift['id']]) {
  623. continue;
  624. }
  625. // 判断是否中奖
  626. if ($userRate <= $rate[$gift['id']]) {
  627. $isWinAward = 1;
  628. $giftId = $gift['id'];
  629. }
  630. // 如果参与活动人数 5000人内 有人中奖过,那么不在计算。
  631. // $total = $logCount + 1;
  632. // if ($orderCount == floor($total/$limit)) {
  633. // $isWinAward = 0;
  634. // $giftId = 0;
  635. // break;
  636. // }
  637. //
  638. // for ($i=0;$i<$gift['count'];$i++) {
  639. // $giftRage = rand(0, $limit);
  640. // if ($userRate == $giftRage) {
  641. // $isWinAward = 1;
  642. // $giftId = $gift['id'];
  643. // break;
  644. // }
  645. // }
  646. //
  647. // // 如果参与活动人数 5000人内 还没有人中奖那最后一人必中。
  648. // if ($orderCount < floor($total/$limit) && $isWinAward ==0 && ($total%$limit) == 0) {
  649. // $isWinAward = 1;
  650. // $giftId = $gift['id'];
  651. // break;
  652. // }
  653. }
  654. // 如果中奖 插入用户中奖信息
  655. if ($isWinAward == 1) {
  656. $order = [
  657. 'uid' => Safe::$user['uid'],
  658. 'gift_id' => $giftId,
  659. 'type' => 1,
  660. 'create_at' => time(),
  661. ];
  662. if (0 == Db::table('awards_order')->insert($order)) {
  663. return $this->response(5001, '系统错误,请稍后再试~');
  664. }
  665. }
  666. return $this->successResponse([
  667. 'isWinAward' => $isWinAward,
  668. 'giftId' => $giftId
  669. ]);
  670. }
  671. /**
  672. * 奖品记录接口
  673. * @throws FuncNotFoundException
  674. * @throws ClassNotFoundException
  675. * @throws DbException
  676. * @throws ModelNotFoundException
  677. * @throws DataNotFoundException
  678. * @return mixed
  679. */
  680. public function getAwardLog()
  681. {
  682. $awards = Db::table('awards_order')->alias('o')
  683. ->leftJoin('awards_gift g', 'o.gift_id = g.id')
  684. ->field(['g.name as giftName', 'o.*'])
  685. ->where('o.uid', Safe::$user['uid'])
  686. ->select();
  687. $res = [];
  688. foreach ($awards as $val) {
  689. $type = '抽奖';
  690. if ($val['type'] == 2) {
  691. $type = '排名奖';
  692. }
  693. $isReceive = 1;
  694. if (empty($val['name']) && empty($val['address']) && empty($val['mobile'])) {
  695. $isReceive = 0;
  696. }
  697. $res[] = [
  698. 'giftId' => $val['gift_id'],
  699. 'giftName' => $val['giftName'],
  700. 'typeLabel' => $type,
  701. 'type' => $val['type'],
  702. 'createAt' => date('y-m-d h:i:s', $val['create_at']),
  703. 'address' => $val['address'],
  704. 'name' => $val['name'],
  705. 'mobile' => $val['mobile'],
  706. 'isReceive' => $isReceive
  707. ];
  708. }
  709. return $this->successResponse($res);
  710. }
  711. public function clearGameData()
  712. {
  713. // 更新用户参与状态
  714. Db::table('awards_user_info')->where('uid', Safe::$user['uid'])->update([
  715. 'is_share' => 0,
  716. 'share_at' => 0,
  717. 'count' => 1,
  718. ]);
  719. Db::table('awards_user_task_log')->where('uid', Safe::$user['uid'])->delete();
  720. Db::table('awards_order')->where('uid', Safe::$user['uid'])->delete();
  721. return $this->successResponse(null, '清除成功');
  722. }
  723. /**
  724. * 品牌任务关注接口
  725. * @throws FuncNotFoundException
  726. * @throws ClassNotFoundException
  727. * @throws ReflectionException
  728. * @throws DbException
  729. * @throws ModelNotFoundException
  730. * @throws DataNotFoundException
  731. * @return mixed
  732. */
  733. public function follow()
  734. {
  735. // 先获取品牌任务配置及状态
  736. $conf = $this->getBrandConfigAndState();
  737. if ($conf instanceof Json) {
  738. return $conf;
  739. }
  740. // 已经关注过了
  741. if ($conf['state']['follow_state'] == 1) {
  742. return $this->successResponse([
  743. 'result' => 2,
  744. 'brand' => $conf,
  745. ], '您已经关注过了');
  746. }
  747. // 调用接口关注
  748. $weiboRes = (new WeiboService(Safe::$user['uid']))->add($conf['config']['follow']['id']);
  749. if (empty($weiboRes) || $weiboRes['code'] != 10000) {
  750. return $this->response(600, $weiboRes['msg'] ?? '关注失败~');
  751. }
  752. $date = date('Y-m-d');
  753. $redisKey = $this->getTaskStateRedisKey(intval(Safe::$user['uid']));
  754. // 如果已经完成了另外两项,则标记品牌任务已经完成
  755. $finishState = $conf['state']['finish_state'];
  756. if ($conf['state']['view_state'] == 1 && $conf['state']['forward_state'] == 1) {
  757. $finishState = 1;
  758. }
  759. // 更新任务状态
  760. $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update([
  761. 'follow_state' => 1,
  762. 'finish_state' => $finishState,
  763. ]);
  764. if ($nums) {
  765. $conf['state']['follow_state'] = 1;
  766. $conf['state']['finish_state'] = $finishState;
  767. Cache::set($redisKey, json_encode($conf['state']), 86400);
  768. return $this->successResponse([
  769. 'result' => 1,
  770. 'brand' => $conf,
  771. ], '关注成功');
  772. } else {
  773. return $this->response(601, '更新任务状态失败');
  774. }
  775. }
  776. /**
  777. * 品牌任务转发博文接口
  778. * @throws FuncNotFoundException
  779. * @throws ClassNotFoundException
  780. * @throws ReflectionException
  781. * @throws DbException
  782. * @throws ModelNotFoundException
  783. * @throws DataNotFoundException
  784. * @return mixed
  785. */
  786. public function forward()
  787. {
  788. // 先获取品牌任务配置及状态
  789. $conf = $this->getBrandConfigAndState();
  790. if ($conf instanceof Json) {
  791. return $conf;
  792. }
  793. // 已经转发过了
  794. if ($conf['state']['forward_state'] == 1) {
  795. return $this->successResponse([
  796. 'result' => 2,
  797. 'brand' => $conf,
  798. ], '您已经转发过了');
  799. }
  800. // 调用微博接口转发
  801. $weiboRes = (new WeiboService(Safe::$user['uid']))->repost($conf['config']['forward']['id'], $conf['config']['forward']['content'] ?? '');
  802. if (empty($weiboRes) || $weiboRes['code'] != 10000) {
  803. return $this->response(600, $weiboRes['msg'] ?? '转发失败~');
  804. }
  805. $date = date('Y-m-d');
  806. $redisKey = $this->getTaskStateRedisKey(intval(Safe::$user['uid']));
  807. // 如果已经完成了另外两项,则标记品牌任务已经完成
  808. $finishState = $conf['state']['finish_state'];
  809. if ($conf['state']['view_state'] == 1 && $conf['state']['follow_state'] == 1) {
  810. $finishState = 1;
  811. }
  812. // 更新任务状态
  813. $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update([
  814. 'forward_state' => 1,
  815. 'finish_state' => $finishState,
  816. ]);
  817. if ($nums) {
  818. $conf['state']['forward_state'] = 1;
  819. $conf['state']['finish_state'] = $finishState;
  820. Cache::set($redisKey, json_encode($conf['state']), 86400);
  821. return $this->successResponse([
  822. 'result' => 1,
  823. 'brand' => $conf,
  824. ], '转发成功');
  825. } else {
  826. return $this->response(601, '更新任务状态失败');
  827. }
  828. }
  829. /**
  830. * 获取活动规则
  831. * @throws FuncNotFoundException
  832. * @throws ReflectionException
  833. * @throws InvalidArgumentException
  834. * @throws ClassNotFoundException
  835. * @return mixed
  836. */
  837. public function getRule()
  838. {
  839. $config = SystemService::instance()->getData('activity:rule');
  840. return $this->successResponse($config);
  841. }
  842. /**
  843. * 聚合页配置信息
  844. * @throws FuncNotFoundException
  845. * @throws ReflectionException
  846. * @throws InvalidArgumentException
  847. * @throws ClassNotFoundException
  848. * @return mixed
  849. */
  850. public function groupPageConfig()
  851. {
  852. return $this->successResponse(['config' => SystemService::instance()->getData('group:page:config')]);
  853. }
  854. // public function index()
  855. // {
  856. // $this->redirect('video');
  857. // }
  858. /**
  859. * 聚合页通知获取
  860. * 查询最近20条通知
  861. * @throws DbException
  862. * @throws ModelNotFoundException
  863. * @throws DataNotFoundException
  864. * @return mixed
  865. */
  866. public function notices()
  867. {
  868. $rows = SystemNotice::limit(20)->order('id', 'desc')->select();
  869. return $this->successResponse([
  870. "lists" => $rows,
  871. ]);
  872. }
  873. /**
  874. * 首次弹窗
  875. * 发放邀请函微博
  876. * @return Json
  877. */
  878. public function sendInviteWeibo()
  879. {
  880. $config = SystemService::instance()->getData('group:page:config');
  881. $sendRes = (new WeiboService(Safe::$user['uid']))->status($config['firstDialog']['content']);
  882. if (empty($sendRes) || $sendRes['code'] != 10000) {
  883. return $this->response(403, $sendRes['msg'] ?? '发布失败');
  884. }
  885. Cache::set('u:f:' . Safe::$user['uid'], 1, 15552000);
  886. return $this->successResponse(null, '发布成功!');
  887. }
  888. /**
  889. * 首次弹窗用户未选择发送邀请函
  890. * 标记已经首次弹窗过了
  891. * @return mixed
  892. */
  893. public function setFirst()
  894. {
  895. Cache::set('u:f:' . Safe::$user['uid'], 1, 15552000);
  896. return $this->successResponse(null, '操作成功!');
  897. }
  898. /**
  899. * 标记用户已经浏览过主页了,用户点了去浏览时调用
  900. * @throws FuncNotFoundException
  901. * @throws ClassNotFoundException
  902. * @throws ReflectionException
  903. * @throws DbException
  904. * @throws ModelNotFoundException
  905. * @throws DataNotFoundException
  906. * @return mixed
  907. */
  908. public function setViewed()
  909. {
  910. // 先获取品牌任务配置及状态
  911. $conf = $this->getBrandConfigAndState();
  912. if ($conf instanceof Json) {
  913. return $conf;
  914. }
  915. if ($conf['state']['view_state'] == 1) {
  916. return $this->successResponse([
  917. 'result' => 2,
  918. 'brand' => $conf,
  919. ], '您已经浏览过了');
  920. }
  921. $date = date('Y-m-d');
  922. $redisKey = $this->getTaskStateRedisKey(intval(Safe::$user['uid']));
  923. // 如果已经完成了另外两项,则标记品牌任务已经完成
  924. $finishState = $conf['state']['finish_state'];
  925. if ($conf['state']['forward_state'] == 1 && $conf['state']['follow_state'] == 1) {
  926. $finishState = 1;
  927. }
  928. $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update([
  929. 'view_state' => 1,
  930. 'finish_state' => $finishState,
  931. ]);
  932. if ($nums) {
  933. $conf['state']['view_state'] = 1;
  934. $conf['state']['finish_state'] = $finishState;
  935. Cache::set($redisKey, json_encode($conf['state']), 86400);
  936. return $this->successResponse([
  937. 'result' => 1,
  938. 'brand' => $conf,
  939. ], '浏览成功');
  940. } else {
  941. return $this->response(601, '更新任务状态失败');
  942. }
  943. }
  944. /**
  945. * 用户完成端外分享时调用接口加票
  946. * @throws FuncNotFoundException
  947. * @throws ClassNotFoundException
  948. * @throws ReflectionException
  949. * @throws DbException
  950. * @throws ModelNotFoundException
  951. * @throws DataNotFoundException
  952. * @return mixed
  953. */
  954. public function shareFinishAddVotes()
  955. {
  956. // 先获取品牌任务配置及状态
  957. $conf = $this->getBrandConfigAndState();
  958. if ($conf instanceof Json) {
  959. return $conf;
  960. }
  961. if ($conf['state']['share_add_votes'] == 1) {
  962. return $this->successResponse([
  963. 'result' => 2,
  964. 'brand' => $conf,
  965. ], '您今日已经领取过投票机会了~');
  966. }
  967. // 调用接口给用户发券(加票)
  968. $weiboRes = (new WeiboService(Safe::$user['uid']))->setcoupons($conf['config']['share']['votes']);
  969. if (empty($weiboRes) || $weiboRes['code'] != 10000) {
  970. return $this->response(600, $weiboRes['msg'] ?? '加票失败~');
  971. }
  972. // 标记状态
  973. $date = date('Y-m-d');
  974. $redisKey = $this->getTaskStateRedisKey(intval(Safe::$user['uid']));
  975. $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update([
  976. 'share_add_votes' => 1,
  977. ]);
  978. if ($nums) {
  979. $conf['state']['share_add_votes'] = 1;
  980. Cache::set($redisKey, json_encode($conf['state']), 86400);
  981. return $this->successResponse([
  982. 'result' => 1,
  983. 'brand' => $conf,
  984. ], '加票成功');
  985. } else {
  986. return $this->response(601, '更新任务状态失败');
  987. }
  988. }
  989. /**
  990. * 完成品牌任务后领取加票机会接口
  991. * @throws FuncNotFoundException
  992. * @throws ClassNotFoundException
  993. * @throws ReflectionException
  994. * @throws DbException
  995. * @throws ModelNotFoundException
  996. * @throws DataNotFoundException
  997. * @return mixed
  998. */
  999. public function taskFinishAddVotes()
  1000. {
  1001. // 先获取品牌任务配置及状态
  1002. $conf = $this->getBrandConfigAndState();
  1003. if ($conf instanceof Json) {
  1004. return $conf;
  1005. }
  1006. if ($conf['state']['finish_add_votes'] == 1) {
  1007. return $this->successResponse([
  1008. 'result' => 2,
  1009. 'brand' => $conf,
  1010. ], '您今日已经领取过投票机会了~');
  1011. }
  1012. // 未完成品牌任务 领取失败
  1013. if ($conf['state']['finish_state'] == 0) {
  1014. return $this->response(603, '请先完成品牌任务,再来领取加票', [
  1015. 'result' => 0,
  1016. 'brand' => $conf,
  1017. ]);
  1018. }
  1019. // 调用微博接口 给用户发券(加票)
  1020. $weiboRes = (new WeiboService(Safe::$user['uid']))->setcoupons($conf['config']['task']['votes']);
  1021. if (empty($weiboRes) || $weiboRes['code'] != 10000) {
  1022. return $this->response(600, $weiboRes['msg'] ?? '加票失败~');
  1023. }
  1024. // 标记加票成功
  1025. $date = date('Y-m-d');
  1026. $redisKey = $this->getTaskStateRedisKey(intval(Safe::$user['uid']));
  1027. $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update([
  1028. 'finish_add_votes' => 1,
  1029. ]);
  1030. if ($nums) {
  1031. $conf['state']['finish_add_votes'] = 1;
  1032. Cache::set($redisKey, json_encode($conf['state']), 86400);
  1033. return $this->successResponse([
  1034. 'result' => 1,
  1035. 'brand' => $conf,
  1036. ], '加票成功');
  1037. } else {
  1038. return $this->response(601, '更新任务状态失败');
  1039. }
  1040. }
  1041. /**
  1042. * 获取品牌任务配置及状态
  1043. * @throws FuncNotFoundException
  1044. * @throws ReflectionException
  1045. * @throws InvalidArgumentException
  1046. * @throws ClassNotFoundException
  1047. * @throws DbException
  1048. * @throws ModelNotFoundException
  1049. * @throws DataNotFoundException
  1050. * @return \think\response\Json|array
  1051. */
  1052. protected function getBrandConfigAndState()
  1053. {
  1054. $config = SystemService::instance()->getData('brand:task:config');
  1055. $date = date('Y-m-d');
  1056. $dateForRedis = date('Ymd');
  1057. if (empty(Safe::$user['uid'])) {
  1058. $state = [
  1059. // 品牌任务完成状态
  1060. 'finish_state' => 0,
  1061. // 关注子任务状态
  1062. 'follow_state' => 0,
  1063. // 转发子任务状态
  1064. 'forward_state' => 0,
  1065. // 查看浏览主页任务状态
  1066. 'view_state' => 0,
  1067. // 品牌任务完成后是否加票
  1068. 'finish_add_votes' => 0,
  1069. // 分享完成后是否加票
  1070. 'share_add_votes' => 0,
  1071. 'uid' => 0,
  1072. 'date' => $date,
  1073. ];
  1074. } else {
  1075. $redisKey = "t:{$dateForRedis}:" . Safe::$user['uid'];
  1076. $state = Cache::get($redisKey);
  1077. if (empty($state)) {
  1078. // 缓存失败 查数据库
  1079. $state = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->find();
  1080. if (empty($state)) {
  1081. // 当天第一次,初始化数据
  1082. $state = [
  1083. // 品牌任务完成状态
  1084. 'finish_state' => 0,
  1085. // 关注子任务状态
  1086. 'follow_state' => 0,
  1087. // 转发子任务状态
  1088. 'forward_state' => 0,
  1089. // 查看浏览主页任务状态
  1090. 'view_state' => 0,
  1091. // 品牌任务完成后是否加票
  1092. 'finish_add_votes' => 0,
  1093. // 分享完成后是否加票
  1094. 'share_add_votes' => 0,
  1095. 'uid' => Safe::$user['uid'],
  1096. 'date' => $date,
  1097. ];
  1098. if (0 == Db::table('awards_user_task')->insert($state)) {
  1099. return $this->response(5001, '系统错误,请稍后再试~');
  1100. }
  1101. }
  1102. Cache::set($redisKey, json_encode($state), 86400);
  1103. } else {
  1104. $state = json_decode($state, true);
  1105. }
  1106. // 如果未关注状态,查询下已经实际已经关注了
  1107. if ($state['follow_state'] == 0) {
  1108. $weiboRes = (new WeiboService(Safe::$user['uid']))->friends($config['follow']['id']);
  1109. if (isset($weiboRes['data'][$config['follow']['id']]) && $weiboRes['data'][$config['follow']['id']] == 1) {
  1110. $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update(['follow_state' => 1]);
  1111. if ($nums) {
  1112. $state['follow_state'] = 1;
  1113. Cache::set($redisKey, json_encode($state), 86400);
  1114. }
  1115. }
  1116. }
  1117. }
  1118. // 校准状态,可能存在异常情况关注,转发,浏览都完成了,但是总的品牌任务状态没标记成已完成
  1119. if ($state['follow_state'] == 1 && $state['forward_state'] == 1 && $state['view_state'] == 1 && $state['finish_state'] != 1) {
  1120. $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update(['finish_state' => 1]);
  1121. if ($nums) {
  1122. $state['finish_state'] = 1;
  1123. Cache::set($redisKey, json_encode($state), 86400);
  1124. }
  1125. }
  1126. return [
  1127. 'config' => $config,
  1128. 'state' => $state,
  1129. ];
  1130. }
  1131. /**
  1132. * 获取品牌任务redis 缓存键
  1133. * @param int $uid
  1134. * @return string
  1135. */
  1136. protected function getTaskStateRedisKey(int $uid): string
  1137. {
  1138. $dateForRedis = date('Ymd');
  1139. return "t:{$dateForRedis}:" . $uid;
  1140. }
  1141. /**
  1142. * @param $code
  1143. * @param $msg
  1144. * @param $data
  1145. * @return Json
  1146. */
  1147. protected function response($code, $msg, $data = null)
  1148. {
  1149. return json(
  1150. [
  1151. 'code' => $code,
  1152. 'message' => $msg,
  1153. 'data' => $data,
  1154. 'trackID' => Log::$logID,
  1155. ],
  1156. 200
  1157. );
  1158. }
  1159. /**
  1160. * @param $data
  1161. * @param $msg
  1162. * @return mixed
  1163. */
  1164. protected function successResponse($data, $msg = '操作成功')
  1165. {
  1166. return $this->response(0, $msg, $data);
  1167. }
  1168. }