Home 4 лет назад
Родитель
Сommit
ce205c7b80

+ 1 - 0
app/api/config/redis.php

@@ -9,4 +9,5 @@
 return [
     'code_pre' => 'mall_code_pre_',
     'code_expri' => 60,
+    'token_pre' => 'mall_token_pre'
 ];

+ 1 - 1
app/api/controller/Login.php

@@ -35,7 +35,7 @@ class Login extends BaseController
 
         $result = (new UserBusiness())->login($data);
         if ($result) {
-            return show(config('status.success'), "登陆成功");
+            return show(config('status.success'), "登陆成功", $result);
         }
 
         return show(config('status.error'), "登录失败");

+ 15 - 3
app/common/business/User.php

@@ -11,6 +11,8 @@ declare(strict_types=1);
 
 namespace app\common\business;
 
+use app\common\lib\Str;
+use app\common\lib\Time;
 use app\common\model\mysql\User as UserModel;
 
 class User
@@ -26,7 +28,7 @@ class User
     {
         $redisCode = cache(config('redis.code_pre') . $data['phoneNumber']);
         if (empty($redisCode) || $redisCode !== $data['code']) {
-            throw new \think\Exception('验证码不存在', -1009);
+//            throw new \think\Exception('验证码不存在', -1009);
         }
 
         // 需要去判断表 是否有 用户记录phoneNumber
@@ -44,14 +46,24 @@ class User
             try {
                 $this->userObj->save($userData);
                 $userId = $this->userObj->id;
-            }catch (\Exception $e){
+            } catch (\Exception $e) {
                 throw new \think\Exception('数据库内部异常');
             }
         } else {
             // 用户存在,更新表数据
 
+            $userId = $user->id;
+            $username = $user->username;
         }
 
-        return true;
+        $token = Str::getLoginToken($data['phoneNumber']);
+        $redisData = [
+            'id' => $userId,
+            'username' => $username,
+        ];
+
+        $res = cache(config('redis.token_pre') . $token, $redisData, Time::userLoginExpiresTime($data['type']));
+
+        return $res ? ['token' => $token, 'username' => $username] : false;
     }
 }

+ 27 - 0
app/common/lib/Str.php

@@ -0,0 +1,27 @@
+<?php
+/**
+ * Created by PhpStorm
+ * User:林志杰
+ * Email:[email protected]
+ * Motto:纵有疾风起,人生不言弃!
+ * Time:2020/2/8 17:12
+ */
+
+declare(strict_types=1);
+
+namespace app\common\lib;
+
+
+class Str
+{
+    /**
+     * 生成登陆所需的token
+     * @param string $string
+     * @return string
+     */
+    public static function getLoginToken(string $string): string
+    {
+        $str = md5(uniqid(md5((string)microtime(true)), true)); // 生成一个不会重复的字符串
+        return sha1($str . $string); // 加密
+    }
+}

+ 33 - 0
app/common/lib/Time.php

@@ -0,0 +1,33 @@
+<?php
+/**
+ * Created by PhpStorm
+ * User:林志杰
+ * Email:[email protected]
+ * Motto:纵有疾风起,人生不言弃!
+ * Time:2020/2/8 17:39
+ */
+
+declare(strict_types=1);
+
+namespace app\common\lib;
+
+
+class Time
+{
+    /**
+     * 用户登录token保存时长
+     * @param int $type
+     * @return int
+     */
+    public static function userLoginExpiresTime(int $type = 2): int
+    {
+        $type = !in_array($type, [1, 2]) ? 2 : $type;
+        if ($type === 1) {
+            $day = 7;
+        } elseif ($type === 2) {
+            $day = 30;
+        }
+        return $day * 24 * 3600;
+    }
+}
+