egg-xc-base
Version:
a base framework with egg.js
109 lines (100 loc) • 4.09 kB
JavaScript
/**
* Created by ThinkPad on 2019/12/19.
*/
/**
* Created by ThinkPad on 2019/12/16.
*/
const config = require('../config/config.default')({name:'apiInit'})
const path = require('path');
const fs = require('fs');
function removeAllSpace(str) {
return str.replace(/\s+/g, "");
}
module.exports = {
doIt : async (info)=>{
const {_api,_module,_ctx} = info
const prefix = _module
const csName = _ctx?_ctx:prefix
const group = (_ctx != _module)? `${_module}_${_ctx}`:_module
_api.method = _api.method.toUpperCase()
if(!config || !config.static || !config.static.prefix){
throw new Error("项目上下文根配置文件不存在!-->"+path.resolve('../config/config.default.js') + ".static.prefix")
}
const basePrefix = config.static.prefix
let apiParams = ``
let funContent = `this.success(await this.ctx.service.${_ctx}.${_api.fun}())`
if(_api.method == 'POST' || _api.method == 'PUT'){
funContent = `try{
const result = await this.ctx.service.${_ctx}.${_api.fun}(this.ctx.request.body)
if(result.affectedRows === 0){
throw new this.app.serverError("${_api.method=='POST'?'添加':'更新'}失败!");
}
this.success([])
}catch(error){
throw(error);
}`
}
let ctrStr = fs.readFileSync(path.join(__dirname,`../api/${_module}/controller/${_ctx}.js`)).toString();
if(removeAllSpace(ctrStr).indexOf(removeAllSpace(` async ${_api.fun}(`))>-1){
throw new Error(`${path.resolve(`../api/${_module}/controller/${csName}.js`)}中${_api.fun}已存在!!`)
}
ctrStr = ctrStr.replace(/(^\s*)|(\s*$)/g, '');
ctrStr = ctrStr.substr(0, ctrStr.lastIndexOf('}'));
ctrStr =
`${ctrStr}
/**
* @api {${_api.method}} ${basePrefix}${_api.url} ${_api.id}${_api.name}
* @apiName ${_api.id}${_api.name}
*
${apiParams}
*
* @apiGroup ${group}
* @apiVersion 1.0.0
*
* @apiUse Error0
* @apiSuccess {array} Error0.content
* @apiUse Error1
* */
async ${_api.fun}(){
${funContent}
}
}
module.exports = ${csName.charAt(0).toUpperCase() + csName.substr(1)}Controller
`
let svStr = fs.readFileSync(path.join(__dirname,`../api/${_module}/service/${_ctx}.js`)).toString();
if(removeAllSpace(svStr).indexOf(removeAllSpace(` async ${_api.fun}(`))>-1){
throw new Error(`${path.resolve(`../api/${_module}/service/${csName}.js`)}中${_api.fun}已存在!!`)
}
svStr = svStr.replace(/(^\s*)|(\s*$)/g, '');
svStr = svStr.substr(0, svStr.lastIndexOf('}'));
svStr =
`${svStr}
/**
* @apiIgnore
* ${_api.name}
* @returns {Promise.<*>}
*/
async ${_api.fun}(){
// todo ${_api.name} service 业务代码
return []
}
}
module.exports = ${csName.charAt(0).toUpperCase() + csName.substr(1)}Service
`
let router = fs.readFileSync(path.join(__dirname,`../api/${_module}/router.js`)).toString();
if(removeAllSpace(router).indexOf(removeAllSpace(`controller.${csName}.${_api.fun})`))>-1){
throw new Error(`${path.resolve(`../api/${_module}/router.js`)}中${`router.${_api.method.toLowerCase()}('${_api.url}', controller.${csName}.${_api.fun})`}已存在!!`)
}
router = router.replace(/(^\s*)|(\s*$)/g, '');
router = router.substr(0, router.lastIndexOf('}'));
router =
`${router}
//${_api.name}
router.${_api.method.toLowerCase()}(config.static.prefix+'${_api.url}', controller.${csName}.${_api.fun})
};`;
fs.writeFileSync(path.join(__dirname,`../api/${_module}/router.js`), router);
fs.writeFileSync(path.join(__dirname,`../api/${_module}/controller/${csName}.js`), ctrStr);
fs.writeFileSync(path.join(__dirname,`../api/${_module}/service/${csName}.js`), svStr);
return []
}
}