* 保存接口数据,如果接口存在则更新数据,如果接口不存在则添加数据 * @interface /interface/save * @method post * @category interface * @foldnumber 10 * @param {Number} project_id 项目id,不能为空 * @param {String} title 接口标题,不能为空 * @param {String} path 接口请求路径,不能为空 * @param {String} method 请求方式 * @param
(ctx)
| 328 | * @returns {Object} |
| 329 | */ |
| 330 | async save(ctx) { |
| 331 | let params = ctx.params; |
| 332 | |
| 333 | if (!this.$tokenAuth) { |
| 334 | let auth = await this.checkAuth(params.project_id, 'project', 'edit'); |
| 335 | if (!auth) { |
| 336 | return (ctx.body = yapi.commons.resReturn(null, 40033, '没有权限')); |
| 337 | } |
| 338 | } |
| 339 | params.method = params.method || 'GET'; |
| 340 | params.method = params.method.toUpperCase(); |
| 341 | |
| 342 | let http_path = url.parse(params.path, true); |
| 343 | |
| 344 | if (!yapi.commons.verifyPath(http_path.pathname)) { |
| 345 | return (ctx.body = yapi.commons.resReturn( |
| 346 | null, |
| 347 | 400, |
| 348 | 'path第一位必需为 /, 只允许由 字母数字-/_:.! 组成' |
| 349 | )); |
| 350 | } |
| 351 | |
| 352 | let result = await this.Model.getByPath(params.project_id, params.path, params.method, '_id res_body'); |
| 353 | |
| 354 | if (result.length > 0) { |
| 355 | result.forEach(async item => { |
| 356 | params.id = item._id; |
| 357 | // console.log(this.schemaMap['up']) |
| 358 | let validParams = Object.assign({}, params) |
| 359 | let validResult = yapi.commons.validateParams(this.schemaMap['up'], validParams); |
| 360 | if (validResult.valid) { |
| 361 | let data = Object.assign({}, ctx); |
| 362 | data.params = validParams; |
| 363 | |
| 364 | if(params.res_body_is_json_schema && params.dataSync === 'good'){ |
| 365 | try{ |
| 366 | let new_res_body = yapi.commons.json_parse(params.res_body) |
| 367 | let old_res_body = yapi.commons.json_parse(item.res_body) |
| 368 | data.params.res_body = JSON.stringify(mergeJsonSchema(old_res_body, new_res_body),null,2); |
| 369 | }catch(err){} |
| 370 | } |
| 371 | await this.up(data); |
| 372 | } else { |
| 373 | return (ctx.body = yapi.commons.resReturn(null, 400, validResult.message)); |
| 374 | } |
| 375 | }); |
| 376 | } else { |
| 377 | let validResult = yapi.commons.validateParams(this.schemaMap['add'], params); |
| 378 | if (validResult.valid) { |
| 379 | let data = {}; |
| 380 | data.params = params; |
| 381 | await this.add(data); |
| 382 | } else { |
| 383 | return (ctx.body = yapi.commons.resReturn(null, 400, validResult.message)); |
| 384 | } |
| 385 | } |
| 386 | ctx.body = yapi.commons.resReturn(result); |
| 387 | // return ctx.body = yapi.commons.resReturn(null, 400, 'path第一位必需为 /, 只允许由 字母数字-/_:.! 组成'); |
no test coverage detected