Index.php 19 KB

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