Index.php 37 KB

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