同步会议室

master
lion 1 week ago
parent 1cd15aa5fd
commit 8b2102696d

@ -2,10 +2,37 @@
namespace App\Http\Controllers\Admin;
use App\Helpers\ResponseCode;
use App\Models\AppointmentConfig;
use Illuminate\Support\Facades\DB;
class AppointmentConfigController extends BaseController
{
protected function isSystemAdmin(): bool
{
$admin = $this->getUser();
$admin->loadMissing('roles');
return $admin->roles->contains(function ($role) {
return $role->name === '系统管理员';
});
}
protected function normalizeJsonField($value): ?string
{
if ($value === null || $value === '') {
return null;
}
if (is_array($value)) {
return json_encode($value, JSON_UNESCAPED_UNICODE);
}
$decoded = json_decode($value, true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new \InvalidArgumentException('JSON 格式不正确');
}
return json_encode($decoded, JSON_UNESCAPED_UNICODE);
}
/**
* 构造函数
@ -91,7 +118,57 @@ class AppointmentConfigController extends BaseController
*/
public function save()
{
return parent::save();
$all = \request()->all();
$technicalFields = ['door', 'room', 'value', 'remark'];
$technicalData = [];
if ($this->isSystemAdmin()) {
foreach (['door', 'room'] as $field) {
if (array_key_exists($field, $all)) {
try {
$technicalData[$field] = $this->normalizeJsonField($all[$field]);
} catch (\InvalidArgumentException $exception) {
return $this->fail([ResponseCode::ERROR_PARAMETER, $exception->getMessage()]);
}
}
}
}
foreach ($technicalFields as $field) {
unset($all[$field]);
}
DB::beginTransaction();
try {
if (isset($all['id'])) {
$model = $this->model->find($all['id']);
if (empty($model)) {
return $this->fail([ResponseCode::ERROR_BUSINESS, '数据不存在']);
}
} else {
$model = $this->model;
$all['admin_id'] = $this->getUserId();
$all['department_id'] = $this->getUser()->department_id;
}
$original = $model->getOriginal();
$model->fill($all);
$model->save();
if ($this->isSystemAdmin() && !empty($technicalData)) {
AppointmentConfig::unguarded(function () use ($model, $technicalData) {
$model->fill($technicalData);
$model->save();
});
}
DB::commit();
$this->saveLogs($original, $model);
return $this->success($model->refresh());
} catch (\Exception $exception) {
DB::rollBack();
return $this->fail([$exception->getCode(), $exception->getMessage()]);
}
}
/**

@ -86,7 +86,7 @@ class AuthController extends Controller
*/
public function me()
{
$user = Admin::with('department')->find($this->guard()->user()->id);
$user = Admin::with(['department', 'roles'])->find($this->guard()->user()->id);
return $this->success($user);
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long
Loading…
Cancel
Save