Index.php 44 KB

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