浏览代码

万能验证码

kphcdr 1 年之前
父节点
当前提交
5e8cd784fc

+ 24 - 6
app/Modules/Mini/Controllers/AuthController.php

@@ -3,8 +3,11 @@
 namespace App\Modules\Mini\Controllers;
 
 use App\Base\BaseController;
+use App\Base\Validation\Phone;
+use App\Exceptions\ClientException;
 use App\Models\User\User;
 use App\Modules\Mini\Services\AuthService;
+use App\Modules\Mini\Services\CaptchaService;
 use App\Modules\Mini\Services\WechatService;
 use App\Modules\Mini\Services\EmailService;
 use Illuminate\Support\Facades\Auth;
@@ -96,19 +99,34 @@ class AuthController extends BaseController
 
     public function register()
     {
+        $params = $this->valid([
+            "email" => "required|email",
+            "phone" => ["required", new Phone()],
+            "password" => "required",
+            "captcha" => "required",
+        ]);
+        if (!app(CaptchaService::class)->checkCaptcha($params['email'], $params['captcha'])) {
+            throw new ClientException("验证码错误");
+        }
+        $user = app(AuthService::class)->register($params);
         return $this->ok([
-            "token" => "token",
-            "name" => "",
-            "phone" => "",
+            "token" => app(AuthService::class)->encryptToken($user->id),
+            "name" => $user->name,
+            "phone" => $user->phone,
         ]);
     }
 
     public function login()
     {
+        $params = $this->valid([
+            "userinfo" => "required",
+            "password" => "required",
+        ]);
+        $user = app(AuthService::class)->login($params);
         return $this->ok([
-            "token" => "token",
-            "name" => "",
-            "phone" => "",
+            "token" => app(AuthService::class)->encryptToken($user->id),
+            "name" => $user->name,
+            "phone" => $user->phone,
         ]);
     }
 

+ 11 - 1
app/Modules/Mini/Services/AuthService.php

@@ -24,7 +24,17 @@ class AuthService extends BaseService
 
     public function login($params)
     {
-
+        $user = User::where("email", $params['userinfo'])->orWhere("phone", $params['userinfo'])->first();
+        if (is_null($user)) {
+            throw new ClientException("用户不存在");
+        }
+        if ($user->status == User::STATUS_STOP) {
+            throw new ClientException("账户已禁用");
+        }
+        if (!$user->checkPassword($params['password'])) {
+            throw new ClientException("账户或密码错误");
+        }
+        return $user;
     }
 
     public function register($params)

+ 0 - 32
app/Modules/Pc/Controllers/AuthController.php

@@ -39,38 +39,6 @@ class AuthController extends BaseController
         ]);
     }
 
