zs 1 год назад
Родитель
Сommit
f6eb404118

+ 8 - 0
.eslintrc.json

@@ -3,5 +3,13 @@
   "ignorePatterns": ["node_modules", "dist", "test", "jest.config.js", "typings"],
   "env": {
     "jest": true
+  },
+  "rules": {
+    "max-len": [
+      "warn",
+      {
+        "code": 250
+      }
+    ]
   }
 }

+ 4 - 0
.gitignore

@@ -13,3 +13,7 @@ run/
 *.un~
 .tsbuildinfo
 .tsbuildinfo.*
+src/config/config.local.ts
+upload
+uploadTemp/
+

+ 1 - 0
package.json

@@ -8,6 +8,7 @@
     "@midwayjs/core": "^3.12.0",
     "@midwayjs/decorator": "^3.12.0",
     "@midwayjs/info": "^3.12.0",
+    "@midwayjs/jwt": "^3.12.10",
     "@midwayjs/koa": "^3.12.0",
     "@midwayjs/logger": "^2.14.0",
     "@midwayjs/redis": "^3.12.10",

+ 33 - 2
src/config/config.default.ts

@@ -1,9 +1,40 @@
 import { MidwayConfig } from '@midwayjs/core';
-
+const ip = '127.0.0.1';
+const project = 'match_v1';
 export default {
   // use for cookie sign key, should change to your own and keep security
   keys: '1699325099761_3014',
   koa: {
-    port: 7001,
+    port: 8890,
+    globalPrefix: '/match/api',
+  },
+  jwt: {
+    secret: 'Ziyouyanfa!@#',
+    expiresIn: '2d',
+  },
+  mongoose: {
+    dataSource: {
+      default: {
+        uri: `mongodb://${ip}:27017/${project}`,
+        options: {
+          user: 'admin',
+          pass: 'admin',
+          authSource: 'admin',
+          useNewUrlParser: true,
+        },
+        entities: ['./entity'],
+      },
+    },
+  },
+  redis: {
+    client: {
+      port: 6379, // Redis port
+      host: ip, // Redis host
+      password: '123456',
+      db: 4,
+    },
+  },
+  upload: {
+    whitelist: null,
   },
 } as MidwayConfig;

+ 23 - 8
src/configuration.ts

@@ -1,16 +1,31 @@
-import { Configuration, App } from '@midwayjs/core';
+import {
+  Configuration,
+  App,
+  Inject,
+  MidwayDecoratorService,
+} from '@midwayjs/core';
 import * as koa from '@midwayjs/koa';
 import * as validate from '@midwayjs/validate';
 import * as info from '@midwayjs/info';
 import { join } from 'path';
-// import { DefaultErrorFilter } from './filter/default.filter';
-// import { NotFoundFilter } from './filter/notfound.filter';
 import { ReportMiddleware } from './middleware/report.middleware';
-
+import * as FreeFrame from 'free-midway-component';
+import * as redis from '@midwayjs/redis';
+import * as swagger from '@midwayjs/swagger';
+import * as jwt from '@midwayjs/jwt';
+// 控制器执行前函数
+import { CheckTokenMiddleware } from './middleware/checkToken.middleware';
 @Configuration({
   imports: [
     koa,
     validate,
+    FreeFrame,
+    redis,
+    jwt,
+    {
+      component: swagger,
+      enabledEnvironment: ['local'],
+    },
     {
       component: info,
       enabledEnvironment: ['local'],
@@ -21,11 +36,11 @@ import { ReportMiddleware } from './middleware/report.middleware';
 export class MainConfiguration {
   @App('koa')
   app: koa.Application;
-
+  @Inject()
+  decoratorService: MidwayDecoratorService;
   async onReady() {
-    // add middleware
+    // 中间件
     this.app.useMiddleware([ReportMiddleware]);
-    // add filter
-    // this.app.useFilter([NotFoundFilter, DefaultErrorFilter]);
+    this.app.getMiddleware().insertFirst(CheckTokenMiddleware);
   }
 }

+ 0 - 18
src/controller/api.controller.ts

@@ -1,18 +0,0 @@
-import { Inject, Controller, Get, Query } from '@midwayjs/core';
-import { Context } from '@midwayjs/koa';
-import { UserService } from '../service/user.service';
-
-@Controller('/api')
-export class APIController {
-  @Inject()
-  ctx: Context;
-
-  @Inject()
-  userService: UserService;
-
-  @Get('/get_user')
-  async getUser(@Query('uid') uid) {
-    const user = await this.userService.getUser({ uid });
-    return { success: true, message: 'OK', data: user };
-  }
-}

+ 0 - 9
src/controller/home.controller.ts

@@ -1,9 +0,0 @@
-import { Controller, Get } from '@midwayjs/core';
-
-@Controller('/')
-export class HomeController {
-  @Get('/')
-  async home(): Promise<string> {
-    return 'Hello Midwayjs!';
-  }
-}

+ 33 - 0
src/middleware/checkToken.middleware.ts

@@ -0,0 +1,33 @@
+import { IMiddleware } from '@midwayjs/core';
+import { Middleware, Inject } from '@midwayjs/decorator';
+import { NextFunction, Context } from '@midwayjs/koa';
+import get = require('lodash/get');
+import { JwtService } from '@midwayjs/jwt';
+
+@Middleware()
+export class CheckTokenMiddleware
+  implements IMiddleware<Context, NextFunction>
+{
+  @Inject()
+  jwtService: JwtService;
+  resolve() {
+    return async (ctx: Context, next: NextFunction) => {
+      const token: any = get(ctx.request, 'header.token');
+      if (token) {
+        const data = this.jwtService.decodeSync(token);
+        if (data) ctx.user = data;
+      }
+      // 添加管理员身份
+      const adminToken: any = get(ctx.request, 'header.admin-token');
+      if (adminToken) {
+        const data = this.jwtService.decodeSync(adminToken);
+        if (data) ctx.admin = data;
+      }
+      await next();
+    };
+  }
+
+  static getName(): string {
+    return 'checkToken';
+  }
+}

+ 0 - 14
src/service/user.service.ts

@@ -1,14 +0,0 @@
-import { Provide } from '@midwayjs/core';
-import { IUserOptions } from '../interface';
-
-@Provide()
-export class UserService {
-  async getUser(options: IUserOptions) {
-    return {
-      uid: options.uid,
-      username: 'mockedName',
-      phone: '12345678901',
-      email: 'xxx.xxx@xxx.com',
-    };
-  }
-}

+ 2 - 1
tsconfig.json

@@ -15,7 +15,8 @@
     "declaration": true,
     "forceConsistentCasingInFileNames": true,
     "typeRoots": [ "./typings", "./node_modules/@types"],
-    "outDir": "dist"
+    "outDir": "dist",
+    "removeComments": true,
   },
   "exclude": [
     "dist",