Index.php 28 KB

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