Index.php 36 KB

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