Index.php 25 KB

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