diff --git a/README.md b/README.md index 4a0c2ac..78219ab 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,7 @@ * 演示地址:https://show.cool-admin.com * 文档地址:https://docs.cool-admin.com * 官网:https://www.cool-admin.com + ## 技术选型 Node版后台基础框架基于[Egg.js](https://eggjs.org/zh-cn/)(阿里出品) * 基础:**[egg.js](https://eggjs.org/zh-cn/)** @@ -42,6 +43,8 @@ Node版后台基础框架基于[Egg.js](https://eggjs.org/zh-cn/)(阿里出品) ![努力开发中](https://cool-admin.com/img/work3.png) ## 快速开发6个接口 + + ## 数据模型 数据模型必须放在`app/entities/*`下,否则[typeorm](https://typeorm.io "typeorm")无法识别,如: ```js diff --git a/app/lib/base/service.ts b/app/lib/base/service.ts index 42c94a9..9b77a7f 100644 --- a/app/lib/base/service.ts +++ b/app/lib/base/service.ts @@ -170,38 +170,43 @@ export abstract class BaseService extends Service { const find = entity .createQueryBuilder() .take(parseInt(size)) - .skip(String((page - 1) * size)) - .where(option.where); - // 附加排序 - if (!_.isEmpty(option.addOrderBy)) { - for (const key in option.addOrderBy) { - find.addOrderBy(key, option.addOrderBy[key].toUpperCase()); + .skip(String((page - 1) * size)); + if (option) { + // 默认条件 + if (option.where) { + find.where(option.where); + } + // 附加排序 + if (!_.isEmpty(option.addOrderBy)) { + for (const key in option.addOrderBy) { + find.addOrderBy(key, option.addOrderBy[key].toUpperCase()); + } + } + // 关键字模糊搜索 + if (keyWord) { + keyWord = `%${ keyWord }%`; + find.andWhere(new Brackets(qb => { + const keyWordLikeFields = option.keyWordLikeFields; + for (let i = 0; i < option.keyWordLikeFields.length; i++) { + qb.orWhere(`${ keyWordLikeFields[i] } like :keyWord`, { keyWord }); + } + })); + } + // 字段全匹配 + if (!_.isEmpty(option.fieldEq)) { + for (const key of option.fieldEq) { + const c = {}; + if (query[key]) { + c[key] = query[key]; + find.andWhere(`${ key } = :${ key }`, c); + } + } } } // 接口请求的排序 if (sort && order) { find.addOrderBy(order, sort.toUpperCase()); } - // 关键字模糊搜索 - if (keyWord) { - keyWord = `%${ keyWord }%`; - find.andWhere(new Brackets(qb => { - const keyWordLikeFields = option.keyWordLikeFields; - for (let i = 0; i < option.keyWordLikeFields.length; i++) { - qb.orWhere(`${ keyWordLikeFields[i] } like :keyWord`, { keyWord }); - } - })); - } - // 字段全匹配 - if (!_.isEmpty(option.fieldEq)) { - for (const key of option.fieldEq) { - const c = {}; - if (query[key]) { - c[key] = query[key]; - find.andWhere(`${ key } = :${ key }`, c); - } - } - } return find; }