client api

This commit is contained in:
pbao9
2026-02-11 10:08:39 +07:00
parent 458ad0a4cb
commit d59b2a0840
27 changed files with 4369 additions and 28 deletions
@@ -0,0 +1,73 @@
<?php
namespace App\Http\Controllers\Api\Client;
use App\Http\Controllers\Controller;
use App\Http\Requests\Api\Client\ClientAuthRequest;
use App\Http\Requests\Api\Client\ClientRegisterRequest;
use App\Http\Resources\Api\Client\ClientResource;
use App\Services\Api\Client\ClientAuthService;
use Illuminate\Support\Facades\Auth;
class ClientController extends Controller
{
protected $authService;
public function __construct(ClientAuthService $authService)
{
$this->authService = $authService;
}
/**
* Lấy thông tin khách hàng hiện tại
*/
public function me()
{
return new ClientResource(auth('sanctum')->user());
}
/**
* Đăng nhập khách hàng
*/
public function login(ClientAuthRequest $request)
{
$result = $this->authService->authenticateClient($request->validated());
return response()->json([
'error' => false,
'data' => [
'client' => new ClientResource($result['client']),
'token' => $result['token'],
],
]);
}
/**
* Đăng ký khách hàng mới
*/
public function register(ClientRegisterRequest $request)
{
$result = $this->authService->registerClient($request->validated());
return response()->json([
'error' => false,
'data' => [
'client' => new ClientResource($result['client']),
'token' => $result['token'],
],
]);
}
/**
* Đăng xuất khách hàng
*/
public function logout()
{
auth('sanctum')->user()->currentAccessToken()->delete();
return response()->json([
'error' => false,
'message' => 'Đã đăng xuất thành công.',
]);
}
}
@@ -0,0 +1,51 @@
<?php
namespace App\Http\Requests\Api\Client;
use Illuminate\Foundation\Http\FormRequest;
class ClientAuthRequest extends FormRequest
{
/**
* Allow request
*/
public function authorize(): bool
{
return true;
}
/**
* Validation rules
*/
public function rules(): array
{
return [
'email' => [
'required',
'email',
'exists:customers,email'
],
'password' => [
'required',
'string',
'min:6'
],
];
}
/**
* Custom messages (optional)
*/
public function messages(): array
{
return [
'email.required' => 'Email là bắt buộc',
'email.email' => 'Email không đúng định dạng',
'email.exists' => 'Email không tồn tại',
'password.required' => 'Mật khẩu là bắt buộc',
'password.min' => 'Mật khẩu tối thiểu 6 ký tự',
];
}
}
@@ -0,0 +1,69 @@
<?php
namespace App\Http\Requests\Api\Client;
use Illuminate\Foundation\Http\FormRequest;
class ClientRegisterRequest extends FormRequest
{
/**
* Allow request
*/
public function authorize(): bool
{
return true;
}
/**
* Validation rules
*/
public function rules(): array
{
return [
'fullname' => [
'required',
'string',
'max:255'
],
'phone' => [
'required',
'digits:10',
'unique:customers,phone'
],
'email' => [
'required',
'email',
'max:255',
'unique:customers,email'
],
'password' => [
'required',
'string',
'min:6',
'confirmed'
],
];
}
/**
* Custom messages
*/
public function messages(): array
{
return [
'fullname.required' => 'Tên là bắt buộc',
'phone.required' => 'Số điện thoại là bắt buộc',
'phone.digits' => 'Số điện thoại phải có 10 chữ số',
'phone.unique' => 'Số điện thoại đã tồn tại',
'email.required' => 'Email là bắt buộc',
'email.email' => 'Email không đúng định dạng',
'email.unique' => 'Email đã tồn tại',
'password.required' => 'Mật khẩu là bắt buộc',
'password.min' => 'Mật khẩu tối thiểu 6 ký tự',
'password.confirmed' => 'Xác nhận mật khẩu không khớp',
];
}
}
@@ -22,11 +22,11 @@ class UserRegisterRequest extends FormRequest
public function rules(): array
{
return [
'email' => 'required|email|unique:customer',
'email' => 'required|email|unique:customers',
'firstname' => 'required',
'lastname' => 'required',
'password' => 'required|min:8',
'phone' => 'required|unique:customer'
'phone' => 'required|unique:customers'
];
}
@@ -24,7 +24,7 @@ class UserStoreInformation extends FormRequest
return [
'firstname' => 'required',
'lastname' => 'required',
'email' => 'nullable|email|unique:customer,email',
'email' => 'nullable|email|unique:customers,email',
'address' => 'nullable',
'phone' => 'required',
'gender' => 'nullable',
@@ -0,0 +1,63 @@
<?php
namespace App\Http\Resources\Api\Client;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
class ClientResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
if (!$this->resource) {
return [];
}
return [
'customer' => [
'name' => $this->fullname,
'email' => $this->email,
'phone' => $this->phone,
'address' => $this->address,
'city' => $this->city,
'ward' => $this->ward,
'province' => $this->province,
'active_phone' => 1,
'active_zalo' => 1,
'gender' => $this->gender,
'date' => $this->created_at ? $this->created_at->format('Y-m-d H:i:s') : null,
'company' => $this->company ?? '',
'mst' => null,
'avatar' => $this->avatar,
'cmnd_after' => $this->cmnd_after,
'cmnd_before' => $this->cmnd_before,
'cmnd_active' => (bool) $this->identity_number,
'cmnd' => $this->cmnd,
'fb_link' => null,
'active_2fa' => 0,
'off_mail_marketing' => $this->off_mail_marketing,
'is_recharge_zalo' => 0,
'is_verify_telegram' => 0,
],
'credit' => [
'credit' => $this->credit,
'total' => $this->total,
'point' => $this->point,
'point_used' => 0,
'point_total' => $this->point,
],
'service' => [
'vps' => $this->vps()->count(),
'proxy' => 0,
'server' => 0,
'hosting' => $this->hosting()->count(),
'domain' => $this->domain()->count(),
]
];
}
}