完善版本
This commit is contained in:
@@ -21,21 +21,22 @@ class BillService
|
||||
$month = $request->input('month');
|
||||
$startDate = $request->input('start_date');
|
||||
$endDate = $request->input('end_date');
|
||||
$familyId = $request->input('family_id');
|
||||
$dataType = $request->input('data_type', 'family'); // 默认为家庭数据
|
||||
|
||||
// 获取用户所在的家庭ID
|
||||
$userFamilyId = $this->getUserFamilyId($userId);
|
||||
|
||||
$query = Bill::with(['user:uid,nickname,username', 'family:id,name']);
|
||||
|
||||
// 如果用户加入了家庭,且没有指定family_id,则显示家庭账单
|
||||
if ($userFamilyId && !$familyId) {
|
||||
// 根据data_type参数判断数据类型
|
||||
if ($dataType === 'personal') {
|
||||
// 个人数据:只显示当前用户的账单
|
||||
$query->where('user_id', $userId);
|
||||
} elseif ($dataType === 'family' && $userFamilyId) {
|
||||
// 家庭数据:显示用户所在家庭的账单
|
||||
$query->where('family_id', $userFamilyId);
|
||||
} elseif ($familyId) {
|
||||
// 如果指定了family_id,则显示该家庭的账单
|
||||
$query->where('family_id', $familyId);
|
||||
} else {
|
||||
// 否则显示个人账单
|
||||
// 默认显示个人数据
|
||||
$query->where('user_id', $userId);
|
||||
}
|
||||
|
||||
@@ -66,13 +67,13 @@ class BillService
|
||||
$monthExpense = Bill::query();
|
||||
$monthIncome = Bill::query();
|
||||
|
||||
// 同样的家庭查询逻辑
|
||||
if ($userFamilyId && !$familyId) {
|
||||
// 同样的数据类型查询逻辑
|
||||
if ($dataType === 'personal') {
|
||||
$monthExpense->where('user_id', $userId);
|
||||
$monthIncome->where('user_id', $userId);
|
||||
} elseif ($dataType === 'family' && $userFamilyId) {
|
||||
$monthExpense->where('family_id', $userFamilyId);
|
||||
$monthIncome->where('family_id', $userFamilyId);
|
||||
} elseif ($familyId) {
|
||||
$monthExpense->where('family_id', $familyId);
|
||||
$monthIncome->where('family_id', $familyId);
|
||||
} else {
|
||||
$monthExpense->where('user_id', $userId);
|
||||
$monthIncome->where('user_id', $userId);
|
||||
@@ -175,8 +176,14 @@ class BillService
|
||||
$data['category'] = $categoryMap[$data['category_id']];
|
||||
$data['bill_date'] = $data['date'];
|
||||
|
||||
// 检查家庭权限
|
||||
if (!empty($data['family_id'])) {
|
||||
// 如果前端没有指定family_id,自动获取用户所在的家庭
|
||||
if (empty($data['family_id'])) {
|
||||
$userFamilyId = $this->getUserFamilyId($userId);
|
||||
if ($userFamilyId) {
|
||||
$data['family_id'] = $userFamilyId;
|
||||
}
|
||||
} else {
|
||||
// 检查家庭权限
|
||||
$this->checkFamilyAccess($userId, $data['family_id']);
|
||||
}
|
||||
|
||||
@@ -233,17 +240,29 @@ class BillService
|
||||
$data = $request->validate([
|
||||
'type' => 'required|in:income,expense',
|
||||
'amount' => 'required|numeric|min:0.01',
|
||||
'category' => 'required|string|max:50',
|
||||
'category_id' => 'required|integer',
|
||||
'remark' => 'nullable|string|max:255',
|
||||
'bill_date' => 'required|date',
|
||||
'date' => 'required|date',
|
||||
'family_id' => 'nullable|integer|exists:account_families,id'
|
||||
]);
|
||||
|
||||
// 将category_id转换为category字符串
|
||||
$categoryMap = $this->getCategoryMap($data['type']);
|
||||
if (!isset($categoryMap[$data['category_id']])) {
|
||||
throw new \Exception('分类不存在');
|
||||
}
|
||||
|
||||
$data['category'] = $categoryMap[$data['category_id']];
|
||||
$data['bill_date'] = $data['date'];
|
||||
|
||||
// 检查家庭权限
|
||||
if (!empty($data['family_id'])) {
|
||||
$this->checkFamilyAccess($userId, $data['family_id']);
|
||||
}
|
||||
|
||||
// 移除前端字段
|
||||
unset($data['category_id'], $data['date']);
|
||||
|
||||
$bill->update($data);
|
||||
|
||||
return $bill;
|
||||
@@ -278,12 +297,25 @@ class BillService
|
||||
$bill = Bill::with(['user:uid,nickname,username', 'family:id,name,owner_id'])
|
||||
->findOrFail($id);
|
||||
|
||||
// 验证权限
|
||||
if ($bill->user_id != $userId && !in_array($userId, $bill->family->members->pluck('uid')->toArray())) {
|
||||
// 验证权限:只有账单创建者才能查看和编辑
|
||||
if ($bill->user_id != $userId) {
|
||||
throw new \Exception('无权查看此账单');
|
||||
}
|
||||
|
||||
return $bill;
|
||||
// 转换数据格式以匹配前端
|
||||
return [
|
||||
'id' => $bill->id,
|
||||
'type' => $bill->type,
|
||||
'amount' => (float)$bill->amount,
|
||||
'category' => $bill->category,
|
||||
'category_id' => $this->getCategoryId($bill->category, $bill->type),
|
||||
'remark' => $bill->remark,
|
||||
'date' => $bill->bill_date,
|
||||
'bill_date' => $bill->bill_date,
|
||||
'created_at' => $bill->created_at->format('Y-m-d H:i:s'),
|
||||
'user' => $bill->user,
|
||||
'family' => $bill->family
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user