Index.php 26 KB

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