Index.php 37 KB

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