Commit 0c1b982d by 杨树贤

独立出请求验证

parent ff4918ff
......@@ -3,8 +3,12 @@
<component name="ChangeListManager">
<list default="true" id="fb90add0-1393-48c2-9f26-72365d42cd03" name="变更" comment="">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/app/Http/ApiHelper/Response.php" beforeDir="false" afterPath="$PROJECT_DIR$/app/Http/ApiHelper/Response.php" afterDir="false" />
<change beforePath="$PROJECT_DIR$/app/Exceptions/InvalidRequestException.php" beforeDir="false" afterPath="$PROJECT_DIR$/app/Exceptions/InvalidRequestException.php" afterDir="false" />
<change beforePath="$PROJECT_DIR$/app/Http/Controllers/Api/AuthApiController.php" beforeDir="false" afterPath="$PROJECT_DIR$/app/Http/Controllers/Api/AuthApiController.php" afterDir="false" />
<change beforePath="$PROJECT_DIR$/app/Http/Controllers/Api/InquiryApiController.php" beforeDir="false" afterPath="$PROJECT_DIR$/app/Http/Controllers/Api/InquiryApiController.php" afterDir="false" />
<change beforePath="$PROJECT_DIR$/app/Http/Middleware/CheckApiLogin.php" beforeDir="false" afterPath="$PROJECT_DIR$/app/Http/Middleware/CheckApiLogin.php" afterDir="false" />
<change beforePath="$PROJECT_DIR$/app/Http/Services/InquiryService.php" beforeDir="false" afterPath="$PROJECT_DIR$/app/Http/Services/InquiryService.php" afterDir="false" />
<change beforePath="$PROJECT_DIR$/routes/api.php" beforeDir="false" afterPath="$PROJECT_DIR$/routes/api.php" afterDir="false" />
<change beforePath="$PROJECT_DIR$/storage/app/.gitignore" beforeDir="false" afterPath="$PROJECT_DIR$/storage/app/.gitignore" afterDir="false" />
<change beforePath="$PROJECT_DIR$/storage/app/public/.gitignore" beforeDir="false" afterPath="$PROJECT_DIR$/storage/app/public/.gitignore" afterDir="false" />
<change beforePath="$PROJECT_DIR$/storage/framework/.gitignore" beforeDir="false" afterPath="$PROJECT_DIR$/storage/framework/.gitignore" afterDir="false" />
......@@ -149,18 +153,18 @@
<property name="RunOnceActivity.OpenProjectViewOnStart" value="true" />
<property name="RunOnceActivity.ShowReadmeOnStart" value="true" />
<property name="WebServerToolWindowFactoryState" value="false" />
<property name="last_opened_file_path" value="$PROJECT_DIR$/app/Http/Services" />
<property name="last_opened_file_path" value="$PROJECT_DIR$/app/Http/Requests" />
<property name="nodejs_interpreter_path.stuck_in_default_project" value="undefined stuck path" />
<property name="nodejs_package_manager_path" value="npm" />
<property name="vue.rearranger.settings.migration" value="true" />
</component>
<component name="RecentsManager">
<key name="CopyFile.RECENT_KEYS">
<recent name="\\wsl$\Ubuntu-20.04\data\www\semour_web\app\Http\Requests" />
<recent name="\\wsl$\Ubuntu-20.04\data\www\semour_web\app\Http\Controllers" />
<recent name="\\wsl$\Ubuntu-20.04\data\www\semour_web\app\Http\Services" />
<recent name="\\wsl$\Ubuntu-20.04\data\www\semour_web\app\Http\Controllers\Api" />
<recent name="\\wsl$\Ubuntu-20.04\data\www\semour_web\app\Exceptions" />
<recent name="\\wsl$\Ubuntu-20.04\data\www\semour_web\app\Http" />
<recent name="\\wsl$\Ubuntu-20.04\data\www\semour_web\resources\views\about" />
</key>
<key name="MoveFile.RECENT_KEYS">
<recent name="\\wsl$\Ubuntu-20.04\data\www\semour_web\resources\views\home" />
......@@ -183,7 +187,7 @@
<updated>1666170258203</updated>
<workItem from="1666170260162" duration="17108000" />
<workItem from="1666835076791" duration="693000" />
<workItem from="1667266026118" duration="18067000" />
<workItem from="1667266026118" duration="19310000" />
</task>
<servers />
</component>
......
......@@ -38,7 +38,7 @@ class InvalidRequestException extends \Exception
];
Log::error(json_encode($err_info, JSON_UNESCAPED_UNICODE));
if ($this->field) {
return response()->json(json_decode(Response::setError($this->message, $this->field), true));
return response()->json(json_decode(Response::setErrorWithField($this->message, $this->field), true));
} else {
return response()->json(json_decode(Response::setError($this->message), true));
}
......
......@@ -2,6 +2,7 @@
namespace App\Http\Controllers\Api;
use App\Http\Requests\UserRegister;
use App\Models\User;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Foundation\Auth\ThrottlesLogins;
......@@ -17,25 +18,8 @@ class AuthApiController extends Controller
use ThrottlesLogins, RegistersUsers;
public function register(Request $request)
public function register(UserRegister $request)
{
$validator = Validator::make($request->all(), [
'email' => 'required|string|email|max:100',
'password' => 'required|string|min:8|confirmed',
'first_name' => 'required|max:100',
'last_name' => 'required|max:100',
'email_code' => 'required|max:100',
'phone' => 'required|max:100',
'company_name' => 'required|max:100',
], [
'password.confirmed' => 'Passwords do not match!',
]);
if ($validator->fails()) {
$keyName = $validator->errors()->keys()[0];
return $this->setError($validator->errors()->first());
}
$email = $request->input('email');
if (User::where('email', $email)->exists()) {
return $this->setError('Email has been taken');
......
......@@ -2,6 +2,7 @@
namespace App\Http\Controllers\Api;
use App\Http\Requests\InquirySave;
use App\Http\Services\InquiryService;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
......@@ -11,20 +12,12 @@ use Illuminate\Support\Facades\Validator;
class InquiryApiController extends Controller
{
public function add(Request $request)
public function add(InquirySave $request)
{
$data = $request->all();
$validator = Validator::make($data, [
'items.*.goods_name' => 'required|string|email|max:100',
'items.*.brand_name' => 'required|string|email|max:100',
'items.*.inquiry_number' => 'required|integer',
'remark' => 'max:255',
$data = $request->only([
'items',
'data',
]);
if ($validator->fails()) {
$keyName = $validator->errors()->keys()[0];
return $this->setError($validator->errors()->first());
}
$user = request()->user();
$result = InquiryService::addInquiry($data, $user);
if (!$result) {
......
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
//询价
class InquiryController extends Controller
{
}
......@@ -22,7 +22,8 @@ class CheckApiLogin
'msg' => 'need login...',
];
return response()->json($response);
}else{
} else {
$request->user = \Auth::user();
return $next($request);
}
}
......
<?php
namespace App\Http\Requests;
use App\Exceptions\InvalidRequestException;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;
class BaseRequest extends FormRequest
{
protected function failedValidation(Validator $validator)
{
$errors = $validator->errors();
$firstError = $errors->first();
$fields = $errors->keys();
\Log::channel("formRequest")->info(
sprintf(
"错误:请求参数 %s,错误原因:%s",
print_r(request()->all(), true),
json_encode($errors->all(), JSON_UNESCAPED_UNICODE)
)
);
throw new InvalidRequestException($firstError, $fields[0]);
}
public function authorize()
{
return true;
}
public function validated()
{
return $this->validator->validated();
}
public function __get($key)
{
if ($key == 'user') {
return request()->get('user');
}
if ($key == 'perms') {
return request()->get('perms');
}
}
}
<?php
namespace App\Http\Requests;
class InquirySave extends BaseRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'items' => 'required',
'items.*.goods_name' => 'required|string|max:100',
'items.*.brand_name' => 'required|string|max:100',
'items.*.inquiry_number' => 'required|integer',
'remark' => 'max:255|required',
];
}
}
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class UserRegister extends BaseRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
[
'email' => 'required|string|email|max:100',
'password' => 'required|string|min:8|confirmed',
'first_name' => 'required|max:100',
'last_name' => 'required|max:100',
'email_code' => 'required|max:100',
'phone' => 'required|max:100',
'company_name' => 'required|max:100',
]
];
}
public function messages()
{
return [
'password.confirmed' => 'Passwords do not match!',
];
}
}
......@@ -4,6 +4,7 @@ namespace App\Http\Services;
use App\Models\Inquiry;
use App\Models\InquiryItems;
use Illuminate\Support\Facades\DB;
class InquiryService
{
......
......@@ -15,16 +15,15 @@ use Illuminate\Support\Facades\Route;
*/
Route::middleware(['api'])->namespace('Api')->group(function () {
Route::ANY('/user/login', 'AuthApiController@login');
Route::POST('/user/register', 'AuthApiController@register');
Route::POST('/auth/login', 'AuthApiController@login');
Route::POST('/auth/register', 'AuthApiController@register');
});
Route::middleware(['api','api.check'])->namespace('Api')->group(function () {
Route::GET('/user/logout', 'AuthApiController@logout');
Route::Any('user/reset_password', 'AuthApiController@resetPassword');
Route::Any('user/send_email_code', 'AuthApiController@sendEmailCode');
Route::middleware(['api', 'api.check'])->namespace('Api')->group(function () {
Route::GET('/auth/logout', 'AuthApiController@logout');
Route::POST('auth/reset_password', 'AuthApiController@resetPassword');
Route::POST('auth/send_email_code', 'AuthApiController@sendEmailCode');
Route::ANY('inquiry/add', 'InquiryApiController@add');
});
Route::middleware(['api','api.check'])->namespace('Auth')->group(function () {
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment