Vue在線接口調試組件


效果圖

組件: InterfaceTest.vue

<template>
  <a-drawer
    title="在線接口測試"
    width="60%"
    placement="right"
    @close="close"
    :destroy-on-close="true"
    :visible="visible">
    <a-card :bordered="false">
      <a-row style="margin-top: 20px">
        <a-col :md="2" :sm="4">
          <a-select style="width: 90px" size="large" default-value="GET" disabled>
            <a-select-option value="GET">GET</a-select-option>
          </a-select>
        </a-col>
        <a-col :md="22" :sm="20">
          <a-input-search
            placeholder="input send url"
            v-model="tokenUrl"
            :expand-depth="5"
            @search="onSearch"
            enterButton="獲 取 token"
            size="large" />
        </a-col>
      </a-row>
      <a-row style="margin-top: 20px">
        <a-col :md="2" :sm="4">
          <a-select style="width: 90px" v-model="requestMethod" size="large">
            <a-select-option value="GET">GET</a-select-option>
            <a-select-option value="POST">POST</a-select-option>
            <a-select-option value="PUT">PUT</a-select-option>
            <a-select-option value="DELETE">DELETE</a-select-option>
          </a-select>
        </a-col>
        <a-col :md="22" :sm="20">
          <a-input-search
            placeholder="input send url"
            v-model="url"
            :expand-depth="5"
            @search="onSearch"
            enterButton="發 送 請 求"
            size="large" />
        </a-col>
      </a-row>
      <a-tabs defaultActiveKey="2" style="margin-top: 10px">
        <a-tab-pane tab="Params" key="2">
          <a-textarea style="width:100%;font-size: 16px;font-weight:500;height: 300px;resize: none;" @blur="changeVal" :placeholder="paramsPlace"></a-textarea>
        </a-tab-pane>
      </a-tabs>
      <a-tabs defaultActiveKey="1" style="margin-top: 10px">
        <a-tab-pane tab="Response" key="1">
          <json-viewer
            style="background-color: #E9EBFE;min-height: 300px;"
            :value="resultJson"
            boxed>
          </json-viewer>
        </a-tab-pane>
      </a-tabs>
    </a-card>
  </a-drawer>
</template>
<script>
  import { axios } from '@/utils/request'
  export default {
    name: 'FlowTest',
    data() {
      return {
        url: '',
        tokenStr: '',
        tokenUrl: '',
        paramJson: '參數轉換后對應的JSON',
        paramsPlace: '{\n“key1” : “value1”,\n“key2” : value2\n}',
        visible: false,
        resultJson: {},
        requestMethod: 'GET'
      }
    },
    methods: {
      show(record) {
        this.url = process.env.VUE_APP_API_BASE_URL + record.address
        this.tokenUrl = process.env.VUE_APP_API_BASE_URL + '/openApi/refreshToken/' + record.appId + '/lzjy@2021!'
        this.requestMethod = record.method_dictText
        this.visible = true
      },
      onSearch (value) {
        let that = this
        if (!value) {
          that.$message.error('請填寫路徑')
          return false
        }
        this.resultJson = {}
        console.log(that.tokenStr)
        axios({
            headers: {
              'Access-Token': that.tokenStr
            },
            url: value,
            method: that.requestMethod,
            data: that.paramJson,
            params: that.paramJson
          }).then((res) => {
            that.resultJson = res
            if (value.indexOf('refreshToken') !== -1) {
              that.tokenStr = res.result
              console.log('***********' + that.tokenStr)
            }
          }).catch((err) => {
            console.log(err)
            that.$message.error('請求異常:' + err)
          })
      },
      changeVal(e) {
        try {
          let json = e.target.value
          if (!json) {
            return false
          }
          this.paramJson = JSON.parse(json)
        } catch (e) {
          console.log(e)
          this.$message.error('非法的JSON字符串')
        }
      },
      close () {
        this.resultJson = {}
        this.paramJson = ''
        this.visible = false
      }
    }
  }
</script>




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM