Index.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  1. <?php
  2. namespace app\index\controller;
  3. use think\db\Where;
  4. use think\facade\Db;
  5. use app\middleware\Log;
  6. use think\facade\Cache;
  7. use ReflectionException;
  8. use app\middleware\Safe;
  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("cookies:" . json_encode(Request::post()));
  56. $sub = "";
  57. $uid = 0;
  58. FacadeLog::info($_COOKIE);
  59. if (!empty($_COOKIE['SUB'])) {
  60. $sub = $_COOKIE['SUB'];
  61. } else {
  62. // 只在调试模式下开启从POST参数中获取UID,方便测试联调
  63. if (env('app_debug')) {
  64. $sub = Request::post('cookie');
  65. $uid = $sub;
  66. } else {
  67. return $this->response(403, 'not login.');
  68. }
  69. }
  70. FacadeLog::info($sub);
  71. $userInfoRes = (new WeiboService($uid))->userinfo($sub);
  72. if (empty($userInfoRes) || $userInfoRes['ok'] != 1) {
  73. return $this->response(403, $userInfoRes['msg'] ?? '没有登录');
  74. }
  75. // 使用客户端信息生成token
  76. $token = md5($_SERVER['HTTP_USER_AGENT'] . $_SERVER['HTTP_ACCEPT_ENCODING'] . $_SERVER['HTTP_ACCEPT_LANGUAGE'] . $_SERVER['HTTP_REFERER'] . get_client_ip(0) . $userInfoRes['data']['uid']);
  77. $user = $userInfoRes['data'];
  78. $userInfo = Db::table('awards_user_info')->where('uid', $user['uid'])->find();
  79. $count = 0;
  80. $isShare = 0;
  81. if (empty($userInfo)) {
  82. $userAttr = [
  83. 'uid' => $user['uid'],
  84. 'portrait' => $user['profile_image_url'],
  85. 'nickname' => $user['name'],
  86. 'is_share' => 0,
  87. 'count' => 1,
  88. 'create_at' => time()
  89. ];
  90. if (0 == Db::table('awards_user_info')->insert($userAttr)) {
  91. return $this->response(5001, '系统错误,请稍后再试~');
  92. }
  93. } else {
  94. $count = Db::table('awards_user_task_log')->where('uid', $user['uid'])->count('id');
  95. $isShare = $userInfo['is_share'];
  96. }
  97. // 生成加密用的密钥和向量
  98. $cipher = "aes-256-gcm";
  99. $ivlen = openssl_cipher_iv_length($cipher);
  100. $iv = bin2hex(openssl_random_pseudo_bytes($ivlen));
  101. $aesKey = bin2hex(openssl_random_pseudo_bytes(32));
  102. $user = array_merge($user, [
  103. 'aes_key' => $aesKey,
  104. 'ase_iv' => $iv,
  105. ]);
  106. $cacheUser = [
  107. 'aes_key' => $aesKey,
  108. 'ase_iv' => $iv,
  109. 'uid' => $user['uid'],
  110. ];
  111. // 缓存用户信息1天
  112. Cache::set('u:' . $token, json_encode($cacheUser), 86400);
  113. return $this->successResponse([
  114. 'user' => $user,
  115. 'token' => $token,
  116. 'isShare' => $isShare,
  117. 'count' => $count
  118. ]);
  119. }
  120. /**
  121. * 是否可以参与游戏
  122. * @throws ClassNotFoundException
  123. * @throws ReflectionException
  124. * @return mixed
  125. */
  126. public function checkRole()
  127. {
  128. $userInfo = Db::table('awards_user_info')->where('uid', Safe::$user['uid'])->find();
  129. $count = Db::table('awards_user_task_log')->where('uid', Safe::$user['uid'])->count('id');
  130. return $this->successResponse([
  131. 'isShare' => $userInfo['is_share'],
  132. 'count' => $count
  133. ]);
  134. }
  135. /**
  136. * 提交任务接口
  137. * @throws FuncNotFoundException
  138. * @throws ClassNotFoundException
  139. * @throws DbException
  140. * @throws ModelNotFoundException
  141. * @throws DataNotFoundException
  142. * @return mixed
  143. */
  144. public function submitTask()
  145. {
  146. // if (empty(Safe::$body)) {
  147. // return $this->response(5003, '参数有误');
  148. // }
  149. //
  150. // if (!isset(Safe::$body['duration']) || Safe::$body['duration'] < 1 || Safe::$body['duration'] > 1000) {
  151. // return $this->response(5003, '时长参数有误');
  152. // }
  153. //
  154. // if (!isset(Safe::$body['number']) || Safe::$body['number'] < 0 || Safe::$body['number'] > 1000) {
  155. // return $this->response(5003, '时长参数有误');
  156. // }
  157. // $duration = Safe::$body['duration'];
  158. // $number = Safe::$body['number'];
  159. $duration = Request::post("duration");
  160. $number = Request::post("number");
  161. $date = date('Y-m-d');
  162. $userInfo = Db::table('awards_user_info')->where('uid', Safe::$user['uid'])->find();
  163. if (empty($userInfo)) {
  164. return $this->response(403, '没有登录');
  165. }
  166. $count = Db::table('awards_user_task_log')->where('uid', Safe::$user['uid'])->count('id');
  167. if ($count >= $userInfo['count']) {
  168. return $this->response(5002, '没有参与次数');
  169. }
  170. $log = [
  171. 'uid' => Safe::$user['uid'],
  172. 'date' => $date,
  173. 'duration' => $duration,
  174. 'number' => $number,
  175. 'create_at' => time()
  176. ];
  177. if (0 == Db::table('awards_user_task_log')->insert($log)) {
  178. return $this->response(5001, '系统错误,请稍后再试~');
  179. }
  180. return $this->successResponse(null, '提交成功');
  181. }
  182. /**
  183. * 提交发布微博接口
  184. * @throws FuncNotFoundException
  185. * @throws ClassNotFoundException
  186. * @throws DbException
  187. * @throws ModelNotFoundException
  188. * @throws DataNotFoundException
  189. * @return mixed
  190. */
  191. public function submitShare()
  192. {
  193. $userInfo = Db::table('awards_user_info')->where('uid', Safe::$user['uid'])->find();
  194. if (empty($userInfo)) {
  195. return $this->response(403, '没有登录');
  196. }
  197. if ($userInfo['is_share'] > 0) {
  198. return $this->response(601, '已发布');
  199. }
  200. // 更新任务状态
  201. $nums = Db::table('awards_user_info')->where('uid', Safe::$user['uid'])->update([
  202. 'is_share' => 1,
  203. 'count' => 2,
  204. 'share_at' => time()
  205. ]);
  206. if ($nums) {
  207. return $this->successResponse(null, '提交成功');
  208. } else {
  209. return $this->response(601, '发布失败');
  210. }
  211. }
  212. /**
  213. * 头号排行接口
  214. * @throws FuncNotFoundException
  215. * @throws ClassNotFoundException
  216. * @throws DbException
  217. * @throws ModelNotFoundException
  218. * @throws DataNotFoundException
  219. * @return mixed
  220. */
  221. public function ranking()
  222. {
  223. $ranking = Db::table('awards_user_task_log')->alias("l")
  224. ->leftJoin('awards_user_info u', 'l.uid = u.uid')
  225. ->field(['u.nickname', 'u.portrait', 'u.uid', 'l.duration', 'l.number'])
  226. ->order('l.number', 'desc')
  227. ->order('l.duration', 'asc')
  228. ->limit(50)
  229. ->select();
  230. $userInfo = Db::table('awards_user_task_log')->alias('l')
  231. ->leftJoin('awards_user_info u', 'l.uid = u.uid')
  232. ->field(['u.nickname', 'u.portrait', 'u.uid', 'l.duration', 'l.number'])
  233. ->order('l.number', 'desc')
  234. ->order('l.duration', 'asc')
  235. ->find();
  236. $res = [
  237. 'self' => $userInfo,
  238. 'ranking' => $ranking
  239. ];
  240. return $this->successResponse($res);
  241. }
  242. /**
  243. * 最新排行接口
  244. * @throws FuncNotFoundException
  245. * @throws ClassNotFoundException
  246. * @throws DbException
  247. * @throws ModelNotFoundException
  248. * @throws DataNotFoundException
  249. * @return mixed
  250. */
  251. public function newRanking()
  252. {
  253. $ranking = Db::table('awards_user_task_log')->alias("l")
  254. ->leftJoin('awards_user_info u', 'l.uid = u.uid')
  255. ->field(['u.nickname', 'u.portrait', 'u.uid', 'l.duration', 'l.number'])
  256. ->order('l.create_at', 'desc')
  257. ->order('l.number', 'desc')
  258. ->order('l.duration', 'asc')
  259. ->limit(15)
  260. ->select();
  261. $userInfo = Db::table('awards_user_task_log')->alias('l')
  262. ->leftJoin('awards_user_info u', 'l.uid = u.uid')
  263. ->field(['u.nickname', 'u.portrait', 'u.uid', 'l.duration', 'l.number'])
  264. ->order('l.number', 'desc')
  265. ->order('l.duration', 'asc')
  266. ->find();
  267. $res = [
  268. 'self' => $userInfo,
  269. 'ranking' => $ranking
  270. ];
  271. return $this->successResponse($res);
  272. }
  273. /**
  274. * 提交领取信息接口
  275. * @throws FuncNotFoundException
  276. * @throws ClassNotFoundException
  277. * @throws DbException
  278. * @throws ModelNotFoundException
  279. * @throws DataNotFoundException
  280. * @return mixed
  281. */
  282. public function submitReceive()
  283. {
  284. $name = trim(Request::post("name"));
  285. $mobile = trim(Request::post("mobile"));
  286. $address = trim(Request::post("address"));
  287. $giftId = trim(Request::post("giftId"));
  288. $type = trim(Request::post("type"));
  289. $userInfo = Db::table('awards_user_info')->where('uid', Safe::$user['uid'])->find();
  290. if (empty($userInfo)) {
  291. return $this->response(403, '没有登录');
  292. }
  293. if (strlen($name) < 1 || strlen($name) > 50) {
  294. return $this->response(5001, '姓名有误');
  295. }
  296. if (strlen($mobile) != 11) {
  297. return $this->response(5001, '手机号有误');
  298. }
  299. if (strlen($address) < 1 || strlen($address) > 1000) {
  300. return $this->response(5001, '地址有误');
  301. }
  302. $order = [
  303. 'uid' => Safe::$user['uid'],
  304. 'name' => $name,
  305. 'mobile' => $mobile,
  306. 'address' => $address,
  307. 'gift_at' => $giftId,
  308. 'create_at' => time(),
  309. ];
  310. if (0 == Db::table('awards_order')->insert($order)) {
  311. return $this->response(5001, '系统错误,请稍后再试~');
  312. }
  313. return $this->successResponse(null, '提交成功');
  314. }
  315. /**
  316. * 获取中奖信息接口
  317. * @throws FuncNotFoundException
  318. * @throws ClassNotFoundException
  319. * @throws DbException
  320. * @throws ModelNotFoundException
  321. * @throws DataNotFoundException
  322. * @return mixed
  323. */
  324. public function getWinAward() {
  325. $orders = Db::table('awards_order')->alias('o')
  326. ->leftJoin('awards_user_info u', 'o.uid = u.uid')
  327. ->leftJoin('awards_gift g', 'o.gift_id = g.id')
  328. ->field(['u.nickname', 'u.portrait', 'u.uid', 'g.name as giftName'])
  329. ->order('o.id', 'desc')
  330. ->select();
  331. return $this->successResponse($orders);
  332. }
  333. /**
  334. * 品牌任务关注接口
  335. * @throws FuncNotFoundException
  336. * @throws ClassNotFoundException
  337. * @throws ReflectionException
  338. * @throws DbException
  339. * @throws ModelNotFoundException
  340. * @throws DataNotFoundException
  341. * @return mixed
  342. */
  343. public function follow()
  344. {
  345. // 先获取品牌任务配置及状态
  346. $conf = $this->getBrandConfigAndState();
  347. if ($conf instanceof Json) {
  348. return $conf;
  349. }
  350. // 已经关注过了
  351. if ($conf['state']['follow_state'] == 1) {
  352. return $this->successResponse([
  353. 'result' => 2,
  354. 'brand' => $conf,
  355. ], '您已经关注过了');
  356. }
  357. // 调用接口关注
  358. $weiboRes = (new WeiboService(Safe::$user['uid']))->add($conf['config']['follow']['id']);
  359. if (empty($weiboRes) || $weiboRes['code'] != 10000) {
  360. return $this->response(600, $weiboRes['msg'] ?? '关注失败~');
  361. }
  362. $date = date('Y-m-d');
  363. $redisKey = $this->getTaskStateRedisKey(intval(Safe::$user['uid']));
  364. // 如果已经完成了另外两项,则标记品牌任务已经完成
  365. $finishState = $conf['state']['finish_state'];
  366. if ($conf['state']['view_state'] == 1 && $conf['state']['forward_state'] == 1) {
  367. $finishState = 1;
  368. }
  369. // 更新任务状态
  370. $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update([
  371. 'follow_state' => 1,
  372. 'finish_state' => $finishState,
  373. ]);
  374. if ($nums) {
  375. $conf['state']['follow_state'] = 1;
  376. $conf['state']['finish_state'] = $finishState;
  377. Cache::set($redisKey, json_encode($conf['state']), 86400);
  378. return $this->successResponse([
  379. 'result' => 1,
  380. 'brand' => $conf,
  381. ], '关注成功');
  382. } else {
  383. return $this->response(601, '更新任务状态失败');
  384. }
  385. }
  386. /**
  387. * 品牌任务转发博文接口
  388. * @throws FuncNotFoundException
  389. * @throws ClassNotFoundException
  390. * @throws ReflectionException
  391. * @throws DbException
  392. * @throws ModelNotFoundException
  393. * @throws DataNotFoundException
  394. * @return mixed
  395. */
  396. public function forward()
  397. {
  398. // 先获取品牌任务配置及状态
  399. $conf = $this->getBrandConfigAndState();
  400. if ($conf instanceof Json) {
  401. return $conf;
  402. }
  403. // 已经转发过了
  404. if ($conf['state']['forward_state'] == 1) {
  405. return $this->successResponse([
  406. 'result' => 2,
  407. 'brand' => $conf,
  408. ], '您已经转发过了');
  409. }
  410. // 调用微博接口转发
  411. $weiboRes = (new WeiboService(Safe::$user['uid']))->repost($conf['config']['forward']['id'], $conf['config']['forward']['content'] ?? '');
  412. if (empty($weiboRes) || $weiboRes['code'] != 10000) {
  413. return $this->response(600, $weiboRes['msg'] ?? '转发失败~');
  414. }
  415. $date = date('Y-m-d');
  416. $redisKey = $this->getTaskStateRedisKey(intval(Safe::$user['uid']));
  417. // 如果已经完成了另外两项,则标记品牌任务已经完成
  418. $finishState = $conf['state']['finish_state'];
  419. if ($conf['state']['view_state'] == 1 && $conf['state']['follow_state'] == 1) {
  420. $finishState = 1;
  421. }
  422. // 更新任务状态
  423. $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update([
  424. 'forward_state' => 1,
  425. 'finish_state' => $finishState,
  426. ]);
  427. if ($nums) {
  428. $conf['state']['forward_state'] = 1;
  429. $conf['state']['finish_state'] = $finishState;
  430. Cache::set($redisKey, json_encode($conf['state']), 86400);
  431. return $this->successResponse([
  432. 'result' => 1,
  433. 'brand' => $conf,
  434. ], '转发成功');
  435. } else {
  436. return $this->response(601, '更新任务状态失败');
  437. }
  438. }
  439. /**
  440. * 获取活动规则
  441. * @throws FuncNotFoundException
  442. * @throws ReflectionException
  443. * @throws InvalidArgumentException
  444. * @throws ClassNotFoundException
  445. * @return mixed
  446. */
  447. public function getRule()
  448. {
  449. $config = SystemService::instance()->getData('activity:rule');
  450. return $this->successResponse($config);
  451. }
  452. /**
  453. * 聚合页配置信息
  454. * @throws FuncNotFoundException
  455. * @throws ReflectionException
  456. * @throws InvalidArgumentException
  457. * @throws ClassNotFoundException
  458. * @return mixed
  459. */
  460. public function groupPageConfig()
  461. {
  462. return $this->successResponse(['config' => SystemService::instance()->getData('group:page:config')]);
  463. }
  464. public function index()
  465. {
  466. $this->redirect(sysuri('admin/login/index'));
  467. }
  468. /**
  469. * 聚合页通知获取
  470. * 查询最近20条通知
  471. * @throws DbException
  472. * @throws ModelNotFoundException
  473. * @throws DataNotFoundException
  474. * @return mixed
  475. */
  476. public function notices()
  477. {
  478. $rows = SystemNotice::limit(20)->order('id', 'desc')->select();
  479. return $this->successResponse([
  480. "lists" => $rows,
  481. ]);
  482. }
  483. /**
  484. * 首次弹窗
  485. * 发放邀请函微博
  486. * @return Json
  487. */
  488. public function sendInviteWeibo()
  489. {
  490. $config = SystemService::instance()->getData('group:page:config');
  491. $sendRes = (new WeiboService(Safe::$user['uid']))->status($config['firstDialog']['content']);
  492. if (empty($sendRes) || $sendRes['code'] != 10000) {
  493. return $this->response(403, $sendRes['msg'] ?? '发布失败');
  494. }
  495. Cache::set('u:f:' . Safe::$user['uid'], 1, 15552000);
  496. return $this->successResponse(null, '发布成功!');
  497. }
  498. /**
  499. * 首次弹窗用户未选择发送邀请函
  500. * 标记已经首次弹窗过了
  501. * @return mixed
  502. */
  503. public function setFirst()
  504. {
  505. Cache::set('u:f:' . Safe::$user['uid'], 1, 15552000);
  506. return $this->successResponse(null, '操作成功!');
  507. }
  508. /**
  509. * 标记用户已经浏览过主页了,用户点了去浏览时调用
  510. * @throws FuncNotFoundException
  511. * @throws ClassNotFoundException
  512. * @throws ReflectionException
  513. * @throws DbException
  514. * @throws ModelNotFoundException
  515. * @throws DataNotFoundException
  516. * @return mixed
  517. */
  518. public function setViewed()
  519. {
  520. // 先获取品牌任务配置及状态
  521. $conf = $this->getBrandConfigAndState();
  522. if ($conf instanceof Json) {
  523. return $conf;
  524. }
  525. if ($conf['state']['view_state'] == 1) {
  526. return $this->successResponse([
  527. 'result' => 2,
  528. 'brand' => $conf,
  529. ], '您已经浏览过了');
  530. }
  531. $date = date('Y-m-d');
  532. $redisKey = $this->getTaskStateRedisKey(intval(Safe::$user['uid']));
  533. // 如果已经完成了另外两项,则标记品牌任务已经完成
  534. $finishState = $conf['state']['finish_state'];
  535. if ($conf['state']['forward_state'] == 1 && $conf['state']['follow_state'] == 1) {
  536. $finishState = 1;
  537. }
  538. $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update([
  539. 'view_state' => 1,
  540. 'finish_state' => $finishState,
  541. ]);
  542. if ($nums) {
  543. $conf['state']['view_state'] = 1;
  544. $conf['state']['finish_state'] = $finishState;
  545. Cache::set($redisKey, json_encode($conf['state']), 86400);
  546. return $this->successResponse([
  547. 'result' => 1,
  548. 'brand' => $conf,
  549. ], '浏览成功');
  550. } else {
  551. return $this->response(601, '更新任务状态失败');
  552. }
  553. }
  554. /**
  555. * 用户完成端外分享时调用接口加票
  556. * @throws FuncNotFoundException
  557. * @throws ClassNotFoundException
  558. * @throws ReflectionException
  559. * @throws DbException
  560. * @throws ModelNotFoundException
  561. * @throws DataNotFoundException
  562. * @return mixed
  563. */
  564. public function shareFinishAddVotes()
  565. {
  566. // 先获取品牌任务配置及状态
  567. $conf = $this->getBrandConfigAndState();
  568. if ($conf instanceof Json) {
  569. return $conf;
  570. }
  571. if ($conf['state']['share_add_votes'] == 1) {
  572. return $this->successResponse([
  573. 'result' => 2,
  574. 'brand' => $conf,
  575. ], '您今日已经领取过投票机会了~');
  576. }
  577. // 调用接口给用户发券(加票)
  578. $weiboRes = (new WeiboService(Safe::$user['uid']))->setcoupons($conf['config']['share']['votes']);
  579. if (empty($weiboRes) || $weiboRes['code'] != 10000) {
  580. return $this->response(600, $weiboRes['msg'] ?? '加票失败~');
  581. }
  582. // 标记状态
  583. $date = date('Y-m-d');
  584. $redisKey = $this->getTaskStateRedisKey(intval(Safe::$user['uid']));
  585. $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update([
  586. 'share_add_votes' => 1,
  587. ]);
  588. if ($nums) {
  589. $conf['state']['share_add_votes'] = 1;
  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 taskFinishAddVotes()
  610. {
  611. // 先获取品牌任务配置及状态
  612. $conf = $this->getBrandConfigAndState();
  613. if ($conf instanceof Json) {
  614. return $conf;
  615. }
  616. if ($conf['state']['finish_add_votes'] == 1) {
  617. return $this->successResponse([
  618. 'result' => 2,
  619. 'brand' => $conf,
  620. ], '您今日已经领取过投票机会了~');
  621. }
  622. // 未完成品牌任务 领取失败
  623. if ($conf['state']['finish_state'] == 0) {
  624. return $this->response(603, '请先完成品牌任务,再来领取加票', [
  625. 'result' => 0,
  626. 'brand' => $conf,
  627. ]);
  628. }
  629. // 调用微博接口 给用户发券(加票)
  630. $weiboRes = (new WeiboService(Safe::$user['uid']))->setcoupons($conf['config']['task']['votes']);
  631. if (empty($weiboRes) || $weiboRes['code'] != 10000) {
  632. return $this->response(600, $weiboRes['msg'] ?? '加票失败~');
  633. }
  634. // 标记加票成功
  635. $date = date('Y-m-d');
  636. $redisKey = $this->getTaskStateRedisKey(intval(Safe::$user['uid']));
  637. $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update([
  638. 'finish_add_votes' => 1,
  639. ]);
  640. if ($nums) {
  641. $conf['state']['finish_add_votes'] = 1;
  642. Cache::set($redisKey, json_encode($conf['state']), 86400);
  643. return $this->successResponse([
  644. 'result' => 1,
  645. 'brand' => $conf,
  646. ], '加票成功');
  647. } else {
  648. return $this->response(601, '更新任务状态失败');
  649. }
  650. }
  651. /**
  652. * 获取品牌任务配置及状态
  653. * @throws FuncNotFoundException
  654. * @throws ReflectionException
  655. * @throws InvalidArgumentException
  656. * @throws ClassNotFoundException
  657. * @throws DbException
  658. * @throws ModelNotFoundException
  659. * @throws DataNotFoundException
  660. * @return \think\response\Json|array
  661. */
  662. protected function getBrandConfigAndState()
  663. {
  664. $config = SystemService::instance()->getData('brand:task:config');
  665. $date = date('Y-m-d');
  666. $dateForRedis = date('Ymd');
  667. if (empty(Safe::$user['uid'])) {
  668. $state = [
  669. // 品牌任务完成状态
  670. 'finish_state' => 0,
  671. // 关注子任务状态
  672. 'follow_state' => 0,
  673. // 转发子任务状态
  674. 'forward_state' => 0,
  675. // 查看浏览主页任务状态
  676. 'view_state' => 0,
  677. // 品牌任务完成后是否加票
  678. 'finish_add_votes' => 0,
  679. // 分享完成后是否加票
  680. 'share_add_votes' => 0,
  681. 'uid' => 0,
  682. 'date' => $date,
  683. ];
  684. } else {
  685. $redisKey = "t:{$dateForRedis}:" . Safe::$user['uid'];
  686. $state = Cache::get($redisKey);
  687. if (empty($state)) {
  688. // 缓存失败 查数据库
  689. $state = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->find();
  690. if (empty($state)) {
  691. // 当天第一次,初始化数据
  692. $state = [
  693. // 品牌任务完成状态
  694. 'finish_state' => 0,
  695. // 关注子任务状态
  696. 'follow_state' => 0,
  697. // 转发子任务状态
  698. 'forward_state' => 0,
  699. // 查看浏览主页任务状态
  700. 'view_state' => 0,
  701. // 品牌任务完成后是否加票
  702. 'finish_add_votes' => 0,
  703. // 分享完成后是否加票
  704. 'share_add_votes' => 0,
  705. 'uid' => Safe::$user['uid'],
  706. 'date' => $date,
  707. ];
  708. if (0 == Db::table('awards_user_task')->insert($state)) {
  709. return $this->response(5001, '系统错误,请稍后再试~');
  710. }
  711. }
  712. Cache::set($redisKey, json_encode($state), 86400);
  713. } else {
  714. $state = json_decode($state, true);
  715. }
  716. // 如果未关注状态,查询下已经实际已经关注了
  717. if ($state['follow_state'] == 0) {
  718. $weiboRes = (new WeiboService(Safe::$user['uid']))->friends($config['follow']['id']);
  719. if (isset($weiboRes['data'][$config['follow']['id']]) && $weiboRes['data'][$config['follow']['id']] == 1) {
  720. $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update(['follow_state' => 1]);
  721. if ($nums) {
  722. $state['follow_state'] = 1;
  723. Cache::set($redisKey, json_encode($state), 86400);
  724. }
  725. }
  726. }
  727. }
  728. // 校准状态,可能存在异常情况关注,转发,浏览都完成了,但是总的品牌任务状态没标记成已完成
  729. if ($state['follow_state'] == 1 && $state['forward_state'] == 1 && $state['view_state'] == 1 && $state['finish_state'] != 1) {
  730. $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update(['finish_state' => 1]);
  731. if ($nums) {
  732. $state['finish_state'] = 1;
  733. Cache::set($redisKey, json_encode($state), 86400);
  734. }
  735. }
  736. return [
  737. 'config' => $config,
  738. 'state' => $state,
  739. ];
  740. }
  741. /**
  742. * 获取品牌任务redis 缓存键
  743. * @param int $uid
  744. * @return string
  745. */
  746. protected function getTaskStateRedisKey(int $uid): string
  747. {
  748. $dateForRedis = date('Ymd');
  749. return "t:{$dateForRedis}:" . $uid;
  750. }
  751. /**
  752. * @param $code
  753. * @param $msg
  754. * @param $data
  755. * @return Json
  756. */
  757. protected function response($code, $msg, $data = null)
  758. {
  759. return json(
  760. [
  761. 'code' => $code,
  762. 'message' => $msg,
  763. 'data' => $data,
  764. 'trackID' => Log::$logID,
  765. ],
  766. 200
  767. );
  768. }
  769. /**
  770. * @param $data
  771. * @param $msg
  772. * @return mixed
  773. */
  774. protected function successResponse($data, $msg = '操作成功')
  775. {
  776. return $this->response(0, $msg, $data);
  777. }
  778. }