Index.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. <?php
  2. namespace app\index\controller;
  3. use app\admin\model\SinaNotice;
  4. use app\middleware\Log;
  5. use app\middleware\Safe;
  6. use app\service\WeiboService;
  7. use InvalidArgumentException;
  8. use ReflectionException;
  9. use think\admin\Controller;
  10. use think\admin\service\SystemService;
  11. use think\exception\FuncNotFoundException;
  12. use think\exception\ClassNotFoundException;
  13. use think\db\exception\DbException;
  14. use think\db\exception\ModelNotFoundException;
  15. use think\db\exception\DataNotFoundException;
  16. use think\facade\Cache;
  17. use think\facade\Db;
  18. use think\facade\Log as FacadeLog;
  19. use think\facade\Request;
  20. use \think\response\Json;
  21. /**
  22. * Class Index
  23. * @package app\index\controller
  24. */
  25. class Index extends Controller
  26. {
  27. public function index()
  28. {
  29. $this->redirect(sysuri('admin/login/index'));
  30. }
  31. /**
  32. * 检测登录 通过cookie中的SUB字段的内容,调用用户接口检测登录状态
  33. * @return mixed
  34. * @throws ClassNotFoundException
  35. * @throws ReflectionException
  36. */
  37. public function checkLogin()
  38. {
  39. FacadeLog::info("cookies:" . json_encode(Request::post()));
  40. $sub = "";
  41. if (!empty($_COOKIE['SUB'])) {
  42. $sub = $_COOKIE['SUB'];
  43. } else {
  44. $sub = Request::post('cookie');
  45. }
  46. FacadeLog::info($sub);
  47. $userInfoRes = (new WeiboService(0))->userinfo($sub);
  48. if (empty($userInfoRes) || $userInfoRes['code'] != 10000) {
  49. return $this->response(403, $userInfoRes['msg'] ?? '没有登录');
  50. }
  51. // 使用客户端信息生成token
  52. $token = md5($_SERVER['HTTP_USER_AGENT'] . $_SERVER['HTTP_ACCEPT_ENCODING'] . $_SERVER['HTTP_ACCEPT_LANGUAGE'] . get_client_ip(0)) . $userInfoRes['data']['uid'];
  53. $user = $userInfoRes['data'];
  54. // 生成加密用的密钥和向量
  55. $cipher = "aes-256-gcm";
  56. $ivlen = openssl_cipher_iv_length($cipher);
  57. $iv = bin2hex(openssl_random_pseudo_bytes($ivlen));
  58. $aesKey = bin2hex(openssl_random_pseudo_bytes(32));
  59. $user = array_merge($user, [
  60. 'aes_key' => $aesKey,
  61. 'ase_iv' => $iv,
  62. ]);
  63. // 缓存用户信息1天
  64. Cache::set('u:' . $token, json_encode($user), 86400);
  65. return $this->successResponse([
  66. 'user' => $user,
  67. 'token' => $token,
  68. 'first' => Cache::get('u:f:' . $user['uid']) != 1,
  69. ]);
  70. }
  71. /**
  72. * 聚合页配置信息
  73. * @return mixed
  74. * @throws FuncNotFoundException
  75. * @throws ReflectionException
  76. * @throws InvalidArgumentException
  77. * @throws ClassNotFoundException
  78. */
  79. public function groupPageConfig()
  80. {
  81. return $this->successResponse(['config' => SystemService::instance()->getData('group:page:config')]);
  82. }
  83. /**
  84. * 聚合页通知获取
  85. * 查询最近20条通知
  86. * @return mixed
  87. * @throws DbException
  88. * @throws ModelNotFoundException
  89. * @throws DataNotFoundException
  90. */
  91. public function notices()
  92. {
  93. $rows = SinaNotice::limit(20)->order('id', 'desc')->select();
  94. return $this->successResponse([
  95. "lists" => $rows,
  96. ]);
  97. }
  98. /**
  99. * 首次弹窗
  100. * 发放邀请函微博
  101. * @return Json
  102. */
  103. public function sendInviteWeibo()
  104. {
  105. $config = SystemService::instance()->getData('group:page:config');
  106. $sendRes = (new WeiboService(Safe::$user['uid']))->status($config['firstDialog']['content']);
  107. if (empty($sendRes) || $sendRes['code'] != 10000) {
  108. return $this->response(403, $sendRes['msg'] ?? '发布失败');
  109. }
  110. Cache::set('u:f:' . Safe::$user['uid'], 1, 0);
  111. return $this->successResponse(null, '发布成功!');
  112. }
  113. /**
  114. * 首次弹窗用户未选择发送邀请函
  115. * 标记已经首次弹窗过了
  116. * @return mixed
  117. */
  118. public function setFirst()
  119. {
  120. Cache::set('u:f:' . Safe::$user['uid'], 1, 0);
  121. return $this->successResponse(null, '操作成功!');
  122. }
  123. /**
  124. * 获取活动规则
  125. * @return mixed
  126. * @throws FuncNotFoundException
  127. * @throws ReflectionException
  128. * @throws InvalidArgumentException
  129. * @throws ClassNotFoundException
  130. */
  131. public function getRule()
  132. {
  133. $config = SystemService::instance()->getData('activity:rule');
  134. return $this->successResponse($config);
  135. }
  136. /**
  137. * 获取品牌任务相关配置
  138. *
  139. * @return mixed
  140. * @throws FuncNotFoundException
  141. * @throws ClassNotFoundException
  142. * @throws ReflectionException
  143. * @throws DbException
  144. * @throws ModelNotFoundException
  145. * @throws DataNotFoundException
  146. */
  147. public function brand()
  148. {
  149. $conf = $this->getBrandConfigAndState();
  150. if ($conf instanceof Json) {
  151. return $conf;
  152. }
  153. return $this->successResponse($conf);
  154. }
  155. /**
  156. * 品牌任务关注接口
  157. * @return mixed
  158. * @throws FuncNotFoundException
  159. * @throws ClassNotFoundException
  160. * @throws ReflectionException
  161. * @throws DbException
  162. * @throws ModelNotFoundException
  163. * @throws DataNotFoundException
  164. */
  165. public function follow()
  166. {
  167. // 先获取品牌任务配置及状态
  168. $conf = $this->getBrandConfigAndState();
  169. if ($conf instanceof Json) {
  170. return $conf;
  171. }
  172. // 已经关注过了
  173. if ($conf['state']['follow_state'] == 1) {
  174. return $this->successResponse([
  175. 'result' => 2,
  176. 'brand' => $conf,
  177. ], '您已经关注过了');
  178. }
  179. // 调用接口关注
  180. $weiboRes = (new WeiboService(Safe::$user['uid']))->add($conf['config']['follow']['id']);
  181. if (empty($weiboRes) || $weiboRes['code'] != 10000) {
  182. return $this->response(600, $weiboRes['msg'] ?? '关注失败~');
  183. }
  184. $date = date('Y-m-d');
  185. $redisKey = $this->getTaskStateRedisKey(intval(Safe::$user['uid']));
  186. // 如果已经完成了另外两项,则标记品牌任务已经完成
  187. $finishState = $conf['state']['finish_state'];
  188. if ($conf['state']['view_state'] == 1 && $conf['state']['forward_state'] == 1) {
  189. $finishState = 1;
  190. }
  191. // 更新任务状态
  192. $nums = Db::table('user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update([
  193. 'follow_state' => 1,
  194. 'finish_state' => $finishState,
  195. ]);
  196. if ($nums) {
  197. $conf['state']['follow_state'] = 1;
  198. $conf['state']['finish_state'] = $finishState;
  199. Cache::set($redisKey, json_encode($conf['state']), 86400);
  200. return $this->successResponse([
  201. 'result' => 1,
  202. 'brand' => $conf,
  203. ], '关注成功');
  204. } else {
  205. return $this->response(601, '更新任务状态失败');
  206. }
  207. }
  208. /**
  209. * 品牌任务转发博文接口
  210. * @return mixed
  211. * @throws FuncNotFoundException
  212. * @throws ClassNotFoundException
  213. * @throws ReflectionException
  214. * @throws DbException
  215. * @throws ModelNotFoundException
  216. * @throws DataNotFoundException
  217. */
  218. public function forward()
  219. {
  220. // 先获取品牌任务配置及状态
  221. $conf = $this->getBrandConfigAndState();
  222. if ($conf instanceof Json) {
  223. return $conf;
  224. }
  225. // 已经转发过了
  226. if ($conf['state']['forward_state'] == 1) {
  227. return $this->successResponse([
  228. 'result' => 2,
  229. 'brand' => $conf,
  230. ], '您已经转发过了');
  231. }
  232. // 调用微博接口转发
  233. $weiboRes = (new WeiboService(Safe::$user['uid']))->repost($conf['config']['forward']['id'], $conf['config']['forward']['content'] ?? '');
  234. if (empty($weiboRes) || $weiboRes['code'] != 10000) {
  235. return $this->response(600, $weiboRes['msg'] ?? '转发失败~');
  236. }
  237. $date = date('Y-m-d');
  238. $redisKey = $this->getTaskStateRedisKey(intval(Safe::$user['uid']));
  239. // 如果已经完成了另外两项,则标记品牌任务已经完成
  240. $finishState = $conf['state']['finish_state'];
  241. if ($conf['state']['view_state'] == 1 && $conf['state']['follow_state'] == 1) {
  242. $finishState = 1;
  243. }
  244. // 更新任务状态
  245. $nums = Db::table('user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update([
  246. 'forward_state' => 1,
  247. 'finish_state' => $finishState,
  248. ]);
  249. if ($nums) {
  250. $conf['state']['forward_state'] = 1;
  251. $conf['state']['finish_state'] = $finishState;
  252. Cache::set($redisKey, json_encode($conf['state']), 86400);
  253. return $this->successResponse([
  254. 'result' => 1,
  255. 'brand' => $conf,
  256. ], '转发成功');
  257. } else {
  258. return $this->response(601, '更新任务状态失败');
  259. }
  260. }
  261. /**
  262. * 完成品牌任务后领取加票机会接口
  263. * @return mixed
  264. * @throws FuncNotFoundException
  265. * @throws ClassNotFoundException
  266. * @throws ReflectionException
  267. * @throws DbException
  268. * @throws ModelNotFoundException
  269. * @throws DataNotFoundException
  270. */
  271. public function taskFinishAddVotes()
  272. {
  273. // 先获取品牌任务配置及状态
  274. $conf = $this->getBrandConfigAndState();
  275. if ($conf instanceof Json) {
  276. return $conf;
  277. }
  278. if ($conf['state']['finish_add_votes'] == 1) {
  279. return $this->successResponse([
  280. 'result' => 2,
  281. 'brand' => $conf,
  282. ], '您今日已经领取过投票机会了~');
  283. }
  284. // 未完成品牌任务 领取失败
  285. if ($conf['state']['finish_state'] == 0) {
  286. return $this->response(603, '请先完成品牌任务,再来领取加票', [
  287. 'result' => 0,
  288. 'brand' => $conf,
  289. ]);
  290. }
  291. // 调用微博接口 给用户发券(加票)
  292. $weiboRes = (new WeiboService(Safe::$user['uid']))->setcoupons($conf['config']['task']['votes']);
  293. if (empty($weiboRes) || $weiboRes['code'] != 10000) {
  294. return $this->response(600, $weiboRes['msg'] ?? '加票失败~');
  295. }
  296. // 标记加票成功
  297. $date = date('Y-m-d');
  298. $redisKey = $this->getTaskStateRedisKey(intval(Safe::$user['uid']));
  299. $nums = Db::table('user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update([
  300. 'finish_add_votes' => 1,
  301. ]);
  302. if ($nums) {
  303. $conf['state']['finish_add_votes'] = 1;
  304. Cache::set($redisKey, json_encode($conf['state']), 86400);
  305. return $this->successResponse([
  306. 'result' => 1,
  307. 'brand' => $conf,
  308. ], '加票成功');
  309. } else {
  310. return $this->response(601, '更新任务状态失败');
  311. }
  312. }
  313. /**
  314. * 用户完成端外分享时调用接口加票
  315. * @return mixed
  316. * @throws FuncNotFoundException
  317. * @throws ClassNotFoundException
  318. * @throws ReflectionException
  319. * @throws DbException
  320. * @throws ModelNotFoundException
  321. * @throws DataNotFoundException
  322. */
  323. public function shareFinishAddVotes()
  324. {
  325. // 先获取品牌任务配置及状态
  326. $conf = $this->getBrandConfigAndState();
  327. if ($conf instanceof Json) {
  328. return $conf;
  329. }
  330. if ($conf['state']['share_add_votes'] == 1) {
  331. return $this->successResponse([
  332. 'result' => 2,
  333. 'brand' => $conf,
  334. ], '您今日已经领取过投票机会了~');
  335. }
  336. // 调用接口给用户发券(加票)
  337. $weiboRes = (new WeiboService(Safe::$user['uid']))->setcoupons($conf['config']['share']['votes']);
  338. if (empty($weiboRes) || $weiboRes['code'] != 10000) {
  339. return $this->response(600, $weiboRes['msg'] ?? '加票失败~');
  340. }
  341. // 标记状态
  342. $date = date('Y-m-d');
  343. $redisKey = $this->getTaskStateRedisKey(intval(Safe::$user['uid']));
  344. $nums = Db::table('user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update([
  345. 'share_add_votes' => 1,
  346. ]);
  347. if ($nums) {
  348. $conf['state']['share_add_votes'] = 1;
  349. Cache::set($redisKey, json_encode($conf['state']), 86400);
  350. return $this->successResponse([
  351. 'result' => 1,
  352. 'brand' => $conf,
  353. ], '加票成功');
  354. } else {
  355. return $this->response(601, '更新任务状态失败');
  356. }
  357. }
  358. /**
  359. * 标记用户已经浏览过主页了,用户点了去浏览时调用
  360. * @return mixed
  361. * @throws FuncNotFoundException
  362. * @throws ClassNotFoundException
  363. * @throws ReflectionException
  364. * @throws DbException
  365. * @throws ModelNotFoundException
  366. * @throws DataNotFoundException
  367. */
  368. public function setViewed()
  369. {
  370. // 先获取品牌任务配置及状态
  371. $conf = $this->getBrandConfigAndState();
  372. if ($conf instanceof Json) {
  373. return $conf;
  374. }
  375. if ($conf['state']['view_state'] == 1) {
  376. return $this->successResponse([
  377. 'result' => 2,
  378. 'brand' => $conf,
  379. ], '您已经浏览过了');
  380. }
  381. $date = date('Y-m-d');
  382. $redisKey = $this->getTaskStateRedisKey(intval(Safe::$user['uid']));
  383. // 如果已经完成了另外两项,则标记品牌任务已经完成
  384. $finishState = $conf['state']['finish_state'];
  385. if ($conf['state']['forward_state'] == 1 && $conf['state']['follow_state'] == 1) {
  386. $finishState = 1;
  387. }
  388. $nums = Db::table('user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update([
  389. 'view_state' => 1,
  390. 'finish_state' => $finishState,
  391. ]);
  392. if ($nums) {
  393. $conf['state']['view_state'] = 1;
  394. $conf['state']['finish_state'] = $finishState;
  395. Cache::set($redisKey, json_encode($conf['state']), 86400);
  396. return $this->successResponse([
  397. 'result' => 1,
  398. 'brand' => $conf,
  399. ], '浏览成功');
  400. } else {
  401. return $this->response(601, '更新任务状态失败');
  402. }
  403. }
  404. /**
  405. * 获取品牌任务redis 缓存键
  406. * @param int $uid
  407. * @return string
  408. */
  409. protected function getTaskStateRedisKey(int $uid): string
  410. {
  411. $dateForRedis = date('Ymd');
  412. return "t:{$dateForRedis}:" . $uid;
  413. }
  414. /**
  415. * 获取品牌任务配置及状态
  416. * @return \think\response\Json|array
  417. * @throws FuncNotFoundException
  418. * @throws ReflectionException
  419. * @throws InvalidArgumentException
  420. * @throws ClassNotFoundException
  421. * @throws DbException
  422. * @throws ModelNotFoundException
  423. * @throws DataNotFoundException
  424. */
  425. protected function getBrandConfigAndState()
  426. {
  427. $config = SystemService::instance()->getData('brand:task:config');
  428. $date = date('Y-m-d');
  429. $dateForRedis = date('Ymd');
  430. $redisKey = "t:{$dateForRedis}:" . Safe::$user['uid'];
  431. $state = Cache::get($redisKey);
  432. if (empty($state)) {
  433. // 缓存失败 查数据库
  434. $state = Db::table('user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->find();
  435. if (empty($state)) {
  436. // 当天第一次,初始化数据
  437. $state = [
  438. // 品牌任务完成状态
  439. 'finish_state' => 0,
  440. // 关注子任务状态
  441. 'follow_state' => 0,
  442. // 转发子任务状态
  443. 'forward_state' => 0,
  444. // 查看浏览主页任务状态
  445. 'view_state' => 0,
  446. // 品牌任务完成后是否加票
  447. 'finish_add_votes' => 0,
  448. // 分享完成后是否加票
  449. 'share_add_votes' => 0,
  450. 'uid' => Safe::$user['uid'],
  451. 'date' => $date,
  452. ];
  453. if (0 == Db::table('user_task')->insert($state)) {
  454. return $this->response(5001, '系统错误,请稍后再试~');
  455. }
  456. }
  457. Cache::set($redisKey, json_encode($state), 86400);
  458. } else {
  459. $state = json_decode($state, true);
  460. }
  461. // 如果未关注状态,查询下已经实际已经关注了
  462. if ($state['follow_state'] == 0) {
  463. $weiboRes = (new WeiboService(Safe::$user['uid']))->friends($config['follow']['id']);
  464. if (isset($weiboRes['data'][$config['follow']['id']]) && $weiboRes['data'][$config['follow']['id']] == 1) {
  465. $nums = Db::table('user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update(['follow_state' => 1,]);
  466. if ($nums) {
  467. $state['follow_state'] = 1;
  468. Cache::set($redisKey, json_encode($state), 86400);
  469. }
  470. }
  471. }
  472. // 校准状态,可能存在异常情况关注,转发,浏览都完成了,但是总的品牌任务状态没标记成已完成
  473. if ($state['follow_state'] == 1 && $state['forward_state'] == 1 && $state['view_state'] == 1 && $state['finish_state'] != 1) {
  474. $nums = Db::table('user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update(['finish_state' => 1,]);
  475. if ($nums) {
  476. $state['finish_state'] = 1;
  477. Cache::set($redisKey, json_encode($state), 86400);
  478. }
  479. }
  480. return [
  481. 'config' => $config,
  482. 'state' => $state,
  483. ];
  484. }
  485. /**
  486. * @param $code
  487. * @param $msg
  488. * @param $data
  489. * @return Json
  490. */
  491. protected function response($code, $msg, $data = null)
  492. {
  493. return json(
  494. [
  495. 'code' => $code,
  496. 'message' => $msg,
  497. 'data' => $data,
  498. 'trackID' => Log::$logID,
  499. ],
  500. 200
  501. );
  502. }
  503. /**
  504. * @param $data
  505. * @param $msg
  506. * @return mixed
  507. */
  508. protected function successResponse($data, $msg = '操作成功')
  509. {
  510. return $this->response(0, $msg, $data);
  511. }
  512. }