Mlbb.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. namespace app\api\controller\mlbb;
  3. use app\common\controller\Base;
  4. use app\common\model\Channel;
  5. use app\common\model\Members;
  6. use think\Cache;
  7. use think\Db;
  8. use think\Env;
  9. use think\Request;
  10. class Mlbb extends Base
  11. {
  12. protected $authKey = 'auth_key'; // token和密码 加密的key
  13. // protected $game_id = 0; // 游戏ID(安卓/IOS)
  14. // protected $channel_id = 0; // 渠道ID
  15. // protected $server_id = '10083'; // 区服ID(旅人-三区)
  16. protected $expiration_date = ['start' => '1766023200', 'end' => '1768701600']; // 活动有效期(2025-12-18 10:00:00 ~ 2026-01-18 23:59:59)
  17. protected $userInfo = null; // 存储验证后的用户信息
  18. protected $activity_status = 1; // 活动状态:1=正常, 2=活动未开启, 3=活动已结束
  19. protected $activity_status_msg = "活动正常使用"; // 活动状态说明
  20. public function _initialize(){
  21. $input = input();
  22. $method = Request::instance()->method();
  23. $controller = Request::instance()->controller();
  24. // $action = request()->action();
  25. log_message("Mlbb.input: {$method} -- " . json_encode($input), 'log', LOG_PATH . 'mlbb_log/');
  26. $current_time = time();
  27. if ($current_time < $this->expiration_date['start']) {
  28. $remaining = $this->expiration_date['start'] - $current_time;
  29. $activity_status_msg = "❌ 活动尚未开始,还有 " . gmdate('d天H小时i分', $remaining) . " 开始";
  30. $this->activity_status = 2;
  31. $this->activity_status_msg = $activity_status_msg;
  32. // $this->resultFail($activity_status_msg, '-160');
  33. } elseif ($current_time > $this->expiration_date['end']) {
  34. $passed = $current_time - $this->expiration_date['end'];
  35. $activity_status_msg = "❌ 活动已结束,已过去 " . gmdate('d天H小时i分', $passed);
  36. $this->activity_status = 3;
  37. $this->activity_status_msg = $activity_status_msg;
  38. // $this->resultFail($activity_status_msg, '-160');
  39. }
  40. // ## 活动状态 ##
  41. if(!in_array($controller, ['Mlbb.User', 'Mlbb.Game', 'Mlbb.Login', 'Mlbb.Other', 'Mlbb.Activity'])){
  42. if($this->activity_status != 1){
  43. $this->resultFail($this->activity_status_msg, '-160');
  44. }
  45. }
  46. if (strpos($controller, '.') !== false) {
  47. list($version, $controller) = explode('.', $controller);
  48. }
  49. if (!in_array($controller, ['Login', 'Other'])) {
  50. // ## Token 验证
  51. $token = Request::instance()->header('token');
  52. if (!$token) {
  53. $this->resultFail('请重新登录!', '-260');
  54. }
  55. $userInfo = $this->validateToken($token);
  56. if ($userInfo['code'] != 200) {
  57. $this->resultFail('登录失效,请重新登录! : ' . $userInfo['code'], '-260');
  58. }
  59. $this->userInfo = $userInfo['data'];
  60. }
  61. }
  62. // 获取渠道ID,并解密
  63. public function getChannelId(){
  64. $this->channel_id = input('channel_id', '');
  65. if($this->channel_id){
  66. $this->channel_id = base64_decode($this->channel_id);
  67. }
  68. return $this->channel_id;
  69. }
  70. /**
  71. * 验证Token
  72. * @param string $token
  73. * @return array|false 返回用户信息或false
  74. */
  75. protected function validateToken($token)
  76. {
  77. try {
  78. // 解密token,参考Login.php中的加密方式
  79. $tokenData = auth_code($token, "DECODE", Env::get($this->authKey));
  80. if (empty($tokenData)) {
  81. return ['code' => -201, 'msg' => 'Token 有误,请重新登录!', 'data' => []];
  82. }
  83. // 解析token数据:user_id|username|token_random|sdk|mlbb
  84. $tokenParts = explode('|', $tokenData);
  85. if (count($tokenParts) !== 5 || $tokenParts[3] !== 'sdk' || $tokenParts[4] !== 'mlbb') {
  86. return ['code' => -202, 'msg' => 'Token 有误,请重新登录!', 'data' => []];
  87. }
  88. $userId = $tokenParts[0];
  89. $username = $tokenParts[1];
  90. $tokenRandom = $tokenParts[2];
  91. // 验证Redis中的token随机码
  92. $redisTokenRandom = Cache::store('redis')->get('token|sdk|mlbb|' . $userId);
  93. if ($redisTokenRandom !== $tokenRandom) {
  94. return ['code' => -203, 'msg' => 'Token 有误,请重新登录!', 'data' => []];
  95. }
  96. // 查询数据库验证用户是否存在
  97. $userInfo = Db::table('cy_members')->where(['id' => $userId])->field('id,username,mobile,flag')->find();
  98. if (empty($userInfo)) {
  99. return ['code' => -204, 'msg' => '当前账户不存在!', 'data' => []];
  100. }
  101. // 检查账号是否被冻结
  102. if (1 == $userInfo['flag']) {
  103. return ['code' => -205, 'msg' => '账号被冻结', 'data' => []];
  104. }
  105. $userLrInfo = Db::table('lr_user_info')->where('user_id', $userInfo['id'])->find();
  106. $userInfo['lr_info'] = $userLrInfo??[];
  107. // 判断渠道
  108. if(!empty($userLrInfo['channel_id'])){
  109. $this->judgmentChannel($userLrInfo['channel_id']);
  110. }
  111. // 判断游戏
  112. if(!empty($userLrInfo['game_id'])){
  113. if(!in_array($userLrInfo['game_id'], [10, 20, 344, 345])){
  114. $this->resultFail('当前游戏不支持此活动!');
  115. }
  116. }
  117. return ['code' => 200, 'msg' => 'success', 'data' => $userInfo];
  118. } catch (\Exception $e) {
  119. \think\Log::error('Mlbb@validateToken error: ' . $e->getMessage());
  120. return ['code' => -200, 'msg' => 'ERROR:'.$e->getMessage(), 'data' => []];
  121. }
  122. }
  123. /**
  124. * 渠道判断
  125. * @param $cahnnel_id string 渠道ID
  126. *
  127. * @return void
  128. * @throws \think\db\exception\DataNotFoundException
  129. * @throws \think\db\exception\ModelNotFoundException
  130. * @throws \think\exception\DbException
  131. */
  132. protected function judgmentChannel($cahnnel_id){
  133. $channelInfo = (new Channel())->where(['status' => 1, 'id' => $cahnnel_id])->field('id,name,level,status')->find();
  134. if (!$channelInfo) {
  135. $this->resultFail('当前渠道不存在或已禁用!');
  136. } else if ($channelInfo['level'] <> 3) {
  137. $this->resultFail('非推广员渠道,不能进行推广,请联系客服');
  138. } else if ($channelInfo['status'] <> 1) {
  139. $this->resultFail('当前推广员渠道已禁用,请联系客服');
  140. }
  141. }
  142. // 判断是否完成任务
  143. protected function getJudgmentTask(){
  144. if(!empty($this->userInfo['lr_info']['role_id'])){
  145. return false;
  146. }
  147. return true;
  148. }
  149. // 获取唯一推广码
  150. protected function getMarkCode()
  151. {
  152. do {
  153. $code = strtoupper(substr(md5(uniqid(mt_rand(), true)), 0, 6));
  154. $exists = Db::table('lr_user_gathering')->where(['code' => $code])->find();
  155. } while ($exists);
  156. return $code;
  157. }
  158. protected function resultSuccess($data = [], $msg = 'success', $code = 200){
  159. $result = [
  160. 'code' => $code,
  161. 'msg' => $msg,
  162. 'data' => $data
  163. ];
  164. log_message("Mlbb.request.success: " . json_encode($result), 'log', LOG_PATH . 'mlbb_log/');
  165. return json($result);
  166. }
  167. protected function resultFail($msg = 'fail', $code = -100, $data = []){
  168. $result = [
  169. 'code' => $code,
  170. 'msg' => $msg,
  171. 'data' => $data
  172. ];
  173. log_message("Mlbb.request.fail: " . json_encode($result), 'log', LOG_PATH . 'mlbb_log/');
  174. exit(json_encode($result));
  175. // return json($result);
  176. }
  177. }