diff --git a/modules/Account/app/Controllers/Api/StatisticsController.php b/modules/Account/app/Controllers/Api/StatisticsController.php index ceabc30..0fcd2e2 100644 --- a/modules/Account/app/Controllers/Api/StatisticsController.php +++ b/modules/Account/app/Controllers/Api/StatisticsController.php @@ -89,4 +89,19 @@ class StatisticsController extends BaseController return response()->json($this->data); } + + /** + * 获取个人中心统计数据 + */ + public function dashboard(Request $request) + { + try { + $this->data['data'] = $this->statisticsService->getDashboardStats($request); + } catch (\Throwable $th) { + $this->data['code'] = 0; + $this->data['message'] = $th->getMessage(); + } + + return response()->json($this->data); + } } diff --git a/modules/Account/app/Services/BillService.php b/modules/Account/app/Services/BillService.php index 742112d..d26a4ba 100644 --- a/modules/Account/app/Services/BillService.php +++ b/modules/Account/app/Services/BillService.php @@ -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 + ]; } /** diff --git a/modules/Account/app/Services/StatisticsService.php b/modules/Account/app/Services/StatisticsService.php index 61201de..7bb7b71 100644 --- a/modules/Account/app/Services/StatisticsService.php +++ b/modules/Account/app/Services/StatisticsService.php @@ -17,9 +17,10 @@ class StatisticsService $userId = auth('api')->user()['uid']; $year = $request->input('year'); $month = $request->input('month'); + $dataType = $request->input('data_type', 'family'); // 默认为家庭数据 // 获取用户所在的家庭ID - $familyId = $this->getUserFamilyId($userId); + $userFamilyId = $this->getUserFamilyId($userId); // 查询当月的账单 $query = Bill::query(); @@ -32,9 +33,15 @@ class StatisticsService ->whereYear('bill_date', date('Y')); } - if ($familyId) { - $query->where('family_id', $familyId); + // 根据data_type参数判断数据类型 + if ($dataType === 'personal') { + // 个人数据:只显示当前用户的账单 + $query->where('user_id', $userId); + } elseif ($dataType === 'family' && $userFamilyId) { + // 家庭数据:显示用户所在家庭的账单 + $query->where('family_id', $userFamilyId); } else { + // 默认显示个人数据 $query->where('user_id', $userId); } @@ -59,9 +66,10 @@ class StatisticsService $year = $request->input('year'); $month = $request->input('month'); $days = 7; // 最近7天 + $dataType = $request->input('data_type', 'family'); // 默认为家庭数据 // 获取用户所在的家庭ID - $familyId = $this->getUserFamilyId($userId); + $userFamilyId = $this->getUserFamilyId($userId); $data = []; for ($i = $days - 1; $i >= 0; $i--) { @@ -69,14 +77,19 @@ class StatisticsService $query = Bill::whereDate('bill_date', $date); - if ($familyId) { - $query->where('family_id', $familyId); + // 根据data_type参数判断数据类型 + if ($dataType === 'personal') { + // 个人数据:只显示当前用户的账单 + $query->where('user_id', $userId); + } elseif ($dataType === 'family' && $userFamilyId) { + // 家庭数据:显示用户所在家庭的账单 + $query->where('family_id', $userFamilyId); } else { + // 默认显示个人数据 $query->where('user_id', $userId); } $bills = $query->get(); - $data[] = [ 'date' => $date, 'income' => (float) $bills->where('type', 'income')->sum('amount'), @@ -96,9 +109,10 @@ class StatisticsService $type = $request->input('type', 'expense'); $year = $request->input('year'); $month = $request->input('month'); + $dataType = $request->input('data_type', 'family'); // 默认为家庭数据 // 获取用户所在的家庭ID - $familyId = $this->getUserFamilyId($userId); + $userFamilyId = $this->getUserFamilyId($userId); $query = Bill::where('type', $type); @@ -110,16 +124,22 @@ class StatisticsService ->whereYear('bill_date', date('Y')); } - if ($familyId) { - $query->where('family_id', $familyId); + // 根据data_type参数判断数据类型 + if ($dataType === 'personal') { + // 个人数据:只显示当前用户的账单 + $query->where('user_id', $userId); + } elseif ($dataType === 'family' && $userFamilyId) { + // 家庭数据:显示用户所在家庭的账单 + $query->where('family_id', $userFamilyId); } else { + // 默认显示个人数据 $query->where('user_id', $userId); } $bills = $query->get(); // 按分类汇总 - $data = $bills->groupBy('category')->map(function ($items) { + $data = $bills->groupBy('category')->map(function ($items) use ($type) { return [ 'category_id' => $this->getCategoryId($items->first()->category, $type), 'amount' => (float) $items->sum('amount') @@ -153,18 +173,25 @@ class StatisticsService { $userId = auth('api')->user()['uid']; $year = $request->input('year', date('Y')); + $dataType = $request->input('data_type', 'family'); // 默认为家庭数据 // 获取用户所在的家庭ID - $familyId = $this->getUserFamilyId($userId); + $userFamilyId = $this->getUserFamilyId($userId); $data = []; for ($month = 1; $month <= 12; $month++) { $query = Bill::whereMonth('bill_date', $month) ->whereYear('bill_date', $year); - if ($familyId) { - $query->where('family_id', $familyId); + // 根据data_type参数判断数据类型 + if ($dataType === 'personal') { + // 个人数据:只显示当前用户的账单 + $query->where('user_id', $userId); + } elseif ($dataType === 'family' && $userFamilyId) { + // 家庭数据:显示用户所在家庭的账单 + $query->where('family_id', $userFamilyId); } else { + // 默认显示个人数据 $query->where('user_id', $userId); } @@ -191,9 +218,10 @@ class StatisticsService { $userId = auth('api')->user()['uid']; $years = $request->input('years', 5); + $dataType = $request->input('data_type', 'family'); // 默认为家庭数据 // 获取用户所在的家庭ID - $familyId = $this->getUserFamilyId($userId); + $userFamilyId = $this->getUserFamilyId($userId); $data = []; for ($i = $years - 1; $i >= 0; $i--) { @@ -201,9 +229,15 @@ class StatisticsService $query = Bill::whereYear('bill_date', $year); - if ($familyId) { - $query->where('family_id', $familyId); + // 根据data_type参数判断数据类型 + if ($dataType === 'personal') { + // 个人数据:只显示当前用户的账单 + $query->where('user_id', $userId); + } elseif ($dataType === 'family' && $userFamilyId) { + // 家庭数据:显示用户所在家庭的账单 + $query->where('family_id', $userFamilyId); } else { + // 默认显示个人数据 $query->where('user_id', $userId); } @@ -228,4 +262,55 @@ class StatisticsService $familyMember = FamilyMember::where('user_id', $userId)->first(); return $familyMember ? $familyMember->family_id : null; } + + /** + * 获取个人中心统计数据 + */ + public function getDashboardStats(Request $request) + { + $userId = auth('api')->user()['uid']; + $dataType = $request->input('data_type', 'family'); // 默认为家庭数据 + + // 获取用户所在的家庭ID + $userFamilyId = $this->getUserFamilyId($userId); + + // 统计账单数 + $billQuery = Bill::query(); + + // 根据data_type参数判断数据类型 + if ($dataType === 'personal') { + // 个人数据:只统计当前用户的账单 + $billQuery->where('user_id', $userId); + } elseif ($dataType === 'family' && $userFamilyId) { + // 家庭数据:统计用户所在家庭的账单 + $billQuery->where('family_id', $userFamilyId); + } else { + // 默认统计个人数据 + $billQuery->where('user_id', $userId); + } + + $billCount = $billQuery->count(); + + // 统计记账天数(计算从第一个账单到现在的天数) + $days = 0; + $firstBill = $billQuery->orderBy('bill_date', 'asc')->first(); + if ($firstBill) { + $firstDate = new \DateTime($firstBill->bill_date); + $now = new \DateTime(); + $interval = $firstDate->diff($now); + $days = $interval->days + 1; // +1 包含当天 + } + + // 统计家庭成员数 + $familyMembers = 0; + if ($userFamilyId) { + $familyMembers = FamilyMember::where('family_id', $userFamilyId)->count(); + } + + return [ + 'bill_count' => $billCount, + 'days' => $days, + 'family_members' => $familyMembers + ]; + } } diff --git a/modules/Account/routes/api.php b/modules/Account/routes/api.php index ff1c1ec..975a0ab 100644 --- a/modules/Account/routes/api.php +++ b/modules/Account/routes/api.php @@ -44,5 +44,6 @@ Route::middleware(['auth:api'])->group(function () { Route::get('category', [StatisticsController::class, 'category']); Route::get('monthly', [StatisticsController::class, 'monthly']); Route::get('yearly', [StatisticsController::class, 'yearly']); + Route::get('dashboard', [StatisticsController::class, 'dashboard']); }); }); diff --git a/resources/mobile/api/modules/auth.js b/resources/mobile/api/modules/auth.js index 04b77b2..e148956 100644 --- a/resources/mobile/api/modules/auth.js +++ b/resources/mobile/api/modules/auth.js @@ -28,5 +28,12 @@ export default { get: async function(params) { return request.get(this.url, {params: params}) } + }, + update: { + url: '/api/member/update', + name: '更新用户信息', + post: async function(params) { + return request.post(this.url, params) + } } } diff --git a/resources/mobile/api/modules/sms.js b/resources/mobile/api/modules/common.js similarity index 66% rename from resources/mobile/api/modules/sms.js rename to resources/mobile/api/modules/common.js index 9c69792..45c9272 100644 --- a/resources/mobile/api/modules/sms.js +++ b/resources/mobile/api/modules/common.js @@ -1,9 +1,9 @@ import request from '@/utils/request' export default { - sendCode: { - url: '/api/member/sms/send', - name: '发送验证码', + upload: { + url: '/api/system/upload', + name: '图片上传', post: async function(params) { return request.post(this.url, params) } diff --git a/resources/mobile/api/modules/member.js b/resources/mobile/api/modules/member.js new file mode 100644 index 0000000..e312520 --- /dev/null +++ b/resources/mobile/api/modules/member.js @@ -0,0 +1,18 @@ +import request from '@/utils/request' + +export default { + edit: { + url: '/api/member/member/edit', + name: '用户修改', + post: async function(params) { + return request.put(this.url, params) + } + }, + editpasswd: { + url: '/api/member/member/editpasswd', + name: '密码修改', + post: async function(params) { + return request.put(this.url, params) + } + } +} diff --git a/resources/mobile/api/modules/statistics.js b/resources/mobile/api/modules/statistics.js index d6bf267..98a42dc 100644 --- a/resources/mobile/api/modules/statistics.js +++ b/resources/mobile/api/modules/statistics.js @@ -40,5 +40,13 @@ export default { get: async function(params) { return request.get(this.url, {params: params}) } + }, + // 获取个人中心统计数据 + dashboard: { + url: '/api/account/statistics/dashboard', + name: '个人中心统计', + get: async function(params) { + return request.get(this.url, {params: params}) + } } } diff --git a/resources/mobile/pages.json b/resources/mobile/pages.json index b11b97c..f5df718 100644 --- a/resources/mobile/pages.json +++ b/resources/mobile/pages.json @@ -30,11 +30,26 @@ { "path": "pages/ucenter/register/index" }, + { + "path": "pages/ucenter/profile/index" + }, + { + "path": "pages/ucenter/profile/edit" + }, + { + "path": "pages/ucenter/profile/password" + }, { "path": "pages/ucenter/agreement/user" }, { "path": "pages/ucenter/agreement/privacy" + }, + { + "path": "pages/ucenter/about/index" + }, + { + "path": "pages/ucenter/help/index" } ], "globalStyle": { diff --git a/resources/mobile/pages/account/bill/add.vue b/resources/mobile/pages/account/bill/add.vue index 3f84f7c..43327f5 100644 --- a/resources/mobile/pages/account/bill/add.vue +++ b/resources/mobile/pages/account/bill/add.vue @@ -1,10 +1,16 @@