kphcdr vor 1 Jahr
Ursprung
Commit
f6f92c24d1

+ 13 - 0
app/Base/BaseController.php

@@ -3,9 +3,22 @@
 namespace App\Base;
 
 use App\Http\Controllers\Controller;
+use Illuminate\Http\Request;
+use Illuminate\Support\Arr;
 
 class BaseController extends Controller
 {
+     public function valid(array $rules, array $messages = [], array $customAttributes = []): array
+     {
+         $data = [];
+          $validated= request()->validate($rules);
+         foreach($rules as $k=>$v) {
+            $data[$k] = Arr::get($validated,$k);
+         }
+
+         return $data;
+     }
+
     public function ok($data = null)
     {
         return [

+ 8 - 0
app/Base/BaseService.php

@@ -0,0 +1,8 @@
+<?php
+
+namespace App\Base;
+
+class BaseService
+{
+
+}

+ 8 - 0
app/Exceptions/ClientException.php

@@ -0,0 +1,8 @@
+<?php
+
+namespace App\Exceptions;
+
+class ClientException extends \Exception
+{
+
+}

+ 17 - 6
app/Exceptions/Handler.php

@@ -3,8 +3,8 @@
 namespace App\Exceptions;
 
 use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
+use Illuminate\Support\Arr;
 use Illuminate\Validation\ValidationException;
-use Throwable;
 
 class Handler extends ExceptionHandler
 {
@@ -14,7 +14,6 @@ class Handler extends ExceptionHandler
      * @var array<class-string<\Throwable>, \Psr\Log\LogLevel::*>
      */
     protected $levels = [
-        //
     ];
 
     /**
@@ -43,15 +42,27 @@ class Handler extends ExceptionHandler
      */
     public function register()
     {
-        $this->reportable(function (Throwable $e) {
+        $this->reportable(function (ClientException $e) {
+            logger()->warning($e->getMessage(), array_merge(request()->all(), request()->headers->all()));
+            return false;
+        });
+        $this->renderable(function (ClientException $e) {
             return response()->json([
                 "error" => $e->getMessage(),
-            ], 400);
+            ]);
+        });
+        $this->renderable(function (\Exception $e) {
+            return response()->json([
+                "error" => $e->getMessage(),
+                "file" => $e->getFile(),
+                "line" => $e->getLine(),
+                "trace" => $e->getTrace(),
+            ]);
         });
         $this->renderable(function (ValidationException $e) {
-
+            $msg = Arr::first($e->errors());
             return response()->json([
-                "error" =>json_encode($e->errors())
+                "error" => $msg[0],
             ], 400);
         });
     }

+ 0 - 8
app/Exceptions/ValidateException.php

@@ -1,8 +0,0 @@
-<?php
-
-namespace App\Exceptions;
-
-class ValidateException extends \Exception
-{
-
-}

+ 0 - 22
app/Http/Controllers/Admin/AuthController.php

@@ -1,22 +0,0 @@
-<?php
-
-namespace App\Http\Controllers\Admin;
-
-use App\Base\BaseController;
-use Illuminate\Http\Request;
-
-class AuthController extends BaseController
-{
-    public function login(Request $request)
-    {
-//        try {
-            $request->validate([
-                "username" => "required",
-                "password" => "required"
-            ]);
-//        } catch (\Exception $e) {
-//        }
-
-        return $this->ok();
-    }
-}

+ 2 - 2
app/Models/User.php

@@ -40,8 +40,8 @@ class User extends BaseModel
     protected $casts = [
         "extra"=>"array"
     ];
-    public function encrypt($password)
+    public function checkPassword($password):bool
     {
-        return hashid_encode($password);
+        return $this->password == hashid_encode($password);
     }
 }

+ 26 - 0
app/Modules/Admin/Controllers/Admin/AuthController.php

@@ -0,0 +1,26 @@
+<?php
+
+namespace App\Modules\Admin\Controllers\Admin;
+
+use App\Base\BaseController;
+use App\Modules\Admin\Services\AuthService;
+
+class AuthController extends BaseController
+{
+    protected $authService;
+
+    public function __construct(AuthService $authService)
+    {
+        $this->authService = $authService;
+    }
+
+    public function login()
+    {
+        $data = $this->valid([
+            "username" => "required",
+            "password" => "required",
+            "fix" => "",
+        ]);
+        return $this->ok($this->authService->login($data));
+    }
+}

+ 25 - 0
app/Modules/Admin/Services/AuthService.php

@@ -0,0 +1,25 @@
+<?php
+
+namespace App\Modules\Admin\Services;
+
+use App\Exceptions\ClientException;
+use App\Models\User;
+
+class AuthService
+{
+    public function login($data)
+    {
+        $u = User::wherePhone($data['username'])->orWhere("email",$data['username'])->first();
+        if(is_null($u)) {
+            throw new ClientException("账号或密码错误,请重新输入");
+        }
+        if(!$u->checkPassword($data['password'])) {
+            throw new ClientException("账号或密码错误,请重新输入!");
+        }
+
+        if($u->status != User::STATUS_OK) {
+            throw new ClientException("当前用户被禁用,请联系管理员");
+        }
+        return $u;
+    }
+}

+ 1 - 1
database/seeders/DatabaseSeeder.php

@@ -19,7 +19,7 @@ class DatabaseSeeder extends Seeder
     {
         $this->auth();
         User::create([
-            "password"=>hashid_encode("111111111111111111111111"),
+            "password"=>hashid_encode("111112"),
             "email"=>"liantiao@lientiao.com",
             "status"=>User::STATUS_OK,
             "group_id"=>1,

+ 20 - 0
lang/en/auth.php

@@ -0,0 +1,20 @@
+<?php
+
+return [
+
+    /*
+    |--------------------------------------------------------------------------
+    | Authentication Language Lines
+    |--------------------------------------------------------------------------
+    |
+    | The following language lines are used during authentication for various
+    | messages that we need to display to the user. You are free to modify
+    | these language lines according to your application's requirements.
+    |
+    */
+
+    'failed' => 'These credentials do not match our records.',
+    'password' => 'The provided password is incorrect.',
+    'throttle' => 'Too many login attempts. Please try again in :seconds seconds.',
+
+];

+ 19 - 0
lang/en/pagination.php

@@ -0,0 +1,19 @@
+<?php
+
+return [
+
+    /*
+    |--------------------------------------------------------------------------
+    | Pagination Language Lines
+    |--------------------------------------------------------------------------
+    |
+    | The following language lines are used by the paginator library to build
+    | the simple pagination links. You are free to change them to anything
+    | you want to customize your views to better match your application.
+    |
+    */
+
+    'previous' => '&laquo; Previous',
+    'next' => 'Next &raquo;',
+
+];

+ 22 - 0
lang/en/passwords.php

@@ -0,0 +1,22 @@
+<?php
+
+return [
+
+    /*
+    |--------------------------------------------------------------------------
+    | Password Reset Language Lines
+    |--------------------------------------------------------------------------
+    |
+    | The following language lines are the default lines which match reasons
+    | that are given by the password broker for a password update attempt
+    | has failed, such as for an invalid token or invalid new password.
+    |
+    */
+
+    'reset' => 'Your password has been reset!',
+    'sent' => 'We have emailed your password reset link!',
+    'throttled' => 'Please wait before retrying.',
+    'token' => 'This password reset token is invalid.',
+    'user' => "We can't find a user with that email address.",
+
+];

+ 151 - 0
lang/en/validation.php

@@ -0,0 +1,151 @@
+<?php
+
+return [
+    'accepted' => 'The :attribute must be accepted.',
+    'accepted_if' => 'The :attribute must be accepted when :other is :value.',
+    'active_url' => 'The :attribute is not a valid URL.',
+    'after' => 'The :attribute must be a date after :date.',
+    'after_or_equal' => 'The :attribute must be a date after or equal to :date.',
+    'alpha' => 'The :attribute must only contain letters.',
+    'alpha_dash' => 'The :attribute must only contain letters, numbers, dashes and underscores.',
+    'alpha_num' => 'The :attribute must only contain letters and numbers.',
+    'array' => 'The :attribute must be an array.',
+    'ascii' => 'The :attribute must only contain single-byte alphanumeric characters and symbols.',
+    'before' => 'The :attribute must be a date before :date.',
+    'before_or_equal' => 'The :attribute must be a date before or equal to :date.',
+    'between' => [
+        'array' => 'The :attribute must have between :min and :max items.',
+        'file' => 'The :attribute must be between :min and :max kilobytes.',
+        'numeric' => 'The :attribute must be between :min and :max.',
+        'string' => 'The :attribute must be between :min and :max characters.',
+    ],
+    'boolean' => 'The :attribute field must be true or false.',
+    'confirmed' => 'The :attribute confirmation does not match.',
+    'current_password' => 'The password is incorrect.',
+    'date' => 'The :attribute is not a valid date.',
+    'date_equals' => 'The :attribute must be a date equal to :date.',
+    'date_format' => 'The :attribute does not match the format :format.',
+    'decimal' => 'The :attribute must have :decimal decimal places.',
+    'declined' => 'The :attribute must be declined.',
+    'declined_if' => 'The :attribute must be declined when :other is :value.',
+    'different' => 'The :attribute and :other must be different.',
+    'digits' => 'The :attribute must be :digits digits.',
+    'digits_between' => 'The :attribute must be between :min and :max digits.',
+    'dimensions' => 'The :attribute has invalid image dimensions.',
+    'distinct' => 'The :attribute field has a duplicate value.',
+    'doesnt_end_with' => 'The :attribute may not end with one of the following: :values.',
+    'doesnt_start_with' => 'The :attribute may not start with one of the following: :values.',
+    'email' => 'The :attribute must be a valid email address.',
+    'ends_with' => 'The :attribute must end with one of the following: :values.',
+    'enum' => 'The selected :attribute is invalid.',
+    'exists' => 'The selected :attribute is invalid.',
+    'file' => 'The :attribute must be a file.',
+    'filled' => 'The :attribute field must have a value.',
+    'gt' => [
+        'array' => 'The :attribute must have more than :value items.',
+        'file' => 'The :attribute must be greater than :value kilobytes.',
+        'numeric' => 'The :attribute must be greater than :value.',
+        'string' => 'The :attribute must be greater than :value characters.',
+    ],
+    'gte' => [
+        'array' => 'The :attribute must have :value items or more.',
+        'file' => 'The :attribute must be greater than or equal to :value kilobytes.',
+        'numeric' => 'The :attribute must be greater than or equal to :value.',
+        'string' => 'The :attribute must be greater than or equal to :value characters.',
+    ],
+    'image' => 'The :attribute must be an image.',
+    'in' => 'The selected :attribute is invalid.',
+    'in_array' => 'The :attribute field does not exist in :other.',
+    'integer' => 'The :attribute must be an integer.',
+    'ip' => 'The :attribute must be a valid IP address.',
+    'ipv4' => 'The :attribute must be a valid IPv4 address.',
+    'ipv6' => 'The :attribute must be a valid IPv6 address.',
+    'json' => 'The :attribute must be a valid JSON string.',
+    'lowercase' => 'The :attribute must be lowercase.',
+    'lt' => [
+        'array' => 'The :attribute must have less than :value items.',
+        'file' => 'The :attribute must be less than :value kilobytes.',
+        'numeric' => 'The :attribute must be less than :value.',
+        'string' => 'The :attribute must be less than :value characters.',
+    ],
+    'lte' => [
+        'array' => 'The :attribute must not have more than :value items.',
+        'file' => 'The :attribute must be less than or equal to :value kilobytes.',
+        'numeric' => 'The :attribute must be less than or equal to :value.',
+        'string' => 'The :attribute must be less than or equal to :value characters.',
+    ],
+    'mac_address' => 'The :attribute must be a valid MAC address.',
+    'max' => [
+        'array' => 'The :attribute must not have more than :max items.',
+        'file' => 'The :attribute must not be greater than :max kilobytes.',
+        'numeric' => 'The :attribute must not be greater than :max.',
+        'string' => 'The :attribute must not be greater than :max characters.',
+    ],
+    'max_digits' => 'The :attribute must not have more than :max digits.',
+    'mimes' => 'The :attribute must be a file of type: :values.',
+    'mimetypes' => 'The :attribute must be a file of type: :values.',
+    'min' => [
+        'array' => 'The :attribute must have at least :min items.',
+        'file' => 'The :attribute must be at least :min kilobytes.',
+        'numeric' => 'The :attribute must be at least :min.',
+        'string' => 'The :attribute must be at least :min characters.',
+    ],
+    'min_digits' => 'The :attribute must have at least :min digits.',
+    'missing' => 'The :attribute field must be missing.',
+    'missing_if' => 'The :attribute field must be missing when :other is :value.',
+    'missing_unless' => 'The :attribute field must be missing unless :other is :value.',
+    'missing_with' => 'The :attribute field must be missing when :values is present.',
+    'missing_with_all' => 'The :attribute field must be missing when :values are present.',
+    'multiple_of' => 'The :attribute must be a multiple of :value.',
+    'not_in' => 'The selected :attribute is invalid.',
+    'not_regex' => 'The :attribute format is invalid.',
+    'numeric' => 'The :attribute must be a number.',
+    'password' => [
+        'letters' => 'The :attribute must contain at least one letter.',
+        'mixed' => 'The :attribute must contain at least one uppercase and one lowercase letter.',
+        'numbers' => 'The :attribute must contain at least one number.',
+        'symbols' => 'The :attribute must contain at least one symbol.',
+        'uncompromised' => 'The given :attribute has appeared in a data leak. Please choose a different :attribute.',
+    ],
+    'present' => 'The :attribute field must be present.',
+    'prohibited' => 'The :attribute field is prohibited.',
+    'prohibited_if' => 'The :attribute field is prohibited when :other is :value.',
+    'prohibited_unless' => 'The :attribute field is prohibited unless :other is in :values.',
+    'prohibits' => 'The :attribute field prohibits :other from being present.',
+    'regex' => 'The :attribute format is invalid.',
+    'required' => 'The :attribute field is required.',
+    'required_array_keys' => 'The :attribute field must contain entries for: :values.',
+    'required_if' => 'The :attribute field is required when :other is :value.',
+    'required_if_accepted' => 'The :attribute field is required when :other is accepted.',
+    'required_unless' => 'The :attribute field is required unless :other is in :values.',
+    'required_with' => 'The :attribute field is required when :values is present.',
+    'required_with_all' => 'The :attribute field is required when :values are present.',
+    'required_without' => 'The :attribute field is required when :values is not present.',
+    'required_without_all' => 'The :attribute field is required when none of :values are present.',
+    'same' => 'The :attribute and :other must match.',
+    'size' => [
+        'array' => 'The :attribute must contain :size items.',
+        'file' => 'The :attribute must be :size kilobytes.',
+        'numeric' => 'The :attribute must be :size.',
+        'string' => 'The :attribute must be :size characters.',
+    ],
+    'starts_with' => 'The :attribute must start with one of the following: :values.',
+    'string' => 'The :attribute must be a string.',
+    'timezone' => 'The :attribute must be a valid timezone.',
+    'unique' => 'The :attribute has already been taken.',
+    'uploaded' => 'The :attribute failed to upload.',
+    'uppercase' => 'The :attribute must be uppercase.',
+    'url' => 'The :attribute must be a valid URL.',
+    'ulid' => 'The :attribute must be a valid ULID.',
+    'uuid' => 'The :attribute must be a valid UUID.',
+
+    'custom' => [
+        'attribute-name' => [
+            'rule-name' => 'custom-message',
+        ],
+    ],
+
+
+    'attributes' => [],
+
+];

+ 1 - 1
routes/api.php

@@ -3,4 +3,4 @@
 use Illuminate\Http\Request;
 use Illuminate\Support\Facades\Route;
 
-Route::post("/admin/auth/login",[\App\Http\Controllers\Admin\AuthController::class,"login"]);
+Route::post("/admin/auth/login",[\App\Modules\Admin\Controllers\Admin\AuthController::class,"login"]);