ApiAutomation.vue 10.1 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
  import MsApiScenarioModule from "@/business/components/api/automation/scenario/ApiScenarioModule";
  import MsEditApiScenario from "./scenario/EditApiScenario";
Q
q4speed 已提交
73

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

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

<style scoped>
308 309 310
  /deep/ .el-tabs__header {
    margin: 0 0 0px;
  }
311
</style>