Index.php 18 KB

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