Răsfoiți Sursa

添加第一个接口的说明

max 7 luni în urmă
părinte
comite
0ffe4a1130

+ 1 - 0
package.json

@@ -64,6 +64,7 @@
   "scripts": {
     "start": "NODE_ENV=production node ./bootstrap.js",
     "dev": "cross-env NODE_ENV=local mwtsc --watch --run @midwayjs/mock/app.js --keepalive",
+    "dev:max": "cross-env NODE_ENV=max mwtsc --watch --run @midwayjs/mock/app.js --keepalive",
     "cov": "jest --coverage",
     "lint": "mwts check",
     "lint:fix": "mwts fix",

+ 43 - 0
src/config/config.max.ts

@@ -0,0 +1,43 @@
+import { CoolConfig } from '@cool-midway/core';
+import { MidwayConfig } from '@midwayjs/core';
+
+/**
+ * 本地开发 npm run dev 读取的配置文件
+ */
+export default {
+  typeorm: {
+    dataSource: {
+      default: {
+        type: 'mysql',
+        host: '192.168.2.101',
+        port: 6806,
+        username: 'root',
+        password: 'admin',
+        database: 'va',
+        // 自动建表 注意:线上部署的时候不要使用,有可能导致数据丢失
+        synchronize: true,
+        // 打印日志
+        logging: false,
+        // 字符集
+        charset: 'utf8mb4',
+        // 是否开启缓存
+        cache: true,
+        // 实体路径
+        entities: ['**/modules/*/entity'],
+      },
+    },
+  },
+  callback: {
+    sunpay: 'http://localhost:8000/payment/webhook/sunpay',
+  },
+  cool: {
+    // 实体与路径,跟生成代码、前端请求、swagger文档相关 注意:线上不建议开启,以免暴露敏感信息
+    eps: true,
+    // 是否自动导入模块数据库
+    initDB: true,
+    // 判断是否初始化的方式
+    initJudge: 'db',
+    // 是否自动导入模块菜单
+    initMenu: true,
+  } as CoolConfig,
+} as MidwayConfig;

+ 2 - 7
src/modules/api/config.ts

@@ -12,16 +12,11 @@ export default () => {
     // 模块描述
     description: '所有对外露出的接口',
     // 中间件
-    globalMiddlewares: [BaseAuthorityMiddleware],
+    // globalMiddlewares: [BaseAuthorityMiddleware],
     // 模块加载顺序,默认为0,值越大越优先加载
     order: 0,
     // app参数配置允许读取的key
-    allowKeys: [
-      'SunPay-Key',
-      'SunPay-Timestamp',
-      'SunPay-Nonce',
-      'SunPay-Sign',
-    ],
+    allowKeys: [],
     // jwt 生成解密token的
     // jwt: {
     //   // 单点登录

+ 13 - 11
src/modules/api/controller/customer.ts

@@ -1,15 +1,18 @@
-0;
+import {CreateCustomerDTO} from "../dto/customer";
 import { CoolController, BaseController } from '@cool-midway/core';
 import {
   ALL,
   Body,
   Get,
-  Inject,
+  Inject, Param,
   Post,
   Provide,
   Put,
 } from '@midwayjs/decorator';
 import { BusinessEntity } from '../../payment/entity/business';
+import {Validate} from "@midwayjs/validate";
+import {BusinessService} from "../../payment/service/business";
+import {PaymentService} from "../../payment/service/payment";
 
 /**
  * 客户管理
@@ -17,19 +20,18 @@ import { BusinessEntity } from '../../payment/entity/business';
 @Provide()
 @CoolController('/api/v1/Fiat')
 export class CustomerController extends BaseController {
+
+  @Inject()
+  businessService: BusinessService;
   /**
    * 创建客户
    */
   @Post('/CreateCustomer', { summary: '创建客户' })
-  async createCustomer(@Body(ALL) business: BusinessEntity) {
-    // if (!this.allowKeys.includes(key)) {
-    //   return this.fail('非法操作');
-    // }
-    // 关键参数校验
-    // 数据落库
-    // 回调
-    console.log(business);
-    return this.ok('hello, cool-admin!!!');
+  @Validate({
+    errorStatus: 422,
+  })
+  async createCustomer(@Body() params: CreateCustomerDTO) {
+    return this.ok(await this.businessService.add(params));
   }
   /**
    * 获取创建客户必填字段

+ 38 - 0
src/modules/api/dto/customer.ts

@@ -0,0 +1,38 @@
+import { Rule, RuleType } from '@midwayjs/validate';
+import {ApiProperty} from "@midwayjs/swagger";
+
+export class CreateCustomerDTO {
+  @ApiProperty({ example: 'out_user_id', description: '客户端用户ID'})
+  @Rule(RuleType.string().required())
+  out_user_id: string;
+
+  @ApiProperty({ example: 'webhook_url', description: '业务回调URL'})
+  @Rule(RuleType.string().required())
+  webhook_url: string;
+
+  @ApiProperty({ example: 'webhook_url', description: '业务回调URL'})
+  @Rule(RuleType.string().required())
+  customer_type: string;
+
+  @ApiProperty({ example: 1, description: 'The name of the Catage'})
+  age: number;
+
+  @ApiProperty({ example: 'OTHER', description: '类型,可选值如下:\n' +
+      '\n' +
+      'OTHER:其他\n' +
+      '\n' +
+      'PUBLIC_LIMITED_COMPANY:公共有限公司\n' +
+      '\n' +
+      'PARTNERSHIP:合伙企业\n' +
+      '\n' +
+      'CHARITY:慈善机构\n' +
+      '\n' +
+      'PRIVATE_LIMITED_COMPANY:私营有限公司\n' +
+      '\n' +
+      'JOINT_STOCK_COMPANY:股份公司\n' +
+      '\n' +
+      'SOLE_TRADER:独资经营企业\n' +
+      '\n' +
+      'LIMITED_LIABILITY:有限责任公司'})
+  type: string;
+}

+ 28 - 24
src/modules/api/middleware/authority.ts

@@ -40,31 +40,35 @@ export class BaseAuthorityMiddleware
   resolve() {
     return async (ctx: Context, next: NextFunction) => {
       // 签名校验
-      let { header } = ctx;
-      const {
-        'va-key': vaKey = '',
-        'va-timestamp': vaTimestamp = '',
-        'va-nonce': vaNonce = '',
-        'va-sign': vaSign = '',
-      } = header;
-      const params =
-        ctx?.req.method === 'GET' ? ctx?.request.query : ctx?.request.body;
+      let { header, url } = ctx;
+      const adminUrl = '/api/v1/';
+      // 路由地址为 api/v1 前缀的 需要权限校验
+      if (url.includes(adminUrl)) {
+        const {
+          'va-key': vaKey = '',
+          'va-timestamp': vaTimestamp = '',
+          'va-nonce': vaNonce = '',
+          'va-sign': vaSign = '',
+        } = header;
+        const params =
+          ctx?.req.method === 'GET' ? ctx?.request.query : ctx?.request.body;
 
-      // 2. 生成签名
-      const sign = this.generateSignature(
-        `${vaTimestamp}`,
-        `${vaNonce}`,
-        JSON.stringify(params),
-        `${vaKey}`
-      );
-
-      if (sign !== vaSign) {
-        ctx.status = 401;
-        ctx.body = {
-          code: RESCODE.AUTHFAIL,
-          message: '签名不匹配,认证失败',
-        };
-        return;
+        // 2. 生成签名
+        const sign = this.generateSignature(
+          `${vaTimestamp}`,
+          `${vaNonce}`,
+          JSON.stringify(params),
+          `${vaKey}`
+        );
+        console.log(6363636, {sign, vaSign})
+        if (sign !== vaSign) {
+          ctx.status = 401;
+          ctx.body = {
+            code: RESCODE.AUTHFAIL,
+            message: '签名不匹配,认证失败',
+          };
+          return;
+        }
       }
 
       await next();