Index.php 36 KB

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