增加场景的按钮,修复复制后id重复问题

This commit is contained in:
q4speed 2020-05-19 19:07:25 +08:00
parent 7ab5fa1987
commit f5bdeb1695
6 changed files with 54 additions and 11 deletions

View File

@ -13,8 +13,12 @@
<el-dropdown trigger="click" @command="handleCommand"> <el-dropdown trigger="click" @command="handleCommand">
<span class="el-dropdown-link el-icon-more"/> <span class="el-dropdown-link el-icon-more"/>
<el-dropdown-menu slot="dropdown"> <el-dropdown-menu slot="dropdown">
<el-dropdown-item :command="{type: 'copy', index: index}">复制请求</el-dropdown-item> <el-dropdown-item :command="{type: 'copy', index: index}">
<el-dropdown-item :command="{type: 'delete', index: index}">删除请求</el-dropdown-item> {{$t('api_test.request.copy')}}
</el-dropdown-item>
<el-dropdown-item :command="{type: 'delete', index: index}">
{{$t('api_test.request.delete')}}
</el-dropdown-item>
</el-dropdown-menu> </el-dropdown-menu>
</el-dropdown> </el-dropdown>
</div> </div>
@ -50,12 +54,12 @@
methods: { methods: {
createRequest: function () { createRequest: function () {
let request = new Request({method: "GET"}); let request = new Request();
this.requests.push(request); this.requests.push(request);
}, },
copyRequest: function (index) { copyRequest: function (index) {
let request = this.requests[index]; let request = this.requests[index];
this.requests.push(JSON.parse(JSON.stringify(request))); this.requests.push(request.clone());
}, },
deleteRequest: function (index) { deleteRequest: function (index) {
this.requests.splice(index, 1); this.requests.splice(index, 1);

View File

@ -12,13 +12,17 @@
{{$t('api_test.scenario.config')}} {{$t('api_test.scenario.config')}}
</span> </span>
</div> </div>
<!-- 暂时去掉将来再加--> <el-dropdown trigger="click" @command="handleCommand">
<!-- <el-dropdown trigger="click" @command="handleCommand">--> <span class="el-dropdown-link el-icon-more scenario-btn"/>
<!-- <span class="el-dropdown-link el-icon-more scenario-btn"/>--> <el-dropdown-menu slot="dropdown">
<!-- <el-dropdown-menu slot="dropdown">--> <el-dropdown-item :command="{type: 'copy', index: index}">
<!-- <el-dropdown-item :command="{type:'delete', index:index}">删除场景</el-dropdown-item>--> {{$t('api_test.scenario.copy')}}
<!-- </el-dropdown-menu>--> </el-dropdown-item>
<!-- </el-dropdown>--> <el-dropdown-item :command="{type:'delete', index:index}">
{{$t('api_test.scenario.delete')}}
</el-dropdown-item>
</el-dropdown-menu>
</el-dropdown>
</template> </template>
<ms-api-request-config :requests="scenario.requests" :open="select"/> <ms-api-request-config :requests="scenario.requests" :open="select"/>
</ms-api-collapse-item> </ms-api-collapse-item>
@ -71,6 +75,10 @@
createScenario: function () { createScenario: function () {
this.scenarios.push(new Scenario()); this.scenarios.push(new Scenario());
}, },
copyScenario: function (index) {
let scenario = this.scenarios[index];
this.scenarios.push(scenario.clone());
},
deleteScenario: function (index) { deleteScenario: function (index) {
this.scenarios.splice(index, 1); this.scenarios.splice(index, 1);
if (this.scenarios.length === 0) { if (this.scenarios.length === 0) {
@ -83,6 +91,9 @@
}, },
handleCommand: function (command) { handleCommand: function (command) {
switch (command.type) { switch (command.type) {
case "copy":
this.copyScenario(command.index);
break;
case "delete": case "delete":
this.deleteScenario(command.index); this.deleteScenario(command.index);
break; break;

View File

@ -137,6 +137,16 @@ export class Scenario extends BaseConfig {
options.requests = options.requests || [new Request()]; options.requests = options.requests || [new Request()];
return options; return options;
} }
clone() {
let scenario = new Scenario(this);
scenario.id = uuid();
scenario.requests.forEach(function (request) {
request.id = uuid();
});
return scenario;
}
} }
export class Request extends BaseConfig { export class Request extends BaseConfig {
@ -168,6 +178,12 @@ export class Request extends BaseConfig {
isValid() { isValid() {
return !!this.url && !!this.method return !!this.url && !!this.method
} }
clone() {
let request = new Request(this);
request.id = uuid();
return request;
}
} }
export class Body extends BaseConfig { export class Body extends BaseConfig {

View File

@ -252,8 +252,12 @@ export default {
variables: "Variables", variables: "Variables",
headers: "Headers", headers: "Headers",
kv_description: "Variables are available for all requests", kv_description: "Variables are available for all requests",
copy: "Copy scenario",
delete: "Delete scenario"
}, },
request: { request: {
copy: "Copy request",
delete: "Delete request",
input_name: "Please enter the request name", input_name: "Please enter the request name",
name: "Name", name: "Name",
method: "Method", method: "Method",

View File

@ -252,8 +252,12 @@ export default {
variables: "自定义变量", variables: "自定义变量",
headers: "请求头", headers: "请求头",
kv_description: "所有请求可以使用自定义变量", kv_description: "所有请求可以使用自定义变量",
copy: "复制场景",
delete: "删除场景"
}, },
request: { request: {
copy: "复制请求",
delete: "删除请求",
input_name: "请输入请求名称", input_name: "请输入请求名称",
name: "请求名称", name: "请求名称",
method: "请求方法", method: "请求方法",

View File

@ -252,8 +252,12 @@ export default {
variables: "自定義變數", variables: "自定義變數",
headers: "請求頭", headers: "請求頭",
kv_description: "所有請求可以使用自定義變數", kv_description: "所有請求可以使用自定義變數",
copy: "複製場景",
delete: "删除場景"
}, },
request: { request: {
copy: "複製請求",
delete: "删除請求",
input_name: "請輸入請求名稱", input_name: "請輸入請求名稱",
name: "請求名稱", name: "請求名稱",
method: "請求方法", method: "請求方法",