Index.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  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(50)
  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 getRotationAward() {
  363. $activity = Db::table('awards_activity')->where('begin_at', '<=', time())
  364. ->where('end_at', '>=', time())->find();
  365. if (empty($activity)) {
  366. return $this->response(5002, '活动末开始');
  367. }
  368. $orders = Db::table('awards_order')->alias('o')
  369. ->leftJoin('awards_user_info u', 'o.uid = u.uid')
  370. ->leftJoin('awards_gift g', 'o.gift_id = g.id')
  371. ->field(['u.nickname', 'u.portrait', 'u.uid', 'g.name as giftName'])
  372. ->order('o.id', 'desc')
  373. ->select();
  374. return $this->successResponse($orders);
  375. }
  376. /**
  377. * 排行中奖接口
  378. * @throws FuncNotFoundException
  379. * @throws ClassNotFoundException
  380. * @throws DbException
  381. * @throws ModelNotFoundException
  382. * @throws DataNotFoundException
  383. * @return mixed
  384. */
  385. public function getRankingWinAward() {
  386. $ranking = Db::table('awards_user_task_log')->alias("l")
  387. ->leftJoin('awards_user_info u', 'l.uid = u.uid')
  388. ->field(['u.nickname', 'u.portrait', 'u.uid', 'l.duration', 'l.number'])
  389. ->order('l.number', 'desc')
  390. ->order('l.duration', 'asc')
  391. ->limit(50)
  392. ->select();
  393. $isWinAward = 0;
  394. foreach ($ranking as $val) {
  395. if ($val['uid'] == Safe::$user['uid']) {
  396. $isWinAward = 1;
  397. }
  398. }
  399. return $this->successResponse([
  400. 'isWinAward' => $isWinAward,
  401. 'giftId' => 0
  402. ]);
  403. }
  404. /**
  405. * 抽奖接口
  406. * @throws FuncNotFoundException
  407. * @throws ClassNotFoundException
  408. * @throws DbException
  409. * @throws ModelNotFoundException
  410. * @throws DataNotFoundException
  411. * @return mixed
  412. */
  413. public function getLuckDraw()
  414. {
  415. $ranking = Db::table('awards_user_task_log')->alias("l")
  416. ->leftJoin('awards_user_info u', 'l.uid = u.uid')
  417. ->field(['u.nickname', 'u.portrait', 'u.uid', 'l.duration', 'l.number'])
  418. ->order('l.number', 'desc')
  419. ->order('l.duration', 'asc')
  420. ->limit(50)
  421. ->select();
  422. $isWinAward = 0;
  423. foreach ($ranking as $val) {
  424. if ($val['uid'] == Safe::$user['uid']) {
  425. $isWinAward = 1;
  426. }
  427. }
  428. return $this->successResponse([
  429. 'isWinAward' => $isWinAward,
  430. 'giftId' => 0
  431. ]);
  432. }
  433. /**
  434. * 品牌任务关注接口
  435. * @throws FuncNotFoundException
  436. * @throws ClassNotFoundException
  437. * @throws ReflectionException
  438. * @throws DbException
  439. * @throws ModelNotFoundException
  440. * @throws DataNotFoundException
  441. * @return mixed
  442. */
  443. public function follow()
  444. {
  445. // 先获取品牌任务配置及状态
  446. $conf = $this->getBrandConfigAndState();
  447. if ($conf instanceof Json) {
  448. return $conf;
  449. }
  450. // 已经关注过了
  451. if ($conf['state']['follow_state'] == 1) {
  452. return $this->successResponse([
  453. 'result' => 2,
  454. 'brand' => $conf,
  455. ], '您已经关注过了');
  456. }
  457. // 调用接口关注
  458. $weiboRes = (new WeiboService(Safe::$user['uid']))->add($conf['config']['follow']['id']);
  459. if (empty($weiboRes) || $weiboRes['code'] != 10000) {
  460. return $this->response(600, $weiboRes['msg'] ?? '关注失败~');
  461. }
  462. $date = date('Y-m-d');
  463. $redisKey = $this->getTaskStateRedisKey(intval(Safe::$user['uid']));
  464. // 如果已经完成了另外两项,则标记品牌任务已经完成
  465. $finishState = $conf['state']['finish_state'];
  466. if ($conf['state']['view_state'] == 1 && $conf['state']['forward_state'] == 1) {
  467. $finishState = 1;
  468. }
  469. // 更新任务状态
  470. $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update([
  471. 'follow_state' => 1,
  472. 'finish_state' => $finishState,
  473. ]);
  474. if ($nums) {
  475. $conf['state']['follow_state'] = 1;
  476. $conf['state']['finish_state'] = $finishState;
  477. Cache::set($redisKey, json_encode($conf['state']), 86400);
  478. return $this->successResponse([
  479. 'result' => 1,
  480. 'brand' => $conf,
  481. ], '关注成功');
  482. } else {
  483. return $this->response(601, '更新任务状态失败');
  484. }
  485. }
  486. /**
  487. * 品牌任务转发博文接口
  488. * @throws FuncNotFoundException
  489. * @throws ClassNotFoundException
  490. * @throws ReflectionException
  491. * @throws DbException
  492. * @throws ModelNotFoundException
  493. * @throws DataNotFoundException
  494. * @return mixed
  495. */
  496. public function forward()
  497. {
  498. // 先获取品牌任务配置及状态
  499. $conf = $this->getBrandConfigAndState();
  500. if ($conf instanceof Json) {
  501. return $conf;
  502. }
  503. // 已经转发过了
  504. if ($conf['state']['forward_state'] == 1) {
  505. return $this->successResponse([
  506. 'result' => 2,
  507. 'brand' => $conf,
  508. ], '您已经转发过了');
  509. }
  510. // 调用微博接口转发
  511. $weiboRes = (new WeiboService(Safe::$user['uid']))->repost($conf['config']['forward']['id'], $conf['config']['forward']['content'] ?? '');
  512. if (empty($weiboRes) || $weiboRes['code'] != 10000) {
  513. return $this->response(600, $weiboRes['msg'] ?? '转发失败~');
  514. }
  515. $date = date('Y-m-d');
  516. $redisKey = $this->getTaskStateRedisKey(intval(Safe::$user['uid']));
  517. // 如果已经完成了另外两项,则标记品牌任务已经完成
  518. $finishState = $conf['state']['finish_state'];
  519. if ($conf['state']['view_state'] == 1 && $conf['state']['follow_state'] == 1) {
  520. $finishState = 1;
  521. }
  522. // 更新任务状态
  523. $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update([
  524. 'forward_state' => 1,
  525. 'finish_state' => $finishState,
  526. ]);
  527. if ($nums) {
  528. $conf['state']['forward_state'] = 1;
  529. $conf['state']['finish_state'] = $finishState;
  530. Cache::set($redisKey, json_encode($conf['state']), 86400);
  531. return $this->successResponse([
  532. 'result' => 1,
  533. 'brand' => $conf,
  534. ], '转发成功');
  535. } else {
  536. return $this->response(601, '更新任务状态失败');
  537. }
  538. }
  539. /**
  540. * 获取活动规则
  541. * @throws FuncNotFoundException
  542. * @throws ReflectionException
  543. * @throws InvalidArgumentException
  544. * @throws ClassNotFoundException
  545. * @return mixed
  546. */
  547. public function getRule()
  548. {
  549. $config = SystemService::instance()->getData('activity:rule');
  550. return $this->successResponse($config);
  551. }
  552. /**
  553. * 聚合页配置信息
  554. * @throws FuncNotFoundException
  555. * @throws ReflectionException
  556. * @throws InvalidArgumentException
  557. * @throws ClassNotFoundException
  558. * @return mixed
  559. */
  560. public function groupPageConfig()
  561. {
  562. return $this->successResponse(['config' => SystemService::instance()->getData('group:page:config')]);
  563. }
  564. public function index()
  565. {
  566. $this->redirect(sysuri('admin/login/index'));
  567. }
  568. /**
  569. * 聚合页通知获取
  570. * 查询最近20条通知
  571. * @throws DbException
  572. * @throws ModelNotFoundException
  573. * @throws DataNotFoundException
  574. * @return mixed
  575. */
  576. public function notices()
  577. {
  578. $rows = SystemNotice::limit(20)->order('id', 'desc')->select();
  579. return $this->successResponse([
  580. "lists" => $rows,
  581. ]);
  582. }
  583. /**
  584. * 首次弹窗
  585. * 发放邀请函微博
  586. * @return Json
  587. */
  588. public function sendInviteWeibo()
  589. {
  590. $config = SystemService::instance()->getData('group:page:config');
  591. $sendRes = (new WeiboService(Safe::$user['uid']))->status($config['firstDialog']['content']);
  592. if (empty($sendRes) || $sendRes['code'] != 10000) {
  593. return $this->response(403, $sendRes['msg'] ?? '发布失败');
  594. }
  595. Cache::set('u:f:' . Safe::$user['uid'], 1, 15552000);
  596. return $this->successResponse(null, '发布成功!');
  597. }
  598. /**
  599. * 首次弹窗用户未选择发送邀请函
  600. * 标记已经首次弹窗过了
  601. * @return mixed
  602. */
  603. public function setFirst()
  604. {
  605. Cache::set('u:f:' . Safe::$user['uid'], 1, 15552000);
  606. return $this->successResponse(null, '操作成功!');
  607. }
  608. /**
  609. * 标记用户已经浏览过主页了,用户点了去浏览时调用
  610. * @throws FuncNotFoundException
  611. * @throws ClassNotFoundException
  612. * @throws ReflectionException
  613. * @throws DbException
  614. * @throws ModelNotFoundException
  615. * @throws DataNotFoundException
  616. * @return mixed
  617. */
  618. public function setViewed()
  619. {
  620. // 先获取品牌任务配置及状态
  621. $conf = $this->getBrandConfigAndState();
  622. if ($conf instanceof Json) {
  623. return $conf;
  624. }
  625. if ($conf['state']['view_state'] == 1) {
  626. return $this->successResponse([
  627. 'result' => 2,
  628. 'brand' => $conf,
  629. ], '您已经浏览过了');
  630. }
  631. $date = date('Y-m-d');
  632. $redisKey = $this->getTaskStateRedisKey(intval(Safe::$user['uid']));
  633. // 如果已经完成了另外两项,则标记品牌任务已经完成
  634. $finishState = $conf['state']['finish_state'];
  635. if ($conf['state']['forward_state'] == 1 && $conf['state']['follow_state'] == 1) {
  636. $finishState = 1;
  637. }
  638. $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update([
  639. 'view_state' => 1,
  640. 'finish_state' => $finishState,
  641. ]);
  642. if ($nums) {
  643. $conf['state']['view_state'] = 1;
  644. $conf['state']['finish_state'] = $finishState;
  645. Cache::set($redisKey, json_encode($conf['state']), 86400);
  646. return $this->successResponse([
  647. 'result' => 1,
  648. 'brand' => $conf,
  649. ], '浏览成功');
  650. } else {
  651. return $this->response(601, '更新任务状态失败');
  652. }
  653. }
  654. /**
  655. * 用户完成端外分享时调用接口加票
  656. * @throws FuncNotFoundException
  657. * @throws ClassNotFoundException
  658. * @throws ReflectionException
  659. * @throws DbException
  660. * @throws ModelNotFoundException
  661. * @throws DataNotFoundException
  662. * @return mixed
  663. */
  664. public function shareFinishAddVotes()
  665. {
  666. // 先获取品牌任务配置及状态
  667. $conf = $this->getBrandConfigAndState();
  668. if ($conf instanceof Json) {
  669. return $conf;
  670. }
  671. if ($conf['state']['share_add_votes'] == 1) {
  672. return $this->successResponse([
  673. 'result' => 2,
  674. 'brand' => $conf,
  675. ], '您今日已经领取过投票机会了~');
  676. }
  677. // 调用接口给用户发券(加票)
  678. $weiboRes = (new WeiboService(Safe::$user['uid']))->setcoupons($conf['config']['share']['votes']);
  679. if (empty($weiboRes) || $weiboRes['code'] != 10000) {
  680. return $this->response(600, $weiboRes['msg'] ?? '加票失败~');
  681. }
  682. // 标记状态
  683. $date = date('Y-m-d');
  684. $redisKey = $this->getTaskStateRedisKey(intval(Safe::$user['uid']));
  685. $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update([
  686. 'share_add_votes' => 1,
  687. ]);
  688. if ($nums) {
  689. $conf['state']['share_add_votes'] = 1;
  690. Cache::set($redisKey, json_encode($conf['state']), 86400);
  691. return $this->successResponse([
  692. 'result' => 1,
  693. 'brand' => $conf,
  694. ], '加票成功');
  695. } else {
  696. return $this->response(601, '更新任务状态失败');
  697. }
  698. }
  699. /**
  700. * 完成品牌任务后领取加票机会接口
  701. * @throws FuncNotFoundException
  702. * @throws ClassNotFoundException
  703. * @throws ReflectionException
  704. * @throws DbException
  705. * @throws ModelNotFoundException
  706. * @throws DataNotFoundException
  707. * @return mixed
  708. */
  709. public function taskFinishAddVotes()
  710. {
  711. // 先获取品牌任务配置及状态
  712. $conf = $this->getBrandConfigAndState();
  713. if ($conf instanceof Json) {
  714. return $conf;
  715. }
  716. if ($conf['state']['finish_add_votes'] == 1) {
  717. return $this->successResponse([
  718. 'result' => 2,
  719. 'brand' => $conf,
  720. ], '您今日已经领取过投票机会了~');
  721. }
  722. // 未完成品牌任务 领取失败
  723. if ($conf['state']['finish_state'] == 0) {
  724. return $this->response(603, '请先完成品牌任务,再来领取加票', [
  725. 'result' => 0,
  726. 'brand' => $conf,
  727. ]);
  728. }
  729. // 调用微博接口 给用户发券(加票)
  730. $weiboRes = (new WeiboService(Safe::$user['uid']))->setcoupons($conf['config']['task']['votes']);
  731. if (empty($weiboRes) || $weiboRes['code'] != 10000) {
  732. return $this->response(600, $weiboRes['msg'] ?? '加票失败~');
  733. }
  734. // 标记加票成功
  735. $date = date('Y-m-d');
  736. $redisKey = $this->getTaskStateRedisKey(intval(Safe::$user['uid']));
  737. $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update([
  738. 'finish_add_votes' => 1,
  739. ]);
  740. if ($nums) {
  741. $conf['state']['finish_add_votes'] = 1;
  742. Cache::set($redisKey, json_encode($conf['state']), 86400);
  743. return $this->successResponse([
  744. 'result' => 1,
  745. 'brand' => $conf,
  746. ], '加票成功');
  747. } else {
  748. return $this->response(601, '更新任务状态失败');
  749. }
  750. }
  751. /**
  752. * 获取品牌任务配置及状态
  753. * @throws FuncNotFoundException
  754. * @throws ReflectionException
  755. * @throws InvalidArgumentException
  756. * @throws ClassNotFoundException
  757. * @throws DbException
  758. * @throws ModelNotFoundException
  759. * @throws DataNotFoundException
  760. * @return \think\response\Json|array
  761. */
  762. protected function getBrandConfigAndState()
  763. {
  764. $config = SystemService::instance()->getData('brand:task:config');
  765. $date = date('Y-m-d');
  766. $dateForRedis = date('Ymd');
  767. if (empty(Safe::$user['uid'])) {
  768. $state = [
  769. // 品牌任务完成状态
  770. 'finish_state' => 0,
  771. // 关注子任务状态
  772. 'follow_state' => 0,
  773. // 转发子任务状态
  774. 'forward_state' => 0,
  775. // 查看浏览主页任务状态
  776. 'view_state' => 0,
  777. // 品牌任务完成后是否加票
  778. 'finish_add_votes' => 0,
  779. // 分享完成后是否加票
  780. 'share_add_votes' => 0,
  781. 'uid' => 0,
  782. 'date' => $date,
  783. ];
  784. } else {
  785. $redisKey = "t:{$dateForRedis}:" . Safe::$user['uid'];
  786. $state = Cache::get($redisKey);
  787. if (empty($state)) {
  788. // 缓存失败 查数据库
  789. $state = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->find();
  790. if (empty($state)) {
  791. // 当天第一次,初始化数据
  792. $state = [
  793. // 品牌任务完成状态
  794. 'finish_state' => 0,
  795. // 关注子任务状态
  796. 'follow_state' => 0,
  797. // 转发子任务状态
  798. 'forward_state' => 0,
  799. // 查看浏览主页任务状态
  800. 'view_state' => 0,
  801. // 品牌任务完成后是否加票
  802. 'finish_add_votes' => 0,
  803. // 分享完成后是否加票
  804. 'share_add_votes' => 0,
  805. 'uid' => Safe::$user['uid'],
  806. 'date' => $date,
  807. ];
  808. if (0 == Db::table('awards_user_task')->insert($state)) {
  809. return $this->response(5001, '系统错误,请稍后再试~');
  810. }
  811. }
  812. Cache::set($redisKey, json_encode($state), 86400);
  813. } else {
  814. $state = json_decode($state, true);
  815. }
  816. // 如果未关注状态,查询下已经实际已经关注了
  817. if ($state['follow_state'] == 0) {
  818. $weiboRes = (new WeiboService(Safe::$user['uid']))->friends($config['follow']['id']);
  819. if (isset($weiboRes['data'][$config['follow']['id']]) && $weiboRes['data'][$config['follow']['id']] == 1) {
  820. $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update(['follow_state' => 1]);
  821. if ($nums) {
  822. $state['follow_state'] = 1;
  823. Cache::set($redisKey, json_encode($state), 86400);
  824. }
  825. }
  826. }
  827. }
  828. // 校准状态,可能存在异常情况关注,转发,浏览都完成了,但是总的品牌任务状态没标记成已完成
  829. if ($state['follow_state'] == 1 && $state['forward_state'] == 1 && $state['view_state'] == 1 && $state['finish_state'] != 1) {
  830. $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update(['finish_state' => 1]);
  831. if ($nums) {
  832. $state['finish_state'] = 1;
  833. Cache::set($redisKey, json_encode($state), 86400);
  834. }
  835. }
  836. return [
  837. 'config' => $config,
  838. 'state' => $state,
  839. ];
  840. }
  841. /**
  842. * 获取品牌任务redis 缓存键
  843. * @param int $uid
  844. * @return string
  845. */
  846. protected function getTaskStateRedisKey(int $uid): string
  847. {
  848. $dateForRedis = date('Ymd');
  849. return "t:{$dateForRedis}:" . $uid;
  850. }
  851. /**
  852. * @param $code
  853. * @param $msg
  854. * @param $data
  855. * @return Json
  856. */
  857. protected function response($code, $msg, $data = null)
  858. {
  859. return json(
  860. [
  861. 'code' => $code,
  862. 'message' => $msg,
  863. 'data' => $data,
  864. 'trackID' => Log::$logID,
  865. ],
  866. 200
  867. );
  868. }
  869. /**
  870. * @param $data
  871. * @param $msg
  872. * @return mixed
  873. */
  874. protected function successResponse($data, $msg = '操作成功')
  875. {
  876. return $this->response(0, $msg, $data);
  877. }
  878. }