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