Index.php 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  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. FacadeLog::info($_COOKIE);
  58. if (!empty($_COOKIE['SUB'])) {
  59. $sub = $_COOKIE['SUB'];
  60. } else {
  61. // 只在调试模式下开启从POST参数中获取UID,方便测试联调
  62. if (env('app_debug')) {
  63. $sub = Request::post('cookie');
  64. $uid = $sub;
  65. } else {
  66. return $this->response(403, 'not login.');
  67. }
  68. }
  69. FacadeLog::info($sub);
  70. $userInfoRes = (new WeiboService($uid))->userinfo($sub);
  71. if (empty($userInfoRes) || $userInfoRes['ok'] != 1) {
  72. return $this->response(403, $userInfoRes['msg'] ?? '没有登录');
  73. }
  74. // 使用客户端信息生成token
  75. $token = md5($_SERVER['HTTP_USER_AGENT'] . $_SERVER['HTTP_ACCEPT_ENCODING'] . $_SERVER['HTTP_ACCEPT_LANGUAGE'] . $_SERVER['HTTP_REFERER'] . get_client_ip(0) . $userInfoRes['data']['uid']);
  76. $user = $userInfoRes['data'];
  77. $userInfo = Db::table('awards_user_info')->where('uid', $user['uid'])->find();
  78. $count = 0;
  79. $isShare = 0;
  80. if (empty($userInfo)) {
  81. $userAttr = [
  82. 'uid' => $user['uid'],
  83. 'portrait' => $user['profile_image_url'],
  84. 'nickname' => $user['name'],
  85. 'is_share' => 0,
  86. 'count' => 1,
  87. 'create_at' => time()
  88. ];
  89. if (0 == Db::table('awards_user_info')->insert($userAttr)) {
  90. return $this->response(5001, '系统错误,请稍后再试~');
  91. }
  92. } else {
  93. $count = Db::table('awards_user_task_log')->where('uid', $user['uid'])->count('id');
  94. $isShare = $userInfo['is_share'];
  95. }
  96. // 生成加密用的密钥和向量
  97. $cipher = "aes-256-gcm";
  98. $ivlen = openssl_cipher_iv_length($cipher);
  99. $iv = bin2hex(openssl_random_pseudo_bytes($ivlen));
  100. $aesKey = bin2hex(openssl_random_pseudo_bytes(32));
  101. $user = array_merge($user, [
  102. 'aes_key' => $aesKey,
  103. 'ase_iv' => $iv,
  104. ]);
  105. $cacheUser = [
  106. 'aes_key' => $aesKey,
  107. 'ase_iv' => $iv,
  108. 'uid' => $user['uid'],
  109. ];
  110. $activity = Db::table('awards_activity')->where('begin_at', '<=', time())
  111. ->where('end_at', '>=', time())->find();
  112. $isBeginActivity = 1;
  113. if (empty($activity)) {
  114. $isBeginActivity = 0;
  115. }
  116. // 缓存用户信息1天
  117. Cache::set('u:' . $token, json_encode($cacheUser), 86400);
  118. return $this->successResponse([
  119. 'user' => $user,
  120. 'token' => $token,
  121. 'isShare' => $isShare,
  122. 'count' => $count,
  123. 'isBeginActivity' => $isBeginActivity
  124. ]);
  125. }
  126. /**
  127. * 是否可以参与游戏
  128. * @throws ClassNotFoundException
  129. * @throws ReflectionException
  130. * @return mixed
  131. */
  132. public function checkRole()
  133. {
  134. $activity = Db::table('awards_activity')->where('begin_at', '<=', time())
  135. ->where('end_at', '>=', time())->find();
  136. if (empty($activity)) {
  137. return $this->response(5002, '活动末开始');
  138. }
  139. $userInfo = Db::table('awards_user_info')->where('uid', Safe::$user['uid'])->find();
  140. $count = Db::table('awards_user_task_log')->where('uid', Safe::$user['uid'])->count('id');
  141. return $this->successResponse([
  142. 'isShare' => $userInfo['is_share'],
  143. 'count' => $count
  144. ]);
  145. }
  146. /**
  147. * 提交任务接口
  148. * @throws FuncNotFoundException
  149. * @throws ClassNotFoundException
  150. * @throws DbException
  151. * @throws ModelNotFoundException
  152. * @throws DataNotFoundException
  153. * @return mixed
  154. */
  155. public function submitTask()
  156. {
  157. $activity = Db::table('awards_activity')->where('begin_at', '<=', time())
  158. ->where('end_at', '>=', time())->find();
  159. if (empty($activity)) {
  160. return $this->response(5002, '活动末开始');
  161. }
  162. // if (empty(Safe::$body)) {
  163. // return $this->response(5003, '参数有误');
  164. // }
  165. //
  166. // if (!isset(Safe::$body['duration']) || Safe::$body['duration'] < 1 || Safe::$body['duration'] > 1000) {
  167. // return $this->response(5003, '时长参数有误');
  168. // }
  169. //
  170. // if (!isset(Safe::$body['number']) || Safe::$body['number'] < 0 || Safe::$body['number'] > 1000) {
  171. // return $this->response(5003, '时长参数有误');
  172. // }
  173. // $duration = Safe::$body['duration'];
  174. // $number = Safe::$body['number'];
  175. $duration = Request::post("duration");
  176. $number = Request::post("number");
  177. $date = date('Y-m-d');
  178. $userInfo = Db::table('awards_user_info')->where('uid', Safe::$user['uid'])->find();
  179. if (empty($userInfo)) {
  180. return $this->response(403, '没有登录');
  181. }
  182. $count = Db::table('awards_user_task_log')->where('uid', Safe::$user['uid'])->count('id');
  183. if ($count >= $userInfo['count']) {
  184. return $this->response(5002, '没有参与次数');
  185. }
  186. $log = [
  187. 'uid' => Safe::$user['uid'],
  188. 'date' => $date,
  189. 'duration' => $duration,
  190. 'number' => $number,
  191. 'create_at' => time()
  192. ];
  193. if (0 == Db::table('awards_user_task_log')->insert($log)) {
  194. return $this->response(5001, '系统错误,请稍后再试~');
  195. }
  196. return $this->successResponse(null, '提交成功');
  197. }
  198. /**
  199. * 提交发布微博接口
  200. * @throws FuncNotFoundException
  201. * @throws ClassNotFoundException
  202. * @throws DbException
  203. * @throws ModelNotFoundException
  204. * @throws DataNotFoundException
  205. * @return mixed
  206. */
  207. public function submitShare()
  208. {
  209. $activity = Db::table('awards_activity')->where('begin_at', '<=', time())
  210. ->where('end_at', '>=', time())->find();
  211. if (empty($activity)) {
  212. return $this->response(5002, '活动末开始');
  213. }
  214. $userInfo = Db::table('awards_user_info')->where('uid', Safe::$user['uid'])->find();
  215. if (empty($userInfo)) {
  216. return $this->response(403, '没有登录');
  217. }
  218. if ($userInfo['is_share'] > 0) {
  219. return $this->response(601, '已发布');
  220. }
  221. // 更新任务状态
  222. $nums = Db::table('awards_user_info')->where('uid', Safe::$user['uid'])->update([
  223. 'is_share' => 1,
  224. 'count' => 2,
  225. 'share_at' => time()
  226. ]);
  227. if ($nums) {
  228. return $this->successResponse(null, '提交成功');
  229. } else {
  230. return $this->response(601, '发布失败');
  231. }
  232. }
  233. /**
  234. * 头号排行接口
  235. * @throws FuncNotFoundException
  236. * @throws ClassNotFoundException
  237. * @throws DbException
  238. * @throws ModelNotFoundException
  239. * @throws DataNotFoundException
  240. * @return mixed
  241. */
  242. public function topRanking()
  243. {
  244. $activity = Db::table('awards_activity')->where('begin_at', '<=', time())
  245. ->where('end_at', '>=', time())->find();
  246. if (empty($activity)) {
  247. return $this->response(5002, '活动末开始');
  248. }
  249. $ranking = Db::table('awards_user_task_log')->alias("l")
  250. ->leftJoin('awards_user_info u', 'l.uid = u.uid')
  251. ->field(['u.nickname', 'u.portrait', 'u.uid', 'l.duration', 'l.number'])
  252. ->order('l.number', 'desc')
  253. ->order('l.duration', 'asc')
  254. ->limit(50)
  255. ->select();
  256. $userInfo = Db::table('awards_user_task_log')->alias('l')
  257. ->leftJoin('awards_user_info u', 'l.uid = u.uid')
  258. ->field(['u.nickname', 'u.portrait', 'u.uid', 'l.duration', 'l.number'])
  259. ->order('l.number', 'desc')
  260. ->order('l.duration', 'asc')
  261. ->find();
  262. $res = [
  263. 'self' => $userInfo,
  264. 'ranking' => $ranking
  265. ];
  266. return $this->successResponse($res);
  267. }
  268. /**
  269. * 最新排行接口
  270. * @throws FuncNotFoundException
  271. * @throws ClassNotFoundException
  272. * @throws DbException
  273. * @throws ModelNotFoundException
  274. * @throws DataNotFoundException
  275. * @return mixed
  276. */
  277. public function newRanking()
  278. {
  279. $activity = Db::table('awards_activity')->where('begin_at', '<=', time())
  280. ->where('end_at', '>=', time())->find();
  281. if (empty($activity)) {
  282. return $this->response(5002, '活动末开始');
  283. }
  284. $ranking = Db::table('awards_user_task_log')->alias("l")
  285. ->leftJoin('awards_user_info u', 'l.uid = u.uid')
  286. ->field(['u.nickname', 'u.portrait', 'u.uid', 'l.duration', 'l.number'])
  287. ->order('l.create_at', 'desc')
  288. ->order('l.number', 'desc')
  289. ->order('l.duration', 'asc')
  290. ->limit(50)
  291. ->select();
  292. $userInfo = Db::table('awards_user_task_log')->alias('l')
  293. ->leftJoin('awards_user_info u', 'l.uid = u.uid')
  294. ->field(['u.nickname', 'u.portrait', 'u.uid', 'l.duration', 'l.number'])
  295. ->order('l.number', 'desc')
  296. ->order('l.duration', 'asc')
  297. ->find();
  298. $res = [
  299. 'self' => $userInfo,
  300. 'ranking' => $ranking
  301. ];
  302. return $this->successResponse($res);
  303. }
  304. /**
  305. * 提交领取信息接口
  306. * @throws FuncNotFoundException
  307. * @throws ClassNotFoundException
  308. * @throws DbException
  309. * @throws ModelNotFoundException
  310. * @throws DataNotFoundException
  311. * @return mixed
  312. */
  313. public function submitReceive()
  314. {
  315. $activity = Db::table('awards_activity')->where('begin_at', '<=', time())
  316. ->where('end_at', '>=', time())->find();
  317. if (empty($activity)) {
  318. return $this->response(5002, '活动末开始');
  319. }
  320. $name = trim(Request::post("name"));
  321. $mobile = trim(Request::post("mobile"));
  322. $address = trim(Request::post("address"));
  323. $giftId = trim(Request::post("giftId"));
  324. $type = trim(Request::post("type"));
  325. $orders = Db::table('awards_order')->where('uid', Safe::$user['uid'])->where('type', $type)->find();
  326. if (empty($orders)) {
  327. return $this->response(5001, '参数有误');
  328. }
  329. $userInfo = Db::table('awards_user_info')->where('uid', Safe::$user['uid'])->find();
  330. if (empty($userInfo)) {
  331. return $this->response(403, '没有登录');
  332. }
  333. if (strlen($name) < 1 || strlen($name) > 50) {
  334. return $this->response(5001, '姓名有误');
  335. }
  336. if (strlen($mobile) != 11) {
  337. return $this->response(5001, '手机号有误');
  338. }
  339. if (strlen($address) < 1 || strlen($address) > 1000) {
  340. return $this->response(5001, '地址有误');
  341. }
  342. // 更新任务状态
  343. $nums = Db::table('awards_order')->where('uid', Safe::$user['uid'])->where('type', $type)->update([
  344. 'name' => $name,
  345. 'mobile' => $mobile,
  346. 'address' => $address,
  347. ]);
  348. if ($nums < 1) {
  349. return $this->response(5001, '系统错误,请稍后再试~');
  350. }
  351. return $this->successResponse(null, '提交成功');
  352. }
  353. /**
  354. * 获取轮播中奖接口
  355. * @throws FuncNotFoundException
  356. * @throws ClassNotFoundException
  357. * @throws DbException
  358. * @throws ModelNotFoundException
  359. * @throws DataNotFoundException
  360. * @return mixed
  361. */
  362. public function getRotationAward() {
  363. $activity = Db::table('awards_activity')->where('begin_at', '<=', time())
  364. ->where('end_at', '>=', time())->find();
  365. if (empty($activity)) {
  366. return $this->response(5002, '活动末开始');
  367. }
  368. $orders = Db::table('awards_order')->alias('o')
  369. ->leftJoin('awards_user_info u', 'o.uid = u.uid')
  370. ->leftJoin('awards_gift g', 'o.gift_id = g.id')
  371. ->field(['u.nickname', 'u.portrait', 'u.uid', 'g.name as giftName'])
  372. ->order('o.id', 'desc')
  373. ->select();
  374. return $this->successResponse($orders);
  375. }
  376. /**
  377. * 排行中奖接口
  378. * @throws FuncNotFoundException
  379. * @throws ClassNotFoundException
  380. * @throws DbException
  381. * @throws ModelNotFoundException
  382. * @throws DataNotFoundException
  383. * @return mixed
  384. */
  385. public function getRankingWinAward() {
  386. $activity = Db::table('awards_activity')->where('begin_at', '<=', time())
  387. ->where('end_at', '>=', time())->find();
  388. if (!empty($activity)) {
  389. return $this->response(5002, '活动末结束');
  390. }
  391. $ranking = Db::table('awards_user_task_log')->alias("l")
  392. ->leftJoin('awards_user_info u', 'l.uid = u.uid')
  393. ->field(['u.nickname', 'u.portrait', 'u.uid', 'l.duration', 'l.number'])
  394. ->distinct('l.uid')
  395. ->order('l.number', 'desc')
  396. ->order('l.duration', 'asc')
  397. ->limit(50)
  398. ->select();
  399. $isWinAward = 0;
  400. $rank = 0;
  401. foreach ($ranking as $key => $val) {
  402. if ($val['uid'] == Safe::$user['uid']) {
  403. $isWinAward = 1;
  404. $rank = $key +1;
  405. break;
  406. }
  407. }
  408. // 如果中奖 插入用户中奖信息
  409. if ($isWinAward == 1) {
  410. $gifts = Db::table('awards_gift')->where('type', 2)->select();
  411. $giftId = 0;
  412. foreach ($gifts as $val) {
  413. if ($rank >= $val['min_rank'] && $rank <= $val['max_rank']) {
  414. $giftId = $val['id'];
  415. $orders = Db::table('awards_order')->where('uid', Safe::$user['uid'])->where('type', 2)->find();
  416. if (empty($orders)) {
  417. $order = [
  418. 'uid' => Safe::$user['uid'],
  419. 'gift_id' => $giftId,
  420. 'type' => 2,
  421. 'create_at' => time(),
  422. ];
  423. Db::table('awards_order')->insert($order);
  424. }
  425. break;
  426. }
  427. }
  428. }
  429. return $this->successResponse([
  430. 'isWinAward' => $isWinAward,
  431. 'giftId' => $giftId
  432. ]);
  433. }
  434. /**
  435. * 抽奖接口
  436. * @throws FuncNotFoundException
  437. * @throws ClassNotFoundException
  438. * @throws DbException
  439. * @throws ModelNotFoundException
  440. * @throws DataNotFoundException
  441. * @return mixed
  442. */
  443. public function getLuckDraw()
  444. {
  445. $orders = Db::table('awards_order')->where('uid', Safe::$user['uid'])->where('type', 1)->find();
  446. $isWinAward = 0;
  447. if ($orders) {
  448. return $this->successResponse([
  449. 'isWinAward' => $isWinAward,
  450. 'giftId' => 0
  451. ]);
  452. }
  453. $gifts = Db::table('awards_gift')->where('type', 1)->select();
  454. $logCount = Db::table('awards_user_task_log')->distinct('uid')->count('id');
  455. $userRate = rand(0, 5000);
  456. $giftId = 0;
  457. foreach ($gifts as $gift) {
  458. $orderCount = Db::table('awards_order')->where('type', 1)->where('gift_id', $gift['id'])->count('id');
  459. if ($orderCount >= $gift['count']) {
  460. continue;
  461. }
  462. // 如果参与活动人数 5000人内 有人中奖过,那么不在计算。
  463. $total = $logCount + 1;
  464. if ($orderCount == floor($total/2)) {
  465. $isWinAward = 0;
  466. $giftId = 0;
  467. break;
  468. }
  469. for ($i=0;$i<$gift['count'];$i++) {
  470. $giftRage = rand(0, 5000);
  471. if ($userRate == $giftRage) {
  472. $isWinAward = 1;
  473. $giftId = $gift['id'];
  474. break;
  475. }
  476. }
  477. // 如果参与活动人数 5000人内 还没有人中奖那最后一人必中。
  478. if ($orderCount < floor($total/2) && $isWinAward ==0 && ($total%2) == 0) {
  479. $isWinAward = 1;
  480. $giftId = $gift['id'];
  481. break;
  482. }
  483. }
  484. // 如果中奖 插入用户中奖信息
  485. if ($isWinAward == 1) {
  486. $order = [
  487. 'uid' => Safe::$user['uid'],
  488. 'gift_id' => $giftId,
  489. 'type' => 1,
  490. 'create_at' => time(),
  491. ];
  492. if (0 == Db::table('awards_order')->insert($order)) {
  493. return $this->response(5001, '系统错误,请稍后再试~');
  494. }
  495. }
  496. return $this->successResponse([
  497. 'isWinAward' => $isWinAward,
  498. 'giftId' => $giftId
  499. ]);
  500. }
  501. /**
  502. * 奖品记录接口
  503. * @throws FuncNotFoundException
  504. * @throws ClassNotFoundException
  505. * @throws DbException
  506. * @throws ModelNotFoundException
  507. * @throws DataNotFoundException
  508. * @return mixed
  509. */
  510. public function getAwardLog()
  511. {
  512. $awards = Db::table('awards_order')->alias('o')
  513. ->leftJoin('awards_gift g', 'o.gift_id = g.id')
  514. ->field(['g.name as giftName', 'o.type', 'o.create_at', 'o.gift_id'])
  515. ->select();
  516. $res = [];
  517. foreach ($awards as $val) {
  518. $type = '抽奖';
  519. if ($val['type'] == 1) {
  520. $type = '排名奖';
  521. }
  522. $res[] = [
  523. 'giftId' => $val['gift_id'],
  524. 'giftName' => $val['giftName'],
  525. 'typeLabel' => $type,
  526. 'type' => $val['type'],
  527. 'createAt' => date('y-m-d h:i:s', $val['create_at'])
  528. ];
  529. }
  530. return $this->successResponse($res);
  531. }
  532. /**
  533. * 品牌任务关注接口
  534. * @throws FuncNotFoundException
  535. * @throws ClassNotFoundException
  536. * @throws ReflectionException
  537. * @throws DbException
  538. * @throws ModelNotFoundException
  539. * @throws DataNotFoundException
  540. * @return mixed
  541. */
  542. public function follow()
  543. {
  544. // 先获取品牌任务配置及状态
  545. $conf = $this->getBrandConfigAndState();
  546. if ($conf instanceof Json) {
  547. return $conf;
  548. }
  549. // 已经关注过了
  550. if ($conf['state']['follow_state'] == 1) {
  551. return $this->successResponse([
  552. 'result' => 2,
  553. 'brand' => $conf,
  554. ], '您已经关注过了');
  555. }
  556. // 调用接口关注
  557. $weiboRes = (new WeiboService(Safe::$user['uid']))->add($conf['config']['follow']['id']);
  558. if (empty($weiboRes) || $weiboRes['code'] != 10000) {
  559. return $this->response(600, $weiboRes['msg'] ?? '关注失败~');
  560. }
  561. $date = date('Y-m-d');
  562. $redisKey = $this->getTaskStateRedisKey(intval(Safe::$user['uid']));
  563. // 如果已经完成了另外两项,则标记品牌任务已经完成
  564. $finishState = $conf['state']['finish_state'];
  565. if ($conf['state']['view_state'] == 1 && $conf['state']['forward_state'] == 1) {
  566. $finishState = 1;
  567. }
  568. // 更新任务状态
  569. $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update([
  570. 'follow_state' => 1,
  571. 'finish_state' => $finishState,
  572. ]);
  573. if ($nums) {
  574. $conf['state']['follow_state'] = 1;
  575. $conf['state']['finish_state'] = $finishState;
  576. Cache::set($redisKey, json_encode($conf['state']), 86400);
  577. return $this->successResponse([
  578. 'result' => 1,
  579. 'brand' => $conf,
  580. ], '关注成功');
  581. } else {
  582. return $this->response(601, '更新任务状态失败');
  583. }
  584. }
  585. /**
  586. * 品牌任务转发博文接口
  587. * @throws FuncNotFoundException
  588. * @throws ClassNotFoundException
  589. * @throws ReflectionException
  590. * @throws DbException
  591. * @throws ModelNotFoundException
  592. * @throws DataNotFoundException
  593. * @return mixed
  594. */
  595. public function forward()
  596. {
  597. // 先获取品牌任务配置及状态
  598. $conf = $this->getBrandConfigAndState();
  599. if ($conf instanceof Json) {
  600. return $conf;
  601. }
  602. // 已经转发过了
  603. if ($conf['state']['forward_state'] == 1) {
  604. return $this->successResponse([
  605. 'result' => 2,
  606. 'brand' => $conf,
  607. ], '您已经转发过了');
  608. }
  609. // 调用微博接口转发
  610. $weiboRes = (new WeiboService(Safe::$user['uid']))->repost($conf['config']['forward']['id'], $conf['config']['forward']['content'] ?? '');
  611. if (empty($weiboRes) || $weiboRes['code'] != 10000) {
  612. return $this->response(600, $weiboRes['msg'] ?? '转发失败~');
  613. }
  614. $date = date('Y-m-d');
  615. $redisKey = $this->getTaskStateRedisKey(intval(Safe::$user['uid']));
  616. // 如果已经完成了另外两项,则标记品牌任务已经完成
  617. $finishState = $conf['state']['finish_state'];
  618. if ($conf['state']['view_state'] == 1 && $conf['state']['follow_state'] == 1) {
  619. $finishState = 1;
  620. }
  621. // 更新任务状态
  622. $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update([
  623. 'forward_state' => 1,
  624. 'finish_state' => $finishState,
  625. ]);
  626. if ($nums) {
  627. $conf['state']['forward_state'] = 1;
  628. $conf['state']['finish_state'] = $finishState;
  629. Cache::set($redisKey, json_encode($conf['state']), 86400);
  630. return $this->successResponse([
  631. 'result' => 1,
  632. 'brand' => $conf,
  633. ], '转发成功');
  634. } else {
  635. return $this->response(601, '更新任务状态失败');
  636. }
  637. }
  638. /**
  639. * 获取活动规则
  640. * @throws FuncNotFoundException
  641. * @throws ReflectionException
  642. * @throws InvalidArgumentException
  643. * @throws ClassNotFoundException
  644. * @return mixed
  645. */
  646. public function getRule()
  647. {
  648. $config = SystemService::instance()->getData('activity:rule');
  649. return $this->successResponse($config);
  650. }
  651. /**
  652. * 聚合页配置信息
  653. * @throws FuncNotFoundException
  654. * @throws ReflectionException
  655. * @throws InvalidArgumentException
  656. * @throws ClassNotFoundException
  657. * @return mixed
  658. */
  659. public function groupPageConfig()
  660. {
  661. return $this->successResponse(['config' => SystemService::instance()->getData('group:page:config')]);
  662. }
  663. public function index()
  664. {
  665. $this->redirect(sysuri('admin/login/index'));
  666. }
  667. /**
  668. * 聚合页通知获取
  669. * 查询最近20条通知
  670. * @throws DbException
  671. * @throws ModelNotFoundException
  672. * @throws DataNotFoundException
  673. * @return mixed
  674. */
  675. public function notices()
  676. {
  677. $rows = SystemNotice::limit(20)->order('id', 'desc')->select();
  678. return $this->successResponse([
  679. "lists" => $rows,
  680. ]);
  681. }
  682. /**
  683. * 首次弹窗
  684. * 发放邀请函微博
  685. * @return Json
  686. */
  687. public function sendInviteWeibo()
  688. {
  689. $config = SystemService::instance()->getData('group:page:config');
  690. $sendRes = (new WeiboService(Safe::$user['uid']))->status($config['firstDialog']['content']);
  691. if (empty($sendRes) || $sendRes['code'] != 10000) {
  692. return $this->response(403, $sendRes['msg'] ?? '发布失败');
  693. }
  694. Cache::set('u:f:' . Safe::$user['uid'], 1, 15552000);
  695. return $this->successResponse(null, '发布成功!');
  696. }
  697. /**
  698. * 首次弹窗用户未选择发送邀请函
  699. * 标记已经首次弹窗过了
  700. * @return mixed
  701. */
  702. public function setFirst()
  703. {
  704. Cache::set('u:f:' . Safe::$user['uid'], 1, 15552000);
  705. return $this->successResponse(null, '操作成功!');
  706. }
  707. /**
  708. * 标记用户已经浏览过主页了,用户点了去浏览时调用
  709. * @throws FuncNotFoundException
  710. * @throws ClassNotFoundException
  711. * @throws ReflectionException
  712. * @throws DbException
  713. * @throws ModelNotFoundException
  714. * @throws DataNotFoundException
  715. * @return mixed
  716. */
  717. public function setViewed()
  718. {
  719. // 先获取品牌任务配置及状态
  720. $conf = $this->getBrandConfigAndState();
  721. if ($conf instanceof Json) {
  722. return $conf;
  723. }
  724. if ($conf['state']['view_state'] == 1) {
  725. return $this->successResponse([
  726. 'result' => 2,
  727. 'brand' => $conf,
  728. ], '您已经浏览过了');
  729. }
  730. $date = date('Y-m-d');
  731. $redisKey = $this->getTaskStateRedisKey(intval(Safe::$user['uid']));
  732. // 如果已经完成了另外两项,则标记品牌任务已经完成
  733. $finishState = $conf['state']['finish_state'];
  734. if ($conf['state']['forward_state'] == 1 && $conf['state']['follow_state'] == 1) {
  735. $finishState = 1;
  736. }
  737. $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update([
  738. 'view_state' => 1,
  739. 'finish_state' => $finishState,
  740. ]);
  741. if ($nums) {
  742. $conf['state']['view_state'] = 1;
  743. $conf['state']['finish_state'] = $finishState;
  744. Cache::set($redisKey, json_encode($conf['state']), 86400);
  745. return $this->successResponse([
  746. 'result' => 1,
  747. 'brand' => $conf,
  748. ], '浏览成功');
  749. } else {
  750. return $this->response(601, '更新任务状态失败');
  751. }
  752. }
  753. /**
  754. * 用户完成端外分享时调用接口加票
  755. * @throws FuncNotFoundException
  756. * @throws ClassNotFoundException
  757. * @throws ReflectionException
  758. * @throws DbException
  759. * @throws ModelNotFoundException
  760. * @throws DataNotFoundException
  761. * @return mixed
  762. */
  763. public function shareFinishAddVotes()
  764. {
  765. // 先获取品牌任务配置及状态
  766. $conf = $this->getBrandConfigAndState();
  767. if ($conf instanceof Json) {
  768. return $conf;
  769. }
  770. if ($conf['state']['share_add_votes'] == 1) {
  771. return $this->successResponse([
  772. 'result' => 2,
  773. 'brand' => $conf,
  774. ], '您今日已经领取过投票机会了~');
  775. }
  776. // 调用接口给用户发券(加票)
  777. $weiboRes = (new WeiboService(Safe::$user['uid']))->setcoupons($conf['config']['share']['votes']);
  778. if (empty($weiboRes) || $weiboRes['code'] != 10000) {
  779. return $this->response(600, $weiboRes['msg'] ?? '加票失败~');
  780. }
  781. // 标记状态
  782. $date = date('Y-m-d');
  783. $redisKey = $this->getTaskStateRedisKey(intval(Safe::$user['uid']));
  784. $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update([
  785. 'share_add_votes' => 1,
  786. ]);
  787. if ($nums) {
  788. $conf['state']['share_add_votes'] = 1;
  789. Cache::set($redisKey, json_encode($conf['state']), 86400);
  790. return $this->successResponse([
  791. 'result' => 1,
  792. 'brand' => $conf,
  793. ], '加票成功');
  794. } else {
  795. return $this->response(601, '更新任务状态失败');
  796. }
  797. }
  798. /**
  799. * 完成品牌任务后领取加票机会接口
  800. * @throws FuncNotFoundException
  801. * @throws ClassNotFoundException
  802. * @throws ReflectionException
  803. * @throws DbException
  804. * @throws ModelNotFoundException
  805. * @throws DataNotFoundException
  806. * @return mixed
  807. */
  808. public function taskFinishAddVotes()
  809. {
  810. // 先获取品牌任务配置及状态
  811. $conf = $this->getBrandConfigAndState();
  812. if ($conf instanceof Json) {
  813. return $conf;
  814. }
  815. if ($conf['state']['finish_add_votes'] == 1) {
  816. return $this->successResponse([
  817. 'result' => 2,
  818. 'brand' => $conf,
  819. ], '您今日已经领取过投票机会了~');
  820. }
  821. // 未完成品牌任务 领取失败
  822. if ($conf['state']['finish_state'] == 0) {
  823. return $this->response(603, '请先完成品牌任务,再来领取加票', [
  824. 'result' => 0,
  825. 'brand' => $conf,
  826. ]);
  827. }
  828. // 调用微博接口 给用户发券(加票)
  829. $weiboRes = (new WeiboService(Safe::$user['uid']))->setcoupons($conf['config']['task']['votes']);
  830. if (empty($weiboRes) || $weiboRes['code'] != 10000) {
  831. return $this->response(600, $weiboRes['msg'] ?? '加票失败~');
  832. }
  833. // 标记加票成功
  834. $date = date('Y-m-d');
  835. $redisKey = $this->getTaskStateRedisKey(intval(Safe::$user['uid']));
  836. $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update([
  837. 'finish_add_votes' => 1,
  838. ]);
  839. if ($nums) {
  840. $conf['state']['finish_add_votes'] = 1;
  841. Cache::set($redisKey, json_encode($conf['state']), 86400);
  842. return $this->successResponse([
  843. 'result' => 1,
  844. 'brand' => $conf,
  845. ], '加票成功');
  846. } else {
  847. return $this->response(601, '更新任务状态失败');
  848. }
  849. }
  850. /**
  851. * 获取品牌任务配置及状态
  852. * @throws FuncNotFoundException
  853. * @throws ReflectionException
  854. * @throws InvalidArgumentException
  855. * @throws ClassNotFoundException
  856. * @throws DbException
  857. * @throws ModelNotFoundException
  858. * @throws DataNotFoundException
  859. * @return \think\response\Json|array
  860. */
  861. protected function getBrandConfigAndState()
  862. {
  863. $config = SystemService::instance()->getData('brand:task:config');
  864. $date = date('Y-m-d');
  865. $dateForRedis = date('Ymd');
  866. if (empty(Safe::$user['uid'])) {
  867. $state = [
  868. // 品牌任务完成状态
  869. 'finish_state' => 0,
  870. // 关注子任务状态
  871. 'follow_state' => 0,
  872. // 转发子任务状态
  873. 'forward_state' => 0,
  874. // 查看浏览主页任务状态
  875. 'view_state' => 0,
  876. // 品牌任务完成后是否加票
  877. 'finish_add_votes' => 0,
  878. // 分享完成后是否加票
  879. 'share_add_votes' => 0,
  880. 'uid' => 0,
  881. 'date' => $date,
  882. ];
  883. } else {
  884. $redisKey = "t:{$dateForRedis}:" . Safe::$user['uid'];
  885. $state = Cache::get($redisKey);
  886. if (empty($state)) {
  887. // 缓存失败 查数据库
  888. $state = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->find();
  889. if (empty($state)) {
  890. // 当天第一次,初始化数据
  891. $state = [
  892. // 品牌任务完成状态
  893. 'finish_state' => 0,
  894. // 关注子任务状态
  895. 'follow_state' => 0,
  896. // 转发子任务状态
  897. 'forward_state' => 0,
  898. // 查看浏览主页任务状态
  899. 'view_state' => 0,
  900. // 品牌任务完成后是否加票
  901. 'finish_add_votes' => 0,
  902. // 分享完成后是否加票
  903. 'share_add_votes' => 0,
  904. 'uid' => Safe::$user['uid'],
  905. 'date' => $date,
  906. ];
  907. if (0 == Db::table('awards_user_task')->insert($state)) {
  908. return $this->response(5001, '系统错误,请稍后再试~');
  909. }
  910. }
  911. Cache::set($redisKey, json_encode($state), 86400);
  912. } else {
  913. $state = json_decode($state, true);
  914. }
  915. // 如果未关注状态,查询下已经实际已经关注了
  916. if ($state['follow_state'] == 0) {
  917. $weiboRes = (new WeiboService(Safe::$user['uid']))->friends($config['follow']['id']);
  918. if (isset($weiboRes['data'][$config['follow']['id']]) && $weiboRes['data'][$config['follow']['id']] == 1) {
  919. $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update(['follow_state' => 1]);
  920. if ($nums) {
  921. $state['follow_state'] = 1;
  922. Cache::set($redisKey, json_encode($state), 86400);
  923. }
  924. }
  925. }
  926. }
  927. // 校准状态,可能存在异常情况关注,转发,浏览都完成了,但是总的品牌任务状态没标记成已完成
  928. if ($state['follow_state'] == 1 && $state['forward_state'] == 1 && $state['view_state'] == 1 && $state['finish_state'] != 1) {
  929. $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update(['finish_state' => 1]);
  930. if ($nums) {
  931. $state['finish_state'] = 1;
  932. Cache::set($redisKey, json_encode($state), 86400);
  933. }
  934. }
  935. return [
  936. 'config' => $config,
  937. 'state' => $state,
  938. ];
  939. }
  940. /**
  941. * 获取品牌任务redis 缓存键
  942. * @param int $uid
  943. * @return string
  944. */
  945. protected function getTaskStateRedisKey(int $uid): string
  946. {
  947. $dateForRedis = date('Ymd');
  948. return "t:{$dateForRedis}:" . $uid;
  949. }
  950. /**
  951. * @param $code
  952. * @param $msg
  953. * @param $data
  954. * @return Json
  955. */
  956. protected function response($code, $msg, $data = null)
  957. {
  958. return json(
  959. [
  960. 'code' => $code,
  961. 'message' => $msg,
  962. 'data' => $data,
  963. 'trackID' => Log::$logID,
  964. ],
  965. 200
  966. );
  967. }
  968. /**
  969. * @param $data
  970. * @param $msg
  971. * @return mixed
  972. */
  973. protected function successResponse($data, $msg = '操作成功')
  974. {
  975. return $this->response(0, $msg, $data);
  976. }
  977. }