Ver Fonte

7-13前端登录登录逻辑1

Home há 5 anos atrás
pai
commit
b719f55c05

+ 40 - 0
app/api/controller/Login.php

@@ -0,0 +1,40 @@
+<?php
+/**
+ * Created by PhpStorm
+ * User:林志杰
+ * Email:[email protected]
+ * Motto:纵有疾风起,人生不言弃!
+ * Time:2020/2/8 1:10
+ */
+
+declare(strict_types=1);
+
+namespace app\api\controller;
+
+use app\api\validate\User as UserValidate;
+use app\BaseController;
+use app\common\business\User as UserBusiness;
+
+class Login extends BaseController
+{
+    public function index(): object
+    {
+        $phoneNumber = $this->request->param("phone_number", '', 'trim');
+        $code = input('param.code', 0, 'intval');
+        $type = input('param.type', 0, 'intval');
+        // 参数校验
+        $data = [
+            'phoneNumber' => $phoneNumber,
+            'code' => $code,
+            'type' => $type,
+        ];
+        $validate = new UserValidate();
+        if (!$validate->scene('login')->check($data)) {
+            return show(config('status.error'), $validate->getError());
+        }
+
+        $result = (new UserBusiness())->login($data);
+
+        return show(config('status.success'), "登陆成功");
+    }
+}

+ 3 - 0
app/api/exception/Http.php

@@ -30,6 +30,9 @@ class Http extends Handle
      */
     public function render($request, Throwable $e): Response
     {
+        if ($e instanceof \think\Exception) {
+            return show($e->getCode(), $e->getMessage());
+        }
         if (method_exists($e, "getStatusCode")) {
             $httpStatus = $e->getStatusCode();
         } else {

+ 9 - 3
app/api/validate/User.php

@@ -18,15 +18,21 @@ class User extends Validate
     protected $rule = [
         'username' => 'require',
         'phoneNumber' => 'require',
+        'code' => 'require|number|min:4',
+        'type' => 'require|in:1,2',
     ];
 
     protected $message = [
         'username' => '用户名必须',
         'phoneNumber' => '电话号码必须',
+        'code.require' => '短信验证码必须',
+        'code.number' => '短信验证码为数字',
+        'code.min' => '短信验证码长度不得低于4',
+        'type.require' => '类型必须',
+        'type.in' => '类型数值错误',
     ];
     protected $scene = [
-        'send_code' => [
-            'phoneNumber'
-        ]
+        'send_code' => ['phoneNumber'],
+        'login' => ['phoneNumber', 'code', 'type'],
     ];
 }

+ 32 - 0
app/common/business/User.php

@@ -0,0 +1,32 @@
+<?php
+/**
+ * Created by PhpStorm
+ * User:林志杰
+ * Email:[email protected]
+ * Motto:纵有疾风起,人生不言弃!
+ * Time:2020/2/8 1:24
+ */
+
+declare(strict_types=1);
+
+namespace app\common\business;
+
+use app\common\model\mysql\User as UserModel;
+
+class User
+{
+    public $userObj = null;
+
+    public function __construct()
+    {
+        $this->userObj = new UserModel();
+    }
+
+    public function login(array $data)
+    {
+        $redisCode = cache(config('redis.code_pre') . $data['phoneNumber']);
+        if (empty($redisCode) || $redisCode !== $data['code']) {
+            throw new \think\Exception('验证码不存在',-1009);
+        }
+    }
+}

+ 56 - 0
app/common/model/mysql/User.php

@@ -0,0 +1,56 @@
+<?php
+/**
+ * Created by PhpStorm
+ * User:林志杰
+ * Email:[email protected]
+ * Motto:纵有疾风起,人生不言弃!
+ * Time:2020/1/3 0:29
+ */
+
+namespace app\common\model\mysql;
+
+
+use think\Model;
+
+class User extends Model
+{
+    /**
+     * 根据用户名获取后端用户表的数据
+     * @param $username
+     * @return array|bool|Model|null
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public function getAdminUserByUsername($username)
+    {
+        if (empty($username)) {
+            return false;
+        }
+
+        $where = [
+            'username' => trim($username),
+        ];
+        return $this->where($where)->find();
+    }
+
+    /**
+     * 根据主键id更新数据表中的数据
+     * @param $id
+     * @param $data
+     * @return bool
+     */
+    public function updateById($id, $data)
+    {
+        $id = (int)$id;
+        if (empty($id) || empty($data) || !is_array($data)) {
+            return false;
+        }
+
+        $where = [
+            'id' => $id,
+        ];
+
+        return $this->where($where)->save($data);
+    }
+}