更新个人中心
This commit is contained in:
@@ -280,9 +280,30 @@ class WechatController extends BaseController
|
||||
Log::info('获取用户信息结果:' . json_encode($userInfo, JSON_UNESCAPED_UNICODE));
|
||||
|
||||
if (isset($userInfo['openid'])) {
|
||||
// 保存用户信息到数据库
|
||||
// 先检查是否存在该openid的用户
|
||||
$user = Users::where('openid', $fromUsername)->find();
|
||||
if (!$user) {
|
||||
|
||||
if ($user) {
|
||||
// 已存在用户,直接登录
|
||||
$user->login_count = $user->login_count + 1; // 增加登录次数
|
||||
$user->update_time = time(); // 更新登录时间
|
||||
|
||||
// 只在头像为空或为默认头像时更新
|
||||
if (empty($user->avatar) || $user->avatar === '/static/images/avatar.png') {
|
||||
if (!empty($userInfo['headimgurl'])) {
|
||||
$user->avatar = $userInfo['headimgurl'];
|
||||
} elseif (empty($user->avatar)) {
|
||||
$user->avatar = '/static/images/avatar.png';
|
||||
}
|
||||
}
|
||||
|
||||
if (!$user->save()) {
|
||||
Log::error('更新用户登录信息失败:' . json_encode($user->getError(), JSON_UNESCAPED_UNICODE));
|
||||
return 'success';
|
||||
}
|
||||
Log::info('用户登录成功:' . json_encode($user->toArray(), JSON_UNESCAPED_UNICODE));
|
||||
} else {
|
||||
// 不存在用户,创建新用户
|
||||
$user = new Users;
|
||||
$user->openid = $fromUsername;
|
||||
// 生成默认账号
|
||||
@@ -295,51 +316,12 @@ class WechatController extends BaseController
|
||||
$user->password = md5(uniqid() . rand(1000, 9999));
|
||||
$user->create_time = time(); // 设置创建时间
|
||||
$user->login_count = 1; // 首次登录,设置登录次数为1
|
||||
|
||||
if (!$user->save()) {
|
||||
Log::error('创建用户失败:' . json_encode($user->getError(), JSON_UNESCAPED_UNICODE));
|
||||
return 'success';
|
||||
}
|
||||
// 重新获取用户信息以确保获取到uid
|
||||
$user = Users::where('openid', $fromUsername)->find();
|
||||
if (!$user) {
|
||||
Log::error('获取新创建的用户信息失败');
|
||||
return 'success';
|
||||
}
|
||||
Log::info('创建新用户成功:' . json_encode($user->toArray(), JSON_UNESCAPED_UNICODE));
|
||||
} else {
|
||||
// 更新用户信息
|
||||
$needUpdate = false;
|
||||
|
||||
// 只在用户名为空时设置
|
||||
if (empty($user->name)) {
|
||||
$user->name = $user->account;
|
||||
$needUpdate = true;
|
||||
}
|
||||
|
||||
// 只在头像为空或为默认头像时更新
|
||||
if (empty($user->avatar) || $user->avatar === '/static/images/avatar.png') {
|
||||
if (!empty($userInfo['headimgurl'])) {
|
||||
$user->avatar = $userInfo['headimgurl'];
|
||||
$needUpdate = true;
|
||||
} elseif (empty($user->avatar)) {
|
||||
$user->avatar = '/static/images/avatar.png';
|
||||
$needUpdate = true;
|
||||
}
|
||||
}
|
||||
|
||||
// 增加登录次数
|
||||
$user->login_count = $user->login_count + 1;
|
||||
$needUpdate = true;
|
||||
|
||||
// 只在需要更新时才更新时间戳
|
||||
if ($needUpdate) {
|
||||
$user->update_time = time();
|
||||
if (!$user->save()) {
|
||||
Log::error('更新用户信息失败:' . json_encode($user->getError(), JSON_UNESCAPED_UNICODE));
|
||||
return 'success';
|
||||
}
|
||||
Log::info('更新用户信息成功:' . json_encode($user->toArray(), JSON_UNESCAPED_UNICODE));
|
||||
}
|
||||
}
|
||||
|
||||
// 更新票据文件
|
||||
@@ -361,13 +343,6 @@ class WechatController extends BaseController
|
||||
Log::info('更新票据文件成功:' . json_encode($data, JSON_UNESCAPED_UNICODE));
|
||||
}
|
||||
|
||||
// 设置登录session
|
||||
session('user_id', (int)$user->uid); // 确保session中的uid是整数
|
||||
session('user_name', $user->name);
|
||||
session('user_avatar', $user->avatar ?: '/static/images/avatar.png');
|
||||
session('user_account', $user->account);
|
||||
session('openid', $user->openid);
|
||||
|
||||
// 设置cookie
|
||||
$expire = 7 * 24 * 3600; // 7天过期
|
||||
cookie('user_account', $user->account, ['expire' => $expire]);
|
||||
@@ -375,6 +350,12 @@ class WechatController extends BaseController
|
||||
cookie('user_name', $user->name, ['expire' => $expire]);
|
||||
cookie('open_id', $user->openid, ['expire' => $expire]);
|
||||
|
||||
// 发送登录成功消息
|
||||
$messageContent = [
|
||||
'content' => "您好!\n您已成功登录系统。\n登录时间:" . date('Y-m-d H:i:s')
|
||||
];
|
||||
$this->sendCustomMessage($fromUsername, 'text', $messageContent);
|
||||
|
||||
Log::info('用户登录成功:' . json_encode([
|
||||
'uid' => (int)$user->uid,
|
||||
'name' => $user->name,
|
||||
@@ -819,4 +800,51 @@ class WechatController extends BaseController
|
||||
return 'error';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送客服消息
|
||||
* @param string $openid 接收者openid
|
||||
* @param string $type 消息类型
|
||||
* @param array $content 消息内容
|
||||
* @return bool
|
||||
*/
|
||||
private function sendCustomMessage($openid, $type = 'text', $content = [])
|
||||
{
|
||||
try {
|
||||
$accessToken = $this->getGZHAccessToken();
|
||||
$url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={$accessToken}";
|
||||
|
||||
$data = [
|
||||
'touser' => $openid,
|
||||
'msgtype' => $type
|
||||
];
|
||||
|
||||
if ($type === 'text') {
|
||||
$data['text'] = ['content' => $content['content'] ?? '登录成功'];
|
||||
}
|
||||
|
||||
$jsonData = json_encode($data, JSON_UNESCAPED_UNICODE);
|
||||
|
||||
$client = new Client(['verify' => false]);
|
||||
$response = $client->post($url, [
|
||||
'body' => $jsonData,
|
||||
'headers' => [
|
||||
'Content-Type' => 'application/json; charset=utf-8'
|
||||
]
|
||||
]);
|
||||
|
||||
$result = json_decode($response->getBody(), true);
|
||||
|
||||
if (isset($result['errcode']) && $result['errcode'] == 0) {
|
||||
Log::info('发送客服消息成功:' . json_encode($result, JSON_UNESCAPED_UNICODE));
|
||||
return true;
|
||||
} else {
|
||||
Log::error('发送客服消息失败:' . json_encode($result, JSON_UNESCAPED_UNICODE));
|
||||
return false;
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
Log::error('发送客服消息异常:' . $e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user