diff --git a/app/index/controller/WechatController.php b/app/index/controller/WechatController.php index 31c6091..d517b2c 100644 --- a/app/index/controller/WechatController.php +++ b/app/index/controller/WechatController.php @@ -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; + } + } } \ No newline at end of file diff --git a/app/index/view/user/component/basic.php b/app/index/view/user/component/basic.php index 15abef1..46425d1 100644 --- a/app/index/view/user/component/basic.php +++ b/app/index/view/user/component/basic.php @@ -17,8 +17,9 @@
-
+
+
diff --git a/app/index/view/user/profile.php b/app/index/view/user/profile.php index 981a179..e2c894f 100644 --- a/app/index/view/user/profile.php +++ b/app/index/view/user/profile.php @@ -3,7 +3,28 @@
- {include file="user/component/sidebar" /} +
@@ -50,6 +71,38 @@ flex-shrink: 0; } + .menu { + background: #fff; + border-radius: 12px; + box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08); + padding: 16px 0; + } + + .menu-item { + padding: 16px 24px; + cursor: pointer; + display: flex; + align-items: center; + gap: 12px; + color: #666; + transition: all 0.3s; + } + + .menu-item:hover { + color: #1677ff; + background: #f5f5f5; + } + + .menu-item.active { + color: #1677ff; + background: #e6f4ff; + border-right: 3px solid #1677ff; + } + + .menu-item .layui-icon { + font-size: 18px; + } + .profile-main { flex: 1; background: #fff; @@ -77,6 +130,23 @@ width: 100%; } + .menu { + display: flex; + overflow-x: auto; + padding: 8px; + } + + .menu-item { + flex-shrink: 0; + border-right: none; + border-bottom: 3px solid transparent; + } + + .menu-item.active { + border-right: none; + border-bottom: 3px solid #1677ff; + } + .profile-main { padding: 20px; } @@ -84,27 +154,22 @@ {include file="component/foot" /} \ No newline at end of file