Index.php 30 KB

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