diff --git a/common/postmanLib.js b/common/postmanLib.js index 143f7a8e..bc414640 100644 --- a/common/postmanLib.js +++ b/common/postmanLib.js @@ -128,7 +128,7 @@ function checkNameIsExistInArray(name, arr) { function handleCurrDomain(domains, case_env) { let currDomain = _.find(domains, item => item.name === case_env); - + if (!currDomain) { currDomain = domains[0]; } @@ -192,7 +192,7 @@ function sandboxByBrowser(context = {}, script) { `; err.message = `Script: ${message} message: ${err.message}`; - + throw err; } return context; @@ -207,22 +207,22 @@ async function crossRequest(defaultOptions, preScript, afterScript) { get href() { return urlObj.href; }, - set href(val){ - throw new Error('context.href 不能被赋值') + set href(val) { + throw new Error('context.href 不能被赋值'); }, get hostname() { return urlObj.hostname; }, - set hostname(val){ - throw new Error('context.hostname 不能被赋值') + set hostname(val) { + throw new Error('context.hostname 不能被赋值'); }, get caseId() { return options.caseId; }, - set caseId(val){ - throw new Error('context.caseId 不能被赋值') + set caseId(val) { + throw new Error('context.caseId 不能被赋值'); }, method: options.method, @@ -244,7 +244,7 @@ async function crossRequest(defaultOptions, preScript, afterScript) { sha512: utils.sha512, unbase64: utils.unbase64, axios: axios - }) + }); if (preScript) { context = await sandbox(context, preScript); @@ -361,35 +361,31 @@ function handleParams(interfaceData, handleValue, requestParams) { }; // 对 raw 类型的 form 处理 - try{ - - if(interfaceRunData.req_body_type === 'raw'){ - if(headers && headers['Content-Type']){ - if(headers['Content-Type'].indexOf('application/x-www-form-urlencoded')>=0) { + try { + if (interfaceRunData.req_body_type === 'raw') { + if (headers && headers['Content-Type']) { + if (headers['Content-Type'].indexOf('application/x-www-form-urlencoded') >= 0) { interfaceRunData.req_body_type = 'form'; let reqData = json_parse(interfaceRunData.req_body_other); - if(reqData && typeof reqData === 'object'){ + if (reqData && typeof reqData === 'object') { interfaceRunData.req_body_form = []; - Object.keys(reqData).forEach(key=>{ + Object.keys(reqData).forEach(key => { interfaceRunData.req_body_form.push({ name: key, type: 'text', value: JSON.stringify(reqData[key]), - enable:true - }) - }) + enable: true + }); + }); } - - } else if(headers['Content-Type'].indexOf('application/json')>=0) { - interfaceRunData.req_body_type = 'json'; + } else if (headers['Content-Type'].indexOf('application/json') >= 0) { + interfaceRunData.req_body_type = 'json'; } } } - - }catch(e){ - console.log('err',e); + } catch (e) { + console.log('err', e); } - if (HTTP_METHOD[interfaceRunData.method].request_body) { if (interfaceRunData.req_body_type === 'form') { diff --git a/exts/yapi-plugin-wiki/controller.js b/exts/yapi-plugin-wiki/controller.js index 1d893b4c..1c9ac4b6 100644 --- a/exts/yapi-plugin-wiki/controller.js +++ b/exts/yapi-plugin-wiki/controller.js @@ -111,10 +111,7 @@ class wikiController extends baseController { 'utf8' ); let htmlCss = fs.readFileSync( - path.resolve( - yapi.WEBROOT, - 'node_modules/jsondiffpatch/dist/formatters-styles/html.css' - ), + path.resolve(yapi.WEBROOT, 'node_modules/jsondiffpatch/dist/formatters-styles/html.css'), 'utf8' ); let project = await this.projectModel.getBaseInfo(params.project_id); @@ -176,38 +173,9 @@ class wikiController extends baseController { return ctx.websocket.send('id 参数有误'); } result = await this.Model.get(id); - - switch (message) { - case 'start': - if (result && result.edit_uid === this.getUid()) { - await this.Model.upEditUid(result._id, 0); - } - break; - case 'editor': - let userInst, userinfo, data; - if (result && result.edit_uid !== 0 && result.edit_uid !== this.getUid()) { - userInst = yapi.getInst(userModel); - userinfo = await userInst.findById(result.edit_uid); - data = { - errno: result.edit_uid, - data: { uid: result.edit_uid, username: userinfo.username } - }; - } else { - if (result) { - await this.Model.upEditUid(result._id, this.getUid()); - } - data = { - errno: 0, - data: result - }; - } - ctx.websocket.send(JSON.stringify(data)); - break; - case 'end': - await this.Model.upEditUid(result._id, 0); - break; - default: - break; + let data = await this.websocketMsgMap(message, result); + if(data) { + ctx.websocket.send(JSON.stringify(data)); } }); ctx.websocket.on('close', async () => {}); @@ -215,6 +183,52 @@ class wikiController extends baseController { yapi.commons.log(err, 'error'); } } + + + websocketMsgMap(msg, result) { + const map = { + start: this.startFunc.bind(this), + end: this.endFunc.bind(this), + editor: this.editorFunc.bind(this) + }; + + return map[msg](result); + } + + // socket 开始链接 + async startFunc(result) { + + if (result && result.edit_uid === this.getUid()) { + await this.Model.upEditUid(result._id, 0); + } + } + + // socket 结束链接 + async endFunc(result) { + await this.Model.upEditUid(result._id, 0); + } + + // 正在编辑 + async editorFunc(result) { + let userInst, userinfo, data; + if (result && result.edit_uid !== 0 && result.edit_uid !== this.getUid()) { + userInst = yapi.getInst(userModel); + userinfo = await userInst.findById(result.edit_uid); + data = { + errno: result.edit_uid, + data: { uid: result.edit_uid, username: userinfo.username } + }; + } else { + if (result) { + await this.Model.upEditUid(result._id, this.getUid()); + } + data = { + errno: 0, + data: result + }; + } + return data; + } } module.exports = wikiController;