ApiAutomation.vue 10.0 KB
Newer Older
1
<template>
2
  <ms-container v-if="renderComponent" v-loading="loading">
Q
q4speed 已提交
3
    <ms-aside-container>
4 5 6 7 8
      <ms-api-scenario-module
        @nodeSelectEvent="nodeChange"
        @refreshTable="refresh"
        @saveAsEdit="editScenario"
        @setModuleOptions="setModuleOptions"
C
chenjianxing 已提交
9
        @setNodeTree="setNodeTree"
10
        @enableTrash="enableTrash"
11
        @exportAPI="exportAPI"
12
        @exportJmx="exportJmx"
13
        @refreshAll="refreshAll"
14 15
        :type="'edit'"
        ref="nodeTree"/>
Q
q4speed 已提交
16
    </ms-aside-container>
17

Q
q4speed 已提交
18 19
    <ms-main-container>
      <el-tabs v-model="activeName" @tab-click="addTab" @tab-remove="removeTab">
20
        <el-tab-pane name="default" :label="$t('api_test.automation.scenario_list')">
Q
q4speed 已提交
21
          <ms-api-scenario-list
C
chenjianxing 已提交
22 23
            :module-tree="nodeTree"
            :module-options="moduleOptions"
24 25
            :select-node-ids="selectNodeIds"
            :trash-enable="trashEnable"
S
song.tianyang 已提交
26 27
            :checkRedirectID="checkRedirectID"
            :isRedirectEdit="isRedirectEdit"
28
            :is-read-only="isReadOnly"
29
            @openScenario="editScenario"
Q
q4speed 已提交
30
            @edit="editScenario"
S
song.tianyang 已提交
31
            @changeSelectDataRangeAll="changeSelectDataRangeAll"
Q
q4speed 已提交
32 33 34 35 36 37 38 39 40 41
            ref="apiScenarioList"/>
        </el-tab-pane>

        <el-tab-pane
          :key="item.name"
          v-for="(item) in tabs"
          :label="item.label"
          :name="item.name"
          closable>
          <div class="ms-api-scenario-div">
42
            <ms-edit-api-scenario @refresh="refresh" @openScenario="editScenario" @closePage="closePage" :currentScenario="item.currentScenario"
43
                                  :moduleOptions="moduleOptions" ref="autoScenarioConfig"/>
Q
q4speed 已提交
44 45 46 47 48
          </div>
        </el-tab-pane>

        <el-tab-pane name="add">
          <template v-slot:label>
49 50 51
            <el-dropdown @command="handleCommand" v-tester>
              <el-button type="primary" plain icon="el-icon-plus" size="mini" v-tester/>
              <el-dropdown-menu slot="dropdown">
W
wenyann 已提交
52
                <el-dropdown-item command="ADD">{{ $t('api_test.automation.add_scenario') }}</el-dropdown-item>
53 54 55 56
                <el-dropdown-item command="CLOSE_ALL">{{ $t('api_test.definition.request.close_all_label') }}
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
Q
q4speed 已提交
57 58 59 60 61
          </template>
        </el-tab-pane>
      </el-tabs>
    </ms-main-container>
  </ms-container>
62 63 64 65
</template>

<script>

66 67 68 69
  import MsContainer from "@/business/components/common/components/MsContainer";
  import MsAsideContainer from "@/business/components/common/components/MsAsideContainer";
  import MsMainContainer from "@/business/components/common/components/MsMainContainer";
  import MsApiScenarioList from "@/business/components/api/automation/scenario/ApiScenarioList";
70
  import {getUUID, downloadFile, checkoutTestManagerOrTestUser} from "@/common/js/utils";
71 72 73
  import MsApiScenarioModule from "@/business/components/api/automation/scenario/ApiScenarioModule";
  import MsEditApiScenario from "./scenario/EditApiScenario";
  import {getCurrentProjectID} from "../../../../common/js/utils";