-    public function register()
-    {
-        $params = $this->valid([
-            "email" => "required|email",
-            "phone" => ["required", new Phone()],
-            "password" => "required",
-            "captcha" => "required",
-        ]);
-        if (!app(CaptchaService::class)->checkCaptcha($params['email'], $params['captcha'])) {
-            throw new ClientException("验证码错误");
-        }
-        $user = app(AuthService::class)->register($params);
-        return $this->ok([
-            "token" => app(AuthService::class)->encryptToken($user->id),
-            "name" => $user->name,
-            "phone" => $user->phone,
-        ]);
-    }
-
-    public function login()
-    {
-        $params = $this->valid([
-            "userinfo" => "required",
-            "password" => "required",
-        ]);
-        app(AuthService::class)->login($params);
-        return $this->ok([
-            "token" => "token",
-            "name" => "",
-            "phone" => "",
-        ]);
-    }
 
     public function resetPassword()
     {

+ 0 - 42
app/Modules/Pc/Controllers/CartController.php

@@ -1,42 +0,0 @@
-<?php
-
-namespace App\Modules\Pc\Controllers;
-
-use App\Base\BaseController;
-use Faker\Provider\Image;
-
-class CartController extends BaseController
-{
-    public function index()
-    {
-        return $this->ok([
-            "total" => 1,
-            "page_total" => 1,
-            "list" => [
-                [
-                    "id" => 1,
-                    "goods_id" => 1,
-                    "thumb" => Image::imageUrl(),
-                    "goods_name" => 1,
-                    "sn" => "型号-xxxx",
-                    "count" => 1,
-                ],
-            ],
-        ]);
-    }
-
-    public function del()
-    {
-        return $this->ok();
-    }
-
-    public function ask()
-    {
-        return $this->ok([
-            "id" => 1,
-            "thumb" => Image::imageUrl(),
-            "name" => "商品名称",
-            "url" => "http://baidu.com/xxxx",
-        ]);
-    }
-}

+ 0 - 97
app/Modules/Pc/Controllers/GoodsController.php

@@ -1,97 +0,0 @@
-<?php
-
-namespace App\Modules\Pc\Controllers;
-
-use App\Base\BaseController;
-use App\Modules\Mini\Services\PageService;
-use Faker\Provider\Image;
-
-class GoodsController extends BaseController
-{
-    protected PageService $service;
-
-    /**
-     * @param PageService $service
-     */
-    public function __construct(PageService $service)
-    {
-        $this->service = $service;
-    }
-
-    public function goods()
-    {
-        return $this->ok([
-            "id" => 1,
-            "image_list" => [
-                Image::imageUrl(),
-                Image::imageUrl(),
-            ],
-            "spec" => [
-                "id" => 1,
-                "name" => "颜色",
-                "attr" => [
-                    [
-                        "id" => 1,
-                        "name" => "红色",
-                    ],
-                ],
-            ],
-            "sku" => [
-                [
-                    "id" => 1,
-                    "sn" => "sn",
-                    "url_3d" => "",
-                    "spec_attr" => [
-                        [
-                            "id" => 1,
-                            "name" => "白色",
-                        ],
-                        [
-                            "id" => 3,
-                            "name" => "大号",
-                        ],
-                    ],
-                ],
-            ],
-            "desc_html" => "<h1>desc_html</h1>",
-            "spec_attr_html" => "<h1>desc_html</h1>",
-            "faq_html" => "<h1>desc_html</h1>",
-        ]);
-    }
-
-    public function search()
-    {
-        return $this->ok([
-            "total" => 1,
-            "page_total" => 1,
-            "list" => [
-                [
-                    "id" => 1,
-                    "name" => "商品名称",
-                    "thumb" => Image::imageUrl(),
-                ],
-            ],
-            "category" => [
-                [
-                    "id" => 1,
-                    "thumb" => Image::imageUrl(),
-                    "name" => "分类名称",
-                ],
-            ],
-        ]);
-    }
-
-    public function fav()
-    {
-        return $this->ok();
-    }
-
-    public function ask()
-    {
-        return $this->ok([
-            "id" => 1,
-            "thumb" => Image::imageUrl(),
-            "name" => "商品名称",
-        ]);
-    }
-}

+ 0 - 142
app/Modules/Pc/Controllers/PageController.php

@@ -1,142 +0,0 @@
-<?php
-
-namespace App\Modules\Pc\Controllers;
-
-use App\Base\BaseController;
-use App\Modules\Mini\Services\PageService;
-use Faker\Provider\Image;
-
-class PageController extends BaseController
-{
-    protected PageService $service;
-
-    /**
-     * @param PageService $service
-     */
-    public function __construct(PageService $service)
-    {
-        $this->service = $service;
-    }
-
-    public function filterConfig()
-    {
-        return $this->ok([
-            [
-                "id" => 1,
-                "name" => "类目示例",
-                "type" => "category",
-                "child" => [
-                    [
-                        "id" => 1,
-                        "name" => "名称",
-                    ],
-                ],
-            ],
-            [
-                "id" => 1,
-                "name" => "属性示例",
-                "type" => "attr",
-                "child" => [
-                    [
-                        "id" => 1,
-                        "name" => "名称",
-                    ],
-                ],
-            ],
-        ]);
-    }
-
-    public function home()
-    {
-        return $this->ok([
-            "banner" => $this->service->banner(),
-            "categoryTree" => [
-                [
-                    "id" => 1,
-                    "name" => "一级分类",
-                    "child" => [
-                        [
-                            "id" => 1,
-                            "name" => "二级分类",
-                        ],
-                    ],
-                ],
-            ],
-            "hot_goods" => [
-                [
-                    "id" => 1,
-                    "name" => "商品名称",
-                    "thumb" => Image::imageUrl(),
-                ],
-            ],
-            "recommend_goods" => [
-                [
-                    "id" => 1,
-                    "name" => "商品名称",
-                    "thumb" => Image::imageUrl(),
-                ],
-            ],
-        ]);
-    }
-
-    public function categoryGoods()
-    {
-        return $this->ok([
-            "total" => 1,
-            "page_total" => 1,
-            "list" => [
-                [
-                    "id" => 1,
-                    "name" => "商品名称",
-                    "thumb" => Image::imageUrl(),
-                ],
-            ],
-        ]);
-    }
-
-    public function categoryTree()
-    {
-        return $this->ok([
-            "categoryTree" => [
-                [
-                    "id" => 1,
-                    "name" => "一级分类",
-                    "child" => [
-                        [
-                            "id" => 1,
-                            "name" => "二级分类",
-                            "child" => [
-                                [
-                                    "id" => 1,
-                                    "name" => "二级分类",
-                                ],
-                            ],
-                        ],
-                    ],
-                ],
-            ],
-        ]);
-    }
-
-    public function search()
-    {
-        return $this->ok([
-            "total" => 1,
-            "page_total" => 1,
-            "list" => [
-                [
-                    "id" => 1,
-                    "name" => "商品名称",
-                    "thumb" => Image::imageUrl(),
-                ],
-            ],
-            "category" => [
-                [
-                    "id" => 1,
-                    "thumb" => Image::imageUrl(),
-                    "name" => "分类名称",
-                ],
-            ],
-        ]);
-    }
-}

+ 0 - 63
app/Modules/Pc/Controllers/UserController.php

@@ -1,63 +0,0 @@
-<?php
-
-namespace App\Modules\Pc\Controllers;
-
-use App\Base\BaseController;
-use Faker\Provider\Image;
-
-class UserController extends BaseController
-{
-    public function company()
-    {
-        return $this->ok();
-    }
-
-    public function fav()
-    {
-        return $this->ok([
-            "total" => 1,
-            "page_total" => 1,
-            "list" => [
-                [
-                    "id" => 1,
-                    "goods_id" => 1,
-                    "thumb" => Image::imageUrl(),
-                    "goods_name" => 1,
-                ],
-            ],
-        ]);
-    }
-
-    public function favDel()
-    {
-        return $this->ok();
-    }
-
-    public function address()
-    {
-        return $this->ok([
-            "total" => 1,
-            "page_total" => 1,
-            "list" => [
-                [
-                    "id" => 1,
-                    "is_default" => 1,
-                    "name" => "收件人姓名",
-                    "address" => "余杭良睦路地铁鼎创中心**位置",
-                    "phone" => "15012341234",
-                    "location" => "浙江省杭州市余杭区",
-                ],
-            ],
-        ]);
-    }
-
-    public function addressDel()
-    {
-        return $this->ok();
-    }
-
-    public function addressStore()
-    {
-        return $this->ok();
-    }
-}

+ 4 - 4
routes/pc.php

@@ -8,10 +8,10 @@ Route::get('/', [\App\Http\Controllers\Controller::class, "ping"]);
 Route::post("/auth/codeToSession", [\App\Modules\Mini\Controllers\AuthController::class, "codeToSession"]);
 Route::get("/auth/test", [\App\Modules\Mini\Controllers\AuthController::class, "test"]);
 
-Route::post("/auth/emailCaptcha", [\App\Modules\Pc\Controllers\AuthController::class, "emailCaptcha"]);
-Route::post("/auth/register", [\App\Modules\Pc\Controllers\AuthController::class, "register"]);
-Route::post("/auth/login", [\App\Modules\Pc\Controllers\AuthController::class, "login"]);
-Route::post("/auth/resetPassword", [\App\Modules\Pc\Controllers\AuthController::class, "resetPassword"]);
+Route::post("/auth/emailCaptcha", [\App\Modules\Mini\Controllers\AuthController::class, "emailCaptcha"]);
+Route::post("/auth/register", [\App\Modules\Mini\Controllers\AuthController::class, "register"]);
+Route::post("/auth/login", [\App\Modules\Mini\Controllers\AuthController::class, "login"]);
+Route::post("/auth/resetPassword", [\App\Modules\Mini\Controllers\AuthController::class, "resetPassword"]);
 Route::middleware([\App\Http\Middleware\CheckAuthMiddleware::class])->group(function () {
     Route::post("/auth/decryptUserInfo", [\App\Modules\Mini\Controllers\AuthController::class, "decryptUserInfo"]);
     Route::post("/auth/decryptPhone", [\App\Modules\Mini\Controllers\AuthController::class, "decryptPhone"]);