131 lines
4.6 KiB
PHP
131 lines
4.6 KiB
PHP
<?php
|
|
|
|
namespace app\controller\app;
|
|
|
|
use app\support\ContentService;
|
|
use support\Request;
|
|
use support\think\Db;
|
|
|
|
class SettingsController
|
|
{
|
|
public function detail(Request $request)
|
|
{
|
|
$userId = app_user_id($request);
|
|
$user = Db::name('users')->where('id', $userId)->find();
|
|
if (!$user) {
|
|
return api_error('用户不存在', 404);
|
|
}
|
|
|
|
return api_success($this->buildPayload($user, $userId));
|
|
}
|
|
|
|
public function save(Request $request)
|
|
{
|
|
$userId = app_user_id($request);
|
|
$user = Db::name('users')->where('id', $userId)->find();
|
|
if (!$user) {
|
|
return api_error('用户不存在', 404);
|
|
}
|
|
|
|
$nickname = trim((string)$request->input('nickname', $user['nickname']));
|
|
if ($nickname === '') {
|
|
return api_error('昵称不能为空', 422);
|
|
}
|
|
|
|
$preferenceInput = (array)$request->input('preferences', []);
|
|
$preferences = $this->normalizePreferences($preferenceInput);
|
|
$now = date('Y-m-d H:i:s');
|
|
|
|
Db::startTrans();
|
|
try {
|
|
Db::name('users')->where('id', $userId)->update([
|
|
'nickname' => $nickname,
|
|
'updated_at' => $now,
|
|
]);
|
|
|
|
$config = Db::name('system_configs')
|
|
->where('config_group', 'user_settings')
|
|
->where('config_key', $this->preferenceConfigKey($userId))
|
|
->find();
|
|
|
|
$payload = [
|
|
'config_group' => 'user_settings',
|
|
'config_key' => $this->preferenceConfigKey($userId),
|
|
'config_value' => json_encode($preferences, JSON_UNESCAPED_UNICODE),
|
|
'remark' => '用户端设置偏好',
|
|
'updated_at' => $now,
|
|
];
|
|
|
|
if ($config) {
|
|
Db::name('system_configs')->where('id', $config['id'])->update($payload);
|
|
} else {
|
|
$payload['created_at'] = $now;
|
|
Db::name('system_configs')->insert($payload);
|
|
}
|
|
|
|
Db::commit();
|
|
} catch (\Throwable $e) {
|
|
Db::rollback();
|
|
return api_error('设置保存失败', 500, [
|
|
'detail' => $e->getMessage(),
|
|
]);
|
|
}
|
|
|
|
$latestUser = Db::name('users')->where('id', $userId)->find();
|
|
|
|
return api_success($this->buildPayload($latestUser ?: $user, $userId), '设置已保存');
|
|
}
|
|
|
|
private function buildPayload(array $user, int $userId): array
|
|
{
|
|
$content = (new ContentService())->getPolicyConfig();
|
|
|
|
return [
|
|
'profile_info' => [
|
|
'user_id' => (int)$user['id'],
|
|
'nickname' => $user['nickname'] ?: '安心验用户',
|
|
'mobile' => $user['mobile'] ?: '',
|
|
'avatar' => $user['avatar'] ?: '',
|
|
'status' => $user['status'] ?: 'enabled',
|
|
'status_text' => ($user['status'] ?? 'enabled') === 'enabled' ? '账号正常' : '账号异常',
|
|
'password_set' => ((string)($user['password'] ?? '')) !== '',
|
|
],
|
|
'preferences' => $this->loadPreferences($userId),
|
|
'legal_entries' => $content['legal_entries'],
|
|
];
|
|
}
|
|
|
|
private function loadPreferences(int $userId): array
|
|
{
|
|
$configValue = Db::name('system_configs')
|
|
->where('config_group', 'user_settings')
|
|
->where('config_key', $this->preferenceConfigKey($userId))
|
|
->value('config_value');
|
|
|
|
$decoded = [];
|
|
if (is_string($configValue) && $configValue !== '') {
|
|
$decoded = json_decode($configValue, true);
|
|
$decoded = is_array($decoded) ? $decoded : [];
|
|
}
|
|
|
|
return $this->normalizePreferences($decoded);
|
|
}
|
|
|
|
private function normalizePreferences(array $input): array
|
|
{
|
|
return [
|
|
'notify_order' => array_key_exists('notify_order', $input) ? (bool)$input['notify_order'] : true,
|
|
'notify_report' => array_key_exists('notify_report', $input) ? (bool)$input['notify_report'] : true,
|
|
'notify_supplement' => array_key_exists('notify_supplement', $input) ? (bool)$input['notify_supplement'] : true,
|
|
'notify_ticket' => array_key_exists('notify_ticket', $input) ? (bool)$input['notify_ticket'] : true,
|
|
'marketing_notify' => array_key_exists('marketing_notify', $input) ? (bool)$input['marketing_notify'] : false,
|
|
'privacy_mode' => array_key_exists('privacy_mode', $input) ? (bool)$input['privacy_mode'] : false,
|
|
];
|
|
}
|
|
|
|
private function preferenceConfigKey(int $userId): string
|
|
{
|
|
return 'user_' . $userId;
|
|
}
|
|
}
|