163 lines
4.8 KiB
PHP
163 lines
4.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace app\admin\controller;
|
|
|
|
use app\admin\BaseController;
|
|
use think\exception\ValidateException;
|
|
use think\facade\Db;
|
|
use think\response\Json;
|
|
use app\model\SystemSiteSettings;
|
|
|
|
class SiteSettingsController extends BaseController
|
|
{
|
|
/**
|
|
* 获取所有站点设置列表
|
|
* @return Json
|
|
*/
|
|
public function getNormalInfos()
|
|
{
|
|
$siteSettings = SystemSiteSettings::where('delete_time', null)
|
|
->field('id, label, value, sort')
|
|
->order('id', 'asc')
|
|
->select();
|
|
return json([
|
|
'code' => 200,
|
|
'msg' => '获取成功',
|
|
'data' => $siteSettings->toArray()
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 保存基本信息
|
|
* @return Json
|
|
*/
|
|
public function saveNormalInfos()
|
|
{
|
|
$params = $this->request->param();
|
|
|
|
if (!isset($params['data']) || empty($params['data'])) {
|
|
return json(['code' => 400, 'msg' => '数据不能为空', 'data' => null]);
|
|
}
|
|
|
|
$data = json_decode($params['data'], true);
|
|
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');
|
|
$allSettings['type'] = 'normal';
|
|
|
|
$labels = array_column($data, 'label');
|
|
$userInfo = $this->getAdminUserInfo();
|
|
$this->logSuccess('站点设置管理', '保存基本信息', ['labels' => $labels], $userInfo);
|
|
|
|
return json([
|
|
'code' => 200,
|
|
'msg' => '保存成功',
|
|
'data' => $allSettings
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 获取所有站点设置列表
|
|
* @return Json
|
|
*/
|
|
public function getLegalInfos()
|
|
{
|
|
$siteSettings = SystemSiteSettings::where('delete_time', null)
|
|
->field('id, label, value, sort')
|
|
->order('id', 'asc')
|
|
->select();
|
|
return json([
|
|
'code' => 200,
|
|
'msg' => '获取成功',
|
|
'data' => $siteSettings->toArray()
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 保存法律声明和隐私条款
|
|
* @return Json
|
|
*/
|
|
public function saveLegalInfos()
|
|
{
|
|
$params = $this->request->param();
|
|
|
|
if (!isset($params['data']) || empty($params['data'])) {
|
|
return json(['code' => 400, 'msg' => '数据不能为空', 'data' => null]);
|
|
}
|
|
|
|
$data = json_decode($params['data'], true);
|
|
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');
|
|
$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
|
|
]);
|
|
}
|
|
|
|
}
|