| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- <?php
- namespace app\api\controller\mlbb;
- use app\common\controller\Base;
- use app\common\model\Channel;
- use app\common\model\Members;
- use think\Cache;
- use think\Db;
- use think\Env;
- use think\Request;
- class Mlbb extends Base
- {
- protected $authKey = 'auth_key'; // token和密码 加密的key
- // protected $game_id = 0; // 游戏ID(安卓/IOS)
- // protected $channel_id = 0; // 渠道ID
- // protected $server_id = '10083'; // 区服ID(旅人-三区)
- protected $expiration_date = ['start' => '1766023200', 'end' => '1768701600']; // 活动有效期(2025-12-18 10:00:00 ~ 2026-01-18 23:59:59)
- protected $userInfo = null; // 存储验证后的用户信息
- protected $activity_status = 1; // 活动状态:1=正常, 2=活动未开启, 3=活动已结束
- protected $activity_status_msg = "活动正常使用"; // 活动状态说明
- public function _initialize(){
- $input = input();
- $method = Request::instance()->method();
- $controller = Request::instance()->controller();
- // $action = request()->action();
- log_message("Mlbb.input: {$method} -- " . json_encode($input), 'log', LOG_PATH . 'mlbb_log/');
- $current_time = time();
- if ($current_time < $this->expiration_date['start']) {
- $remaining = $this->expiration_date['start'] - $current_time;
- $activity_status_msg = "❌ 活动尚未开始,还有 " . gmdate('d天H小时i分', $remaining) . " 开始";
- $this->activity_status = 2;
- $this->activity_status_msg = $activity_status_msg;
- // $this->resultFail($activity_status_msg, '-160');
- } elseif ($current_time > $this->expiration_date['end']) {
- $passed = $current_time - $this->expiration_date['end'];
- $activity_status_msg = "❌ 活动已结束,已过去 " . gmdate('d天H小时i分', $passed);
- $this->activity_status = 3;
- $this->activity_status_msg = $activity_status_msg;
- // $this->resultFail($activity_status_msg, '-160');
- }
- // ## 活动状态 ##
- if(!in_array($controller, ['Mlbb.User', 'Mlbb.Game', 'Mlbb.Login', 'Mlbb.Other', 'Mlbb.Activity'])){
- if($this->activity_status != 1){
- $this->resultFail($this->activity_status_msg, '-160');
- }
- }
- if (strpos($controller, '.') !== false) {
- list($version, $controller) = explode('.', $controller);
- }
- if (!in_array($controller, ['Login', 'Other'])) {
- // ## Token 验证
- $token = Request::instance()->header('token');
- if (!$token) {
- $this->resultFail('请重新登录!', '-260');
- }
- $userInfo = $this->validateToken($token);
- if ($userInfo['code'] != 200) {
- $this->resultFail('登录失效,请重新登录! : ' . $userInfo['code'], '-260');
- }
- $this->userInfo = $userInfo['data'];
- }
- }
- // 获取渠道ID,并解密
- public function getChannelId(){
- $this->channel_id = input('channel_id', '');
- if($this->channel_id){
- $this->channel_id = base64_decode($this->channel_id);
- }
- return $this->channel_id;
- }
- /**
- * 验证Token
- * @param string $token
- * @return array|false 返回用户信息或false
- */
- protected function validateToken($token)
- {
- try {
- // 解密token,参考Login.php中的加密方式
- $tokenData = auth_code($token, "DECODE", Env::get($this->authKey));
- if (empty($tokenData)) {
- return ['code' => -201, 'msg' => 'Token 有误,请重新登录!', 'data' => []];
- }
- // 解析token数据:user_id|username|token_random|sdk|mlbb
- $tokenParts = explode('|', $tokenData);
- if (count($tokenParts) !== 5 || $tokenParts[3] !== 'sdk' || $tokenParts[4] !== 'mlbb') {
- return ['code' => -202, 'msg' => 'Token 有误,请重新登录!', 'data' => []];
- }
- $userId = $tokenParts[0];
- $username = $tokenParts[1];
- $tokenRandom = $tokenParts[2];
- // 验证Redis中的token随机码
- $redisTokenRandom = Cache::store('redis')->get('token|sdk|mlbb|' . $userId);
- if ($redisTokenRandom !== $tokenRandom) {
- return ['code' => -203, 'msg' => 'Token 有误,请重新登录!', 'data' => []];
- }
- // 查询数据库验证用户是否存在
- $userInfo = Db::table('cy_members')->where(['id' => $userId])->field('id,username,mobile,flag')->find();
- if (empty($userInfo)) {
- return ['code' => -204, 'msg' => '当前账户不存在!', 'data' => []];
- }
- // 检查账号是否被冻结
- if (1 == $userInfo['flag']) {
- return ['code' => -205, 'msg' => '账号被冻结', 'data' => []];
- }
- $userLrInfo = Db::table('lr_user_info')->where('user_id', $userInfo['id'])->find();
- $userInfo['lr_info'] = $userLrInfo??[];
- // 判断渠道
- if(!empty($userLrInfo['channel_id'])){
- $this->judgmentChannel($userLrInfo['channel_id']);
- }
- // 判断游戏
- if(!empty($userLrInfo['game_id'])){
- if(!in_array($userLrInfo['game_id'], [10, 20, 344, 345])){
- $this->resultFail('当前游戏不支持此活动!');
- }
- }
- return ['code' => 200, 'msg' => 'success', 'data' => $userInfo];
- } catch (\Exception $e) {
- \think\Log::error('Mlbb@validateToken error: ' . $e->getMessage());
- return ['code' => -200, 'msg' => 'ERROR:'.$e->getMessage(), 'data' => []];
- }
- }
- /**
- * 渠道判断
- * @param $cahnnel_id string 渠道ID
- *
- * @return void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- protected function judgmentChannel($cahnnel_id){
- $channelInfo = (new Channel())->where(['status' => 1, 'id' => $cahnnel_id])->field('id,name,level,status')->find();
- if (!$channelInfo) {
- $this->resultFail('当前渠道不存在或已禁用!');
- } else if ($channelInfo['level'] <> 3) {
- $this->resultFail('非推广员渠道,不能进行推广,请联系客服');
- } else if ($channelInfo['status'] <> 1) {
- $this->resultFail('当前推广员渠道已禁用,请联系客服');
- }
- }
- // 判断是否完成任务
- protected function getJudgmentTask(){
- if(!empty($this->userInfo['lr_info']['role_id'])){
- return false;
- }
- return true;
- }
- // 获取唯一推广码
- protected function getMarkCode()
- {
- do {
- $code = strtoupper(substr(md5(uniqid(mt_rand(), true)), 0, 6));
- $exists = Db::table('lr_user_gathering')->where(['code' => $code])->find();
- } while ($exists);
- return $code;
- }
- protected function resultSuccess($data = [], $msg = 'success', $code = 200){
- $result = [
- 'code' => $code,
- 'msg' => $msg,
- 'data' => $data
- ];
- log_message("Mlbb.request.success: " . json_encode($result), 'log', LOG_PATH . 'mlbb_log/');
- return json($result);
- }
- protected function resultFail($msg = 'fail', $code = -100, $data = []){
- $result = [
- 'code' => $code,
- 'msg' => $msg,
- 'data' => $data
- ];
- log_message("Mlbb.request.fail: " . json_encode($result), 'log', LOG_PATH . 'mlbb_log/');
- exit(json_encode($result));
- // return json($result);
- }
- }
|