Index.php 27 KB

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