mirror of
https://github.com/YMFE/yapi.git
synced 2025-02-23 13:59:28 +08:00
feat: 增加全局变量
This commit is contained in:
parent
5223586f5f
commit
9d3e41e1a1
@ -25,7 +25,7 @@ import CheckCrossInstall, { initCrossRequest } from './CheckCrossInstall.js';
|
||||
import './Postman.scss';
|
||||
import ProjectEnv from '../../containers/Project/Setting/ProjectEnv/index.js';
|
||||
import json5 from 'json5';
|
||||
const { handleParamsValue } = require('common/utils.js');
|
||||
const { handleParamsValue, ArrayToObject } = require('common/utils.js');
|
||||
const {
|
||||
handleParams,
|
||||
checkRequestBodyIsRaw,
|
||||
@ -259,12 +259,12 @@ export default class Run extends Component {
|
||||
}
|
||||
}
|
||||
|
||||
handleValue(val) {
|
||||
|
||||
handleValue(val, global) {
|
||||
// console.log('val',val);
|
||||
// console.log('global',global);
|
||||
let globalValue = ArrayToObject(global);
|
||||
return handleParamsValue(val, {
|
||||
global: {
|
||||
err: 12
|
||||
}
|
||||
global:globalValue
|
||||
});
|
||||
}
|
||||
|
||||
@ -295,7 +295,7 @@ export default class Run extends Component {
|
||||
loading: true
|
||||
});
|
||||
|
||||
|
||||
|
||||
let options = handleParams(this.state, this.handleValue),
|
||||
result;
|
||||
|
||||
|
@ -29,7 +29,7 @@ const {
|
||||
handleCurrDomain,
|
||||
checkNameIsExistInArray
|
||||
} = require('common/postmanLib.js');
|
||||
const { handleParamsValue, json_parse } = require('common/utils.js');
|
||||
const { handleParamsValue, json_parse, ArrayToObject } = require('common/utils.js');
|
||||
import CaseEnv from 'client/components/CaseEnv';
|
||||
import Label from '../../../../components/Label/Label.js';
|
||||
|
||||
@ -362,8 +362,12 @@ class InterfaceColContent extends Component {
|
||||
}
|
||||
};
|
||||
|
||||
handleValue = val => {
|
||||
return handleParamsValue(val, this.records);
|
||||
handleValue = (val, global) => {
|
||||
|
||||
let globalValue = ArrayToObject(global);
|
||||
let context = Object.assign({}, {global: globalValue}, this.records);
|
||||
|
||||
return handleParamsValue(val, context);
|
||||
};
|
||||
|
||||
arrToObj = (arr, requestParams) => {
|
||||
|
@ -49,9 +49,19 @@ class ProjectEnvContent extends Component {
|
||||
}
|
||||
];
|
||||
|
||||
console.log(curdata);
|
||||
if (curdata && curdata.length !== 0) {
|
||||
curdata.forEach(item => {
|
||||
let global = [
|
||||
{
|
||||
name: '',
|
||||
value: ''
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
const curheader = curdata.header;
|
||||
const curGlobal = curdata.global;
|
||||
|
||||
if (curheader && curheader.length !== 0) {
|
||||
curheader.forEach(item => {
|
||||
if (item.name === 'Cookie') {
|
||||
let cookieStr = item.value;
|
||||
if (cookieStr) {
|
||||
@ -69,34 +79,19 @@ class ProjectEnvContent extends Component {
|
||||
header.unshift(item);
|
||||
}
|
||||
});
|
||||
return { header, cookie };
|
||||
} else {
|
||||
return { header, cookie };
|
||||
}
|
||||
|
||||
if(curGlobal && curGlobal.length !== 0) {
|
||||
curGlobal.forEach(item => {
|
||||
global.unshift(item)
|
||||
})
|
||||
}
|
||||
return { header, cookie, global }
|
||||
}
|
||||
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.state = {
|
||||
header: [
|
||||
{
|
||||
name: '',
|
||||
value: ''
|
||||
}
|
||||
],
|
||||
cookie: [
|
||||
{
|
||||
name: '',
|
||||
value: ''
|
||||
}
|
||||
],
|
||||
global: [
|
||||
{
|
||||
name: '',
|
||||
value: ''
|
||||
}
|
||||
]
|
||||
};
|
||||
this.state = Object.assign({}, initMap);
|
||||
}
|
||||
addHeader = (value, index, name) => {
|
||||
let nextHeader = this.state[name][index + 1];
|
||||
@ -129,16 +124,7 @@ class ProjectEnvContent extends Component {
|
||||
let curEnvName = this.props.projectMsg.name;
|
||||
let nextEnvName = nextProps.projectMsg.name;
|
||||
if (curEnvName !== nextEnvName) {
|
||||
this.handleInit(nextProps.projectMsg.header);
|
||||
|
||||
let global = nextProps.projectMsg.global;
|
||||
if (global && global.length !== 0) {
|
||||
global.push({
|
||||
name: '',
|
||||
value: ''
|
||||
});
|
||||
this.setState({ global });
|
||||
}
|
||||
this.handleInit(nextProps.projectMsg);
|
||||
}
|
||||
}
|
||||
|
||||
@ -153,6 +139,9 @@ class ProjectEnvContent extends Component {
|
||||
let cookie = values.cookie.filter(val => {
|
||||
return val.name !== '';
|
||||
});
|
||||
let global = values.global.filter(val => {
|
||||
return val.name !== '';
|
||||
})
|
||||
if (cookie.length > 0) {
|
||||
header.push({
|
||||
name: 'Cookie',
|
||||
@ -165,7 +154,8 @@ class ProjectEnvContent extends Component {
|
||||
{
|
||||
name: values.env.name,
|
||||
domain: values.env.protocol + values.env.domain,
|
||||
header: header
|
||||
header: header,
|
||||
global
|
||||
}
|
||||
);
|
||||
onSubmit(assignValue);
|
||||
@ -222,47 +212,6 @@ class ProjectEnvContent extends Component {
|
||||
);
|
||||
};
|
||||
|
||||
// const cookieTpl = (item, index) => {
|
||||
// const cookieLength = this.state.cookie.length - 1;
|
||||
// return (
|
||||
// <Row gutter={2} key={index}>
|
||||
// <Col span={10}>
|
||||
// <FormItem>
|
||||
// {getFieldDecorator('cookie[' + index + '].name', {
|
||||
// validateTrigger: ['onChange', 'onBlur'],
|
||||
// initialValue: item.name || ''
|
||||
// })(
|
||||
// <Input
|
||||
// placeholder="请输入 Cookie Name"
|
||||
// style={{ width: '200px' }}
|
||||
// onChange={() => this.addHeader(item, index, 'cookie')}
|
||||
// />
|
||||
// )}
|
||||
// </FormItem>
|
||||
// </Col>
|
||||
// <Col span={12}>
|
||||
// <FormItem>
|
||||
// {getFieldDecorator('cookie[' + index + '].value', {
|
||||
// validateTrigger: ['onChange', 'onBlur'],
|
||||
// initialValue: item.value || ''
|
||||
// })(<Input placeholder="请输入参数内容" style={{ width: '90%', marginRight: 8 }} />)}
|
||||
// </FormItem>
|
||||
// </Col>
|
||||
// <Col span={2} className={index === cookieLength ? ' env-last-row' : null}>
|
||||
// {/* 新增的项中,只有最后一项没有有删除按钮 */}
|
||||
// <Icon
|
||||
// className="dynamic-delete-button delete"
|
||||
// type="delete"
|
||||
// onClick={e => {
|
||||
// e.stopPropagation();
|
||||
// this.delHeader(index, 'cookie');
|
||||
// }}
|
||||
// />
|
||||
// </Col>
|
||||
// </Row>
|
||||
// );
|
||||
// };
|
||||
|
||||
const commonTpl = (item, index, name) => {
|
||||
const length = this.state[name].length - 1;
|
||||
return (
|
||||
@ -274,7 +223,7 @@ class ProjectEnvContent extends Component {
|
||||
initialValue: item.name || ''
|
||||
})(
|
||||
<Input
|
||||
placeholder="请输入 global Name"
|
||||
placeholder={`请输入 ${name} Name`}
|
||||
style={{ width: '200px' }}
|
||||
onChange={() => this.addHeader(item, index, name)}
|
||||
/>
|
||||
|
@ -307,7 +307,7 @@ function handleParams(interfaceData, handleValue, requestParams) {
|
||||
const obj = {};
|
||||
safeArray(arr).forEach(item => {
|
||||
if (item && item.name && (item.enable || item.required === '1')) {
|
||||
obj[item.name] = handleValue(item.value);
|
||||
obj[item.name] = handleValue(item.value, currDomain.global);
|
||||
if (requestParams) {
|
||||
requestParams[item.name] = obj[item.name];
|
||||
}
|
||||
@ -320,7 +320,7 @@ function handleParams(interfaceData, handleValue, requestParams) {
|
||||
const obj = {};
|
||||
safeArray(arr).forEach(item => {
|
||||
if (item && item.name) {
|
||||
obj[item.name] = handleValue(item.value);
|
||||
obj[item.name] = handleValue(item.value, currDomain.global);
|
||||
if (requestParams) {
|
||||
requestParams[item.name] = obj[item.name];
|
||||
}
|
||||
@ -333,10 +333,10 @@ function handleParams(interfaceData, handleValue, requestParams) {
|
||||
let currDomain,
|
||||
requestBody,
|
||||
requestOptions = {};
|
||||
|
||||
currDomain = handleCurrDomain(env, case_env);
|
||||
interfaceRunData.req_params = interfaceRunData.req_params || [];
|
||||
interfaceRunData.req_params.forEach(item => {
|
||||
let val = handleValue(item.value);
|
||||
let val = handleValue(item.value, currDomain.global);
|
||||
if (requestParams) {
|
||||
requestParams[item.name] = val;
|
||||
}
|
||||
@ -344,7 +344,7 @@ function handleParams(interfaceData, handleValue, requestParams) {
|
||||
path = path.replace(`{${item.name}}`, val || `{${item.name}}`);
|
||||
});
|
||||
|
||||
currDomain = handleCurrDomain(env, case_env);
|
||||
|
||||
const urlObj = URL.parse(joinPath(currDomain.domain, path), true);
|
||||
const url = URL.format({
|
||||
protocol: urlObj.protocol || 'http',
|
||||
@ -404,7 +404,7 @@ function handleParams(interfaceData, handleValue, requestParams) {
|
||||
if (requestParams) {
|
||||
requestParams = Object.assign(requestParams, reqBody);
|
||||
}
|
||||
requestBody = handleJson(reqBody, handleValue);
|
||||
requestBody = handleJson(reqBody, (val)=>handleValue(val, currDomain.global));
|
||||
}
|
||||
} else {
|
||||
requestBody = interfaceRunData.req_body_other;
|
||||
|
@ -138,9 +138,14 @@ exports.joinPath = (domain, joinPath) => {
|
||||
return domain + joinPath;
|
||||
};
|
||||
|
||||
exports.safeArray = arr => {
|
||||
// exports.safeArray = arr => {
|
||||
// return Array.isArray(arr) ? arr : [];
|
||||
// };
|
||||
function safeArray(arr) {
|
||||
return Array.isArray(arr) ? arr : [];
|
||||
};
|
||||
}
|
||||
exports.safeArray = safeArray;
|
||||
|
||||
|
||||
exports.isJson5 = function isJson5(json) {
|
||||
if (!json) return false;
|
||||
@ -179,3 +184,12 @@ exports.json_format = function(json) {
|
||||
return json;
|
||||
}
|
||||
};
|
||||
|
||||
exports.ArrayToObject = function(arr) {
|
||||
let obj = {} ;
|
||||
safeArray(arr).forEach(item => {
|
||||
obj[item.name] = item.value;
|
||||
})
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ const {
|
||||
handleCurrDomain,
|
||||
checkNameIsExistInArray
|
||||
} = require('../../common/postmanLib');
|
||||
const { handleParamsValue } = require('../../common/utils.js');
|
||||
const { handleParamsValue, ArrayToObject } = require('../../common/utils.js');
|
||||
const renderToHtml = require('../utils/reportHtml');
|
||||
const axios = require('axios');
|
||||
const HanldeImportData = require('../../common/HandleImportData');
|
||||
@ -121,8 +121,10 @@ class openController extends baseController {
|
||||
ctx.body = 'projectInterfaceData';
|
||||
}
|
||||
|
||||
handleValue(val) {
|
||||
return handleParamsValue(val, this.records);
|
||||
handleValue(val, global) {
|
||||
let globalValue = ArrayToObject(global);
|
||||
let context = Object.assign({}, {global: globalValue}, this.records);
|
||||
return handleParamsValue(val, context);
|
||||
}
|
||||
|
||||
handleEvnParams(params) {
|
||||
|
@ -159,17 +159,17 @@ module.exports = {
|
||||
baseConfig.output.prd.publicPath = '';
|
||||
baseConfig.output.prd.filename = '[name]@[chunkhash][ext]';
|
||||
|
||||
(baseConfig.module.noParse = /node_modules\/jsondiffpatch\/public\/build\/.*js/),
|
||||
baseConfig.module.loaders.push({
|
||||
test: /\.less$/,
|
||||
loader: ykit.ExtractTextPlugin.extract(
|
||||
require.resolve('style-loader'),
|
||||
require.resolve('css-loader') +
|
||||
'?sourceMap!' +
|
||||
require.resolve('less-loader') +
|
||||
'?sourceMap'
|
||||
)
|
||||
});
|
||||
baseConfig.module.noParse = /node_modules\/jsondiffpatch\/public\/build\/.*js/;
|
||||
baseConfig.module.loaders.push({
|
||||
test: /\.less$/,
|
||||
loader: ykit.ExtractTextPlugin.extract(
|
||||
require.resolve('style-loader'),
|
||||
require.resolve('css-loader') +
|
||||
'?sourceMap!' +
|
||||
require.resolve('less-loader') +
|
||||
'?sourceMap'
|
||||
)
|
||||
});
|
||||
baseConfig.module.loaders.push({
|
||||
test: /\.(sass|scss)$/,
|
||||
loader: ykit.ExtractTextPlugin.extract(
|
||||
|
Loading…
Reference in New Issue
Block a user