增加登录验证

This commit is contained in:
李志强 2026-02-26 11:47:46 +08:00
parent bfa5292962
commit f80e1f71d4
2 changed files with 104 additions and 90 deletions

View File

@ -13,15 +13,29 @@ use app\model\System\SystemSiteSettings;
class SiteSettingsController extends BaseController class SiteSettingsController extends BaseController
{ {
/** /**
* 获取所有站点设置列表 * 获取指定站点设置列表
* @return Json * @return Json
*/ */
public function getNormalInfos() public function getNormalInfos()
{ {
// 定义你需要的 label 列表
$targetLabels = [
'sitename',
'logo',
'logow',
'description',
'copyright',
'companyname',
'icp'
];
$siteSettings = SystemSiteSettings::where('delete_time', null) $siteSettings = SystemSiteSettings::where('delete_time', null)
->field('id, label, value, sort') ->whereIn('label', $targetLabels) // 仅筛选指定的 label
->order('id', 'asc') ->field('label, value')
->select(); ->select();
$this->logSuccess('站点设置管理', '查看基本信息配置', [], $this->getAdminUserInfo());
return json([ return json([
'code' => 200, 'code' => 200,
'msg' => '获取成功', 'msg' => '获取成功',
@ -36,65 +50,101 @@ class SiteSettingsController extends BaseController
public function saveNormalInfos() public function saveNormalInfos()
{ {
$params = $this->request->param(); $params = $this->request->param();
$rawData = isset($params['data']) ? json_decode($params['data'], true) : $params;
$allowedLabels = ['sitename', 'logo', 'logow', 'description', 'copyright', 'companyname', 'icp'];
if (!isset($params['data']) || empty($params['data'])) { foreach ($allowedLabels as $label) {
return json(['code' => 400, 'msg' => '数据不能为空', 'data' => null]); if (isset($rawData[$label])) {
} SystemSiteSettings::update(
['value' => (string)$rawData[$label]],
$data = json_decode($params['data'], true); ['label' => $label]
if (!is_array($data)) { );
return json(['code' => 400, 'msg' => '数据格式错误', 'data' => null]);
}
foreach ($data as $item) {
if (!isset($item['label']) || !isset($item['value'])) {
continue;
}
$label = $item['label'];
$value = $item['value'] ?? '';
$setting = SystemSiteSettings::where('label', $label)->find();
if ($setting) {
$setting->value = $value;
$setting->save();
} else {
SystemSiteSettings::create([
'label' => $label,
'value' => $value,
'create_time' => date('Y-m-d H:i:s')
]);
} }
} }
$allSettings = SystemSiteSettings::column('value', 'label'); $this->logSuccess('站点设置管理', '保存基本信息', ['labels' => $allowedLabels], $this->getAdminUserInfo());
$allSettings['type'] = 'normal';
$labels = array_column($data, 'label'); return json(['code' => 200, 'msg' => '基本信息保存成功']);
$userInfo = $this->getAdminUserInfo(); }
$this->logSuccess('站点设置管理', '保存基本信息', ['labels' => $labels], $userInfo);
/**
* 获取登录验证数据
* @return Json
*/
public function getVerifyInfos()
{
// 定义你需要的 label 列表
$targetLabels = [
'openVerify',
'verifyModel',
'geetestID',
'geetestKEY'
];
$legalInfos = SystemSiteSettings::where('delete_time', null)
->whereIn('label', $targetLabels) // 仅筛选指定的 label
->field('label, value')
->select();
$this->logSuccess('站点设置管理', '查看登录验证数据', [], $this->getAdminUserInfo());
return json([ return json([
'code' => 200, 'code' => 200,
'msg' => '保存成功', 'msg' => '获取成功',
'data' => $allSettings 'data' => $legalInfos->toArray()
]); ]);
} }
/** /**
* 获取所有站点设置列表 * 保存登录验证数据
* @return Json
*/
public function saveVerifyInfos()
{
$params = $this->request->param();
$allowedLabels = ['openVerify', 'verifyModel', 'geetestID', 'geetestKEY'];
foreach ($allowedLabels as $label) {
if (isset($params[$label])) {
$val = $params[$label];
if (is_bool($val)) {
$val = $val ? '1' : '0';
}
SystemSiteSettings::where('label', $label)->update([
'value' => (string)$val
]);
}
}
$this->logSuccess('站点设置管理', '保存登录验证配置', $params, $this->getAdminUserInfo());
return json(['code' => 200, 'msg' => '保存成功']);
}
/**
* 获取法律声明和隐私条款
* @return Json * @return Json
*/ */
public function getLegalInfos() public function getLegalInfos()
{ {
$siteSettings = SystemSiteSettings::where('delete_time', null) // 定义你需要的 label 列表
->field('id, label, value, sort') $targetLabels = [
->order('id', 'asc') 'legalNotice',
'privacyTerms'
];
$legalInfos = SystemSiteSettings::where('delete_time', null)
->whereIn('label', $targetLabels) // 仅筛选指定的 label
->field('label, value')
->select(); ->select();
$this->logSuccess('站点设置管理', '查看法律声明和隐私条款', [], $this->getAdminUserInfo());
return json([ return json([
'code' => 200, 'code' => 200,
'msg' => '获取成功', 'msg' => '获取成功',
'data' => $siteSettings->toArray() 'data' => $legalInfos->toArray()
]); ]);
} }
@ -105,58 +155,20 @@ class SiteSettingsController extends BaseController
public function saveLegalInfos() public function saveLegalInfos()
{ {
$params = $this->request->param(); $params = $this->request->param();
$rawData = isset($params['data']) ? json_decode($params['data'], true) : $params;
$allowedLabels = ['legalNotice', 'privacyTerms'];
if (!isset($params['data']) || empty($params['data'])) { foreach ($allowedLabels as $label) {
return json(['code' => 400, 'msg' => '数据不能为空', 'data' => null]); if (isset($rawData[$label])) {
} SystemSiteSettings::update(
['value' => (string)$rawData[$label]],
$data = json_decode($params['data'], true); ['label' => $label]
if (!is_array($data)) { );
return json(['code' => 400, 'msg' => '数据格式错误', 'data' => null]);
}
foreach ($data as $item) {
if (!isset($item['label']) || !isset($item['value'])) {
continue;
}
$label = $item['label'];
$value = $item['value'] ?? '';
$setting = SystemSiteSettings::where('label', $label)->find();
if ($setting) {
$setting->value = $value;
$setting->save();
} else {
SystemSiteSettings::create([
'label' => $label,
'value' => $value,
'create_time' => date('Y-m-d H:i:s')
]);
} }
} }
$this->logSuccess('站点设置管理', '保存法律声明和隐私条款', ['labels' => $allowedLabels], $this->getAdminUserInfo());
$allSettings = SystemSiteSettings::column('value', 'label'); return json(['code' => 200, 'msg' => '法律声明和隐私条款保存成功']);
$allSettings['type'] = 'legal_notice';
$labels = array_column($data, 'label');
$userId = Session::get('user_id', 0);
$userFromSession = Session::get('user', []);
$userFromCache = Cache::get('userInfo_' . $userId, []);
$account = Session::get('account', '');
$name = Session::get('name', '');
\think\facade\Log::record('SiteSettings Debug - user_id: ' . $userId . ', userFromSession: ' . json_encode($userFromSession) . ', userFromCache: ' . json_encode($userFromCache) . ', account: ' . $account . ', name: ' . $name);
$this->logSuccess('站点设置管理', '保存法律声明和隐私条款', ['labels' => $labels]);
return json([
'code' => 200,
'msg' => '保存成功',
'data' => $allSettings
]);
} }
} }

View File

@ -4,5 +4,7 @@ use think\facade\Route;
// 站点设置路由 // 站点设置路由
Route::get('normalInfos', 'app\\admin\\controller\\SiteSettingsController@getNormalInfos'); Route::get('normalInfos', 'app\\admin\\controller\\SiteSettingsController@getNormalInfos');
Route::post('saveNormalInfos', 'app\\admin\\controller\\SiteSettingsController@saveNormalInfos'); Route::post('saveNormalInfos', 'app\\admin\\controller\\SiteSettingsController@saveNormalInfos');
Route::get('loginVerifyInfos', 'app\\admin\\controller\\SiteSettingsController@getVerifyInfos');
Route::post('saveloginVerifyInfos', 'app\\admin\\controller\\SiteSettingsController@saveVerifyInfos');
Route::get('legalInfos', 'app\\admin\\controller\\SiteSettingsController@getLegalInfos'); Route::get('legalInfos', 'app\\admin\\controller\\SiteSettingsController@getLegalInfos');
Route::post('saveLegalInfos', 'app\\admin\\controller\\SiteSettingsController@saveLegalInfos'); Route::post('saveLegalInfos', 'app\\admin\\controller\\SiteSettingsController@saveLegalInfos');