Index.php 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183
  1. <?php
  2. namespace app\index\controller;
  3. use think\facade\Db;
  4. use app\middleware\Log;
  5. use think\facade\Cache;
  6. use ReflectionException;
  7. use app\middleware\Safe;
  8. use think\file\UploadedFile;
  9. use \think\response\Json;
  10. use think\facade\Request;
  11. use think\admin\Controller;
  12. use InvalidArgumentException;
  13. use app\service\WeiboService;
  14. use app\admin\model\SystemNotice;
  15. use think\facade\Log as FacadeLog;
  16. use think\db\exception\DbException;
  17. use think\admin\service\SystemService;
  18. use think\exception\FuncNotFoundException;
  19. use think\exception\ClassNotFoundException;
  20. use think\db\exception\DataNotFoundException;
  21. use think\db\exception\ModelNotFoundException;
  22. /**
  23. * Class Index
  24. * @package app\index\controller
  25. */
  26. class Index extends Controller
  27. {
  28. public function index()
  29. {
  30. FacadeLog::info("cookies:" . json_encode(Request::post()));
  31. $sub = "";
  32. $uid = 0;
  33. FacadeLog::info($_COOKIE);
  34. if (!empty($_COOKIE['SUB'])) {
  35. $sub = $_COOKIE['SUB'];
  36. } else {
  37. // 只在调试模式下开启从POST参数中获取UID,方便测试联调
  38. if (env('app_debug')) {
  39. $sub = Request::post('cookie');
  40. $uid = $sub;
  41. } else {
  42. return $this->response(403, 'not login.');
  43. }
  44. }
  45. FacadeLog::info($sub);
  46. $userInfoRes = (new WeiboService($uid))->userinfo($sub);
  47. if (empty($userInfoRes) || $userInfoRes['ok'] != 1) {
  48. return $this->response(403, $userInfoRes['msg'] ?? '没有登录');
  49. }
  50. // 使用客户端信息生成token
  51. $token = md5($_SERVER['HTTP_USER_AGENT'] . $_SERVER['HTTP_ACCEPT_ENCODING'] . $_SERVER['HTTP_ACCEPT_LANGUAGE'] . $_SERVER['HTTP_REFERER'] . get_client_ip(0) . $userInfoRes['data']['uid']);
  52. $user = $userInfoRes['data'];
  53. $userInfo = Db::table('awards_user_info')->where('uid', $user['uid'])->find();
  54. $count = 0;
  55. $isShare = 0;
  56. if (empty($userInfo)) {
  57. $userAttr = [
  58. 'uid' => $user['uid'],
  59. 'portrait' => $user['profile_image_url'],
  60. 'nickname' => $user['name'],
  61. 'is_share' => 0,
  62. 'count' => 1,
  63. 'create_at' => time()
  64. ];
  65. if (0 == Db::table('awards_user_info')->insert($userAttr)) {
  66. return $this->response(5001, '系统错误,请稍后再试~');
  67. }
  68. } else {
  69. $count = Db::table('awards_user_task_log')->where('uid', $user['uid'])->count('id');
  70. $isShare = $userInfo['is_share'];
  71. }
  72. // 生成加密用的密钥和向量
  73. $cipher = "aes-256-gcm";
  74. $ivlen = openssl_cipher_iv_length($cipher);
  75. $iv = bin2hex(openssl_random_pseudo_bytes($ivlen));
  76. $aesKey = bin2hex(openssl_random_pseudo_bytes(32));
  77. $user = array_merge($user, [
  78. 'aes_key' => $aesKey,
  79. 'ase_iv' => $iv,
  80. ]);
  81. $cacheUser = [
  82. 'aes_key' => $aesKey,
  83. 'ase_iv' => $iv,
  84. 'uid' => $user['uid'],
  85. ];
  86. $activity = Db::table('awards_activity')->where('begin_at', '<=', time())
  87. ->where('end_at', '>=', time())->find();
  88. $isBeginActivity = 1;
  89. if (empty($activity)) {
  90. $isBeginActivity = 0;
  91. }
  92. $userInfo = [];
  93. if ($count > 0) {
  94. $userInfo = Db::table('awards_user_task_log')->alias('l')
  95. ->leftJoin('awards_user_info u', 'l.uid = u.uid')
  96. ->field(['u.nickname', 'u.portrait', 'u.uid', 'l.duration', 'l.number'])
  97. ->order('l.number', 'desc')
  98. ->order('l.duration', 'asc')
  99. ->find();
  100. }
  101. // 缓存用户信息1天
  102. Cache::set('u:' . $token, json_encode($cacheUser), 86400);
  103. $this->assign('user', $user);
  104. $this->assign('token', $token);
  105. $this->assign('isShare', $isShare);
  106. $this->assign('count', $count);
  107. $this->assign('isBeginActivity', $isBeginActivity);
  108. $this->assign('task', $userInfo);
  109. $this->fetch();
  110. }
  111. /**
  112. * 获取品牌任务相关配置
  113. *
  114. * @throws FuncNotFoundException
  115. * @throws ClassNotFoundException
  116. * @throws ReflectionException
  117. * @throws DbException
  118. * @throws ModelNotFoundException
  119. * @throws DataNotFoundException
  120. * @return mixed
  121. */
  122. public function brand()
  123. {
  124. $conf = $this->getBrandConfigAndState();
  125. if ($conf instanceof Json) {
  126. return $conf;
  127. }
  128. return $this->successResponse($conf);
  129. }
  130. /**
  131. * 检测登录 通过cookie中的SUB字段的内容,调用用户接口检测登录状态
  132. * @throws ClassNotFoundException
  133. * @throws ReflectionException
  134. * @return mixed
  135. */
  136. public function checkLogin()
  137. {
  138. FacadeLog::info("cookies:" . json_encode(Request::post()));
  139. $sub = "";
  140. $uid = 0;
  141. FacadeLog::info($_COOKIE);
  142. if (!empty($_COOKIE['SUB'])) {
  143. $sub = $_COOKIE['SUB'];
  144. } else {
  145. // 只在调试模式下开启从POST参数中获取UID,方便测试联调
  146. if (env('app_debug')) {
  147. $sub = Request::post('cookie');
  148. $uid = $sub;
  149. } else {
  150. return $this->response(403, 'not login.');
  151. }
  152. }
  153. FacadeLog::info($sub);
  154. $userInfoRes = (new WeiboService($uid))->userinfo($sub);
  155. if (empty($userInfoRes) || $userInfoRes['ok'] != 1) {
  156. return $this->response(403, $userInfoRes['msg'] ?? '没有登录');
  157. }
  158. // 使用客户端信息生成token
  159. $token = md5($_SERVER['HTTP_USER_AGENT'] . $_SERVER['HTTP_ACCEPT_ENCODING'] . $_SERVER['HTTP_ACCEPT_LANGUAGE'] . $_SERVER['HTTP_REFERER'] . get_client_ip(0) . $userInfoRes['data']['uid']);
  160. $user = $userInfoRes['data'];
  161. $userInfo = Db::table('awards_user_info')->where('uid', $user['uid'])->find();
  162. $count = 0;
  163. $isShare = 0;
  164. if (empty($userInfo)) {
  165. $userAttr = [
  166. 'uid' => $user['uid'],
  167. 'portrait' => $user['profile_image_url'],
  168. 'nickname' => $user['name'],
  169. 'is_share' => 0,
  170. 'count' => 1,
  171. 'create_at' => time()
  172. ];
  173. if (0 == Db::table('awards_user_info')->insert($userAttr)) {
  174. return $this->response(5001, '系统错误,请稍后再试~');
  175. }
  176. } else {
  177. $count = Db::table('awards_user_task_log')->where('uid', $user['uid'])->count('id');
  178. $isShare = $userInfo['is_share'];
  179. }
  180. // 生成加密用的密钥和向量
  181. $cipher = "aes-256-gcm";
  182. $ivlen = openssl_cipher_iv_length($cipher);
  183. $iv = bin2hex(openssl_random_pseudo_bytes($ivlen));
  184. $aesKey = bin2hex(openssl_random_pseudo_bytes(32));
  185. $user = array_merge($user, [
  186. 'aes_key' => $aesKey,
  187. 'ase_iv' => $iv,
  188. ]);
  189. $cacheUser = [
  190. 'aes_key' => $aesKey,
  191. 'ase_iv' => $iv,
  192. 'uid' => $user['uid'],
  193. ];
  194. $activity = Db::table('awards_activity')->where('begin_at', '<=', time())
  195. ->where('end_at', '>=', time())->find();
  196. $isBeginActivity = 1;
  197. if (empty($activity)) {
  198. $isBeginActivity = 0;
  199. }
  200. $userInfo = [];
  201. if ($count > 0) {
  202. $userInfo = Db::table('awards_user_task_log')->alias('l')
  203. ->leftJoin('awards_user_info u', 'l.uid = u.uid')
  204. ->field(['u.nickname', 'u.portrait', 'u.uid', 'l.duration', 'l.number'])
  205. ->order('l.number', 'desc')
  206. ->order('l.duration', 'asc')
  207. ->find();
  208. }
  209. // 缓存用户信息1天
  210. Cache::set('u:' . $token, json_encode($cacheUser), 86400);
  211. return $this->successResponse([
  212. 'user' => $user,
  213. 'token' => $token,
  214. 'isShare' => $isShare,
  215. 'count' => $count,
  216. 'isBeginActivity' => $isBeginActivity,
  217. 'task' => $userInfo
  218. ]);
  219. }
  220. /**
  221. * 是否可以参与游戏
  222. * @throws ClassNotFoundException
  223. * @throws ReflectionException
  224. * @return mixed
  225. */
  226. public function checkRole()
  227. {
  228. $activity = Db::table('awards_activity')->where('begin_at', '<=', time())
  229. ->where('end_at', '>=', time())->find();
  230. if (empty($activity)) {
  231. return $this->response(5002, '活动末开始');
  232. }
  233. $userInfo = Db::table('awards_user_info')->where('uid', Safe::$user['uid'])->find();
  234. $count = Db::table('awards_user_task_log')->where('uid', Safe::$user['uid'])->count('id');
  235. return $this->successResponse([
  236. 'isShare' => $userInfo['is_share'],
  237. 'count' => $count
  238. ]);
  239. }
  240. /**
  241. * 提交任务接口
  242. * @throws FuncNotFoundException
  243. * @throws ClassNotFoundException
  244. * @throws DbException
  245. * @throws ModelNotFoundException
  246. * @throws DataNotFoundException
  247. * @return mixed
  248. */
  249. public function submitTask()
  250. {
  251. $activity = Db::table('awards_activity')->where('begin_at', '<=', time())
  252. ->where('end_at', '>=', time())->find();
  253. if (empty($activity)) {
  254. return $this->response(5002, '活动末开始');
  255. }
  256. // if (empty(Safe::$body)) {
  257. // return $this->response(5003, '参数有误');
  258. // }
  259. //
  260. // if (!isset(Safe::$body['duration']) || Safe::$body['duration'] < 1 || Safe::$body['duration'] > 1000) {
  261. // return $this->response(5003, '时长参数有误');
  262. // }
  263. //
  264. // if (!isset(Safe::$body['number']) || Safe::$body['number'] < 0 || Safe::$body['number'] > 1000) {
  265. // return $this->response(5003, '时长参数有误');
  266. // }
  267. // $duration = Safe::$body['duration'];
  268. // $number = Safe::$body['number'];
  269. $duration = Request::post("duration");
  270. $number = Request::post("number");
  271. $date = date('Y-m-d');
  272. $userInfo = Db::table('awards_user_info')->where('uid', Safe::$user['uid'])->find();
  273. if (empty($userInfo)) {
  274. return $this->response(403, '没有登录');
  275. }
  276. $count = Db::table('awards_user_task_log')->where('uid', Safe::$user['uid'])->count('id');
  277. if ($count >= $userInfo['count']) {
  278. return $this->response(5002, '没有参与次数');
  279. }
  280. $log = [
  281. 'uid' => Safe::$user['uid'],
  282. 'date' => $date,
  283. 'duration' => $duration,
  284. 'number' => $number,
  285. 'create_at' => time()
  286. ];
  287. if (0 == Db::table('awards_user_task_log')->insert($log)) {
  288. return $this->response(5001, '系统错误,请稍后再试~');
  289. }
  290. return $this->successResponse(null, '提交成功');
  291. }
  292. /**
  293. * 提交发布微博接口
  294. * @throws FuncNotFoundException
  295. * @throws ClassNotFoundException
  296. * @throws DbException
  297. * @throws ModelNotFoundException
  298. * @throws DataNotFoundException
  299. * @return mixed
  300. */
  301. public function submitShare()
  302. {
  303. $activity = Db::table('awards_activity')->where('begin_at', '<=', time())
  304. ->where('end_at', '>=', time())->find();
  305. if (empty($activity)) {
  306. return $this->response(5002, '活动末开始');
  307. }
  308. $userInfo = Db::table('awards_user_info')->where('uid', Safe::$user['uid'])->find();
  309. if (empty($userInfo)) {
  310. return $this->response(403, '没有登录');
  311. }
  312. if ($userInfo['is_share'] > 0) {
  313. return $this->response(601, '已发布');
  314. }
  315. // 更新任务状态
  316. $nums = Db::table('awards_user_info')->where('uid', Safe::$user['uid'])->update([
  317. 'is_share' => 1,
  318. 'count' => 2,
  319. 'share_at' => time()
  320. ]);
  321. if ($nums) {
  322. return $this->successResponse(null, '提交成功');
  323. } else {
  324. return $this->response(601, '发布失败');
  325. }
  326. }
  327. /**
  328. * 头号排行接口
  329. * @throws FuncNotFoundException
  330. * @throws ClassNotFoundException
  331. * @throws DbException
  332. * @throws ModelNotFoundException
  333. * @throws DataNotFoundException
  334. * @return mixed
  335. */
  336. public function topRanking()
  337. {
  338. $activity = Db::table('awards_activity')->where('begin_at', '<=', time())
  339. ->where('end_at', '>=', time())->find();
  340. if (empty($activity)) {
  341. return $this->response(5002, '活动末开始');
  342. }
  343. $ranking = Db::table('awards_user_task_log')->alias("l")
  344. ->leftJoin('awards_user_info u', 'l.uid = u.uid')
  345. ->field(['u.nickname', 'u.portrait', 'u.uid', 'l.duration', 'l.number'])
  346. ->distinct('uid')
  347. ->order('l.number', 'desc')
  348. ->order('l.duration', 'asc')
  349. ->limit(50)
  350. ->select();
  351. $userInfo = Db::table('awards_user_task_log')->alias('l')
  352. ->leftJoin('awards_user_info u', 'l.uid = u.uid')
  353. ->field(['u.nickname', 'u.portrait', 'u.uid', 'l.duration', 'l.number'])
  354. ->order('l.number', 'desc')
  355. ->order('l.duration', 'asc')
  356. ->find();
  357. $res = [
  358. 'self' => $userInfo,
  359. 'ranking' => $ranking
  360. ];
  361. return $this->successResponse($res);
  362. }
  363. /**
  364. * 最新排行接口
  365. * @throws FuncNotFoundException
  366. * @throws ClassNotFoundException
  367. * @throws DbException
  368. * @throws ModelNotFoundException
  369. * @throws DataNotFoundException
  370. * @return mixed
  371. */
  372. public function newRanking()
  373. {
  374. $activity = Db::table('awards_activity')->where('begin_at', '<=', time())
  375. ->where('end_at', '>=', time())->find();
  376. if (empty($activity)) {
  377. return $this->response(5002, '活动末开始');
  378. }
  379. $ranking = Db::table('awards_user_task_log')->alias("l")
  380. ->leftJoin('awards_user_info u', 'l.uid = u.uid')
  381. ->field(['u.nickname', 'u.portrait', 'u.uid', 'l.duration', 'l.number'])
  382. ->distinct('uid')
  383. ->order('l.create_at', 'desc')
  384. ->order('l.number', 'desc')
  385. ->order('l.duration', 'asc')
  386. ->limit(50)
  387. ->select();
  388. $userInfo = Db::table('awards_user_task_log')->alias('l')
  389. ->leftJoin('awards_user_info u', 'l.uid = u.uid')
  390. ->field(['u.nickname', 'u.portrait', 'u.uid', 'l.duration', 'l.number'])
  391. ->order('l.number', 'desc')
  392. ->order('l.duration', 'asc')
  393. ->find();
  394. $res = [
  395. 'self' => $userInfo,
  396. 'ranking' => $ranking
  397. ];
  398. return $this->successResponse($res);
  399. }
  400. /**
  401. * 提交领取信息接口
  402. * @throws FuncNotFoundException
  403. * @throws ClassNotFoundException
  404. * @throws DbException
  405. * @throws ModelNotFoundException
  406. * @throws DataNotFoundException
  407. * @return mixed
  408. */
  409. public function submitReceive()
  410. {
  411. $activity = Db::table('awards_activity')->where('begin_at', '<=', time())
  412. ->where('end_at', '>=', time())->find();
  413. if (empty($activity)) {
  414. return $this->response(5002, '活动末开始');
  415. }
  416. $name = trim(Request::post("name"));
  417. $mobile = trim(Request::post("mobile"));
  418. $address = trim(Request::post("address"));
  419. $giftId = trim(Request::post("giftId"));
  420. $type = trim(Request::post("type"));
  421. $orders = Db::table('awards_order')->where('uid', Safe::$user['uid'])->where('type', $type)->find();
  422. if (empty($orders)) {
  423. return $this->response(5001, '参数有误');
  424. }
  425. $userInfo = Db::table('awards_user_info')->where('uid', Safe::$user['uid'])->find();
  426. if (empty($userInfo)) {
  427. return $this->response(403, '没有登录');
  428. }
  429. if (strlen($name) < 1 || strlen($name) > 50) {
  430. return $this->response(5001, '姓名有误');
  431. }
  432. if (strlen($mobile) != 11) {
  433. return $this->response(5001, '手机号有误');
  434. }
  435. if (strlen($address) < 1 || strlen($address) > 1000) {
  436. return $this->response(5001, '地址有误');
  437. }
  438. // 更新任务状态
  439. $nums = Db::table('awards_order')->where('uid', Safe::$user['uid'])->where('type', $type)->update([
  440. 'name' => $name,
  441. 'mobile' => $mobile,
  442. 'address' => $address,
  443. ]);
  444. if ($nums < 1) {
  445. return $this->response(5001, '系统错误,请稍后再试~');
  446. }
  447. return $this->successResponse(null, '提交成功');
  448. }
  449. /**
  450. * 获取轮播中奖接口
  451. * @throws FuncNotFoundException
  452. * @throws ClassNotFoundException
  453. * @throws DbException
  454. * @throws ModelNotFoundException
  455. * @throws DataNotFoundException
  456. * @return mixed
  457. */
  458. public function getRotationAward() {
  459. $activity = Db::table('awards_activity')->where('begin_at', '<=', time())
  460. ->where('end_at', '>=', time())->find();
  461. if (empty($activity)) {
  462. return $this->response(5002, '活动末开始');
  463. }
  464. $orders = Db::table('awards_order')->alias('o')
  465. ->leftJoin('awards_user_info u', 'o.uid = u.uid')
  466. ->leftJoin('awards_gift g', 'o.gift_id = g.id')
  467. ->field(['u.nickname', 'u.portrait', 'u.uid', 'g.name as giftName'])
  468. ->order('o.id', 'desc')
  469. ->select();
  470. return $this->successResponse($orders);
  471. }
  472. /**
  473. * 排行中奖接口
  474. * @throws FuncNotFoundException
  475. * @throws ClassNotFoundException
  476. * @throws DbException
  477. * @throws ModelNotFoundException
  478. * @throws DataNotFoundException
  479. * @return mixed
  480. */
  481. public function getRankingWinAward() {
  482. $activity = Db::table('awards_activity')->where('begin_at', '<=', time())
  483. ->where('end_at', '>=', time())->find();
  484. if (!empty($activity)) {
  485. return $this->response(5002, '活动末结束');
  486. }
  487. $ranking = Db::table('awards_user_task_log')->alias("l")
  488. ->leftJoin('awards_user_info u', 'l.uid = u.uid')
  489. ->field(['u.nickname', 'u.portrait', 'u.uid', 'l.duration', 'l.number'])
  490. ->distinct('l.uid')
  491. ->order('l.number', 'desc')
  492. ->order('l.duration', 'asc')
  493. ->limit(50)
  494. ->select();
  495. $isWinAward = 0;
  496. $rank = 0;
  497. foreach ($ranking as $key => $val) {
  498. if ($val['uid'] == Safe::$user['uid']) {
  499. $isWinAward = 1;
  500. $rank = $key +1;
  501. break;
  502. }
  503. }
  504. // 如果中奖 插入用户中奖信息
  505. if ($isWinAward == 1) {
  506. $gifts = Db::table('awards_gift')->where('type', 2)->select();
  507. $giftId = 0;
  508. foreach ($gifts as $val) {
  509. if ($rank >= $val['min_rank'] && $rank <= $val['max_rank']) {
  510. $giftId = $val['id'];
  511. $orders = Db::table('awards_order')->where('uid', Safe::$user['uid'])->where('type', 2)->find();
  512. if (empty($orders)) {
  513. $order = [
  514. 'uid' => Safe::$user['uid'],
  515. 'gift_id' => $giftId,
  516. 'type' => 2,
  517. 'create_at' => time(),
  518. ];
  519. Db::table('awards_order')->insert($order);
  520. }
  521. break;
  522. }
  523. }
  524. }
  525. return $this->successResponse([
  526. 'isWinAward' => $isWinAward,
  527. 'giftId' => $giftId
  528. ]);
  529. }
  530. /**
  531. * 抽奖接口
  532. * @throws FuncNotFoundException
  533. * @throws ClassNotFoundException
  534. * @throws DbException
  535. * @throws ModelNotFoundException
  536. * @throws DataNotFoundException
  537. * @return mixed
  538. */
  539. public function getLuckDraw()
  540. {
  541. $orders = Db::table('awards_order')->where('uid', Safe::$user['uid'])->where('type', 1)->find();
  542. $isWinAward = 0;
  543. if ($orders) {
  544. return $this->successResponse([
  545. 'isWinAward' => $isWinAward,
  546. 'giftId' => 0
  547. ]);
  548. }
  549. $gifts = Db::table('awards_gift')->where('type', 1)->select();
  550. $logCount = Db::table('awards_user_task_log')->distinct('uid')->count('id');
  551. $userRate = rand(0, 5000);
  552. $giftId = 0;
  553. foreach ($gifts as $gift) {
  554. $orderCount = Db::table('awards_order')->where('type', 1)->where('gift_id', $gift['id'])->count('id');
  555. if ($orderCount >= $gift['count']) {
  556. continue;
  557. }
  558. // 如果参与活动人数 5000人内 有人中奖过,那么不在计算。
  559. $limit = 5000;
  560. $total = $logCount + 1;
  561. if ($orderCount == floor($total/$limit)) {
  562. $isWinAward = 0;
  563. $giftId = 0;
  564. break;
  565. }
  566. for ($i=0;$i<$gift['count'];$i++) {
  567. $giftRage = rand(0, 5000);
  568. if ($userRate == $giftRage) {
  569. $isWinAward = 1;
  570. $giftId = $gift['id'];
  571. break;
  572. }
  573. }
  574. // 如果参与活动人数 5000人内 还没有人中奖那最后一人必中。
  575. if ($orderCount < floor($total/$limit) && $isWinAward ==0 && ($total%$limit) == 0) {
  576. $isWinAward = 1;
  577. $giftId = $gift['id'];
  578. break;
  579. }
  580. }
  581. // 如果中奖 插入用户中奖信息
  582. if ($isWinAward == 1) {
  583. $order = [
  584. 'uid' => Safe::$user['uid'],
  585. 'gift_id' => $giftId,
  586. 'type' => 1,
  587. 'create_at' => time(),
  588. ];
  589. if (0 == Db::table('awards_order')->insert($order)) {
  590. return $this->response(5001, '系统错误,请稍后再试~');
  591. }
  592. }
  593. return $this->successResponse([
  594. 'isWinAward' => $isWinAward,
  595. 'giftId' => $giftId
  596. ]);
  597. }
  598. /**
  599. * 奖品记录接口
  600. * @throws FuncNotFoundException
  601. * @throws ClassNotFoundException
  602. * @throws DbException
  603. * @throws ModelNotFoundException
  604. * @throws DataNotFoundException
  605. * @return mixed
  606. */
  607. public function getAwardLog()
  608. {
  609. $awards = Db::table('awards_order')->alias('o')
  610. ->leftJoin('awards_gift g', 'o.gift_id = g.id')
  611. ->field(['g.name as giftName', 'o.type', 'o.create_at', 'o.gift_id'])
  612. ->where('o.uid', Safe::$user['uid'])
  613. ->select();
  614. $res = [];
  615. foreach ($awards as $val) {
  616. $type = '抽奖';
  617. if ($val['type'] == 2) {
  618. $type = '排名奖';
  619. }
  620. $res[] = [
  621. 'giftId' => $val['gift_id'],
  622. 'giftName' => $val['giftName'],
  623. 'typeLabel' => $type,
  624. 'type' => $val['type'],
  625. 'createAt' => date('y-m-d h:i:s', $val['create_at'])
  626. ];
  627. }
  628. return $this->successResponse($res);
  629. }
  630. public function clearGameData()
  631. {
  632. // 更新用户参与状态
  633. Db::table('awards_user_info')->where('uid', Safe::$user['uid'])->update([
  634. 'is_share' => 0,
  635. 'share_at' => 0,
  636. 'count' => 1,
  637. ]);
  638. Db::table('awards_user_task_log')->where('uid', Safe::$user['uid'])->delete();
  639. Db::table('awards_order')->where('uid', Safe::$user['uid'])->delete();
  640. return $this->successResponse(null, '清除成功');
  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 follow()
  653. {
  654. // 先获取品牌任务配置及状态
  655. $conf = $this->getBrandConfigAndState();
  656. if ($conf instanceof Json) {
  657. return $conf;
  658. }
  659. // 已经关注过了
  660. if ($conf['state']['follow_state'] == 1) {
  661. return $this->successResponse([
  662. 'result' => 2,
  663. 'brand' => $conf,
  664. ], '您已经关注过了');
  665. }
  666. // 调用接口关注
  667. $weiboRes = (new WeiboService(Safe::$user['uid']))->add($conf['config']['follow']['id']);
  668. if (empty($weiboRes) || $weiboRes['code'] != 10000) {
  669. return $this->response(600, $weiboRes['msg'] ?? '关注失败~');
  670. }
  671. $date = date('Y-m-d');
  672. $redisKey = $this->getTaskStateRedisKey(intval(Safe::$user['uid']));
  673. // 如果已经完成了另外两项,则标记品牌任务已经完成
  674. $finishState = $conf['state']['finish_state'];
  675. if ($conf['state']['view_state'] == 1 && $conf['state']['forward_state'] == 1) {
  676. $finishState = 1;
  677. }
  678. // 更新任务状态
  679. $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update([
  680. 'follow_state' => 1,
  681. 'finish_state' => $finishState,
  682. ]);
  683. if ($nums) {
  684. $conf['state']['follow_state'] = 1;
  685. $conf['state']['finish_state'] = $finishState;
  686. Cache::set($redisKey, json_encode($conf['state']), 86400);
  687. return $this->successResponse([
  688. 'result' => 1,
  689. 'brand' => $conf,
  690. ], '关注成功');
  691. } else {
  692. return $this->response(601, '更新任务状态失败');
  693. }
  694. }
  695. /**
  696. * 品牌任务转发博文接口
  697. * @throws FuncNotFoundException
  698. * @throws ClassNotFoundException
  699. * @throws ReflectionException
  700. * @throws DbException
  701. * @throws ModelNotFoundException
  702. * @throws DataNotFoundException
  703. * @return mixed
  704. */
  705. public function forward()
  706. {
  707. // 先获取品牌任务配置及状态
  708. $conf = $this->getBrandConfigAndState();
  709. if ($conf instanceof Json) {
  710. return $conf;
  711. }
  712. // 已经转发过了
  713. if ($conf['state']['forward_state'] == 1) {
  714. return $this->successResponse([
  715. 'result' => 2,
  716. 'brand' => $conf,
  717. ], '您已经转发过了');
  718. }
  719. // 调用微博接口转发
  720. $weiboRes = (new WeiboService(Safe::$user['uid']))->repost($conf['config']['forward']['id'], $conf['config']['forward']['content'] ?? '');
  721. if (empty($weiboRes) || $weiboRes['code'] != 10000) {
  722. return $this->response(600, $weiboRes['msg'] ?? '转发失败~');
  723. }
  724. $date = date('Y-m-d');
  725. $redisKey = $this->getTaskStateRedisKey(intval(Safe::$user['uid']));
  726. // 如果已经完成了另外两项,则标记品牌任务已经完成
  727. $finishState = $conf['state']['finish_state'];
  728. if ($conf['state']['view_state'] == 1 && $conf['state']['follow_state'] == 1) {
  729. $finishState = 1;
  730. }
  731. // 更新任务状态
  732. $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update([
  733. 'forward_state' => 1,
  734. 'finish_state' => $finishState,
  735. ]);
  736. if ($nums) {
  737. $conf['state']['forward_state'] = 1;
  738. $conf['state']['finish_state'] = $finishState;
  739. Cache::set($redisKey, json_encode($conf['state']), 86400);
  740. return $this->successResponse([
  741. 'result' => 1,
  742. 'brand' => $conf,
  743. ], '转发成功');
  744. } else {
  745. return $this->response(601, '更新任务状态失败');
  746. }
  747. }
  748. /**
  749. * 获取活动规则
  750. * @throws FuncNotFoundException
  751. * @throws ReflectionException
  752. * @throws InvalidArgumentException
  753. * @throws ClassNotFoundException
  754. * @return mixed
  755. */
  756. public function getRule()
  757. {
  758. $config = SystemService::instance()->getData('activity:rule');
  759. return $this->successResponse($config);
  760. }
  761. /**
  762. * 聚合页配置信息
  763. * @throws FuncNotFoundException
  764. * @throws ReflectionException
  765. * @throws InvalidArgumentException
  766. * @throws ClassNotFoundException
  767. * @return mixed
  768. */
  769. public function groupPageConfig()
  770. {
  771. return $this->successResponse(['config' => SystemService::instance()->getData('group:page:config')]);
  772. }
  773. // public function index()
  774. // {
  775. // $this->redirect(sysuri('admin/login/index'));
  776. // }
  777. /**
  778. * 聚合页通知获取
  779. * 查询最近20条通知
  780. * @throws DbException
  781. * @throws ModelNotFoundException
  782. * @throws DataNotFoundException
  783. * @return mixed
  784. */
  785. public function notices()
  786. {
  787. $rows = SystemNotice::limit(20)->order('id', 'desc')->select();
  788. return $this->successResponse([
  789. "lists" => $rows,
  790. ]);
  791. }
  792. /**
  793. * 首次弹窗
  794. * 发放邀请函微博
  795. * @return Json
  796. */
  797. public function sendInviteWeibo()
  798. {
  799. $config = SystemService::instance()->getData('group:page:config');
  800. $sendRes = (new WeiboService(Safe::$user['uid']))->status($config['firstDialog']['content']);
  801. if (empty($sendRes) || $sendRes['code'] != 10000) {
  802. return $this->response(403, $sendRes['msg'] ?? '发布失败');
  803. }
  804. Cache::set('u:f:' . Safe::$user['uid'], 1, 15552000);
  805. return $this->successResponse(null, '发布成功!');
  806. }
  807. /**
  808. * 首次弹窗用户未选择发送邀请函
  809. * 标记已经首次弹窗过了
  810. * @return mixed
  811. */
  812. public function setFirst()
  813. {
  814. Cache::set('u:f:' . Safe::$user['uid'], 1, 15552000);
  815. return $this->successResponse(null, '操作成功!');
  816. }
  817. /**
  818. * 标记用户已经浏览过主页了,用户点了去浏览时调用
  819. * @throws FuncNotFoundException
  820. * @throws ClassNotFoundException
  821. * @throws ReflectionException
  822. * @throws DbException
  823. * @throws ModelNotFoundException
  824. * @throws DataNotFoundException
  825. * @return mixed
  826. */
  827. public function setViewed()
  828. {
  829. // 先获取品牌任务配置及状态
  830. $conf = $this->getBrandConfigAndState();
  831. if ($conf instanceof Json) {
  832. return $conf;
  833. }
  834. if ($conf['state']['view_state'] == 1) {
  835. return $this->successResponse([
  836. 'result' => 2,
  837. 'brand' => $conf,
  838. ], '您已经浏览过了');
  839. }
  840. $date = date('Y-m-d');
  841. $redisKey = $this->getTaskStateRedisKey(intval(Safe::$user['uid']));
  842. // 如果已经完成了另外两项,则标记品牌任务已经完成
  843. $finishState = $conf['state']['finish_state'];
  844. if ($conf['state']['forward_state'] == 1 && $conf['state']['follow_state'] == 1) {
  845. $finishState = 1;
  846. }
  847. $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update([
  848. 'view_state' => 1,
  849. 'finish_state' => $finishState,
  850. ]);
  851. if ($nums) {
  852. $conf['state']['view_state'] = 1;
  853. $conf['state']['finish_state'] = $finishState;
  854. Cache::set($redisKey, json_encode($conf['state']), 86400);
  855. return $this->successResponse([
  856. 'result' => 1,
  857. 'brand' => $conf,
  858. ], '浏览成功');
  859. } else {
  860. return $this->response(601, '更新任务状态失败');
  861. }
  862. }
  863. /**
  864. * 用户完成端外分享时调用接口加票
  865. * @throws FuncNotFoundException
  866. * @throws ClassNotFoundException
  867. * @throws ReflectionException
  868. * @throws DbException
  869. * @throws ModelNotFoundException
  870. * @throws DataNotFoundException
  871. * @return mixed
  872. */
  873. public function shareFinishAddVotes()
  874. {
  875. // 先获取品牌任务配置及状态
  876. $conf = $this->getBrandConfigAndState();
  877. if ($conf instanceof Json) {
  878. return $conf;
  879. }
  880. if ($conf['state']['share_add_votes'] == 1) {
  881. return $this->successResponse([
  882. 'result' => 2,
  883. 'brand' => $conf,
  884. ], '您今日已经领取过投票机会了~');
  885. }
  886. // 调用接口给用户发券(加票)
  887. $weiboRes = (new WeiboService(Safe::$user['uid']))->setcoupons($conf['config']['share']['votes']);
  888. if (empty($weiboRes) || $weiboRes['code'] != 10000) {
  889. return $this->response(600, $weiboRes['msg'] ?? '加票失败~');
  890. }
  891. // 标记状态
  892. $date = date('Y-m-d');
  893. $redisKey = $this->getTaskStateRedisKey(intval(Safe::$user['uid']));
  894. $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update([
  895. 'share_add_votes' => 1,
  896. ]);
  897. if ($nums) {
  898. $conf['state']['share_add_votes'] = 1;
  899. Cache::set($redisKey, json_encode($conf['state']), 86400);
  900. return $this->successResponse([
  901. 'result' => 1,
  902. 'brand' => $conf,
  903. ], '加票成功');
  904. } else {
  905. return $this->response(601, '更新任务状态失败');
  906. }
  907. }
  908. /**
  909. * 完成品牌任务后领取加票机会接口
  910. * @throws FuncNotFoundException
  911. * @throws ClassNotFoundException
  912. * @throws ReflectionException
  913. * @throws DbException
  914. * @throws ModelNotFoundException
  915. * @throws DataNotFoundException
  916. * @return mixed
  917. */
  918. public function taskFinishAddVotes()
  919. {
  920. // 先获取品牌任务配置及状态
  921. $conf = $this->getBrandConfigAndState();
  922. if ($conf instanceof Json) {
  923. return $conf;
  924. }
  925. if ($conf['state']['finish_add_votes'] == 1) {
  926. return $this->successResponse([
  927. 'result' => 2,
  928. 'brand' => $conf,
  929. ], '您今日已经领取过投票机会了~');
  930. }
  931. // 未完成品牌任务 领取失败
  932. if ($conf['state']['finish_state'] == 0) {
  933. return $this->response(603, '请先完成品牌任务,再来领取加票', [
  934. 'result' => 0,
  935. 'brand' => $conf,
  936. ]);
  937. }
  938. // 调用微博接口 给用户发券(加票)
  939. $weiboRes = (new WeiboService(Safe::$user['uid']))->setcoupons($conf['config']['task']['votes']);
  940. if (empty($weiboRes) || $weiboRes['code'] != 10000) {
  941. return $this->response(600, $weiboRes['msg'] ?? '加票失败~');
  942. }
  943. // 标记加票成功
  944. $date = date('Y-m-d');
  945. $redisKey = $this->getTaskStateRedisKey(intval(Safe::$user['uid']));
  946. $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update([
  947. 'finish_add_votes' => 1,
  948. ]);
  949. if ($nums) {
  950. $conf['state']['finish_add_votes'] = 1;
  951. Cache::set($redisKey, json_encode($conf['state']), 86400);
  952. return $this->successResponse([
  953. 'result' => 1,
  954. 'brand' => $conf,
  955. ], '加票成功');
  956. } else {
  957. return $this->response(601, '更新任务状态失败');
  958. }
  959. }
  960. /**
  961. * 获取品牌任务配置及状态
  962. * @throws FuncNotFoundException
  963. * @throws ReflectionException
  964. * @throws InvalidArgumentException
  965. * @throws ClassNotFoundException
  966. * @throws DbException
  967. * @throws ModelNotFoundException
  968. * @throws DataNotFoundException
  969. * @return \think\response\Json|array
  970. */
  971. protected function getBrandConfigAndState()
  972. {
  973. $config = SystemService::instance()->getData('brand:task:config');
  974. $date = date('Y-m-d');
  975. $dateForRedis = date('Ymd');
  976. if (empty(Safe::$user['uid'])) {
  977. $state = [
  978. // 品牌任务完成状态
  979. 'finish_state' => 0,
  980. // 关注子任务状态
  981. 'follow_state' => 0,
  982. // 转发子任务状态
  983. 'forward_state' => 0,
  984. // 查看浏览主页任务状态
  985. 'view_state' => 0,
  986. // 品牌任务完成后是否加票
  987. 'finish_add_votes' => 0,
  988. // 分享完成后是否加票
  989. 'share_add_votes' => 0,
  990. 'uid' => 0,
  991. 'date' => $date,
  992. ];
  993. } else {
  994. $redisKey = "t:{$dateForRedis}:" . Safe::$user['uid'];
  995. $state = Cache::get($redisKey);
  996. if (empty($state)) {
  997. // 缓存失败 查数据库
  998. $state = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->find();
  999. if (empty($state)) {
  1000. // 当天第一次,初始化数据
  1001. $state = [
  1002. // 品牌任务完成状态
  1003. 'finish_state' => 0,
  1004. // 关注子任务状态
  1005. 'follow_state' => 0,
  1006. // 转发子任务状态
  1007. 'forward_state' => 0,
  1008. // 查看浏览主页任务状态
  1009. 'view_state' => 0,
  1010. // 品牌任务完成后是否加票
  1011. 'finish_add_votes' => 0,
  1012. // 分享完成后是否加票
  1013. 'share_add_votes' => 0,
  1014. 'uid' => Safe::$user['uid'],
  1015. 'date' => $date,
  1016. ];
  1017. if (0 == Db::table('awards_user_task')->insert($state)) {
  1018. return $this->response(5001, '系统错误,请稍后再试~');
  1019. }
  1020. }
  1021. Cache::set($redisKey, json_encode($state), 86400);
  1022. } else {
  1023. $state = json_decode($state, true);
  1024. }
  1025. // 如果未关注状态,查询下已经实际已经关注了
  1026. if ($state['follow_state'] == 0) {
  1027. $weiboRes = (new WeiboService(Safe::$user['uid']))->friends($config['follow']['id']);
  1028. if (isset($weiboRes['data'][$config['follow']['id']]) && $weiboRes['data'][$config['follow']['id']] == 1) {
  1029. $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update(['follow_state' => 1]);
  1030. if ($nums) {
  1031. $state['follow_state'] = 1;
  1032. Cache::set($redisKey, json_encode($state), 86400);
  1033. }
  1034. }
  1035. }
  1036. }
  1037. // 校准状态,可能存在异常情况关注,转发,浏览都完成了,但是总的品牌任务状态没标记成已完成
  1038. if ($state['follow_state'] == 1 && $state['forward_state'] == 1 && $state['view_state'] == 1 && $state['finish_state'] != 1) {
  1039. $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update(['finish_state' => 1]);
  1040. if ($nums) {
  1041. $state['finish_state'] = 1;
  1042. Cache::set($redisKey, json_encode($state), 86400);
  1043. }
  1044. }
  1045. return [
  1046. 'config' => $config,
  1047. 'state' => $state,
  1048. ];
  1049. }
  1050. /**
  1051. * 获取品牌任务redis 缓存键
  1052. * @param int $uid
  1053. * @return string
  1054. */
  1055. protected function getTaskStateRedisKey(int $uid): string
  1056. {
  1057. $dateForRedis = date('Ymd');
  1058. return "t:{$dateForRedis}:" . $uid;
  1059. }
  1060. /**
  1061. * @param $code
  1062. * @param $msg
  1063. * @param $data
  1064. * @return Json
  1065. */
  1066. protected function response($code, $msg, $data = null)
  1067. {
  1068. return json(
  1069. [
  1070. 'code' => $code,
  1071. 'message' => $msg,
  1072. 'data' => $data,
  1073. 'trackID' => Log::$logID,
  1074. ],
  1075. 200
  1076. );
  1077. }
  1078. /**
  1079. * @param $data
  1080. * @param $msg
  1081. * @return mixed
  1082. */
  1083. protected function successResponse($data, $msg = '操作成功')
  1084. {
  1085. return $this->response(0, $msg, $data);
  1086. }
  1087. }