更新个人中心
This commit is contained in:
parent
6f30eedf4a
commit
60252e26d5
@ -280,9 +280,30 @@ class WechatController extends BaseController
|
|||||||
Log::info('获取用户信息结果:' . json_encode($userInfo, JSON_UNESCAPED_UNICODE));
|
Log::info('获取用户信息结果:' . json_encode($userInfo, JSON_UNESCAPED_UNICODE));
|
||||||
|
|
||||||
if (isset($userInfo['openid'])) {
|
if (isset($userInfo['openid'])) {
|
||||||
// 保存用户信息到数据库
|
// 先检查是否存在该openid的用户
|
||||||
$user = Users::where('openid', $fromUsername)->find();
|
$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 = new Users;
|
||||||
$user->openid = $fromUsername;
|
$user->openid = $fromUsername;
|
||||||
// 生成默认账号
|
// 生成默认账号
|
||||||
@ -295,51 +316,12 @@ class WechatController extends BaseController
|
|||||||
$user->password = md5(uniqid() . rand(1000, 9999));
|
$user->password = md5(uniqid() . rand(1000, 9999));
|
||||||
$user->create_time = time(); // 设置创建时间
|
$user->create_time = time(); // 设置创建时间
|
||||||
$user->login_count = 1; // 首次登录,设置登录次数为1
|
$user->login_count = 1; // 首次登录,设置登录次数为1
|
||||||
|
|
||||||
if (!$user->save()) {
|
if (!$user->save()) {
|
||||||
Log::error('创建用户失败:' . json_encode($user->getError(), JSON_UNESCAPED_UNICODE));
|
Log::error('创建用户失败:' . json_encode($user->getError(), JSON_UNESCAPED_UNICODE));
|
||||||
return 'success';
|
return 'success';
|
||||||
}
|
}
|
||||||
// 重新获取用户信息以确保获取到uid
|
|
||||||
$user = Users::where('openid', $fromUsername)->find();
|
|
||||||
if (!$user) {
|
|
||||||
Log::error('获取新创建的用户信息失败');
|
|
||||||
return 'success';
|
|
||||||
}
|
|
||||||
Log::info('创建新用户成功:' . json_encode($user->toArray(), JSON_UNESCAPED_UNICODE));
|
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));
|
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
|
// 设置cookie
|
||||||
$expire = 7 * 24 * 3600; // 7天过期
|
$expire = 7 * 24 * 3600; // 7天过期
|
||||||
cookie('user_account', $user->account, ['expire' => $expire]);
|
cookie('user_account', $user->account, ['expire' => $expire]);
|
||||||
@ -375,6 +350,12 @@ class WechatController extends BaseController
|
|||||||
cookie('user_name', $user->name, ['expire' => $expire]);
|
cookie('user_name', $user->name, ['expire' => $expire]);
|
||||||
cookie('open_id', $user->openid, ['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([
|
Log::info('用户登录成功:' . json_encode([
|
||||||
'uid' => (int)$user->uid,
|
'uid' => (int)$user->uid,
|
||||||
'name' => $user->name,
|
'name' => $user->name,
|
||||||
@ -819,4 +800,51 @@ class WechatController extends BaseController
|
|||||||
return 'error';
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -17,8 +17,9 @@
|
|||||||
|
|
||||||
<div class="layui-form-item">
|
<div class="layui-form-item">
|
||||||
<label class="layui-form-label">账号</label>
|
<label class="layui-form-label">账号</label>
|
||||||
<div class="layui-input-block">
|
<div class="layui-input-block" style="display: flex; align-items: center;">
|
||||||
<input type="text" value="{$user.account}" class="layui-input" disabled>
|
<input type="text" value="{$user.account}" class="layui-input" disabled>
|
||||||
|
<button class="layui-btn" style="margin-left: 20px;">编辑</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
@ -3,7 +3,28 @@
|
|||||||
|
|
||||||
<div class="profile-container">
|
<div class="profile-container">
|
||||||
<div class="profile-sidebar">
|
<div class="profile-sidebar">
|
||||||
{include file="user/component/sidebar" /}
|
<div class="menu">
|
||||||
|
<div class="menu-item active" data-target="profile-basic">
|
||||||
|
<i class="layui-icon layui-icon-user"></i>
|
||||||
|
<span>基本资料</span>
|
||||||
|
</div>
|
||||||
|
<div class="menu-item" data-target="profile-wallet">
|
||||||
|
<i class="layui-icon layui-icon-wallet"></i>
|
||||||
|
<span>我的钱包</span>
|
||||||
|
</div>
|
||||||
|
<div class="menu-item" data-target="profile-messages">
|
||||||
|
<i class="layui-icon layui-icon-message"></i>
|
||||||
|
<span>我的消息</span>
|
||||||
|
</div>
|
||||||
|
<div class="menu-item" data-target="profile-notifications">
|
||||||
|
<i class="layui-icon layui-icon-notice"></i>
|
||||||
|
<span>系统通知</span>
|
||||||
|
</div>
|
||||||
|
<div class="menu-item" data-target="profile-security">
|
||||||
|
<i class="layui-icon layui-icon-password"></i>
|
||||||
|
<span>安全设置</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="profile-main">
|
<div class="profile-main">
|
||||||
@ -50,6 +71,38 @@
|
|||||||
flex-shrink: 0;
|
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 {
|
.profile-main {
|
||||||
flex: 1;
|
flex: 1;
|
||||||
background: #fff;
|
background: #fff;
|
||||||
@ -77,6 +130,23 @@
|
|||||||
width: 100%;
|
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 {
|
.profile-main {
|
||||||
padding: 20px;
|
padding: 20px;
|
||||||
}
|
}
|
||||||
@ -84,27 +154,22 @@
|
|||||||
</style>
|
</style>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
document.addEventListener('DOMContentLoaded', function () {
|
layui.use(['jquery'], function(){
|
||||||
const menuItems = document.querySelectorAll('.menu-item');
|
var $ = layui.jquery;
|
||||||
|
|
||||||
menuItems.forEach(item => {
|
// 菜单切换
|
||||||
item.addEventListener('click', function () {
|
$('.menu-item').on('click', function(){
|
||||||
const target = this.getAttribute('data-target');
|
var target = $(this).data('target');
|
||||||
|
|
||||||
// 移除所有active类
|
// 移除所有active类
|
||||||
document.querySelectorAll('.menu-item').forEach(menuItem => {
|
$('.menu-item').removeClass('active');
|
||||||
menuItem.classList.remove('active');
|
$('.content-section').removeClass('active');
|
||||||
});
|
|
||||||
document.querySelectorAll('.content-section').forEach(section => {
|
// 添加active类
|
||||||
section.classList.remove('active');
|
$(this).addClass('active');
|
||||||
});
|
$('#' + target).addClass('active');
|
||||||
|
|
||||||
// 添加active类
|
|
||||||
this.classList.add('active');
|
|
||||||
document.getElementById(target).classList.add('active');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
{include file="component/foot" /}
|
{include file="component/foot" /}
|
||||||
Loading…
x
Reference in New Issue
Block a user