提交代码

This commit is contained in:
youda 2025-04-19 16:25:02 +08:00
parent 037725180f
commit 276be1d6d8

View File

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