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("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. $name = trim(Request::post("name"));
  316. $mobile = trim(Request::post("mobile"));
  317. $address = trim(Request::post("address"));
  318. $giftId = trim(Request::post("giftId"));
  319. $type = trim(Request::post("type"));
  320. if ($type == 1) {
  321. $activity = Db::table('awards_activity')->where('begin_at', '<=', time())
  322. ->where('end_at', '>=', time())->find();
  323. if (empty($activity)) {
  324. return $this->response(5002, '活动末开始');
  325. }
  326. }
  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. $giftId = 0;
  411. // 如果中奖 插入用户中奖信息
  412. if ($isWinAward == 1) {
  413. $gifts = Db::table('awards_gift')->where('type', 2)->select();
  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. $limit = 2;
  466. $total = $logCount + 1;
  467. if ($orderCount == floor($total/$limit)) {
  468. $isWinAward = 0;
  469. $giftId = 0;
  470. break;
  471. }
  472. for ($i=0;$i<$gift['count'];$i++) {
  473. $giftRage = rand(0, 5000);
  474. if ($userRate == $giftRage) {
  475. $isWinAward = 1;
  476. $giftId = $gift['id'];
  477. break;
  478. }
  479. }
  480. // 如果参与活动人数 5000人内 还没有人中奖那最后一人必中。
  481. if ($orderCount < floor($total/$limit) && $isWinAward ==0 && ($total%$limit) == 0) {
  482. $isWinAward = 1;
  483. $giftId = $gift['id'];
  484. break;
  485. }
  486. }
  487. // 如果中奖 插入用户中奖信息
  488. if ($isWinAward == 1) {
  489. $order = [
  490. 'uid' => Safe::$user['uid'],
  491. 'gift_id' => $giftId,
  492. 'type' => 1,
  493. 'create_at' => time(),
  494. ];
  495. if (0 == Db::table('awards_order')->insert($order)) {
  496. return $this->response(5001, '系统错误,请稍后再试~');
  497. }
  498. }
  499. return $this->successResponse([
  500. 'isWinAward' => $isWinAward,
  501. 'giftId' => $giftId
  502. ]);
  503. }
  504. /**
  505. * 奖品记录接口
  506. * @throws FuncNotFoundException
  507. * @throws ClassNotFoundException
  508. * @throws DbException
  509. * @throws ModelNotFoundException
  510. * @throws DataNotFoundException
  511. * @return mixed
  512. */
  513. public function getAwardLog()
  514. {
  515. $awards = Db::table('awards_order')->alias('o')
  516. ->leftJoin('awards_gift g', 'o.gift_id = g.id')
  517. ->field(['g.name as giftName', 'o.*'])
  518. ->where('o.uid', Safe::$user['uid'])
  519. ->select();
  520. $res = [];
  521. foreach ($awards as $val) {
  522. $type = '抽奖';
  523. if ($val['type'] == 2) {
  524. $type = '排名奖';
  525. }
  526. $isReceive = 1;
  527. if (empty($val['name']) && empty($val['address']) && empty($val['mobile'])) {
  528. $isReceive = 0;
  529. }
  530. $res[] = [
  531. 'giftId' => $val['gift_id'],
  532. 'giftName' => $val['giftName'],
  533. 'typeLabel' => $type,
  534. 'type' => $val['type'],
  535. 'createAt' => date('y-m-d h:i:s', $val['create_at']),
  536. 'address' => $val['address'],
  537. 'name' => $val['name'],
  538. 'mobile' => $val['mobile'],
  539. 'isReceive' => $isReceive
  540. ];
  541. }
  542. return $this->successResponse($res);
  543. }
  544. public function clearGameData()
  545. {
  546. // 更新用户参与状态
  547. Db::table('awards_user_info')->where('uid', Safe::$user['uid'])->update([
  548. 'is_share' => 0,
  549. 'share_at' => 0,
  550. 'count' => 1,
  551. ]);
  552. Db::table('awards_user_task_log')->where('uid', Safe::$user['uid'])->delete();
  553. Db::table('awards_order')->where('uid', Safe::$user['uid'])->delete();
  554. return $this->successResponse(null, '清除成功');
  555. }
  556. /**
  557. * 品牌任务关注接口
  558. * @throws FuncNotFoundException
  559. * @throws ClassNotFoundException
  560. * @throws ReflectionException
  561. * @throws DbException
  562. * @throws ModelNotFoundException
  563. * @throws DataNotFoundException
  564. * @return mixed
  565. */
  566. public function follow()
  567. {
  568. // 先获取品牌任务配置及状态
  569. $conf = $this->getBrandConfigAndState();
  570. if ($conf instanceof Json) {
  571. return $conf;
  572. }
  573. // 已经关注过了
  574. if ($conf['state']['follow_state'] == 1) {
  575. return $this->successResponse([
  576. 'result' => 2,
  577. 'brand' => $conf,
  578. ], '您已经关注过了');
  579. }
  580. // 调用接口关注
  581. $weiboRes = (new WeiboService(Safe::$user['uid']))->add($conf['config']['follow']['id']);
  582. if (empty($weiboRes) || $weiboRes['code'] != 10000) {
  583. return $this->response(600, $weiboRes['msg'] ?? '关注失败~');
  584. }
  585. $date = date('Y-m-d');
  586. $redisKey = $this->getTaskStateRedisKey(intval(Safe::$user['uid']));
  587. // 如果已经完成了另外两项,则标记品牌任务已经完成
  588. $finishState = $conf['state']['finish_state'];
  589. if ($conf['state']['view_state'] == 1 && $conf['state']['forward_state'] == 1) {
  590. $finishState = 1;
  591. }
  592. // 更新任务状态
  593. $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update([
  594. 'follow_state' => 1,
  595. 'finish_state' => $finishState,
  596. ]);
  597. if ($nums) {
  598. $conf['state']['follow_state'] = 1;
  599. $conf['state']['finish_state'] = $finishState;
  600. Cache::set($redisKey, json_encode($conf['state']), 86400);
  601. return $this->successResponse([
  602. 'result' => 1,
  603. 'brand' => $conf,
  604. ], '关注成功');
  605. } else {
  606. return $this->response(601, '更新任务状态失败');
  607. }
  608. }
  609. /**
  610. * 品牌任务转发博文接口
  611. * @throws FuncNotFoundException
  612. * @throws ClassNotFoundException
  613. * @throws ReflectionException
  614. * @throws DbException
  615. * @throws ModelNotFoundException
  616. * @throws DataNotFoundException
  617. * @return mixed
  618. */
  619. public function forward()
  620. {
  621. // 先获取品牌任务配置及状态
  622. $conf = $this->getBrandConfigAndState();
  623. if ($conf instanceof Json) {
  624. return $conf;
  625. }
  626. // 已经转发过了
  627. if ($conf['state']['forward_state'] == 1) {
  628. return $this->successResponse([
  629. 'result' => 2,
  630. 'brand' => $conf,
  631. ], '您已经转发过了');
  632. }
  633. // 调用微博接口转发
  634. $weiboRes = (new WeiboService(Safe::$user['uid']))->repost($conf['config']['forward']['id'], $conf['config']['forward']['content'] ?? '');
  635. if (empty($weiboRes) || $weiboRes['code'] != 10000) {
  636. return $this->response(600, $weiboRes['msg'] ?? '转发失败~');
  637. }
  638. $date = date('Y-m-d');
  639. $redisKey = $this->getTaskStateRedisKey(intval(Safe::$user['uid']));
  640. // 如果已经完成了另外两项,则标记品牌任务已经完成
  641. $finishState = $conf['state']['finish_state'];
  642. if ($conf['state']['view_state'] == 1 && $conf['state']['follow_state'] == 1) {
  643. $finishState = 1;
  644. }
  645. // 更新任务状态
  646. $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update([
  647. 'forward_state' => 1,
  648. 'finish_state' => $finishState,
  649. ]);
  650. if ($nums) {
  651. $conf['state']['forward_state'] = 1;
  652. $conf['state']['finish_state'] = $finishState;
  653. Cache::set($redisKey, json_encode($conf['state']), 86400);
  654. return $this->successResponse([
  655. 'result' => 1,
  656. 'brand' => $conf,
  657. ], '转发成功');
  658. } else {
  659. return $this->response(601, '更新任务状态失败');
  660. }
  661. }
  662. /**
  663. * 获取活动规则
  664. * @throws FuncNotFoundException
  665. * @throws ReflectionException
  666. * @throws InvalidArgumentException
  667. * @throws ClassNotFoundException
  668. * @return mixed
  669. */
  670. public function getRule()
  671. {
  672. $config = SystemService::instance()->getData('activity:rule');
  673. return $this->successResponse($config);
  674. }
  675. /**
  676. * 聚合页配置信息
  677. * @throws FuncNotFoundException
  678. * @throws ReflectionException
  679. * @throws InvalidArgumentException
  680. * @throws ClassNotFoundException
  681. * @return mixed
  682. */
  683. public function groupPageConfig()
  684. {
  685. return $this->successResponse(['config' => SystemService::instance()->getData('group:page:config')]);
  686. }
  687. // public function index()
  688. // {
  689. // $this->redirect('video');
  690. // }
  691. /**
  692. * 聚合页通知获取
  693. * 查询最近20条通知
  694. * @throws DbException
  695. * @throws ModelNotFoundException
  696. * @throws DataNotFoundException
  697. * @return mixed
  698. */
  699. public function notices()
  700. {
  701. $rows = SystemNotice::limit(20)->order('id', 'desc')->select();
  702. return $this->successResponse([
  703. "lists" => $rows,
  704. ]);
  705. }
  706. /**
  707. * 首次弹窗
  708. * 发放邀请函微博
  709. * @return Json
  710. */
  711. public function sendInviteWeibo()
  712. {
  713. $config = SystemService::instance()->getData('group:page:config');
  714. $sendRes = (new WeiboService(Safe::$user['uid']))->status($config['firstDialog']['content']);
  715. if (empty($sendRes) || $sendRes['code'] != 10000) {
  716. return $this->response(403, $sendRes['msg'] ?? '发布失败');
  717. }
  718. Cache::set('u:f:' . Safe::$user['uid'], 1, 15552000);
  719. return $this->successResponse(null, '发布成功!');
  720. }
  721. /**
  722. * 首次弹窗用户未选择发送邀请函
  723. * 标记已经首次弹窗过了
  724. * @return mixed
  725. */
  726. public function setFirst()
  727. {
  728. Cache::set('u:f:' . Safe::$user['uid'], 1, 15552000);
  729. return $this->successResponse(null, '操作成功!');
  730. }
  731. /**
  732. * 标记用户已经浏览过主页了,用户点了去浏览时调用
  733. * @throws FuncNotFoundException
  734. * @throws ClassNotFoundException
  735. * @throws ReflectionException
  736. * @throws DbException
  737. * @throws ModelNotFoundException
  738. * @throws DataNotFoundException
  739. * @return mixed
  740. */
  741. public function setViewed()
  742. {
  743. // 先获取品牌任务配置及状态
  744. $conf = $this->getBrandConfigAndState();
  745. if ($conf instanceof Json) {
  746. return $conf;
  747. }
  748. if ($conf['state']['view_state'] == 1) {
  749. return $this->successResponse([
  750. 'result' => 2,
  751. 'brand' => $conf,
  752. ], '您已经浏览过了');
  753. }
  754. $date = date('Y-m-d');
  755. $redisKey = $this->getTaskStateRedisKey(intval(Safe::$user['uid']));
  756. // 如果已经完成了另外两项,则标记品牌任务已经完成
  757. $finishState = $conf['state']['finish_state'];
  758. if ($conf['state']['forward_state'] == 1 && $conf['state']['follow_state'] == 1) {
  759. $finishState = 1;
  760. }
  761. $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update([
  762. 'view_state' => 1,
  763. 'finish_state' => $finishState,
  764. ]);
  765. if ($nums) {
  766. $conf['state']['view_state'] = 1;
  767. $conf['state']['finish_state'] = $finishState;
  768. Cache::set($redisKey, json_encode($conf['state']), 86400);
  769. return $this->successResponse([
  770. 'result' => 1,
  771. 'brand' => $conf,
  772. ], '浏览成功');
  773. } else {
  774. return $this->response(601, '更新任务状态失败');
  775. }
  776. }
  777. /**
  778. * 用户完成端外分享时调用接口加票
  779. * @throws FuncNotFoundException
  780. * @throws ClassNotFoundException
  781. * @throws ReflectionException
  782. * @throws DbException
  783. * @throws ModelNotFoundException
  784. * @throws DataNotFoundException
  785. * @return mixed
  786. */
  787. public function shareFinishAddVotes()
  788. {
  789. // 先获取品牌任务配置及状态
  790. $conf = $this->getBrandConfigAndState();
  791. if ($conf instanceof Json) {
  792. return $conf;
  793. }
  794. if ($conf['state']['share_add_votes'] == 1) {
  795. return $this->successResponse([
  796. 'result' => 2,
  797. 'brand' => $conf,
  798. ], '您今日已经领取过投票机会了~');
  799. }
  800. // 调用接口给用户发券(加票)
  801. $weiboRes = (new WeiboService(Safe::$user['uid']))->setcoupons($conf['config']['share']['votes']);
  802. if (empty($weiboRes) || $weiboRes['code'] != 10000) {
  803. return $this->response(600, $weiboRes['msg'] ?? '加票失败~');
  804. }
  805. // 标记状态
  806. $date = date('Y-m-d');
  807. $redisKey = $this->getTaskStateRedisKey(intval(Safe::$user['uid']));
  808. $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update([
  809. 'share_add_votes' => 1,
  810. ]);
  811. if ($nums) {
  812. $conf['state']['share_add_votes'] = 1;
  813. Cache::set($redisKey, json_encode($conf['state']), 86400);
  814. return $this->successResponse([
  815. 'result' => 1,
  816. 'brand' => $conf,
  817. ], '加票成功');
  818. } else {
  819. return $this->response(601, '更新任务状态失败');
  820. }
  821. }
  822. /**
  823. * 完成品牌任务后领取加票机会接口
  824. * @throws FuncNotFoundException
  825. * @throws ClassNotFoundException
  826. * @throws ReflectionException
  827. * @throws DbException
  828. * @throws ModelNotFoundException
  829. * @throws DataNotFoundException
  830. * @return mixed
  831. */
  832. public function taskFinishAddVotes()
  833. {
  834. // 先获取品牌任务配置及状态
  835. $conf = $this->getBrandConfigAndState();
  836. if ($conf instanceof Json) {
  837. return $conf;
  838. }
  839. if ($conf['state']['finish_add_votes'] == 1) {
  840. return $this->successResponse([
  841. 'result' => 2,
  842. 'brand' => $conf,
  843. ], '您今日已经领取过投票机会了~');
  844. }
  845. // 未完成品牌任务 领取失败
  846. if ($conf['state']['finish_state'] == 0) {
  847. return $this->response(603, '请先完成品牌任务,再来领取加票', [
  848. 'result' => 0,
  849. 'brand' => $conf,
  850. ]);
  851. }
  852. // 调用微博接口 给用户发券(加票)
  853. $weiboRes = (new WeiboService(Safe::$user['uid']))->setcoupons($conf['config']['task']['votes']);
  854. if (empty($weiboRes) || $weiboRes['code'] != 10000) {
  855. return $this->response(600, $weiboRes['msg'] ?? '加票失败~');
  856. }
  857. // 标记加票成功
  858. $date = date('Y-m-d');
  859. $redisKey = $this->getTaskStateRedisKey(intval(Safe::$user['uid']));
  860. $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update([
  861. 'finish_add_votes' => 1,
  862. ]);
  863. if ($nums) {
  864. $conf['state']['finish_add_votes'] = 1;
  865. Cache::set($redisKey, json_encode($conf['state']), 86400);
  866. return $this->successResponse([
  867. 'result' => 1,
  868. 'brand' => $conf,
  869. ], '加票成功');
  870. } else {
  871. return $this->response(601, '更新任务状态失败');
  872. }
  873. }
  874. /**
  875. * 获取品牌任务配置及状态
  876. * @throws FuncNotFoundException
  877. * @throws ReflectionException
  878. * @throws InvalidArgumentException
  879. * @throws ClassNotFoundException
  880. * @throws DbException
  881. * @throws ModelNotFoundException
  882. * @throws DataNotFoundException
  883. * @return \think\response\Json|array
  884. */
  885. protected function getBrandConfigAndState()
  886. {
  887. $config = SystemService::instance()->getData('brand:task:config');
  888. $date = date('Y-m-d');
  889. $dateForRedis = date('Ymd');
  890. if (empty(Safe::$user['uid'])) {
  891. $state = [
  892. // 品牌任务完成状态
  893. 'finish_state' => 0,
  894. // 关注子任务状态
  895. 'follow_state' => 0,
  896. // 转发子任务状态
  897. 'forward_state' => 0,
  898. // 查看浏览主页任务状态
  899. 'view_state' => 0,
  900. // 品牌任务完成后是否加票
  901. 'finish_add_votes' => 0,
  902. // 分享完成后是否加票
  903. 'share_add_votes' => 0,
  904. 'uid' => 0,
  905. 'date' => $date,
  906. ];
  907. } else {
  908. $redisKey = "t:{$dateForRedis}:" . Safe::$user['uid'];
  909. $state = Cache::get($redisKey);
  910. if (empty($state)) {
  911. // 缓存失败 查数据库
  912. $state = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->find();
  913. if (empty($state)) {
  914. // 当天第一次,初始化数据
  915. $state = [
  916. // 品牌任务完成状态
  917. 'finish_state' => 0,
  918. // 关注子任务状态
  919. 'follow_state' => 0,
  920. // 转发子任务状态
  921. 'forward_state' => 0,
  922. // 查看浏览主页任务状态
  923. 'view_state' => 0,
  924. // 品牌任务完成后是否加票
  925. 'finish_add_votes' => 0,
  926. // 分享完成后是否加票
  927. 'share_add_votes' => 0,
  928. 'uid' => Safe::$user['uid'],
  929. 'date' => $date,
  930. ];
  931. if (0 == Db::table('awards_user_task')->insert($state)) {
  932. return $this->response(5001, '系统错误,请稍后再试~');
  933. }
  934. }
  935. Cache::set($redisKey, json_encode($state), 86400);
  936. } else {
  937. $state = json_decode($state, true);
  938. }
  939. // 如果未关注状态,查询下已经实际已经关注了
  940. if ($state['follow_state'] == 0) {
  941. $weiboRes = (new WeiboService(Safe::$user['uid']))->friends($config['follow']['id']);
  942. if (isset($weiboRes['data'][$config['follow']['id']]) && $weiboRes['data'][$config['follow']['id']] == 1) {
  943. $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update(['follow_state' => 1]);
  944. if ($nums) {
  945. $state['follow_state'] = 1;
  946. Cache::set($redisKey, json_encode($state), 86400);
  947. }
  948. }
  949. }
  950. }
  951. // 校准状态,可能存在异常情况关注,转发,浏览都完成了,但是总的品牌任务状态没标记成已完成
  952. if ($state['follow_state'] == 1 && $state['forward_state'] == 1 && $state['view_state'] == 1 && $state['finish_state'] != 1) {
  953. $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update(['finish_state' => 1]);
  954. if ($nums) {
  955. $state['finish_state'] = 1;
  956. Cache::set($redisKey, json_encode($state), 86400);
  957. }
  958. }
  959. return [
  960. 'config' => $config,
  961. 'state' => $state,
  962. ];
  963. }
  964. /**
  965. * 获取品牌任务redis 缓存键
  966. * @param int $uid
  967. * @return string
  968. */
  969. protected function getTaskStateRedisKey(int $uid): string
  970. {
  971. $dateForRedis = date('Ymd');
  972. return "t:{$dateForRedis}:" . $uid;
  973. }
  974. /**
  975. * @param $code
  976. * @param $msg
  977. * @param $data
  978. * @return Json
  979. */
  980. protected function response($code, $msg, $data = null)
  981. {
  982. return json(
  983. [
  984. 'code' => $code,
  985. 'message' => $msg,
  986. 'data' => $data,
  987. 'trackID' => Log::$logID,
  988. ],
  989. 200
  990. );
  991. }
  992. /**
  993. * @param $data
  994. * @param $msg
  995. * @return mixed
  996. */
  997. protected function successResponse($data, $msg = '操作成功')
  998. {
  999. return $this->response(0, $msg, $data);
  1000. }
  1001. }