Index.php 20 KB

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