Index.php 36 KB

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