Index.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  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\response\Json;
  9. use think\facade\Request;
  10. use think\admin\Controller;
  11. use InvalidArgumentException;
  12. use app\service\WeiboService;
  13. use app\admin\model\SystemNotice;
  14. use think\facade\Log as FacadeLog;
  15. use think\db\exception\DbException;
  16. use think\admin\service\SystemService;
  17. use think\exception\FuncNotFoundException;
  18. use think\exception\ClassNotFoundException;
  19. use think\db\exception\DataNotFoundException;
  20. use think\db\exception\ModelNotFoundException;
  21. /**
  22. * Class Index
  23. * @package app\index\controller
  24. */
  25. class Index extends Controller
  26. {
  27. /**
  28. * 获取品牌任务相关配置
  29. *
  30. * @throws FuncNotFoundException
  31. * @throws ClassNotFoundException
  32. * @throws ReflectionException
  33. * @throws DbException
  34. * @throws ModelNotFoundException
  35. * @throws DataNotFoundException
  36. * @return mixed
  37. */
  38. public function brand()
  39. {
  40. $conf = $this->getBrandConfigAndState();
  41. if ($conf instanceof Json) {
  42. return $conf;
  43. }
  44. return $this->successResponse($conf);
  45. }
  46. /**
  47. * 检测登录 通过cookie中的SUB字段的内容,调用用户接口检测登录状态
  48. * @throws ClassNotFoundException
  49. * @throws ReflectionException
  50. * @return mixed
  51. */
  52. public function checkLogin()
  53. {
  54. FacadeLog::info("cookies:" . json_encode(Request::post()));
  55. $sub = "";
  56. $uid = 0;
  57. if (!empty($_COOKIE['SUB'])) {
  58. $sub = $_COOKIE['SUB'];
  59. } else {
  60. // 只在调试模式下开启从POST参数中获取UID,方便测试联调
  61. if (env('app_debug')) {
  62. $sub = Request::post('cookie');
  63. $uid = $sub;
  64. } else {
  65. return $this->response(403, 'not login.');
  66. }
  67. }
  68. FacadeLog::info($sub);
  69. $userInfoRes = (new WeiboService($uid))->userinfo($sub);
  70. if (empty($userInfoRes) || $userInfoRes['code'] != 10000) {
  71. return $this->response(403, $userInfoRes['msg'] ?? '没有登录');
  72. }
  73. // 使用客户端信息生成token
  74. $token = md5($_SERVER['HTTP_USER_AGENT'] . $_SERVER['HTTP_ACCEPT_ENCODING'] . $_SERVER['HTTP_ACCEPT_LANGUAGE'] . $_SERVER['HTTP_REFERER'] . get_client_ip(0) . $userInfoRes['data']['uid']);
  75. $user = $userInfoRes['data'];
  76. // 生成加密用的密钥和向量
  77. $cipher = "aes-256-gcm";
  78. $ivlen = openssl_cipher_iv_length($cipher);
  79. $iv = bin2hex(openssl_random_pseudo_bytes($ivlen));
  80. $aesKey = bin2hex(openssl_random_pseudo_bytes(32));
  81. $user = array_merge($user, [
  82. 'aes_key' => $aesKey,
  83. 'ase_iv' => $iv,
  84. ]);
  85. $cacheUser = [
  86. 'aes_key' => $aesKey,
  87. 'ase_iv' => $iv,
  88. 'uid' => $user['uid'],
  89. ];
  90. $first = 0;
  91. $date = date('Y-m-d');
  92. $userTask = Db::table('awards_user_task')->where('uid', $user['uid'])->where('date', $date)->find();
  93. if (empty($userTask)) {
  94. // 当天第一次,初始化数据
  95. $state = [
  96. // 品牌任务完成状态
  97. 'finish_state' => 0,
  98. // 关注子任务状态
  99. 'follow_state' => 0,
  100. // 转发子任务状态
  101. 'forward_state' => 0,
  102. // 查看浏览主页任务状态
  103. 'view_state' => 0,
  104. // 品牌任务完成后是否加票
  105. 'finish_add_votes' => 0,
  106. // 分享完成后是否加票
  107. 'share_add_votes' => 0,
  108. 'uid' => $user['uid'],
  109. 'date' => $date,
  110. ];
  111. if (0 == Db::table('awards_user_task')->insert($state)) {
  112. return $this->response(5001, '系统错误,请稍后再试~');
  113. }
  114. } else if ($userTask['finish_state'] == 0 && $userTask['share_add_votes'] == 0) {
  115. $count = Db::table('awards_user_task_log')->where('uid', Safe::$user['uid'])->where('date', $date)->count('id');
  116. if ($count < 1) {
  117. $first = 1;
  118. }
  119. } else if ($userTask['finish_state'] > 0 && $userTask['share_add_votes'] > 0) {
  120. $count = Db::table('awards_user_task_log')->where('uid', Safe::$user['uid'])->where('date', $date)->count('id');
  121. if ($count < 2) {
  122. $first = 1;
  123. }
  124. }
  125. // 缓存用户信息1天
  126. Cache::set('u:' . $token, json_encode($cacheUser), 86400);
  127. return $this->successResponse([
  128. 'user' => $user,
  129. 'token' => $token,
  130. 'first' => $first,
  131. ]);
  132. }
  133. /**
  134. * 提交任务接口
  135. * @throws FuncNotFoundException
  136. * @throws ClassNotFoundException
  137. * @throws DbException
  138. * @throws ModelNotFoundException
  139. * @throws DataNotFoundException
  140. * @return mixed
  141. */
  142. public function submitTask()
  143. {
  144. $duration = Request::post("duration");
  145. $number = Request::post("number");
  146. $date = date('Y-m-d');
  147. $userTask = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->find();
  148. if (empty($userTask)) {
  149. return $this->response(403, '没有登录');
  150. }
  151. $log = [];
  152. if ($userTask['finish_state'] == 0) {
  153. $count = Db::table('awards_user_task_log')->where('uid', Safe::$user['uid'])->where('date', $date)->count('id');
  154. if ($count > 0) {
  155. return $this->response(403, '没有参与次数');
  156. }
  157. $log = [
  158. 'uid' => Safe::$user['uid'],
  159. 'date' => $date,
  160. 'duration' => $duration,
  161. 'number' => $number,
  162. 'create_at' => time()
  163. ];
  164. Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update([
  165. 'finish_state' => 1,
  166. ]);
  167. } else if ($userTask['share_add_votes'] == 1) {
  168. $count = Db::table('awards_user_task_log')->where('uid', Safe::$user['uid'])->where('date', $date)->count('id');
  169. if ($count > 1) {
  170. return $this->response(403, '没有参与次数');
  171. }
  172. $log = [
  173. 'uid' => Safe::$user['uid'],
  174. 'date' => $date,
  175. 'duration' => $duration,
  176. 'number' => $number,
  177. 'create_at' => time()
  178. ];
  179. }
  180. if (empty($log)) {
  181. return $this->response(403, '没有参与次数');
  182. }
  183. if (0 == Db::table('awards_user_task_log')->insert($log)) {
  184. return $this->response(5001, '系统错误,请稍后再试~');
  185. }
  186. return $this->successResponse(null, '提交成功');
  187. }
  188. /**
  189. * 提交发布微博接口
  190. * @throws FuncNotFoundException
  191. * @throws ClassNotFoundException
  192. * @throws DbException
  193. * @throws ModelNotFoundException
  194. * @throws DataNotFoundException
  195. * @return mixed
  196. */
  197. public function submitShare()
  198. {
  199. $date = date('Y-m-d');
  200. $userTask = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->find();
  201. if (empty($userTask)) {
  202. return $this->response(403, '没有登录');
  203. }
  204. if ($userTask['share_add_votes'] > 0) {
  205. return $this->response(601, '今日已发布');
  206. }
  207. // 更新任务状态
  208. $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update([
  209. 'share_add_votes' => 1,
  210. ]);
  211. if ($nums) {
  212. return $this->response(601, '发布成功');
  213. } else {
  214. return $this->response(601, '发布失败');
  215. }
  216. }
  217. /**
  218. * 排行接口
  219. * @throws FuncNotFoundException
  220. * @throws ClassNotFoundException
  221. * @throws DbException
  222. * @throws ModelNotFoundException
  223. * @throws DataNotFoundException
  224. * @return mixed
  225. */
  226. public function ranking()
  227. {
  228. $date = date('Y-m-d');
  229. $res = Db::table('awards_user_task_log')->alias("l")
  230. ->leftJoin('awards_user_info u', 'l.uid = u.uid')
  231. ->field(['u.nickname', 'u.portrait', 'u.uid', 'l.duration', 'l.number'])
  232. ->where('l.date', $date)
  233. ->order('l.number', 'desc')
  234. ->order('l.duration', 'asc')
  235. ->limit(15)
  236. ->select();
  237. return $this->successResponse($res);
  238. }
  239. /**
  240. * 品牌任务关注接口
  241. * @throws FuncNotFoundException
  242. * @throws ClassNotFoundException
  243. * @throws ReflectionException
  244. * @throws DbException
  245. * @throws ModelNotFoundException
  246. * @throws DataNotFoundException
  247. * @return mixed
  248. */
  249. public function follow()
  250. {
  251. // 先获取品牌任务配置及状态
  252. $conf = $this->getBrandConfigAndState();
  253. if ($conf instanceof Json) {
  254. return $conf;
  255. }
  256. // 已经关注过了
  257. if ($conf['state']['follow_state'] == 1) {
  258. return $this->successResponse([
  259. 'result' => 2,
  260. 'brand' => $conf,
  261. ], '您已经关注过了');
  262. }
  263. // 调用接口关注
  264. $weiboRes = (new WeiboService(Safe::$user['uid']))->add($conf['config']['follow']['id']);
  265. if (empty($weiboRes) || $weiboRes['code'] != 10000) {
  266. return $this->response(600, $weiboRes['msg'] ?? '关注失败~');
  267. }
  268. $date = date('Y-m-d');
  269. $redisKey = $this->getTaskStateRedisKey(intval(Safe::$user['uid']));
  270. // 如果已经完成了另外两项,则标记品牌任务已经完成
  271. $finishState = $conf['state']['finish_state'];
  272. if ($conf['state']['view_state'] == 1 && $conf['state']['forward_state'] == 1) {
  273. $finishState = 1;
  274. }
  275. // 更新任务状态
  276. $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update([
  277. 'follow_state' => 1,
  278. 'finish_state' => $finishState,
  279. ]);
  280. if ($nums) {
  281. $conf['state']['follow_state'] = 1;
  282. $conf['state']['finish_state'] = $finishState;
  283. Cache::set($redisKey, json_encode($conf['state']), 86400);
  284. return $this->successResponse([
  285. 'result' => 1,
  286. 'brand' => $conf,
  287. ], '关注成功');
  288. } else {
  289. return $this->response(601, '更新任务状态失败');
  290. }
  291. }
  292. /**
  293. * 品牌任务转发博文接口
  294. * @throws FuncNotFoundException
  295. * @throws ClassNotFoundException
  296. * @throws ReflectionException
  297. * @throws DbException
  298. * @throws ModelNotFoundException
  299. * @throws DataNotFoundException
  300. * @return mixed
  301. */
  302. public function forward()
  303. {
  304. // 先获取品牌任务配置及状态
  305. $conf = $this->getBrandConfigAndState();
  306. if ($conf instanceof Json) {
  307. return $conf;
  308. }
  309. // 已经转发过了
  310. if ($conf['state']['forward_state'] == 1) {
  311. return $this->successResponse([
  312. 'result' => 2,
  313. 'brand' => $conf,
  314. ], '您已经转发过了');
  315. }
  316. // 调用微博接口转发
  317. $weiboRes = (new WeiboService(Safe::$user['uid']))->repost($conf['config']['forward']['id'], $conf['config']['forward']['content'] ?? '');
  318. if (empty($weiboRes) || $weiboRes['code'] != 10000) {
  319. return $this->response(600, $weiboRes['msg'] ?? '转发失败~');
  320. }
  321. $date = date('Y-m-d');
  322. $redisKey = $this->getTaskStateRedisKey(intval(Safe::$user['uid']));
  323. // 如果已经完成了另外两项,则标记品牌任务已经完成
  324. $finishState = $conf['state']['finish_state'];
  325. if ($conf['state']['view_state'] == 1 && $conf['state']['follow_state'] == 1) {
  326. $finishState = 1;
  327. }
  328. // 更新任务状态
  329. $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update([
  330. 'forward_state' => 1,
  331. 'finish_state' => $finishState,
  332. ]);
  333. if ($nums) {
  334. $conf['state']['forward_state'] = 1;
  335. $conf['state']['finish_state'] = $finishState;
  336. Cache::set($redisKey, json_encode($conf['state']), 86400);
  337. return $this->successResponse([
  338. 'result' => 1,
  339. 'brand' => $conf,
  340. ], '转发成功');
  341. } else {
  342. return $this->response(601, '更新任务状态失败');
  343. }
  344. }
  345. /**
  346. * 获取活动规则
  347. * @throws FuncNotFoundException
  348. * @throws ReflectionException
  349. * @throws InvalidArgumentException
  350. * @throws ClassNotFoundException
  351. * @return mixed
  352. */
  353. public function getRule()
  354. {
  355. $config = SystemService::instance()->getData('activity:rule');
  356. return $this->successResponse($config);
  357. }
  358. /**
  359. * 聚合页配置信息
  360. * @throws FuncNotFoundException
  361. * @throws ReflectionException
  362. * @throws InvalidArgumentException
  363. * @throws ClassNotFoundException
  364. * @return mixed
  365. */
  366. public function groupPageConfig()
  367. {
  368. return $this->successResponse(['config' => SystemService::instance()->getData('group:page:config')]);
  369. }
  370. public function index()
  371. {
  372. $this->redirect(sysuri('admin/login/index'));
  373. }
  374. /**
  375. * 聚合页通知获取
  376. * 查询最近20条通知
  377. * @throws DbException
  378. * @throws ModelNotFoundException
  379. * @throws DataNotFoundException
  380. * @return mixed
  381. */
  382. public function notices()
  383. {
  384. $rows = SystemNotice::limit(20)->order('id', 'desc')->select();
  385. return $this->successResponse([
  386. "lists" => $rows,
  387. ]);
  388. }
  389. /**
  390. * 首次弹窗
  391. * 发放邀请函微博
  392. * @return Json
  393. */
  394. public function sendInviteWeibo()
  395. {
  396. $config = SystemService::instance()->getData('group:page:config');
  397. $sendRes = (new WeiboService(Safe::$user['uid']))->status($config['firstDialog']['content']);
  398. if (empty($sendRes) || $sendRes['code'] != 10000) {
  399. return $this->response(403, $sendRes['msg'] ?? '发布失败');
  400. }
  401. Cache::set('u:f:' . Safe::$user['uid'], 1, 15552000);
  402. return $this->successResponse(null, '发布成功!');
  403. }
  404. /**
  405. * 首次弹窗用户未选择发送邀请函
  406. * 标记已经首次弹窗过了
  407. * @return mixed
  408. */
  409. public function setFirst()
  410. {
  411. Cache::set('u:f:' . Safe::$user['uid'], 1, 15552000);
  412. return $this->successResponse(null, '操作成功!');
  413. }
  414. /**
  415. * 标记用户已经浏览过主页了,用户点了去浏览时调用
  416. * @throws FuncNotFoundException
  417. * @throws ClassNotFoundException
  418. * @throws ReflectionException
  419. * @throws DbException
  420. * @throws ModelNotFoundException
  421. * @throws DataNotFoundException
  422. * @return mixed
  423. */
  424. public function setViewed()
  425. {
  426. // 先获取品牌任务配置及状态
  427. $conf = $this->getBrandConfigAndState();
  428. if ($conf instanceof Json) {
  429. return $conf;
  430. }
  431. if ($conf['state']['view_state'] == 1) {
  432. return $this->successResponse([
  433. 'result' => 2,
  434. 'brand' => $conf,
  435. ], '您已经浏览过了');
  436. }
  437. $date = date('Y-m-d');
  438. $redisKey = $this->getTaskStateRedisKey(intval(Safe::$user['uid']));
  439. // 如果已经完成了另外两项,则标记品牌任务已经完成
  440. $finishState = $conf['state']['finish_state'];
  441. if ($conf['state']['forward_state'] == 1 && $conf['state']['follow_state'] == 1) {
  442. $finishState = 1;
  443. }
  444. $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update([
  445. 'view_state' => 1,
  446. 'finish_state' => $finishState,
  447. ]);
  448. if ($nums) {
  449. $conf['state']['view_state'] = 1;
  450. $conf['state']['finish_state'] = $finishState;
  451. Cache::set($redisKey, json_encode($conf['state']), 86400);
  452. return $this->successResponse([
  453. 'result' => 1,
  454. 'brand' => $conf,
  455. ], '浏览成功');
  456. } else {
  457. return $this->response(601, '更新任务状态失败');
  458. }
  459. }
  460. /**
  461. * 用户完成端外分享时调用接口加票
  462. * @throws FuncNotFoundException
  463. * @throws ClassNotFoundException
  464. * @throws ReflectionException
  465. * @throws DbException
  466. * @throws ModelNotFoundException
  467. * @throws DataNotFoundException
  468. * @return mixed
  469. */
  470. public function shareFinishAddVotes()
  471. {
  472. // 先获取品牌任务配置及状态
  473. $conf = $this->getBrandConfigAndState();
  474. if ($conf instanceof Json) {
  475. return $conf;
  476. }
  477. if ($conf['state']['share_add_votes'] == 1) {
  478. return $this->successResponse([
  479. 'result' => 2,
  480. 'brand' => $conf,
  481. ], '您今日已经领取过投票机会了~');
  482. }
  483. // 调用接口给用户发券(加票)
  484. $weiboRes = (new WeiboService(Safe::$user['uid']))->setcoupons($conf['config']['share']['votes']);
  485. if (empty($weiboRes) || $weiboRes['code'] != 10000) {
  486. return $this->response(600, $weiboRes['msg'] ?? '加票失败~');
  487. }
  488. // 标记状态
  489. $date = date('Y-m-d');
  490. $redisKey = $this->getTaskStateRedisKey(intval(Safe::$user['uid']));
  491. $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update([
  492. 'share_add_votes' => 1,
  493. ]);
  494. if ($nums) {
  495. $conf['state']['share_add_votes'] = 1;
  496. Cache::set($redisKey, json_encode($conf['state']), 86400);
  497. return $this->successResponse([
  498. 'result' => 1,
  499. 'brand' => $conf,
  500. ], '加票成功');
  501. } else {
  502. return $this->response(601, '更新任务状态失败');
  503. }
  504. }
  505. /**
  506. * 完成品牌任务后领取加票机会接口
  507. * @throws FuncNotFoundException
  508. * @throws ClassNotFoundException
  509. * @throws ReflectionException
  510. * @throws DbException
  511. * @throws ModelNotFoundException
  512. * @throws DataNotFoundException
  513. * @return mixed
  514. */
  515. public function taskFinishAddVotes()
  516. {
  517. // 先获取品牌任务配置及状态
  518. $conf = $this->getBrandConfigAndState();
  519. if ($conf instanceof Json) {
  520. return $conf;
  521. }
  522. if ($conf['state']['finish_add_votes'] == 1) {
  523. return $this->successResponse([
  524. 'result' => 2,
  525. 'brand' => $conf,
  526. ], '您今日已经领取过投票机会了~');
  527. }
  528. // 未完成品牌任务 领取失败
  529. if ($conf['state']['finish_state'] == 0) {
  530. return $this->response(603, '请先完成品牌任务,再来领取加票', [
  531. 'result' => 0,
  532. 'brand' => $conf,
  533. ]);
  534. }
  535. // 调用微博接口 给用户发券(加票)
  536. $weiboRes = (new WeiboService(Safe::$user['uid']))->setcoupons($conf['config']['task']['votes']);
  537. if (empty($weiboRes) || $weiboRes['code'] != 10000) {
  538. return $this->response(600, $weiboRes['msg'] ?? '加票失败~');
  539. }
  540. // 标记加票成功
  541. $date = date('Y-m-d');
  542. $redisKey = $this->getTaskStateRedisKey(intval(Safe::$user['uid']));
  543. $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update([
  544. 'finish_add_votes' => 1,
  545. ]);
  546. if ($nums) {
  547. $conf['state']['finish_add_votes'] = 1;
  548. Cache::set($redisKey, json_encode($conf['state']), 86400);
  549. return $this->successResponse([
  550. 'result' => 1,
  551. 'brand' => $conf,
  552. ], '加票成功');
  553. } else {
  554. return $this->response(601, '更新任务状态失败');
  555. }
  556. }
  557. /**
  558. * 获取品牌任务配置及状态
  559. * @throws FuncNotFoundException
  560. * @throws ReflectionException
  561. * @throws InvalidArgumentException
  562. * @throws ClassNotFoundException
  563. * @throws DbException
  564. * @throws ModelNotFoundException
  565. * @throws DataNotFoundException
  566. * @return \think\response\Json|array
  567. */
  568. protected function getBrandConfigAndState()
  569. {
  570. $config = SystemService::instance()->getData('brand:task:config');
  571. $date = date('Y-m-d');
  572. $dateForRedis = date('Ymd');
  573. if (empty(Safe::$user['uid'])) {
  574. $state = [
  575. // 品牌任务完成状态
  576. 'finish_state' => 0,
  577. // 关注子任务状态
  578. 'follow_state' => 0,
  579. // 转发子任务状态
  580. 'forward_state' => 0,
  581. // 查看浏览主页任务状态
  582. 'view_state' => 0,
  583. // 品牌任务完成后是否加票
  584. 'finish_add_votes' => 0,
  585. // 分享完成后是否加票
  586. 'share_add_votes' => 0,
  587. 'uid' => 0,
  588. 'date' => $date,
  589. ];
  590. } else {
  591. $redisKey = "t:{$dateForRedis}:" . Safe::$user['uid'];
  592. $state = Cache::get($redisKey);
  593. if (empty($state)) {
  594. // 缓存失败 查数据库
  595. $state = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->find();
  596. if (empty($state)) {
  597. // 当天第一次,初始化数据
  598. $state = [
  599. // 品牌任务完成状态
  600. 'finish_state' => 0,
  601. // 关注子任务状态
  602. 'follow_state' => 0,
  603. // 转发子任务状态
  604. 'forward_state' => 0,
  605. // 查看浏览主页任务状态
  606. 'view_state' => 0,
  607. // 品牌任务完成后是否加票
  608. 'finish_add_votes' => 0,
  609. // 分享完成后是否加票
  610. 'share_add_votes' => 0,
  611. 'uid' => Safe::$user['uid'],
  612. 'date' => $date,
  613. ];
  614. if (0 == Db::table('awards_user_task')->insert($state)) {
  615. return $this->response(5001, '系统错误,请稍后再试~');
  616. }
  617. }
  618. Cache::set($redisKey, json_encode($state), 86400);
  619. } else {
  620. $state = json_decode($state, true);
  621. }
  622. // 如果未关注状态,查询下已经实际已经关注了
  623. if ($state['follow_state'] == 0) {
  624. $weiboRes = (new WeiboService(Safe::$user['uid']))->friends($config['follow']['id']);
  625. if (isset($weiboRes['data'][$config['follow']['id']]) && $weiboRes['data'][$config['follow']['id']] == 1) {
  626. $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update(['follow_state' => 1]);
  627. if ($nums) {
  628. $state['follow_state'] = 1;
  629. Cache::set($redisKey, json_encode($state), 86400);
  630. }
  631. }
  632. }
  633. }
  634. // 校准状态,可能存在异常情况关注,转发,浏览都完成了,但是总的品牌任务状态没标记成已完成
  635. if ($state['follow_state'] == 1 && $state['forward_state'] == 1 && $state['view_state'] == 1 && $state['finish_state'] != 1) {
  636. $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update(['finish_state' => 1]);
  637. if ($nums) {
  638. $state['finish_state'] = 1;
  639. Cache::set($redisKey, json_encode($state), 86400);
  640. }
  641. }
  642. return [
  643. 'config' => $config,
  644. 'state' => $state,
  645. ];
  646. }
  647. /**
  648. * 获取品牌任务redis 缓存键
  649. * @param int $uid
  650. * @return string
  651. */
  652. protected function getTaskStateRedisKey(int $uid): string
  653. {
  654. $dateForRedis = date('Ymd');
  655. return "t:{$dateForRedis}:" . $uid;
  656. }
  657. /**
  658. * @param $code
  659. * @param $msg
  660. * @param $data
  661. * @return Json
  662. */
  663. protected function response($code, $msg, $data = null)
  664. {
  665. return json(
  666. [
  667. 'code' => $code,
  668. 'message' => $msg,
  669. 'data' => $data,
  670. 'trackID' => Log::$logID,
  671. ],
  672. 200
  673. );
  674. }
  675. /**
  676. * @param $data
  677. * @param $msg
  678. * @return mixed
  679. */
  680. protected function successResponse($data, $msg = '操作成功')
  681. {
  682. return $this->response(0, $msg, $data);
  683. }
  684. }