Index.php 36 KB

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