| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293 |
- <?php
- namespace app\index\controller;
- use think\facade\Db;
- use app\middleware\Log;
- use think\facade\Cache;
- use ReflectionException;
- use app\middleware\Safe;
- use think\file\UploadedFile;
- use \think\response\Json;
- use think\facade\Request;
- use think\admin\Controller;
- use InvalidArgumentException;
- use app\service\WeiboService;
- use app\admin\model\SystemNotice;
- use think\facade\Log as FacadeLog;
- use think\db\exception\DbException;
- use think\admin\service\SystemService;
- use think\exception\FuncNotFoundException;
- use think\exception\ClassNotFoundException;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\ModelNotFoundException;
- /**
- * Class Index
- * @package app\index\controller
- */
- class Index extends Controller
- {
- /**
- * 获取品牌任务相关配置
- *
- * @throws FuncNotFoundException
- * @throws ClassNotFoundException
- * @throws ReflectionException
- * @throws DbException
- * @throws ModelNotFoundException
- * @throws DataNotFoundException
- * @return mixed
- */
- public function brand()
- {
- $conf = $this->getBrandConfigAndState();
- if ($conf instanceof Json) {
- return $conf;
- }
- return $this->successResponse($conf);
- }
- /**
- * 检测登录 通过cookie中的SUB字段的内容,调用用户接口检测登录状态
- * @throws ClassNotFoundException
- * @throws ReflectionException
- * @return mixed
- */
- public function checkLogin()
- {
- FacadeLog::info("checkLogin cookies:" . json_encode(Request::post()));
- $sub = Request::post('cookie');
- $uid = 0;
- $userInfoRes = (new WeiboService($uid))->userinfo($sub);
- if (empty($userInfoRes) || $userInfoRes['ok'] != 1) {
- return $this->response(403, $userInfoRes['msg'] ?? '没有登录');
- }
- // 使用客户端信息生成token
- $token = md5($_SERVER['HTTP_USER_AGENT'] . $_SERVER['HTTP_ACCEPT_ENCODING'] . $_SERVER['HTTP_ACCEPT_LANGUAGE'] . get_client_ip(0) . $userInfoRes['data']['uid']);
- $user = $userInfoRes['data'];
- $userInfo = Db::table('awards_user_info')->where('uid', $user['uid'])->find();
- $count = 0;
- $isShare = 0;
- if (empty($userInfo)) {
- $userAttr = [
- 'uid' => $user['uid'],
- 'portrait' => $user['profile_image_url'],
- 'nickname' => $user['name'],
- 'is_share' => 0,
- 'count' => 1,
- 'create_at' => time()
- ];
- if (0 == Db::table('awards_user_info')->insert($userAttr)) {
- return $this->response(5001, '系统错误,请稍后再试~');
- }
- } else {
- $count = Db::table('awards_user_task_log')->where('uid', $user['uid'])->count('id');
- $isShare = $userInfo['is_share'];
- }
- // 生成加密用的密钥和向量
- $cipher = "aes-256-gcm";
- $ivlen = openssl_cipher_iv_length($cipher);
- $iv = bin2hex(openssl_random_pseudo_bytes($ivlen));
- $aesKey = bin2hex(openssl_random_pseudo_bytes(32));
- // $user = array_merge($user, [
- // 'aes_key' => $aesKey,
- // 'ase_iv' => $iv,
- // ]);
- $cacheUser = [
- 'aes_key' => $aesKey,
- 'ase_iv' => $iv,
- 'uid' => $user['uid'],
- ];
- $activity = Db::table('awards_activity')->where('begin_at', '<=', time())
- ->where('end_at', '>=', time())->find();
- $isBeginActivity = 1;
- if (empty($activity)) {
- $isBeginActivity = 0;
- }
- $userInfo = [];
- if ($count > 0) {
- $userInfo = Db::table('awards_user_task_log')->alias('l')
- ->leftJoin('awards_user_info u', 'l.uid = u.uid')
- ->field(['u.nickname', 'u.portrait', 'u.uid', 'l.duration', 'l.number'])
- ->order('l.number', 'desc')
- ->order('l.duration', 'asc')
- ->find();
- }
- // 缓存用户信息1天
- Cache::set('u:' . $token, json_encode($cacheUser), 86400);
- return $this->successResponse([
- 'user' => $user,
- 'token' => $token,
- 'isShare' => $isShare,
- 'count' => $count,
- 'isBeginActivity' => $isBeginActivity,
- 'task' => $userInfo
- ]);
- }
- /**
- * 是否可以参与游戏
- * @throws ClassNotFoundException
- * @throws ReflectionException
- * @return mixed
- */
- public function checkRole()
- {
- $activity = Db::table('awards_activity')->where('begin_at', '<=', time())
- ->where('end_at', '>=', time())->find();
- if (empty($activity)) {
- return $this->response(5002, '活动末开始');
- }
- $userInfo = Db::table('awards_user_info')->where('uid', Safe::$user['uid'])->find();
- $count = Db::table('awards_user_task_log')->where('uid', Safe::$user['uid'])->count('id');
- return $this->successResponse([
- 'isShare' => $userInfo['is_share'],
- 'count' => $count
- ]);
- }
- /**
- * 提交任务接口
- * @throws FuncNotFoundException
- * @throws ClassNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- * @throws DataNotFoundException
- * @return mixed
- */
- public function submitTask()
- {
- $activity = Db::table('awards_activity')->where('begin_at', '<=', time())
- ->where('end_at', '>=', time())->find();
- if (empty($activity)) {
- return $this->response(5002, '活动末开始');
- }
- // if (empty(Safe::$body)) {
- // return $this->response(5003, '参数有误');
- // }
- //
- // if (!isset(Safe::$body['duration']) || Safe::$body['duration'] < 1 || Safe::$body['duration'] > 1000) {
- // return $this->response(5003, '时长参数有误');
- // }
- //
- // if (!isset(Safe::$body['number']) || Safe::$body['number'] < 0 || Safe::$body['number'] > 1000) {
- // return $this->response(5003, '时长参数有误');
- // }
- // $duration = Safe::$body['duration'];
- // $number = Safe::$body['number'];
- // $duration = Request::post("duration");
- // $number = Request::post("number");
- //
- // $date = date('Y-m-d');
- //
- // $userInfo = Db::table('awards_user_info')->where('uid', Safe::$user['uid'])->find();
- // if (empty($userInfo)) {
- // return $this->response(403, '没有登录');
- // }
- //
- // $count = Db::table('awards_user_task_log')->where('uid', Safe::$user['uid'])->count('id');
- // if ($count >= $userInfo['count']) {
- // return $this->response(5002, '没有参与次数');
- // }
- //
- // $log = [
- // 'uid' => Safe::$user['uid'],
- // 'date' => $date,
- // 'duration' => $duration,
- // 'number' => $number,
- // 'create_at' => time()
- // ];
- // if (0 == Db::table('awards_user_task_log')->insert($log)) {
- // return $this->response(5001, '系统错误,请稍后再试~');
- // }
- return $this->successResponse(null, '提交成功');
- }
- /**
- * 提交发布微博接口
- * @throws FuncNotFoundException
- * @throws ClassNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- * @throws DataNotFoundException
- * @return mixed
- */
- public function submitShare()
- {
- $activity = Db::table('awards_activity')->where('begin_at', '<=', time())
- ->where('end_at', '>=', time())->find();
- if (empty($activity)) {
- return $this->response(5002, '活动末开始');
- }
- $userInfo = Db::table('awards_user_info')->where('uid', Safe::$user['uid'])->find();
- if (empty($userInfo)) {
- return $this->response(403, '没有登录');
- }
- if ($userInfo['is_share'] > 0) {
- return $this->response(601, '已发布');
- }
- // 更新任务状态
- $nums = Db::table('awards_user_info')->where('uid', Safe::$user['uid'])->update([
- 'is_share' => 1,
- 'count' => 2,
- 'share_at' => time()
- ]);
- if ($nums) {
- return $this->successResponse(null, '提交成功');
- } else {
- return $this->response(601, '发布失败');
- }
- }
- /**
- * 开始游戏
- * @return mixed|Json
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public function startGame()
- {
- $activity = Db::table('awards_activity')->where('begin_at', '<=', time())
- ->where('end_at', '>=', time())->find();
- if (empty($activity)) {
- return $this->response(5002, '活动末开始');
- }
- $userInfo = Db::table('awards_user_info')->where('uid', Safe::$user['uid'])->find();
- if (empty($userInfo)) {
- return $this->response(403, '没有登录');
- }
- $count = Db::table('awards_user_task_log')->where('uid', Safe::$user['uid'])->count('id');
- if ($count >= $userInfo['count']) {
- return $this->response(5002, '没有参与次数');
- }
- $game = Db::table('awards_user_game')
- ->where('uid', '=', Safe::$user['uid'])
- ->where('state', '=', 1)
- ->find();
- if (!empty($game)) {
- Db::table('awards_user_game')->where('id', '=', $game['id'])->delete(true);
- }
- $gameId = $this->uuid();
- $attr = [
- 'game_id' => $gameId,
- 'uid' => Safe::$user['uid'],
- 'begin_at' => time(),
- 'end_at' => 0,
- 'state' => 1,
- 'create_at' => time()
- ];
- Db::table('awards_user_game')->insert($attr);
- return $this->successResponse(['gameId' => $gameId], '开始游戏成功');
- }
- /**
- * 结束游戏
- * @return mixed|Json
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public function endGame()
- {
- $activity = Db::table('awards_activity')->where('begin_at', '<=', time())
- ->where('end_at', '>=', time())->find();
- if (empty($activity)) {
- return $this->response(5002, '活动末开始');
- }
- $userInfo = Db::table('awards_user_info')->where('uid', Safe::$user['uid'])->find();
- if (empty($userInfo)) {
- return $this->response(403, '没有登录');
- }
- $gameId = Request::post("gameId");
- $game = Db::table('awards_user_game')
- ->where('uid', '=', Safe::$user['uid'])
- ->where('game_id', '=', $gameId)
- ->where('state', '=', 1)
- ->find();
- if (empty($game)) {
- return $this->response(6001, '游戏不存在');
- }
- if((time() - $game['begin_at']) > 122 || (time() - $game['begin_at']) < 13) {
- return $this->response(6003, '游戏参数有误');
- }
- $endAt = time();
- $duration = $endAt - $game['begin_at'];
- if($duration < 14) {
- return $this->response(6001, '游戏有误');
- }
- Db::table('awards_user_game')->where('game_id', '=', $gameId)->update([
- 'end_at' => $endAt,
- 'state' => 2,
- 'update_at' => time()
- ]);
- $count = Db::table('awards_user_task_log')->where('uid', Safe::$user['uid'])->count('id');
- if ($count >= $userInfo['count']) {
- return $this->response(5002, '没有参与次数');
- }
- $number = Db::table('awards_user_game_log')
- ->where('uid', '=', Safe::$user['uid'])
- ->where('game_id', '=', $gameId)
- ->count('id');
- $date = date('Y-m-d');
- $log = [
- 'uid' => Safe::$user['uid'],
- 'date' => $date,
- 'duration' => $duration,
- 'number' => $number,
- 'create_at' => time()
- ];
- Db::table('awards_user_task_log')->insert($log);
- return $this->successResponse(null, '提交成功');
- }
- /**
- * 提交彩蛋
- * @return mixed|Json
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public function submitEasterEgg()
- {
- $easterEgg = Request::post("easterEgg");
- if (!in_array($easterEgg, [1, 2, 3,4 ,5 ,6,7, 8, 9, 10, 11, 12, 13, 14, 15])) {
- return $this->response(6003, '游戏参数有误');
- }
- $activity = Db::table('awards_activity')->where('begin_at', '<=', time())
- ->where('end_at', '>=', time())->find();
- if (empty($activity)) {
- return $this->response(5002, '活动末开始');
- }
- $userInfo = Db::table('awards_user_info')->where('uid', Safe::$user['uid'])->find();
- if (empty($userInfo)) {
- return $this->response(403, '没有登录');
- }
- $gameId = Request::post("gameId");
- $game = Db::table('awards_user_game')
- ->where('uid', '=', Safe::$user['uid'])
- ->where('game_id', '=', $gameId)
- ->where('state', '=', 1)
- ->find();
- if (empty($game)) {
- return $this->response(6001, '游戏不存在');
- }
- if((time() - $game['begin_at']) > 120) {
- return $this->response(6003, '游戏已结束');
- }
- $log = Db::table('awards_user_game_log')
- ->where('uid', '=', Safe::$user['uid'])
- ->where('game_id', '=', $gameId)
- ->where('draw', '=', $easterEgg)
- ->find();
- if (!empty($log)) {
- return $this->successResponse(null, '提交成功');
- }
- $log = Db::table('awards_user_game_log')
- ->where('uid', '=', Safe::$user['uid'])
- ->where('game_id', '=', $gameId)
- ->order('create_at', 'desc')
- ->find();
- if (empty($log)) {
- $startTime = $game['begin_at'];
- } else {
- $startTime = $log['create_at'];
- }
- $duration = time() - $startTime;
- if ($duration < 0) {
- return $this->response(6002, '游戏参数有误');
- }
- $attr = [
- 'uid' => Safe::$user['uid'],
- 'game_id' => $gameId,
- 'draw' => $easterEgg,
- 'duration' => $duration,
- 'create_at' => time()
- ];
- Db::table('awards_user_game_log')->insert($attr);
- return $this->successResponse(null, '提交成功');
- }
- function uuid()
- {
- $chars = md5(uniqid(mt_rand(), true));
- $uuid = substr ( $chars, 0, 8 )
- . substr ( $chars, 8, 4 )
- . substr ( $chars, 12, 4 )
- . substr ( $chars, 16, 4 )
- . substr ( $chars, 20, 12 );
- return $uuid ;
- }
- /**
- * 头号排行接口
- * @throws FuncNotFoundException
- * @throws ClassNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- * @throws DataNotFoundException
- * @return mixed
- */
- public function topRanking()
- {
- $ranking = Db::table('awards_user_task_log')->alias("l")
- ->leftJoin('awards_user_info u', 'l.uid = u.uid')
- ->field(['u.nickname', 'u.portrait', 'l.uid', 'min(l.duration) as duration', 'max(l.number) as number'])
- ->where('u.portrait', '<>', '')
- ->whereNotNull('u.portrait')
- ->group('l.uid')
- ->order('l.number', 'desc')
- ->order('l.duration', 'asc')
- ->limit(50)
- ->select();
- $userInfo = Db::table('awards_user_task_log')->alias('l')
- ->leftJoin('awards_user_info u', 'l.uid = u.uid')
- ->field(['u.nickname', 'u.portrait', 'l.uid', 'l.duration', 'l.number'])
- ->where('l.uid', '=', Safe::$user['uid'])
- ->order('l.number', 'desc')
- ->order('l.duration', 'asc')
- ->find();
- $res = [
- 'self' => $userInfo,
- 'ranking' => $ranking
- ];
- return $this->successResponse($res);
- }
- /**
- * 最新排行接口
- * @throws FuncNotFoundException
- * @throws ClassNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- * @throws DataNotFoundException
- * @return mixed
- */
- public function newRanking()
- {
- $ranking = Db::table('awards_user_task_log')->alias("l")
- ->leftJoin('awards_user_info u', 'l.uid = u.uid')
- ->field(['u.nickname', 'u.portrait', 'l.uid', 'min(l.duration) as duration', 'max(l.number) as number', 'l.create_at'])
- ->where('u.portrait', '<>', '')
- ->whereNotNull('u.portrait')
- ->group('l.uid')
- ->order('l.create_at', 'desc')
- ->order('l.number', 'desc')
- ->order('l.duration', 'asc')
- ->limit(50)
- ->select();
- $userInfo = Db::table('awards_user_task_log')->alias('l')
- ->leftJoin('awards_user_info u', 'l.uid = u.uid')
- ->field(['u.nickname', 'u.portrait', 'l.uid', 'l.duration', 'l.number'])
- ->where('l.uid', '=', Safe::$user['uid'])
- ->order('l.number', 'desc')
- ->order('l.duration', 'asc')
- ->find();
- $res = [
- 'self' => $userInfo,
- 'ranking' => $ranking
- ];
- return $this->successResponse($res);
- }
- /**
- * 提交领取信息接口
- * @throws FuncNotFoundException
- * @throws ClassNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- * @throws DataNotFoundException
- * @return mixed
- */
- public function submitReceive()
- {
- $name = trim(Request::post("name"));
- $mobile = trim(Request::post("mobile"));
- $address = trim(Request::post("address"));
- $giftId = trim(Request::post("giftId"));
- $type = trim(Request::post("type"));
- $orders = Db::table('awards_order')->where('uid', Safe::$user['uid'])->where('type', $type)->find();
- if (empty($orders)) {
- return $this->response(5001, '参数有误');
- }
- $userInfo = Db::table('awards_user_info')->where('uid', Safe::$user['uid'])->find();
- if (empty($userInfo)) {
- return $this->response(403, '没有登录');
- }
- if (strlen($name) < 1 || strlen($name) > 50) {
- return $this->response(5001, '姓名有误');
- }
- if (strlen($mobile) != 11) {
- return $this->response(5001, '手机号有误');
- }
- if (strlen($address) < 1 || strlen($address) > 1000) {
- return $this->response(5001, '地址有误');
- }
- // 更新任务状态
- $nums = Db::table('awards_order')->where('uid', Safe::$user['uid'])->where('type', $type)->update([
- 'name' => $name,
- 'mobile' => $mobile,
- 'address' => $address,
- ]);
- if ($nums < 1) {
- return $this->response(5001, '系统错误,请稍后再试~');
- }
- return $this->successResponse(null, '提交成功');
- }
- /**
- * 获取轮播中奖接口
- * @throws FuncNotFoundException
- * @throws ClassNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- * @throws DataNotFoundException
- * @return mixed
- */
- public function getRotationAward() {
- $activity = Db::table('awards_activity')->where('begin_at', '<=', time())
- ->where('end_at', '>=', time())->find();
- if (empty($activity)) {
- return $this->response(5002, '活动末开始');
- }
- $orders = Db::table('awards_order')->alias('o')
- ->leftJoin('awards_user_info u', 'o.uid = u.uid')
- ->leftJoin('awards_gift g', 'o.gift_id = g.id')
- ->field(['u.nickname', 'u.portrait', 'u.uid', 'g.name as giftName'])
- ->order('o.id', 'desc')
- ->select();
- return $this->successResponse($orders);
- }
- /**
- * 排行中奖接口
- * @throws FuncNotFoundException
- * @throws ClassNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- * @throws DataNotFoundException
- * @return mixed
- */
- public function getRankingWinAward() {
- $activity = Db::table('awards_activity')->where('begin_at', '<=', time())
- ->where('end_at', '>=', time())->find();
- if (!empty($activity)) {
- return $this->response(5002, '活动末结束');
- }
- $ranking = Db::table('awards_user_task_log')->alias("l")
- ->leftJoin('awards_user_info u', 'l.uid = u.uid')
- ->field(['u.nickname', 'u.portrait', 'u.uid', 'l.duration', 'l.number'])
- ->distinct('l.uid')
- ->order('l.number', 'desc')
- ->order('l.duration', 'asc')
- ->limit(50)
- ->select();
- $isWinAward = 0;
- $rank = 0;
- foreach ($ranking as $key => $val) {
- if ($val['uid'] == Safe::$user['uid']) {
- $isWinAward = 1;
- $rank = $key +1;
- break;
- }
- }
- $giftId = 0;
- // 如果中奖 插入用户中奖信息
- if ($isWinAward == 1) {
- $gifts = Db::table('awards_gift')->where('type', 2)->select();
- foreach ($gifts as $val) {
- if ($rank >= $val['min_rank'] && $rank <= $val['max_rank']) {
- $giftId = $val['id'];
- $orders = Db::table('awards_order')->where('uid', Safe::$user['uid'])->where('type', 2)->find();
- if (empty($orders)) {
- $order = [
- 'uid' => Safe::$user['uid'],
- 'gift_id' => $giftId,
- 'type' => 2,
- 'create_at' => time(),
- ];
- Db::table('awards_order')->insert($order);
- }
- break;
- }
- }
- }
- return $this->successResponse([
- 'isWinAward' => $isWinAward,
- 'giftId' => $giftId
- ]);
- }
- /**
- * 抽奖接口
- * @throws FuncNotFoundException
- * @throws ClassNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- * @throws DataNotFoundException
- * @return mixed
- */
- public function getLuckDraw()
- {
- $userInfo = Db::table('awards_user_info')->where('uid', Safe::$user['uid'])->find();
- if ($userInfo['draw_count'] >= 30) {
- return $this->response(5002, '抽奖次数已达上线~');
- }
- Db::table('awards_user_info')->where('uid', Safe::$user['uid'])->update([
- 'draw_count' => $userInfo['draw_count'] + 1
- ]);
- $orders = Db::table('awards_order')->where('uid', Safe::$user['uid'])->where('type', 1)->find();
- $isWinAward = 0;
- if ($orders) {
- return $this->successResponse([
- 'isWinAward' => $isWinAward,
- 'giftId' => 0
- ]);
- }
- $gifts = Db::table('awards_gift')->where('type', 1)->select();
- $logCount = Db::table('awards_user_task_log')->distinct('uid')->count('id');
- $limit = 5000;
- $userRate = rand(0, $limit);
- $giftId = 0;
- foreach ($gifts as $gift) {
- $orderCount = Db::table('awards_order')->where('type', 1)->where('gift_id', $gift['id'])->count('id');
- if ($orderCount >= $gift['count']) {
- continue;
- }
- // 如果参与活动人数 5000人内 有人中奖过,那么不在计算。
- $total = $logCount + 1;
- if ($orderCount == floor($total/$limit)) {
- $isWinAward = 0;
- $giftId = 0;
- break;
- }
- for ($i=0;$i<$gift['count'];$i++) {
- $giftRage = rand(0, $limit);
- if ($userRate == $giftRage) {
- $isWinAward = 1;
- $giftId = $gift['id'];
- break;
- }
- }
- // 如果参与活动人数 5000人内 还没有人中奖那最后一人必中。
- if ($orderCount < floor($total/$limit) && $isWinAward ==0 && ($total%$limit) == 0) {
- $isWinAward = 1;
- $giftId = $gift['id'];
- break;
- }
- }
- // 如果中奖 插入用户中奖信息
- if ($isWinAward == 1) {
- $order = [
- 'uid' => Safe::$user['uid'],
- 'gift_id' => $giftId,
- 'type' => 1,
- 'create_at' => time(),
- ];
- if (0 == Db::table('awards_order')->insert($order)) {
- return $this->response(5001, '系统错误,请稍后再试~');
- }
- }
- return $this->successResponse([
- 'isWinAward' => $isWinAward,
- 'giftId' => $giftId
- ]);
- }
- /**
- * 奖品记录接口
- * @throws FuncNotFoundException
- * @throws ClassNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- * @throws DataNotFoundException
- * @return mixed
- */
- public function getAwardLog()
- {
- $awards = Db::table('awards_order')->alias('o')
- ->leftJoin('awards_gift g', 'o.gift_id = g.id')
- ->field(['g.name as giftName', 'o.*'])
- ->where('o.uid', Safe::$user['uid'])
- ->select();
- $res = [];
- foreach ($awards as $val) {
- $type = '抽奖';
- if ($val['type'] == 2) {
- $type = '排名奖';
- }
- $isReceive = 1;
- if (empty($val['name']) && empty($val['address']) && empty($val['mobile'])) {
- $isReceive = 0;
- }
- $res[] = [
- 'giftId' => $val['gift_id'],
- 'giftName' => $val['giftName'],
- 'typeLabel' => $type,
- 'type' => $val['type'],
- 'createAt' => date('y-m-d h:i:s', $val['create_at']),
- 'address' => $val['address'],
- 'name' => $val['name'],
- 'mobile' => $val['mobile'],
- 'isReceive' => $isReceive
- ];
- }
- return $this->successResponse($res);
- }
- public function clearGameData()
- {
- // 更新用户参与状态
- Db::table('awards_user_info')->where('uid', Safe::$user['uid'])->update([
- 'is_share' => 0,
- 'share_at' => 0,
- 'count' => 1,
- ]);
- Db::table('awards_user_task_log')->where('uid', Safe::$user['uid'])->delete();
- Db::table('awards_order')->where('uid', Safe::$user['uid'])->delete();
- return $this->successResponse(null, '清除成功');
- }
- /**
- * 品牌任务关注接口
- * @throws FuncNotFoundException
- * @throws ClassNotFoundException
- * @throws ReflectionException
- * @throws DbException
- * @throws ModelNotFoundException
- * @throws DataNotFoundException
- * @return mixed
- */
- public function follow()
- {
- // 先获取品牌任务配置及状态
- $conf = $this->getBrandConfigAndState();
- if ($conf instanceof Json) {
- return $conf;
- }
- // 已经关注过了
- if ($conf['state']['follow_state'] == 1) {
- return $this->successResponse([
- 'result' => 2,
- 'brand' => $conf,
- ], '您已经关注过了');
- }
- // 调用接口关注
- $weiboRes = (new WeiboService(Safe::$user['uid']))->add($conf['config']['follow']['id']);
- if (empty($weiboRes) || $weiboRes['code'] != 10000) {
- return $this->response(600, $weiboRes['msg'] ?? '关注失败~');
- }
- $date = date('Y-m-d');
- $redisKey = $this->getTaskStateRedisKey(intval(Safe::$user['uid']));
- // 如果已经完成了另外两项,则标记品牌任务已经完成
- $finishState = $conf['state']['finish_state'];
- if ($conf['state']['view_state'] == 1 && $conf['state']['forward_state'] == 1) {
- $finishState = 1;
- }
- // 更新任务状态
- $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update([
- 'follow_state' => 1,
- 'finish_state' => $finishState,
- ]);
- if ($nums) {
- $conf['state']['follow_state'] = 1;
- $conf['state']['finish_state'] = $finishState;
- Cache::set($redisKey, json_encode($conf['state']), 86400);
- return $this->successResponse([
- 'result' => 1,
- 'brand' => $conf,
- ], '关注成功');
- } else {
- return $this->response(601, '更新任务状态失败');
- }
- }
- /**
- * 品牌任务转发博文接口
- * @throws FuncNotFoundException
- * @throws ClassNotFoundException
- * @throws ReflectionException
- * @throws DbException
- * @throws ModelNotFoundException
- * @throws DataNotFoundException
- * @return mixed
- */
- public function forward()
- {
- // 先获取品牌任务配置及状态
- $conf = $this->getBrandConfigAndState();
- if ($conf instanceof Json) {
- return $conf;
- }
- // 已经转发过了
- if ($conf['state']['forward_state'] == 1) {
- return $this->successResponse([
- 'result' => 2,
- 'brand' => $conf,
- ], '您已经转发过了');
- }
- // 调用微博接口转发
- $weiboRes = (new WeiboService(Safe::$user['uid']))->repost($conf['config']['forward']['id'], $conf['config']['forward']['content'] ?? '');
- if (empty($weiboRes) || $weiboRes['code'] != 10000) {
- return $this->response(600, $weiboRes['msg'] ?? '转发失败~');
- }
- $date = date('Y-m-d');
- $redisKey = $this->getTaskStateRedisKey(intval(Safe::$user['uid']));
- // 如果已经完成了另外两项,则标记品牌任务已经完成
- $finishState = $conf['state']['finish_state'];
- if ($conf['state']['view_state'] == 1 && $conf['state']['follow_state'] == 1) {
- $finishState = 1;
- }
- // 更新任务状态
- $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update([
- 'forward_state' => 1,
- 'finish_state' => $finishState,
- ]);
- if ($nums) {
- $conf['state']['forward_state'] = 1;
- $conf['state']['finish_state'] = $finishState;
- Cache::set($redisKey, json_encode($conf['state']), 86400);
- return $this->successResponse([
- 'result' => 1,
- 'brand' => $conf,
- ], '转发成功');
- } else {
- return $this->response(601, '更新任务状态失败');
- }
- }
- /**
- * 获取活动规则
- * @throws FuncNotFoundException
- * @throws ReflectionException
- * @throws InvalidArgumentException
- * @throws ClassNotFoundException
- * @return mixed
- */
- public function getRule()
- {
- $config = SystemService::instance()->getData('activity:rule');
- return $this->successResponse($config);
- }
- /**
- * 聚合页配置信息
- * @throws FuncNotFoundException
- * @throws ReflectionException
- * @throws InvalidArgumentException
- * @throws ClassNotFoundException
- * @return mixed
- */
- public function groupPageConfig()
- {
- return $this->successResponse(['config' => SystemService::instance()->getData('group:page:config')]);
- }
- // public function index()
- // {
- // $this->redirect('video');
- // }
- /**
- * 聚合页通知获取
- * 查询最近20条通知
- * @throws DbException
- * @throws ModelNotFoundException
- * @throws DataNotFoundException
- * @return mixed
- */
- public function notices()
- {
- $rows = SystemNotice::limit(20)->order('id', 'desc')->select();
- return $this->successResponse([
- "lists" => $rows,
- ]);
- }
- /**
- * 首次弹窗
- * 发放邀请函微博
- * @return Json
- */
- public function sendInviteWeibo()
- {
- $config = SystemService::instance()->getData('group:page:config');
- $sendRes = (new WeiboService(Safe::$user['uid']))->status($config['firstDialog']['content']);
- if (empty($sendRes) || $sendRes['code'] != 10000) {
- return $this->response(403, $sendRes['msg'] ?? '发布失败');
- }
- Cache::set('u:f:' . Safe::$user['uid'], 1, 15552000);
- return $this->successResponse(null, '发布成功!');
- }
- /**
- * 首次弹窗用户未选择发送邀请函
- * 标记已经首次弹窗过了
- * @return mixed
- */
- public function setFirst()
- {
- Cache::set('u:f:' . Safe::$user['uid'], 1, 15552000);
- return $this->successResponse(null, '操作成功!');
- }
- /**
- * 标记用户已经浏览过主页了,用户点了去浏览时调用
- * @throws FuncNotFoundException
- * @throws ClassNotFoundException
- * @throws ReflectionException
- * @throws DbException
- * @throws ModelNotFoundException
- * @throws DataNotFoundException
- * @return mixed
- */
- public function setViewed()
- {
- // 先获取品牌任务配置及状态
- $conf = $this->getBrandConfigAndState();
- if ($conf instanceof Json) {
- return $conf;
- }
- if ($conf['state']['view_state'] == 1) {
- return $this->successResponse([
- 'result' => 2,
- 'brand' => $conf,
- ], '您已经浏览过了');
- }
- $date = date('Y-m-d');
- $redisKey = $this->getTaskStateRedisKey(intval(Safe::$user['uid']));
- // 如果已经完成了另外两项,则标记品牌任务已经完成
- $finishState = $conf['state']['finish_state'];
- if ($conf['state']['forward_state'] == 1 && $conf['state']['follow_state'] == 1) {
- $finishState = 1;
- }
- $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update([
- 'view_state' => 1,
- 'finish_state' => $finishState,
- ]);
- if ($nums) {
- $conf['state']['view_state'] = 1;
- $conf['state']['finish_state'] = $finishState;
- Cache::set($redisKey, json_encode($conf['state']), 86400);
- return $this->successResponse([
- 'result' => 1,
- 'brand' => $conf,
- ], '浏览成功');
- } else {
- return $this->response(601, '更新任务状态失败');
- }
- }
- /**
- * 用户完成端外分享时调用接口加票
- * @throws FuncNotFoundException
- * @throws ClassNotFoundException
- * @throws ReflectionException
- * @throws DbException
- * @throws ModelNotFoundException
- * @throws DataNotFoundException
- * @return mixed
- */
- public function shareFinishAddVotes()
- {
- // 先获取品牌任务配置及状态
- $conf = $this->getBrandConfigAndState();
- if ($conf instanceof Json) {
- return $conf;
- }
- if ($conf['state']['share_add_votes'] == 1) {
- return $this->successResponse([
- 'result' => 2,
- 'brand' => $conf,
- ], '您今日已经领取过投票机会了~');
- }
- // 调用接口给用户发券(加票)
- $weiboRes = (new WeiboService(Safe::$user['uid']))->setcoupons($conf['config']['share']['votes']);
- if (empty($weiboRes) || $weiboRes['code'] != 10000) {
- return $this->response(600, $weiboRes['msg'] ?? '加票失败~');
- }
- // 标记状态
- $date = date('Y-m-d');
- $redisKey = $this->getTaskStateRedisKey(intval(Safe::$user['uid']));
- $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update([
- 'share_add_votes' => 1,
- ]);
- if ($nums) {
- $conf['state']['share_add_votes'] = 1;
- Cache::set($redisKey, json_encode($conf['state']), 86400);
- return $this->successResponse([
- 'result' => 1,
- 'brand' => $conf,
- ], '加票成功');
- } else {
- return $this->response(601, '更新任务状态失败');
- }
- }
- /**
- * 完成品牌任务后领取加票机会接口
- * @throws FuncNotFoundException
- * @throws ClassNotFoundException
- * @throws ReflectionException
- * @throws DbException
- * @throws ModelNotFoundException
- * @throws DataNotFoundException
- * @return mixed
- */
- public function taskFinishAddVotes()
- {
- // 先获取品牌任务配置及状态
- $conf = $this->getBrandConfigAndState();
- if ($conf instanceof Json) {
- return $conf;
- }
- if ($conf['state']['finish_add_votes'] == 1) {
- return $this->successResponse([
- 'result' => 2,
- 'brand' => $conf,
- ], '您今日已经领取过投票机会了~');
- }
- // 未完成品牌任务 领取失败
- if ($conf['state']['finish_state'] == 0) {
- return $this->response(603, '请先完成品牌任务,再来领取加票', [
- 'result' => 0,
- 'brand' => $conf,
- ]);
- }
- // 调用微博接口 给用户发券(加票)
- $weiboRes = (new WeiboService(Safe::$user['uid']))->setcoupons($conf['config']['task']['votes']);
- if (empty($weiboRes) || $weiboRes['code'] != 10000) {
- return $this->response(600, $weiboRes['msg'] ?? '加票失败~');
- }
- // 标记加票成功
- $date = date('Y-m-d');
- $redisKey = $this->getTaskStateRedisKey(intval(Safe::$user['uid']));
- $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update([
- 'finish_add_votes' => 1,
- ]);
- if ($nums) {
- $conf['state']['finish_add_votes'] = 1;
- Cache::set($redisKey, json_encode($conf['state']), 86400);
- return $this->successResponse([
- 'result' => 1,
- 'brand' => $conf,
- ], '加票成功');
- } else {
- return $this->response(601, '更新任务状态失败');
- }
- }
- /**
- * 获取品牌任务配置及状态
- * @throws FuncNotFoundException
- * @throws ReflectionException
- * @throws InvalidArgumentException
- * @throws ClassNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- * @throws DataNotFoundException
- * @return \think\response\Json|array
- */
- protected function getBrandConfigAndState()
- {
- $config = SystemService::instance()->getData('brand:task:config');
- $date = date('Y-m-d');
- $dateForRedis = date('Ymd');
- if (empty(Safe::$user['uid'])) {
- $state = [
- // 品牌任务完成状态
- 'finish_state' => 0,
- // 关注子任务状态
- 'follow_state' => 0,
- // 转发子任务状态
- 'forward_state' => 0,
- // 查看浏览主页任务状态
- 'view_state' => 0,
- // 品牌任务完成后是否加票
- 'finish_add_votes' => 0,
- // 分享完成后是否加票
- 'share_add_votes' => 0,
- 'uid' => 0,
- 'date' => $date,
- ];
- } else {
- $redisKey = "t:{$dateForRedis}:" . Safe::$user['uid'];
- $state = Cache::get($redisKey);
- if (empty($state)) {
- // 缓存失败 查数据库
- $state = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->find();
- if (empty($state)) {
- // 当天第一次,初始化数据
- $state = [
- // 品牌任务完成状态
- 'finish_state' => 0,
- // 关注子任务状态
- 'follow_state' => 0,
- // 转发子任务状态
- 'forward_state' => 0,
- // 查看浏览主页任务状态
- 'view_state' => 0,
- // 品牌任务完成后是否加票
- 'finish_add_votes' => 0,
- // 分享完成后是否加票
- 'share_add_votes' => 0,
- 'uid' => Safe::$user['uid'],
- 'date' => $date,
- ];
- if (0 == Db::table('awards_user_task')->insert($state)) {
- return $this->response(5001, '系统错误,请稍后再试~');
- }
- }
- Cache::set($redisKey, json_encode($state), 86400);
- } else {
- $state = json_decode($state, true);
- }
- // 如果未关注状态,查询下已经实际已经关注了
- if ($state['follow_state'] == 0) {
- $weiboRes = (new WeiboService(Safe::$user['uid']))->friends($config['follow']['id']);
- if (isset($weiboRes['data'][$config['follow']['id']]) && $weiboRes['data'][$config['follow']['id']] == 1) {
- $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update(['follow_state' => 1]);
- if ($nums) {
- $state['follow_state'] = 1;
- Cache::set($redisKey, json_encode($state), 86400);
- }
- }
- }
- }
- // 校准状态,可能存在异常情况关注,转发,浏览都完成了,但是总的品牌任务状态没标记成已完成
- if ($state['follow_state'] == 1 && $state['forward_state'] == 1 && $state['view_state'] == 1 && $state['finish_state'] != 1) {
- $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update(['finish_state' => 1]);
- if ($nums) {
- $state['finish_state'] = 1;
- Cache::set($redisKey, json_encode($state), 86400);
- }
- }
- return [
- 'config' => $config,
- 'state' => $state,
- ];
- }
- /**
- * 获取品牌任务redis 缓存键
- * @param int $uid
- * @return string
- */
- protected function getTaskStateRedisKey(int $uid): string
- {
- $dateForRedis = date('Ymd');
- return "t:{$dateForRedis}:" . $uid;
- }
- /**
- * @param $code
- * @param $msg
- * @param $data
- * @return Json
- */
- protected function response($code, $msg, $data = null)
- {
- return json(
- [
- 'code' => $code,
- 'message' => $msg,
- 'data' => $data,
- 'trackID' => Log::$logID,
- ],
- 200
- );
- }
- /**
- * @param $data
- * @param $msg
- * @return mixed
- */
- protected function successResponse($data, $msg = '操作成功')
- {
- return $this->response(0, $msg, $data);
- }
- }
|