Index.php 36 KB

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