diff --git a/app/api/controller/Login.php b/app/api/controller/Login.php index 2ece308..bf0e17c 100755 --- a/app/api/controller/Login.php +++ b/app/api/controller/Login.php @@ -183,18 +183,12 @@ class Login extends Base */ public function login() { - // 初始化日志收集变量 - $logMessages = []; try { $code = request()->param("code", ''); if (empty($code)) { - $logMessages[] = '用户未获取到code:' . $code; - Log::error(end($logMessages)); return $this->renderError('请求参数错误'); } - - $logMessages[] = '用户开始登录: ' . $code; $click_id = request()->header('clickid'); $wxServer = new \app\common\server\Wx($this->app); $user_base = $wxServer->getOpenid($code); @@ -247,7 +241,6 @@ class Login extends Base } else { } - // 检查UserAccount是否存在 $userAccount = UserAccount::where(['user_id' => $user['id']])->find(); if ($userAccount) { @@ -279,6 +272,7 @@ class Login extends Base ]); } + // 记录用户登录日志(每天只记录一次) UserLoginLog::recordLogin( $user['id'], @@ -287,35 +281,32 @@ class Login extends Base ''//$ip_province . $ip_city ); - $logMessages[] = '用户登录成功: ' . $code . ' 用户ID: ' . $user['id'] . '用户手机号' . $user['mobile']; - // 输出收集的所有日志 - Log::info(implode("==》", $logMessages)); - return $this->renderSuccess("登录成功", $account_token); } else { $nickname = request()->param('nickname', ''); $headimg = request()->param('headimg', ''); - // if (!$nickname) { - // return $this->renderError('请求参数错误!'); - // } + if (!$nickname) { + return $this->renderError('请求参数错误!'); + } $pid = 0; $pid_pid = request()->param('pid', 0); - + if ($pid_pid > 0) { + log::info("获取推荐人id" . $pid_pid); + } $randx = rand(1000, 9999); - $nickname = "微信用户" . $randx; - $logMessages[] = $nickname; - $randx = rand(10000, 99999); + if ($nickname == "微信用户") { + $nickname = $nickname . $randx; + } $identicon = new \Identicon\Identicon(); $imageData = $identicon->getImageData($openid . $nickname); $uploadResult = $this->uploader->uploadFile($imageData, "storage/users/icon/default/" . $randx . ".png"); $headimg = $uploadResult['full_url']; if ($pid_pid) { - $logMessages[] = "尝试获取推荐人ID: " . $pid_pid; $pid_info = User::where('id', '=', $pid_pid)->value("id"); if ($pid_info) { - $logMessages[] = "获取推荐人ID成功: " . $pid_info; + log::info("获取推荐人id" . $pid_info); $pid = $pid_info; } } @@ -348,10 +339,12 @@ class Login extends Base $redis = (new RedisHelper())->getRedis(); $lockKey = 'user:beta_reward:' . $user_id; if ($redis->set($lockKey, 1, ['nx', 'ex' => 60])) { - $res[] = User::changeMoney($user_id, 50000, 8, '内测免费送'); - $logMessages[] = '赠送钻石: 50000'; - // 释放锁 - $redis->del($lockKey); + try { + $res[] = User::changeMoney($user_id, 50000, 8, '内测免费送'); + } finally { + // 释放锁 + $redis->del($lockKey); + } } } } @@ -408,22 +401,12 @@ class Login extends Base 'wechat:v1.0.0', $last_login_ip1, ''//$ip_province . $ip_city - ''//$ip_province . $ip_city ); - $logMessages[] = '==》用户注册成功: ' . $code . ' 用户ID: ' . $user_id; - // 输出收集的所有日志 - Log::info(implode("==>", $logMessages)); - Db::commit(); return $this->renderSuccess("登录成功", $account_token); } else { Db::rollback(); - - $logMessages[] = '==》用户注册失败: ' . $code . ' 用户ID: ' . $user_id; - // 输出收集的所有日志 - Log::info(implode("==>", $logMessages)); - return $this->renderError("登录失败"); } } @@ -440,24 +423,13 @@ class Login extends Base */ public function h5login() { - // 获取请求参数 - $code = request()->param("code", ''); - if (empty($code)) { - return $this->renderError('请求参数错误'); - } - $click_id = request()->header('clickid'); - - // 使用Redis全局锁防止并发请求 - $redis = (new RedisHelper())->getRedis(); - $lockKey = 'global:h5login:lock:' . md5($code . $click_id); - - // 尝试获取锁,设置过期时间为30秒 - if (!$redis->set($lockKey, 1, ['nx', 'ex' => 30])) { - // 如果获取锁失败,表示有并发请求正在处理 - return $this->renderError('登录请求处理中,请稍后再试'); - } try { + $code = request()->param("code", ''); + if (empty($code)) { + return $this->renderError('请求参数错误'); + } + $click_id = request()->header('clickid'); $wxServer = new \app\common\server\WechatOfficialAccount($this->app); $user_base = $wxServer->getAccessToken($code); // $user_base_info = $wxServer->getUserInfo($user_base); @@ -508,37 +480,17 @@ class Login extends Base } - - // 检查UserAccount是否存在 - $userAccount = UserAccount::where(['user_id' => $user['id']])->find(); - if ($userAccount) { - // 存在则更新 - $res[] = UserAccount::where(['user_id' => $user['id']])->update([ - 'account_token' => $account_token, - 'token_num' => $token_num, - 'token_time' => $time, - 'last_login_time' => $time, - 'last_login_ip' => ip2long($last_login_ip1), - 'last_login_ip1' => $last_login_ip1, - 'ip_adcode' => $ip_adcode, - 'ip_province' => $ip_province, - 'ip_city' => $ip_city, - ]); - } else { - // 不存在则新增 - $res[] = UserAccount::insert([ - 'user_id' => $user['id'], - 'account_token' => $account_token, - 'token_num' => $token_num, - 'token_time' => $time, - 'last_login_time' => $time, - 'last_login_ip' => ip2long($last_login_ip1), - 'last_login_ip1' => $last_login_ip1, - 'ip_adcode' => $ip_adcode, - 'ip_province' => $ip_province, - 'ip_city' => $ip_city, - ]); - } + $res[] = UserAccount::where(['user_id' => $user['id']])->update([ + 'account_token' => $account_token, + 'token_num' => $token_num, + 'token_time' => $time, + 'last_login_time' => $time, + 'last_login_ip' => ip2long($last_login_ip1), + 'last_login_ip1' => $last_login_ip1, + 'ip_adcode' => $ip_adcode, + 'ip_province' => $ip_province, + 'ip_city' => $ip_city, + ]); // 记录用户登录日志(每天只记录一次) UserLoginLog::recordLogin( @@ -583,13 +535,6 @@ class Login extends Base 'click_id' => $click_id, 'uid' => '', ]); - - // 生成用户uid - $uid = $this->generateUid($user_id); - if ($uid) { - User::where('id', $user_id)->update(['uid' => $uid]); - } - $time = time(); #token字符串 $token_num = getRandStr(10); @@ -656,9 +601,6 @@ class Login extends Base Log::error('错误信息' . $e->getMessage()); Log::error('错误行数' . $e->getLine()); return $this->renderError('非法请求'); - } finally { - // 确保释放锁 - $redis->del($lockKey); } } @@ -691,89 +633,62 @@ class Login extends Base public function login_bind_mobile() { $user = $this->getUser(); - if (empty($user)) { - return $this->renderError('用户不存在'); - } - $user_id = $user['id']; $code = request()->param("code", ''); - if (empty($code)) { - return $this->renderError('参数错误,缺少code'); - } - - // 使用Redis全局锁防止并发绑定请求 - $redis = (new RedisHelper())->getRedis(); - $lockKey = 'global:bind_mobile:lock:' . $user_id; - - // 尝试获取锁,设置过期时间为20秒 - if (!$redis->set($lockKey, 1, ['nx', 'ex' => 20])) { - // 如果获取锁失败,表示有并发请求正在处理 - return $this->renderError('绑定请求处理中,请稍后再试'); - } - - try { - $wxServer = new \app\common\server\Wx($this->app); - $mobile = $wxServer->getMobile($code); - // return $this->renderError($mobile,[$mobile,$code]); - Db::startTrans(); - $res = []; - // $res[] = User::where(['id' => $user['id']])->update([ - // 'mobile' => $mobile, - // 'update_time' => time(), + $wxServer = new \app\common\server\Wx($this->app); + $mobile = $wxServer->getMobile($code); + // return $this->renderError($mobile,[$mobile,$code]); + Db::startTrans(); + $res = []; + // $res[] = User::where(['id' => $user['id']])->update([ + // 'mobile' => $mobile, + // 'update_time' => time(), + // ]); + $data = []; + $user_mobile = User::where(['mobile' => $mobile])->find(); + if ($user_mobile) { + $old_user_account = UserAccount::where(['user_id' => $user_id])->find(); + #修改openid + $res[] = User::where(['id' => $user_mobile['id']]) + ->update([ + 'openid' => $user['openid'], + // 'nickname' => $user['nickname'], + // 'headimg' => $user['headimg'], + ]); + $time = time(); + #token字符串 + $token_num = getRandStr(10); + #加密token + $account_token = user_md5($user_mobile['id'] . $token_num . $time); + #修改token + $res[] = UserAccount::where(['user_id' => $user_mobile['id']])->update([ + 'account_token' => $account_token, + 'token_num' => $token_num, + 'token_time' => $time, + 'last_login_time' => $old_user_account['last_login_time'], + 'last_login_ip' => $old_user_account['last_login_ip'], + ]); + #修改 + $res[] = User::where(['id' => $user['id']])->delete(); + $res[] = UserAccount::where(['user_id' => $user_id])->delete(); + $data['token'] = $account_token; + // $res[] = UserAccount::where(['user_id' => $user['id']])->update([ + // 'token_time' => time(), // ]); - $data = []; - $user_mobile = User::where(['mobile' => $mobile])->find(); - if ($user_mobile) { - $old_user_account = UserAccount::where(['user_id' => $user_id])->find(); - #修改openid - $res[] = User::where(['id' => $user_mobile['id']]) - ->update([ - 'openid' => $user['openid'], - // 'nickname' => $user['nickname'], - // 'headimg' => $user['headimg'], - ]); - $time = time(); - #token字符串 - $token_num = getRandStr(10); - #加密token - $account_token = user_md5($user_mobile['id'] . $token_num . $time); - #修改token - $res[] = UserAccount::where(['user_id' => $user_mobile['id']])->update([ - 'account_token' => $account_token, - 'token_num' => $token_num, - 'token_time' => $time, - 'last_login_time' => $old_user_account['last_login_time'], - 'last_login_ip' => $old_user_account['last_login_ip'], - ]); - #修改 - $res[] = User::where(['id' => $user['id']])->delete(); - $res[] = UserAccount::where(['user_id' => $user_id])->delete(); - $data['token'] = $account_token; - // $res[] = UserAccount::where(['user_id' => $user['id']])->update([ - // 'token_time' => time(), - // ]); - } else { - $res[] = User::where(['id' => $user['id']])->update([ - 'mobile' => $mobile, - 'update_time' => time(), - ]); - } - if (resCheck($res)) { - Db::commit(); - return $this->renderSuccess("绑定成功", $data); - } else { - Db::rollback(); - return $this->renderError("绑定失败"); - } - } catch (\Exception $e) { - Db::rollback(); - Log::error('绑定手机号错误: ' . $e->getMessage()); - Log::error('错误行数: ' . $e->getLine()); - return $this->renderError('绑定失败: ' . $e->getMessage()); - } finally { - // 确保释放锁 - $redis->del($lockKey); + } else { + $res[] = User::where(['id' => $user['id']])->update([ + 'mobile' => $mobile, + 'update_time' => time(), + ]); } + if (resCheck($res)) { + Db::commit(); + return $this->renderSuccess("绑定成功2", $data); + } else { + Db::rollback(); + return $this->renderSuccess("绑定成功3"); + } + } @@ -783,49 +698,22 @@ class Login extends Base public function login_bind_mobile_h5() { $user = $this->getUser(); - if (empty($user)) { - return $this->renderError('用户不存在'); - } - $user_id = $user['id']; $mobile = request()->param("mobile", ''); - if (empty($mobile)) { - return $this->renderError('请输入手机号'); - } - - // 使用Redis全局锁防止并发绑定请求 - $redis = (new RedisHelper())->getRedis(); - $lockKey = 'global:bind_mobile_h5:lock:' . $user_id; - - // 尝试获取锁,设置过期时间为10秒 - if (!$redis->set($lockKey, 1, ['nx', 'ex' => 10])) { - // 如果获取锁失败,表示有并发请求正在处理 - return $this->renderError('绑定请求处理中,请稍后再试'); - } - - try { - Db::startTrans(); - $res = []; - $res[] = User::where(['id' => $user_id])->update([ - 'mobile' => $mobile, - 'update_time' => time(), - ]); - if (resCheck($res)) { - Db::commit(); - return $this->renderSuccess("绑定成功"); - } else { - Db::rollback(); - return $this->renderError("绑定失败"); - } - } catch (\Exception $e) { + Db::startTrans(); + $res = []; + $res[] = User::where(['id' => $user_id])->update([ + 'mobile' => $mobile, + 'update_time' => time(), + ]); + if (resCheck($res)) { + Db::commit(); + return $this->renderSuccess("绑定成功2"); + } else { Db::rollback(); - Log::error('绑定手机号H5错误: ' . $e->getMessage()); - Log::error('错误行数: ' . $e->getLine()); - return $this->renderError('绑定失败: ' . $e->getMessage()); - } finally { - // 确保释放锁 - $redis->del($lockKey); + return $this->renderSuccess("绑定成功3"); } + } /** @@ -850,46 +738,37 @@ class Login extends Base public function recordLogin() { try { - // 获取用户信息 + // $user_id = $this->getUserid1(); + // //去redis中查询一下,如果存在今日的数据,则不在往下执行 + // $redis = (new RedisHelper())->getRedis(); + // $today = date('Y-m-d'); + // $redis_key = "login_record:" . $today . ":" . $user_id; + // $redis_data = $redis->get($redis_key); + // if ($redis_data) { + // return $this->renderSuccess('登录记录成功'); + // } $user = $this->getUser(); if (empty($user)) { return $this->renderError('用户不存在'); } - $user_id = $user['id']; + // 获取设备信息 + $device = request()->param('device', ''); // 设备类型 + $device_info = request()->param('device_info', ''); // 设备详细信息 - // 使用Redis全局锁防止并发请求 - $redis = (new RedisHelper())->getRedis(); - $lockKey = 'global:record_login:lock:' . $user_id; - - // 尝试获取锁,设置过期时间为10秒 - if (!$redis->set($lockKey, 1, ['nx', 'ex' => 10])) { - // 如果获取锁失败,直接返回成功,避免用户等待 - return $this->renderSuccess('登录成功', [ - 'uid' => $user['uid'] ?: $user['id'], - 'nickname' => $user['nickname'], - 'headimg' => imageUrl($user['headimg']) - ]); - } - - try { - // 获取设备信息 - $device = request()->param('device', ''); // 设备类型 - $device_info = request()->param('device_info', ''); // 设备详细信息 - - // 获取IP和地理位置信息 - $ip = $this->getRealIp(); - $location = ''; - $ip_province = ''; - $ip_city = ''; - $ip_adcode = ''; + // 获取IP和地理位置信息 + $ip = $this->getRealIp(); + $location = ''; + $ip_province = ''; + $ip_city = ''; + $ip_adcode = ''; $result = $this->ip_location($ip); if ($result) { $ip_province = $result['province']; $ip_city = $result['city']; $ip_adcode = $result['adcode']; - $location ="";// $ip_province . $ip_city; + $location = "";// $ip_province . $ip_city; } $user_id = $user['id']; $isTest = \app\common\helper\ConfigHelper::getSystemTestKey("enable_test"); @@ -907,30 +786,29 @@ class Login extends Base try { $res[] = User::changeMoney($user_id, 50000, 8, '内测免费送'); } finally { - // 释放内测奖励锁 - $redis->del($bonusLockKey); + // 释放锁 + $redis->del($lockKey); } } } - - // 记录登录日志 - UserLoginLog::recordLogin( - $user['id'], - $device, - $ip, - $location - ); - - return $this->renderSuccess('登录成功', [ - 'uid' => $user['uid'] ?: $user['id'], - 'nickname' => $user['nickname'], - 'headimg' => imageUrl($user['headimg']) - ]); - } finally { - // 确保释放锁 - $redis->del($lockKey); } + // 记录登录日志 + UserLoginLog::recordLogin( + $user['id'], + $device, + $ip, + $location + ); + // //将数据写入redis,过期时间为当天剩余的时间 + // $redis->set($redis_key, json_encode($user), 86400 - time() % 86400); + return $this->renderSuccess('登录成功', [ + 'uid' => $user['uid'] ?: $user['id'], + 'nickname' => $user['nickname'], + 'headimg' => imageUrl($user['headimg']) + ]); + + } catch (\Exception $e) { Log::error('记录登录错误: ' . $e->getMessage()); Log::error('错误行数: ' . $e->getLine());