Index.php 36 KB

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