Q
q4speed 已提交
74

75 76 77 78 79 80 81 82 83
  export default {
    name: "ApiAutomation",
    components: {
      MsApiScenarioModule,
      MsApiScenarioList,
      MsMainContainer,
      MsAsideContainer,
      MsContainer,
      MsEditApiScenario
Q
q4speed 已提交
84
    },
85 86 87 88 89 90 91 92 93 94 95
    comments: {},
    computed: {
      checkRedirectID: function () {
        let redirectIDParam = this.$route.params.redirectID;
        this.changeRedirectParam(redirectIDParam);
        return redirectIDParam;
      },
      isRedirectEdit: function () {
        let redirectParam = this.$route.params.dataSelectRange;
        this.checkRedirectEditPage(redirectParam);
        return redirectParam;
96
      },
97
      isReadOnly() {
98
        return !checkoutTestManagerOrTestUser();
S
song.tianyang 已提交
99 100
      }
    },
101 102 103 104 105 106
    data() {
      return {
        redirectID: '',
        renderComponent: true,
        isHide: true,
        activeName: 'default',
107
        redirectFlag: 'none',
108 109 110
        currentModule: null,
        moduleOptions: [],
        tabs: [],
111
        loading: false,
112 113
        trashEnable: false,
        selectNodeIds: [],
C
chenjianxing 已提交
114
        nodeTree: []
S
song.tianyang 已提交
115 116
      }
    },
117 118 119 120 121 122 123
    watch: {
      redirectID() {
        this.renderComponent = false;
        this.$nextTick(() => {
          // 在 DOM 中添加 my-component 组件
          this.renderComponent = true;
        });
124 125 126 127 128 129 130 131 132 133
      },
      '$route'(to, from) {  //  路由改变时,把接口定义界面中的 ctrl s 保存快捷键监听移除
        if (to.path.indexOf('/api/automation') == -1) {
          if (this.$refs && this.$refs.autoScenarioConfig) {
            console.log(this.$refs.autoScenarioConfig);
            this.$refs.autoScenarioConfig.forEach(item => {
              item.removeListener();
            });
          }
        }
S
song.tianyang 已提交
134 135
      }
    },
136
    methods: {
137
      exportAPI() {
C
chenjianxing 已提交
138
        this.$refs.apiScenarioList.exportApi();
139
      },
140
      exportJmx() {
141 142
        this.$refs.apiScenarioList.exportJmx();
      },
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
      checkRedirectEditPage(redirectParam) {
        if (redirectParam != null) {
          let selectParamArr = redirectParam.split("edit:");
          if (selectParamArr.length == 2) {
            let scenarioId = selectParamArr[1];
            let projectId = getCurrentProjectID();
            //查找单条数据,跳转修改页面
            let url = "/api/automation/list/" + 1 + "/" + 1;
            this.$post(url, {id: scenarioId, projectId: projectId}, response => {
              let data = response.data;
              if (data != null) {
                //如果树未加载
                if (JSON.stringify(this.moduleOptions) === '{}') {
                  this.$refs.nodeTree.list();
                }
                let row = data.listObject[0];
                row.tags = JSON.parse(row.tags);
                this.editScenario(row);
              }
            });
          }
164
        }
165 166 167
      },
      changeRedirectParam(redirectIDParam) {
        this.redirectID = redirectIDParam;
168 169
        if (redirectIDParam != null) {
          if (this.redirectFlag == "none") {
170 171 172 173
            this.activeName = "default";
            this.addListener();
            this.redirectFlag = "redirected";
          }
174
        } else {
175 176
          this.redirectFlag = "none";
        }
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
      },
      addTab(tab) {
        if (!getCurrentProjectID()) {
          this.$warning(this.$t('commons.check_project_tip'));
          return;
        }
        if (tab.name === 'add') {
          let label = this.$t('api_test.automation.add_scenario');
          let name = getUUID().substring(0, 8);
          this.activeName = name;
          this.tabs.push({label: label, name: name, currentScenario: {apiScenarioModuleId: "", id: getUUID()}});
        }
        if (tab.name === 'edit') {
          let label = this.$t('api_test.automation.add_scenario');
          let name = getUUID().substring(0, 8);
          this.activeName = name;
          label = tab.currentScenario.name;
          this.tabs.push({label: label, name: name, currentScenario: tab.currentScenario});
        }
196 197 198 199 200 201 202 203 204
        if (this.$refs && this.$refs.autoScenarioConfig) {
          this.$refs.autoScenarioConfig.forEach(item => {
            item.removeListener();
          });  //  删除所有tab的 ctrl + s 监听
          this.addListener();
        }
      },
      addListener() {
        let index = this.tabs.findIndex(item => item.name === this.activeName); //  找到当前选中tab的index
205 206
        if (index != -1) {   //  为当前选中的tab添加监听
          this.$nextTick(() => {
207 208 209
            this.$refs.autoScenarioConfig[index].addListener();
          });
        }
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
      },
      handleTabClose() {
        this.tabs = [];
        this.activeName = "default";
        this.refresh();
      },
      handleCommand(e) {
        switch (e) {
          case "ADD":
            this.addTab({name: 'add'});
            break;
          case "CLOSE_ALL":
            this.handleTabClose();
            break;
          default:
            this.addTab({name: 'add'});
            break;
        }
      },
229 230 231 232 233 234 235 236 237
      closePage(targetName) {
        this.tabs = this.tabs.filter(tab => tab.label !== targetName);
        if (this.tabs.length > 0) {
          this.activeName = this.tabs[this.tabs.length - 1].name;
          this.addListener(); //  自动切换当前标签时,也添加监听
        } else {
          this.activeName = "default"
        }
      },
238 239 240 241
      removeTab(targetName) {
        this.tabs = this.tabs.filter(tab => tab.name !== targetName);
        if (this.tabs.length > 0) {
          this.activeName = this.tabs[this.tabs.length - 1].name;
242
          this.addListener(); //  自动切换当前标签时,也添加监听
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
        } else {
          this.activeName = "default"
        }
      },
      setTabLabel(data) {
        for (const tab of this.tabs) {
          if (tab.name === this.activeName) {
            tab.label = data.name;
            break;
          }
        }
      },
      selectModule(data) {
        this.currentModule = data;
      },
      saveScenario(data) {
        this.setTabLabel(data);
        this.$refs.apiScenarioList.search(data);
      },
      refresh(data) {
263
        this.setTabTitle(data);
264 265
        this.$refs.apiScenarioList.search(data);
      },
266 267 268 269
      refreshAll() {
        this.$refs.nodeTree.list();
        this.$refs.apiScenarioList.search();
      },
270 271 272 273 274 275 276 277 278
      setTabTitle(data) {
        for (let index in this.tabs) {
          let tab = this.tabs[index];
          if (tab.name === this.activeName) {
            tab.label = data.name;
            break;
          }
        }
      },
279 280 281
      editScenario(row) {
        this.addTab({name: 'edit', currentScenario: row});
      },
S
song.tianyang 已提交
282

283 284 285 286 287 288
      nodeChange(node, nodeIds, pNodes) {
        this.selectNodeIds = nodeIds;
      },
      setModuleOptions(data) {
        this.moduleOptions = data;
      },
C
chenjianxing 已提交
289 290 291
      setNodeTree(data) {
        this.nodeTree = data;
      },
292 293 294 295 296 297
      changeSelectDataRangeAll(tableType) {
        this.$route.params.dataSelectRange = 'all';
      },
      enableTrash(data) {
        this.trashEnable = data;
      }
298
    }
299 300 301 302
  }
</script>

<style scoped>
303 304 305
  /deep/ .el-tabs__header {
    margin: 0 0 0px;
  }
306
</style>