更新宝贝监护功能
This commit is contained in:
@@ -11,6 +11,7 @@ use think\facade\Session;
|
||||
use think\response\Json;
|
||||
use think\db\exception\DbException;
|
||||
use app\model\AppsBabyhealthBabys;
|
||||
use app\model\AppsBabyhealthUsers;
|
||||
|
||||
|
||||
class BabysController extends BaseController
|
||||
@@ -23,9 +24,20 @@ class BabysController extends BaseController
|
||||
{
|
||||
try {
|
||||
$babylist = AppsBabyhealthBabys::where('delete_time', null)
|
||||
->field('id, name, nickname, sex, avatar, birth, height, weight, status, create_time, update_time')
|
||||
->field('id, name, nickname, sex, avatar, birth, father, mother, height, weight, status, create_time, update_time')
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
// 查询每个宝贝的父母名称
|
||||
foreach ($babylist as &$baby) {
|
||||
$parentNames = $this->findParentNames(
|
||||
(int)($baby['father'] ?? 0),
|
||||
(int)($baby['mother'] ?? 0)
|
||||
);
|
||||
$baby['father'] = $parentNames['father_name'];
|
||||
$baby['mother'] = $parentNames['mother_name'];
|
||||
}
|
||||
|
||||
// 记录操作日志
|
||||
$this->logSuccess('宝贝管理', '获取宝贝信息', ['data' => $babylist]);
|
||||
return json([
|
||||
@@ -52,7 +64,7 @@ class BabysController extends BaseController
|
||||
{
|
||||
try {
|
||||
// 获取请求参数
|
||||
$data = $this->request->param();
|
||||
$data = $this->request->post();
|
||||
|
||||
// 准备宝贝数据
|
||||
$babyData = [
|
||||
@@ -110,7 +122,7 @@ class BabysController extends BaseController
|
||||
{
|
||||
try {
|
||||
// 获取请求参数
|
||||
$data = $this->request->param();
|
||||
$data = $this->request->post();
|
||||
|
||||
// 检查宝贝信息是否存在
|
||||
$baby = AppsBabyhealthBabys::where('id', $id)
|
||||
@@ -256,4 +268,150 @@ class BabysController extends BaseController
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 绑定父母
|
||||
* @param int $id Baby ID
|
||||
* @return Json
|
||||
*/
|
||||
public function bindParent(int $id)
|
||||
{
|
||||
try {
|
||||
// 获取请求参数(支持POST)
|
||||
$data = $this->request->post();
|
||||
|
||||
// 检查宝贝信息是否存在
|
||||
$baby = AppsBabyhealthBabys::where('id', $id)
|
||||
->where('delete_time', null)
|
||||
->find();
|
||||
|
||||
if (!$baby) {
|
||||
return json([
|
||||
'code' => 404,
|
||||
'msg' => '宝贝信息不存在'
|
||||
]);
|
||||
}
|
||||
|
||||
// 准备更新数据
|
||||
$updateData = [
|
||||
'update_time' => date('Y-m-d H:i:s'),
|
||||
];
|
||||
|
||||
// 更新父亲ID
|
||||
if (isset($data['father_id'])) {
|
||||
$updateData['father_id'] = $data['father_id'];
|
||||
}
|
||||
|
||||
// 更新母亲ID
|
||||
if (isset($data['mother_id'])) {
|
||||
$updateData['mother_id'] = $data['mother_id'];
|
||||
}
|
||||
|
||||
// 执行更新
|
||||
AppsBabyhealthBabys::where('id', $id)->update($updateData);
|
||||
|
||||
// 记录操作日志
|
||||
$this->logSuccess('宝贝管理', '绑定父母', ['id' => $id, 'father_id' => $data['father_id'] ?? null, 'mother_id' => $data['mother_id'] ?? null]);
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => '绑定成功'
|
||||
]);
|
||||
} catch (DbException $e) {
|
||||
// 记录失败日志
|
||||
$this->logFail('宝贝管理', '绑定父母', $e->getMessage());
|
||||
return json([
|
||||
'code' => 500,
|
||||
'msg' => '绑定失败:' . $e->getMessage()
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取父母名称
|
||||
* @param int $id 宝贝ID
|
||||
* @return Json
|
||||
*/
|
||||
public function getParents(int $id)
|
||||
{
|
||||
try {
|
||||
// 查询宝贝的父母ID
|
||||
$baby = AppsBabyhealthBabys::where('id', $id)
|
||||
->where('delete_time', null)
|
||||
->field('father, mother')
|
||||
->find();
|
||||
|
||||
if (!$baby) {
|
||||
return json([
|
||||
'code' => 404,
|
||||
'msg' => '宝贝信息不存在'
|
||||
]);
|
||||
}
|
||||
|
||||
// 获取父母名称
|
||||
$parentNames = $this->findParentNames(
|
||||
(int)($baby['father'] ?? 0),
|
||||
(int)($baby['mother'] ?? 0)
|
||||
);
|
||||
|
||||
// 记录操作日志
|
||||
$this->logSuccess('宝贝管理', '获取父母信息', ['id' => $id]);
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => '获取成功',
|
||||
'data' => [
|
||||
'father_id' => $baby['father'] ?? null,
|
||||
'mother_id' => $baby['mother'] ?? null,
|
||||
'father' => $parentNames['father_name'],
|
||||
'mother' => $parentNames['mother_name']
|
||||
]
|
||||
]);
|
||||
} catch (DbException $e) {
|
||||
// 记录失败日志
|
||||
$this->logFail('宝贝管理', '获取父母信息', $e->getMessage());
|
||||
return json([
|
||||
'code' => 500,
|
||||
'msg' => '获取失败:' . $e->getMessage(),
|
||||
'data' => []
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 查找父母名称
|
||||
* @param int $father 父亲ID
|
||||
* @param int $mother 母亲ID
|
||||
* @return array
|
||||
*/
|
||||
private function findParentNames(int $father = 0, int $mother = 0): array
|
||||
{
|
||||
$result = [
|
||||
'father_name' => '',
|
||||
'mother_name' => ''
|
||||
];
|
||||
|
||||
// 查询父亲名称
|
||||
if ($father > 0) {
|
||||
$father = \app\model\AppsBabyhealthUsers::where('id', $father)
|
||||
->where('delete_time', null)
|
||||
->field('name')
|
||||
->find();
|
||||
if ($father) {
|
||||
$result['father_name'] = $father['name'];
|
||||
}
|
||||
}
|
||||
|
||||
// 查询母亲名称
|
||||
if ($mother > 0) {
|
||||
$mother = \app\model\AppsBabyhealthUsers::where('id', $mother)
|
||||
->where('delete_time', null)
|
||||
->field('name')
|
||||
->find();
|
||||
if ($mother) {
|
||||
$result['mother_name'] = $mother['name'];
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@@ -22,7 +22,7 @@ class UserController extends BaseController
|
||||
{
|
||||
try {
|
||||
$babylist = AppsBabyhealthUsers::where('delete_time', null)
|
||||
->field('id, account, password, phone, email, name, sex, avatar, birth, status, create_time, update_time')
|
||||
->field('id, account, phone, email, name, sex, avatar, birth, status, create_time, update_time')
|
||||
->select()
|
||||
->toArray();
|
||||
// 记录操作日志
|
||||
|
||||
Reference in New Issue
Block a user