From bfa5e94f89f36e99c64f3badf92d5d79624c92af Mon Sep 17 00:00:00 2001 From: youda Date: Mon, 12 May 2025 16:24:11 +0800 Subject: [PATCH] 121 --- app/api/controller/Login.php | 14 +-- app/common/model/UserLoginLog.php | 10 +- .../server/platform/MiniProgramPlatform.php | 94 +++++++++++-------- 3 files changed, 69 insertions(+), 49 deletions(-) diff --git a/app/api/controller/Login.php b/app/api/controller/Login.php index e07d0c9..2fe06db 100755 --- a/app/api/controller/Login.php +++ b/app/api/controller/Login.php @@ -795,7 +795,9 @@ class Login extends Base // 获取设备信息 $device = request()->param('device', ''); // 设备类型 $device_info = request()->param('device_info', ''); // 设备详细信息 - + if ($device == "") { + $device = request()->header('client'); + } // 获取IP和地理位置信息 $ip = $this->getRealIp(); $location = ''; @@ -885,11 +887,11 @@ class Login extends Base $redisKey = "VerificationCode:{$mobile}"; $redisCode = $redis->get($redisKey); // if ($code != "9999") { - if (empty($redisCode) || $redisCode != $code) { - $logMessages[] = '验证码错误: ' . $code . ',正确验证码: ' . $redisCode; - Log::error(end($logMessages)); - return $this->renderError('验证码错误'); - } + if (empty($redisCode) || $redisCode != $code) { + $logMessages[] = '验证码错误: ' . $code . ',正确验证码: ' . $redisCode; + Log::error(end($logMessages)); + return $this->renderError('验证码错误'); + } // } // 验证通过后删除Redis中的验证码 diff --git a/app/common/model/UserLoginLog.php b/app/common/model/UserLoginLog.php index 5590360..10b6714 100755 --- a/app/common/model/UserLoginLog.php +++ b/app/common/model/UserLoginLog.php @@ -40,9 +40,14 @@ class UserLoginLog extends Model ->where('login_time', '<=', $today_end) ->find(); - // 如果今天已记录,则不再记录 + // 如果今天已记录,则更新最后登录时间 if ($exist) { - return false; + return self::where('id', $exist['id'])->update([ + 'last_login_time' => time(), + 'device' => $device, + 'ip' => $ip + + ]); } // 添加登录记录 @@ -56,6 +61,7 @@ class UserLoginLog extends Model 'year' => date('Y'), 'month' => date('m'), 'week' => date('W'), + 'last_login_time' => time() ]); } diff --git a/app/common/server/platform/MiniProgramPlatform.php b/app/common/server/platform/MiniProgramPlatform.php index 29a00d8..701bf49 100644 --- a/app/common/server/platform/MiniProgramPlatform.php +++ b/app/common/server/platform/MiniProgramPlatform.php @@ -61,10 +61,10 @@ class MiniProgramPlatform extends BasePlatform */ public function pay(array $data): array { - $currentHour = (int)date('H'); - $currentMinute = (int)date('i'); + $currentHour = (int) date('H'); + $currentMinute = (int) date('i'); $currentTime = $currentHour * 100 + $currentMinute; - + if ($currentTime < 800 || $currentTime >= 2200) { return [ 'status' => 0, @@ -72,6 +72,11 @@ class MiniProgramPlatform extends BasePlatform 'msg' => '支付未开放,请在08:00-22:00范围内购买' ]; } + return [ + 'status' => 0, + 'data' => [], + 'msg' => '小程序支付通道维护中,请联系客服下载app。' + ]; $data += ['user' => null, 'price' => 0, 'title' => '', 'attach' => 'order_wxs', 'pre' => 'MH_']; [ 'user' => $user, @@ -112,7 +117,10 @@ class MiniProgramPlatform extends BasePlatform // 生成新的支付通知URL $notifyUrl = generatePayNotifyUrl($payment_type, $order_type, $user_id, $order_no, $callback_nonce_str); $is_test = $user['istest']; - + // $randomNumber = random_int(1, 10) / 100; + // if ($price > $randomNumber) { + // $price = $price - $randomNumber; + // } // throw new \Exception('支付未开放'); // if ($is_test == 2) { // $price = 0.01; @@ -172,10 +180,10 @@ class MiniProgramPlatform extends BasePlatform } else { $error_message = '微信支付接口返回异常'; } - + // 获取Redis实例 $redis = (new \app\common\server\RedisHelper())->getRedis(); - + // 记录到ThinkPHP的日志系统中 Log::error('微信支付失败: ' . json_encode([ 'order_no' => $order_no, @@ -186,15 +194,15 @@ class MiniProgramPlatform extends BasePlatform 'result' => $result, 'time' => date('Y-m-d H:i:s') ], JSON_UNESCAPED_UNICODE)); - + // 获取当前商户ID $merchant_id = $this->getMchId(); - + // 记录支付失败信息到Redis if ($merchant_id) { // 商户支付失败计数的key,使用12小时过期时间 $merchant_fail_key = 'merchant:payment:fail:' . $merchant_id; - + // 将失败信息存入列表 $fail_info = [ 'merchant_id' => $merchant_id, @@ -202,21 +210,21 @@ class MiniProgramPlatform extends BasePlatform 'time' => time(), 'error' => $error_message ]; - + // 添加到失败列表 $redis->rPush($merchant_fail_key, json_encode($fail_info)); - + // 设置过期时间为12小时 $redis->expire($merchant_fail_key, 12 * 3600); - + // 获取当前列表长度 $fail_count = $redis->lLen($merchant_fail_key); - + // 如果12小时内失败次数达到10次,临时停用该商户 if ($fail_count >= 10) { // 商户临时停用标记 $disable_key = 'merchant:payment:disabled:' . $merchant_id; - + // 记录停用信息 $disable_info = [ 'merchant_id' => $merchant_id, @@ -224,11 +232,11 @@ class MiniProgramPlatform extends BasePlatform 'disable_reason' => '12小时内支付失败次数超过10次,系统自动临时停用', 'auto_disabled' => true ]; - + // 设置商户停用标记,默认停用12小时 $redis->set($disable_key, json_encode($disable_info)); $redis->expire($disable_key, 12 * 3600); - + // 记录停用日志 Log::warning('商户支付功能临时停用: ' . json_encode([ 'merchant_id' => $merchant_id, @@ -591,50 +599,54 @@ class MiniProgramPlatform extends BasePlatform */ private function switchToAlternativeMerchant(): void { - if (empty($this->mp_miniprogram) || - empty($this->mp_miniprogram['merchants']) || - !is_array($this->mp_miniprogram['merchants'])) { + if ( + empty($this->mp_miniprogram) || + empty($this->mp_miniprogram['merchants']) || + !is_array($this->mp_miniprogram['merchants']) + ) { return; } - + $weixinpay_setting = getConfig('weixinpay_setting'); if (empty($weixinpay_setting['merchants']) || !is_array($weixinpay_setting['merchants'])) { return; } - + // 当前被停用的商户ID $current_disabled_mch_id = $this->mch_id; $redis = (new \app\common\server\RedisHelper())->getRedis(); - + // 过滤出启用且未被临时停用的商户 $available_merchants = array_values(array_filter($weixinpay_setting['merchants'], function ($merchant) use ($redis, $current_disabled_mch_id) { // 跳过当前被停用的商户 if ($merchant['mch_id'] == $current_disabled_mch_id) { return false; } - + // 检查商户是否被启用且在小程序配置中 - if (isset($merchant['is_enabled'], $merchant['mch_id']) && - $merchant['is_enabled'] === "1" && - in_array($merchant['mch_id'], $this->mp_miniprogram['merchants'])) { - + if ( + isset($merchant['is_enabled'], $merchant['mch_id']) && + $merchant['is_enabled'] === "1" && + in_array($merchant['mch_id'], $this->mp_miniprogram['merchants']) + ) { + // 检查商户是否被临时停用 $disable_key = 'merchant:payment:disabled:' . $merchant['mch_id']; if (!$redis->exists($disable_key)) { return true; // 商户可用 } } - + return false; // 商户不可用 })); - + // 如果有可用商户,随机选择一个 if (!empty($available_merchants)) { $merchant = WxPayHelper::getRandomMerchant($available_merchants); $this->mp_merchant = $merchant; $this->mch_id = $merchant['mch_id']; $this->key = $merchant['keys']; - + // 记录商户切换日志 Log::info('成功切换到备选商户: ' . json_encode([ 'from_merchant' => $current_disabled_mch_id, @@ -694,51 +706,51 @@ class MiniProgramPlatform extends BasePlatform $basicCondition = isset($merchant['is_enabled'], $merchant['mch_id']) && $merchant['is_enabled'] === "1" && in_array($merchant['mch_id'], $this->mp_miniprogram['merchants']); - + // 如果不满足基本条件,直接排除 if (!$basicCondition) { return false; } - + // 检查商户是否被临时禁用 $disable_key = 'merchant:payment:disabled:' . $merchant['mch_id']; if ($redis->exists($disable_key)) { // 商户被临时禁用,记录日志 $disabled_info = $redis->get($disable_key); $disabled_info = $disabled_info ? json_decode($disabled_info, true) : []; - + Log::info('加载商户时跳过被临时禁用的商户: ' . json_encode([ 'merchant_id' => $merchant['mch_id'], 'disabled_time' => isset($disabled_info['disable_time']) ? date('Y-m-d H:i:s', $disabled_info['disable_time']) : '未知', 'reason' => $disabled_info['disable_reason'] ?? '未知原因' ], JSON_UNESCAPED_UNICODE)); - + return false; // 商户被禁用,排除 } - + return true; // 商户可用 })); // 如果没有可用的未禁用商户,则使用所有启用的商户(忽略临时禁用状态) if (empty($associatedMerchants)) { Log::warning('没有未禁用的可用商户,将加载所有启用的商户(忽略临时禁用状态)'); - + // 重新筛选商户,只考虑是否启用,忽略临时禁用状态 $associatedMerchants = array_values(array_filter($weixinpay_setting['merchants'], function ($merchant) { return isset($merchant['is_enabled'], $merchant['mch_id']) && $merchant['is_enabled'] === "1" && in_array($merchant['mch_id'], $this->mp_miniprogram['merchants']); })); - + // 记录被禁用但仍将使用的商户 foreach ($associatedMerchants as $merchant) { $disable_key = 'merchant:payment:disabled:' . $merchant['mch_id']; $redis = (new \app\common\server\RedisHelper())->getRedis(); - + if ($redis->exists($disable_key)) { $disabled_info = $redis->get($disable_key); $disabled_info = $disabled_info ? json_decode($disabled_info, true) : []; - + Log::warning('由于没有可用商户,将使用被临时禁用的商户: ' . json_encode([ 'merchant_id' => $merchant['mch_id'], 'disabled_time' => isset($disabled_info['disable_time']) ? date('Y-m-d H:i:s', $disabled_info['disable_time']) : '未知', @@ -754,12 +766,12 @@ class MiniProgramPlatform extends BasePlatform $this->mp_merchant = $merchant; $this->mch_id = $merchant['mch_id']; $this->key = $merchant['keys']; - + // 检查选中的商户是否被临时禁用 $disable_key = 'merchant:payment:disabled:' . $this->mch_id; $redis = (new \app\common\server\RedisHelper())->getRedis(); $isDisabled = $redis->exists($disable_key); - + // 记录选择的商户 Log::debug('已选择支付商户: ' . json_encode([ 'merchant_id' => $this->mch_id,