Index.php 36 KB

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