feat: 微客宝H5客户端 - 基础架构+核心页面
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Client;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Crm\Customer;
|
||||
use App\Models\Meal\DailyMealPlan;
|
||||
use App\Models\Meal\MealReview;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Carbon;
|
||||
|
||||
class MealController extends Controller
|
||||
{
|
||||
/**
|
||||
* 我的用膳列表(分页)
|
||||
*/
|
||||
public function list(Request $request): JsonResponse
|
||||
{
|
||||
/** @var Customer $customer */
|
||||
$customer = $request->user();
|
||||
|
||||
$query = DailyMealPlan::withoutGlobalScope('store')
|
||||
->where('customer_id', $customer->id)
|
||||
->with('review');
|
||||
|
||||
// 按指定日期筛选
|
||||
if ($request->filled('date')) {
|
||||
$query->whereDate('plan_date', $request->input('date'));
|
||||
}
|
||||
|
||||
// 最近N天
|
||||
if ($request->filled('days') && !$request->filled('date')) {
|
||||
$days = (int) $request->input('days', 7);
|
||||
$query->where('plan_date', '>=', Carbon::today()->subDays($days - 1));
|
||||
}
|
||||
|
||||
return $this->paginate(
|
||||
$query->orderByDesc('plan_date')->paginate(20)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 今日用膳
|
||||
*/
|
||||
public function today(Request $request): JsonResponse
|
||||
{
|
||||
/** @var Customer $customer */
|
||||
$customer = $request->user();
|
||||
|
||||
$list = DailyMealPlan::withoutGlobalScope('store')
|
||||
->where('customer_id', $customer->id)
|
||||
->whereDate('plan_date', today())
|
||||
->get();
|
||||
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 提交膳食点评
|
||||
*/
|
||||
public function review(Request $request, int $planId): JsonResponse
|
||||
{
|
||||
/** @var Customer $customer */
|
||||
$customer = $request->user();
|
||||
|
||||
$validated = $request->validate([
|
||||
'rating' => 'required|integer|min:1|max:5',
|
||||
'content' => 'nullable|string|max:500',
|
||||
]);
|
||||
|
||||
// 确认该膳食计划属于当前客户
|
||||
$plan = DailyMealPlan::withoutGlobalScope('store')
|
||||
->where('id', $planId)
|
||||
->where('customer_id', $customer->id)
|
||||
->first();
|
||||
|
||||
if (!$plan) {
|
||||
return $this->error('膳食记录不存在或无权操作', 40401, 404);
|
||||
}
|
||||
|
||||
$review = MealReview::updateOrCreate(
|
||||
['daily_meal_plan_id' => $planId],
|
||||
[
|
||||
'score' => $validated['rating'],
|
||||
'content' => $validated['content'] ?? null,
|
||||
'customer_id' => $customer->id,
|
||||
]
|
||||
);
|
||||
|
||||
return $this->success($review);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user