tp/docs/编辑模块(新增&编辑).md

37 lines
1.3 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 编辑分类(添加/编辑)
* @param int $id 分类ID为空时新增有值时编辑
*/
public function editCategory(int $id = 0)
{
$data = request()->param();
// 过滤只保留数据库表需要的字段
$allowedFields = ['id', 'title', 'status'];
$saveData = [];
foreach ($allowedFields as $field) {
if (isset($data[$field])) {
$saveData[$field] = $data[$field];
}
}
if ($id > 0) {
// 编辑模式
$saveData['update_time'] = date('Y-m-d H:i:s');
SystemModuleCategory::where('id', $id)->update($saveData);
$this->logSuccess('模块市场', '编辑模块分类', ['id' => $id, 'data' => $saveData]);
return json([
'code' => 200,
'msg' => '编辑成功'
]);
} else {
// 新增模式
$saveData['create_time'] = date('Y-m-d H:i:s');
$cateId = SystemModuleCategory::insertGetId($saveData);
$this->logSuccess('模块市场', '添加模块分类', ['data' => $saveData]);
return json([
'code' => 200,
'msg' => '添加成功'
]);
}
}