Index.php 18 KB

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