lrf 2 năm trước cách đây
mục cha
commit
2c65df7e0f

+ 2 - 2
src/controller/BaseController.ts

@@ -9,12 +9,12 @@ export abstract class BaseController {
   @Inject()
   ctx: Context;
 
+  abstract create(...args);
   abstract query(...args);
   abstract fetch(...args);
   abstract update(...args);
-  abstract updateMany(...args);
   abstract delete(...args);
+  abstract updateMany(...args);
   abstract deleteMany(...args);
-  abstract create(...args);
   abstract createMany(...args);
 }

+ 14 - 1
src/error/service.error.ts

@@ -5,18 +5,30 @@ export enum ErrorCode {
   UNKNOWN = '-1',
   /** 缺少地址参数 */
   NEED_PARAMS = '-2',
+  /** 错误地址参数 */
+  BAD_PARAMS = '-20',
   /** 缺少地址后参数 */
   NEED_QUERY = '-3',
+  /** 错误地址后参数 */
+  BAD_QUERY = '-30',
   /** 缺少body参数 */
   NEED_BODY = '-4',
+  /** 错误body参数 */
+  BAD_BODY = '-40',
   /** 缺少参数(函数调用中) */
   NEED_ARGS = '-5',
+  /** 错误参数 */
+  BAD_ARGS = '-50',
   /** 未找到数据 */
   NOT_FOUND_DATA = '-10',
+  /** 未找到数据,写错了,兼容下 */
+  DATA_NOT_FOUND = '-10',
   /** 业务错误 */
   SERVICE_FAULT = '-100',
   /** 用户没有登录 */
   NOT_LOGIN = '-101',
+  /** 密码错误 */
+  BAD_PASSWORD = '-102',
 }
 
 /** 自定义错误枚举数据 */
@@ -27,7 +39,8 @@ export const FrameworkErrorEnum = registerErrorCode('ServiceError', ErrorCode);
  * @param {ErrorCode} errcode 错误代码
  */
 export class ServiceError extends MidwayError {
-  constructor(str: string, errcode: string = ErrorCode.UNKNOWN) {
+  constructor(str: string, errcode: string = ErrorCode.UNKNOWN, detail?: any) {
+    if (detail) console.log(detail);
     super(str, errcode);
   }
 }

+ 1 - 1
src/index.ts

@@ -24,4 +24,4 @@ export { GetModel } from './util/getModel';
 export { TransactionService } from './util/transactions';
 
 /**service工具 */
-export { pageOptions, resultOptions } from './service/options';
+export { PageOptions, ResultOptions } from './service/options';

+ 8 - 4
src/interface/SearchBase.ts

@@ -21,16 +21,20 @@ export class SearchBase {
    * 此处将字段转换为使用的方式
    */
   getFilter() {
-    const others = _.omit(this, ['like_prop', 'props', 'mapping']);
+    const that = _.cloneDeep(this);
+    const others = _.omit(that, ['like_prop', 'props', 'mapping']);
     let keys = Object.keys(others);
     keys = keys.filter(f => this.props.includes(f));
     const result = {};
     for (const i of keys) {
       const value = this[i];
+      // 布尔值放过
       // 值不存在或者为空字符串,过滤掉
-      if (!value || value === '') {
-        delete this[i];
-        continue;
+      if (!_.isBoolean(value)) {
+        if (!value || value === '') {
+          delete this[i];
+          continue;
+        }
       }
       if (this.like_prop.includes(i)) {
         // 处理模糊查询

+ 7 - 6
src/service/BaseService.ts

@@ -6,7 +6,8 @@ import _ = require('lodash');
 import { FrameworkErrorEnum, ServiceError } from '../error/service.error';
 import { GetModel } from '../util/getModel';
 import { SearchBase } from '../interface/SearchBase';
-import { pageOptions, resultOptions } from './options';
+import { PageOptions, ResultOptions } from './options';
+import { PopulateOptions } from 'mongoose';
 /**
  * Service基类,实现了一些基础的crud
  */
@@ -27,7 +28,7 @@ export abstract class BaseService<T extends AnyParamConstructor<any>> {
    * @param {Boolean} populate 是否进行ref关联数据
    * @returns {Promise<object>} 返回列表
    */
-  async query(filter: SearchBase, pageOptions: pageOptions = {}, resultOptions: resultOptions = { lean: true, populate: true }): Promise<Array<any>> {
+  async query(filter: SearchBase, pageOptions: PageOptions = {}, resultOptions: ResultOptions = { lean: true, populate: true }): Promise<Array<any>> {
     const dup = _.cloneDeep(filter.getFilter());
     const { lean, populate } = resultOptions;
     let refs = [];
@@ -56,7 +57,7 @@ export abstract class BaseService<T extends AnyParamConstructor<any>> {
    * @param {boolean} lean 是否使用JavaScript形式数据
    * @returns {object|undefined}
    */
-  async fetch(id: string, resultOptions: resultOptions = { lean: true, populate: true }): Promise<object | undefined> {
+  async fetch(id: string, resultOptions: ResultOptions = { lean: true, populate: true }): Promise<object | undefined> {
     const { lean, populate } = resultOptions;
     let refs = [];
     if (populate) refs = this.getRefs();
@@ -68,7 +69,7 @@ export abstract class BaseService<T extends AnyParamConstructor<any>> {
    * @param query 查询条件
    * @param resultOptions 结果处理
    */
-  async findOne(query: object = {}, resultOptions: resultOptions = { lean: true, populate: true }): Promise<object | undefined> {
+  async findOne(query: object = {}, resultOptions: ResultOptions = { lean: true, populate: true }): Promise<object | undefined> {
     const { lean, populate } = resultOptions;
     let refs = [];
     if (populate) refs = this.getRefs();
@@ -149,8 +150,8 @@ export abstract class BaseService<T extends AnyParamConstructor<any>> {
   /**
    * 获取本服务默认表的ref关系
    */
-  getRefs(): Array<object> {
-    const schema = _.get(this.model, 'schema.tree');
+  getRefs(): Array<PopulateOptions> {
+    const schema: any = _.get(this.model, 'schema.tree');
     const refs = [];
     for (const key in schema) {
       const f = schema[key];

+ 2 - 2
src/service/options.ts

@@ -1,11 +1,11 @@
 /**分页处理参数 */
-export interface pageOptions {
+export interface PageOptions {
   skip?: number;
   limit?: number;
   [propName: string]: any;
 }
 /**对查询结果处理参数 */
-export interface resultOptions {
+export interface ResultOptions {
   lean?: boolean;
   populate?: boolean;
   [propName: string]: any;