Bladeren bron

增加一些提示

zhusiqing 4 jaren geleden
bovenliggende
commit
ef548397cf
3 gewijzigde bestanden met toevoegingen van 10 en 5 verwijderingen
  1. 5 1
      src/controllers/index.ts
  2. 1 1
      src/middlewares/auth.ts
  3. 4 3
      src/utils/response.ts

+ 5 - 1
src/controllers/index.ts

@@ -39,6 +39,10 @@ const login = async (ctx: Context) => {
   }
   if (username === dbInfo.username && password === dbInfo.password) {
     if (ctx.session) {
+      if (ctx.session.user) {
+        ctx.body = ctx.$response(null, '用户已登录', true)
+        return
+      }
       ctx.session.user = username
     }
     ctx.body = ctx.$response({ username }, '登录成功', true)
@@ -48,7 +52,7 @@ const login = async (ctx: Context) => {
 }
 const userInfo = async (ctx: Context) => {
   if (!ctx.session || !ctx.session.user ) {
-    ctx.body = ctx.$response(null, '未登录', false)
+    ctx.body = ctx.$response(null, '未登录', false, 400000)
     return
   }
   const session: sessionInterface = ctx.session.toJSON()

+ 1 - 1
src/middlewares/auth.ts

@@ -13,7 +13,7 @@ export default () => {
       const session: sessionInterface = ctx.session.toJSON()
       if (!session.user) {
         ctx.session.user = null
-        ctx.body = ctx.$response(null, '未登录', false)
+        ctx.body = ctx.$response(null, '未登录', false, 400000)
         return
       }
       await next()

+ 4 - 3
src/utils/response.ts

@@ -1,12 +1,13 @@
 interface responseHandle {
-  (data: any, message?: string, success?: boolean): { data: any, message: string, success: boolean }
+  (data: any, message?: string, success?: boolean, code?: number): { data: any, message: string, success: boolean, code: number }
 }
 
-const responseHandle: responseHandle = (data, message = 'success', success = true) => {
+const responseHandle: responseHandle = (data, message = 'success', success = true, code = 200) => {
   return {
     data,
     message,
-    success
+    success,
+    code
   };
 };