Index.php 35 KB

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