Files
yuezi-saas/backend/app/Http/Controllers/Admin/Crm/CustomerController.php
T
li 16bcd9bcf0 feat: 第二阶段核心业务模块(CRM/房务/护理/月子餐)
后端:
- 4个迁移文件新增23张业务表
- 23个Eloquent Model(含关联/类型转换/门店隔离)
- 20个Controller(含CRUD+业务操作: 合同审核/入住退房/护理异常处理等)
- 110+条API路由(crm/room/care/meal四大前缀)

前端:
- 4个API模块(crm.js/room.js/care.js/meal.js)
- 13个业务页面(线索/客户/合同/渠道/投诉/房型/房间/预定/护理档案/护理计划/护理记录/菜品/排餐)
2026-03-13 20:18:29 +08:00

87 lines
3.1 KiB
PHP

<?php
namespace App\Http\Controllers\Admin\Crm;
use App\Http\Controllers\Controller;
use App\Models\Crm\Customer;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class CustomerController extends Controller
{
public function index(Request $request): JsonResponse
{
$query = Customer::query()->with(['owner']);
$query->when($request->name, fn($q, $v) => $q->where('name', 'like', "%{$v}%"));
$query->when($request->phone, fn($q, $v) => $q->where('phone', 'like', "%{$v}%"));
$query->when($request->status, fn($q, $v) => $q->where('status', $v));
$query->when($request->owner_id, fn($q, $v) => $q->where('owner_id', $v));
return $this->paginate($query->latest()->paginate($request->input('per_page', 15)));
}
public function store(Request $request): JsonResponse
{
$validated = $request->validate([
'lead_id' => 'nullable|exists:leads,id',
'name' => 'required|string|max:50',
'phone' => 'required|string|max:20',
'id_card' => 'nullable|string|max:20',
'wechat' => 'nullable|string|max:50',
'birthday' => 'nullable|date',
'expected_date' => 'nullable|date',
'actual_date' => 'nullable|date',
'baby_count' => 'sometimes|integer|min:1',
'tags' => 'nullable|array',
'remark' => 'nullable|string',
'families' => 'nullable|array',
'families.*.name' => 'nullable|string|max:50',
'families.*.phone' => 'nullable|string|max:20',
'families.*.relation' => 'nullable|string|max:20',
'families.*.is_emergency' => 'nullable|boolean',
]);
$families = $validated['families'] ?? [];
unset($validated['families']);
$validated['status'] = 1;
$customer = Customer::create($validated);
if ($families) {
$customer->families()->createMany($families);
}
return $this->success($customer->load('families'));
}
public function show(Customer $customer): JsonResponse
{
return $this->success($customer->load(['owner', 'families', 'contracts', 'complaints']));
}
public function update(Request $request, Customer $customer): JsonResponse
{
$validated = $request->validate([
'name' => 'sometimes|string|max:50',
'phone' => 'sometimes|string|max:20',
'id_card' => 'nullable|string|max:20',
'wechat' => 'nullable|string|max:50',
'birthday' => 'nullable|date',
'expected_date' => 'nullable|date',
'actual_date' => 'nullable|date',
'baby_count' => 'sometimes|integer|min:1',
'tags' => 'nullable|array',
'status' => 'sometimes|integer|in:1,2,3,4,5',
'owner_id' => 'nullable|exists:users,id',
'remark' => 'nullable|string',
]);
$customer->update($validated);
return $this->success($customer);
}
public function destroy(Customer $customer): JsonResponse
{
$customer->delete();
return $this->success(null);
}
}