Index.php 37 KB

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