This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
cyber-narrator-cn-ui/src/components/setting/GalaxyProxyDebug.vue

330 lines
10 KiB
Vue
Raw Normal View History

2021-11-11 23:28:52 +08:00
<template>
<el-dialog
v-model="show"
:top="top"
:modal="modal"
:custom-class="customClass"
:show-close="showClose"
:width="width"
@close="closeDebug"
@open="openDebug"
destroy-on-close
>
<div class="debug-wrapper">
2021-11-11 23:28:52 +08:00
<el-select v-model="name" filterable placeholder="Select" class="item item-1" allow-create
@change="changPath">
<template v-for="(proxy,index) in galaxyProxyData" :key="proxy.id">
<el-option :label="proxy.name" :value="index"></el-option>
</template>
</el-select>
<div class="item item-2 sub-grid-send">
<el-select v-model="method" filterable placeholder="Select" style="width:100px;" >
<el-option
v-for="item in methodOptions"
:key="item.value"
:label="item.label"
:value="item.value"
>
</el-option>
</el-select>
2021-11-19 10:57:45 +08:00
<el-input v-model="fullPathWithParams" placeholder="Please input" readonly/>
2021-11-11 23:28:52 +08:00
<el-button @click="send">{{$t('galaxyProxy.debug.send')}}</el-button>
</div>
<div class="item item-3 sub-grid-params">
<div class="sub-grid-params-add">
<span style="padding-left: 15px;">{{$t('galaxyProxy.debug.requestParams')}}</span>
<el-button size="mini" @click="handleAddDetails"><i class="cn-icon-add cn-icon"></i></el-button>
</div>
2021-11-19 10:57:45 +08:00
<el-table :data="paramsTableData"
width="100%"
2021-11-11 23:28:52 +08:00
@selection-change="handleDetailSelectionChange"
2021-11-19 10:57:45 +08:00
:row-class-name="rowClassName"
empty-text=""
2021-11-11 23:28:52 +08:00
ref="tb">
2021-11-19 10:57:45 +08:00
<el-table-column type="selection" align="center" label="" width="30px" style="border-left:0px;" />
<el-table-column prop="key" align="center" width="80px" >
<template #header>
<div class="table-operation-title">{{$t('galaxyProxy.debug.key')}}</div>
</template>
2021-11-11 23:28:52 +08:00
<template #default="scope">
2021-11-19 10:57:45 +08:00
<el-input @change="paramsChange(scope.row)" v-model="scope.row.key"></el-input>
2021-11-11 23:28:52 +08:00
</template>
</el-table-column>
2021-11-19 10:57:45 +08:00
<el-table-column prop="value" align="center" >
<template #header>
<div class="table-operation-title">{{$t('galaxyProxy.debug.value')}}</div>
</template>
2021-11-11 23:28:52 +08:00
<template #default="scope">
2021-11-19 10:57:45 +08:00
<el-input @change="paramsChange(scope.row)" v-model="scope.row.value"></el-input>
2021-11-11 23:28:52 +08:00
</template>
</el-table-column>
2021-11-19 10:57:45 +08:00
<el-table-column align="center" style="border-right:0px;" width="70px">
<template #header>
<div class="table-operation-title">{{$t('overall.option')}}</div>
</template>
<template #default="scope">
<div @click="deleteRow(scope.$index)" class="debug__params-delete">{{$t('overall.delete')}}</div>
</template>
</el-table-column>
<template v-slot:empty>
<div style="height:0px;"></div>
</template>
2021-11-11 23:28:52 +08:00
</el-table>
</div>
<div class="item item-4">
<div class="request-header" >{{$t('galaxyProxy.debug.request.header')}}</div>
<div class="request-header-content" ><pre v-html="proxyRequestHeader" style="height: 98%;width: 98%;margin-left: -15px;margin-top: -10px;"></pre></div>
<div class="response-header">{{$t('galaxyProxy.debug.response.header')}}</div>
<div class="response-body">{{$t('galaxyProxy.debug.response.body')}}</div>
<div class="response-header-content"><pre v-html="proxyResponseHeader" style="height: 98%;width: 98%;margin-left: -15px;margin-top: -10px;"></pre></div>
<div class="response-body-content color-pre__style">
2021-11-19 10:57:45 +08:00
<v-ace-editor
v-model:value="proxyResponseBody"
lang="json"
readonly="true"
theme="chrome"
style="height: 100%" />
2021-11-11 23:28:52 +08:00
</div>
</div>
</div>
</el-dialog>
</template>
<script>
import { api } from '@/utils/api'
import { getForDebug, postForDebug } from '@/utils/http'
2021-11-11 23:28:52 +08:00
import axios from 'axios'
import { VAceEditor } from 'vue3-ace-editor'
import 'ace-builds/src-noconflict/mode-javascript'
import 'ace-builds/src-noconflict/mode-json'
import 'ace-builds/src-noconflict/theme-chrome'
export default {
name: 'GalaxyProxyDebug',
2021-11-19 10:57:45 +08:00
components: {
VAceEditor
},
2021-11-11 23:28:52 +08:00
props: {
showDebug: Boolean,
top: {
type: String,
default: '5vh'
},
modal: {
type: Boolean,
default: true
},
customClass: {
type: String,
default: 'proxy-debug__dialog'
},
showClose: {
type: Boolean,
default: true
},
width: { // 高度需要通过customClass自行定义
type: [String, Number],
default: '90vw'
},
curGalaxyProxy: Object // 实体,{ name: '', path: '', icon: icon-class }
},
data () {
return {
show: false,
galaxyProxyData: [],
name: '',
paramsTableData: [
2021-11-19 10:57:45 +08:00
2021-11-11 23:28:52 +08:00
],
// 选中的数据
checkedDetail: {},
2021-11-19 10:57:45 +08:00
checkedRowData: [],
2021-11-11 23:28:52 +08:00
methodOptions: [
{
value: 'GET',
label: 'GET'
},
{
value: 'POST',
label: 'POST'
}
],
method: 'GET',
2021-11-19 10:57:45 +08:00
path: '', // 不带参数的路径用于请求URL
fullPathWithParams: '', // 带参数的完整路径,用于界面显示
2021-11-11 23:28:52 +08:00
proxyResponseBody: '',
proxyRequestHeader: '',
proxyResponseHeader: ''
}
},
setup () {
},
methods: {
handleAddDetails () {
if (this.paramsTableData == undefined) {
this.paramsTableData = new Array()
}
const obj = {}
obj.key = ''
obj.value = ''
this.paramsTableData.push(obj)
},
2021-11-19 10:57:45 +08:00
// 删除当前行
deleteRow (index) {
this.paramsTableData.splice(index, 1)
this.updateUrlParams()
},
rowClassName ({ row, rowIndex }) {
// 把每一行的索引放进row
row.index = rowIndex
},
// 参数发送改变时
paramsChange (row) {
this.fullPathWithParams = this.path
this.checkedDetail = {}
this.checkedRowData.forEach(t => {
if (t.key != '') {
this.checkedDetail[t.key] = t.value
}
})
if (this.fullPathWithParams != '') {
this.updateUrlParams()
}
},
updateUrlParams () {
this.fullPathWithParams = this.path
let params = ''
for (const key in this.checkedDetail) {
if (key != '') {
params = params + key + '=' + this.checkedDetail[key] + '&'
}
}
if (params.endsWith('&')) {
params = params.substring(0, params.length - 1)
}
if (params != '') {
this.fullPathWithParams = this.fullPathWithParams + '?' + params
}
},
2021-11-11 23:28:52 +08:00
// 单选框选中数据
handleDetailSelectionChange (selection) {
this.checkedDetail = {}
2021-11-19 10:57:45 +08:00
this.checkedRowData = selection
selection.forEach((item, index) => {
this.checkedDetail[item.key] = item.value
2021-11-11 23:28:52 +08:00
})
2021-11-19 10:57:45 +08:00
this.updateUrlParams()
2021-11-11 23:28:52 +08:00
},
async getGalaxyProxyData (value) {
await axios.get(api.galaxyProxy + '?pageSize=-1').then(response => {
if (response.status === 200) {
this.galaxyProxyData = response.data.data.list
2021-11-11 23:28:52 +08:00
}
})
},
2021-11-19 10:57:45 +08:00
syntaxUrl (json) {
if (typeof json != 'string') {
json = JSON.stringify(json, undefined, 2)
}
return json = json.replace(/,/g, '&').replace(/{/g, '').replace(/}/g, '').replace(/"/g, '').replace(/:/g, '=').replace(/ /g, '')
},
2021-11-11 23:28:52 +08:00
changPath (index) {
// 修改input的值
2021-11-19 10:57:45 +08:00
this.path = axios.defaults.baseURL + 'interface' + this.galaxyProxyData[index].path
this.updateUrlParams()
2021-11-11 23:28:52 +08:00
this.method = this.galaxyProxyData[index].method.toUpperCase()
},
send () {
// this.path = "galaxy/setting?pageNo=1&pageSize=1"
// 注意有GET与POST两种方式
if (this.method.toUpperCase() == 'GET') {
2021-11-19 10:57:45 +08:00
getForDebug(this.path, this.checkedDetail).then(response => {
this.proxyResponseBody = this.syntaxJson(JSON.parse(JSON.stringify(response.data)))
this.proxyResponseHeader = this.syntaxJson(JSON.parse(JSON.stringify(response.headers)))
this.proxyRequestHeader = this.syntaxJson(JSON.parse(JSON.stringify(response.config.headers)))
2021-11-11 23:28:52 +08:00
this.proxyResponseHeader = this.proxyResponseHeader.replace(/{/g, '').replace(/}/g, '').replace(/"/g, '')
this.proxyRequestHeader = this.proxyRequestHeader.replace(/{/g, '').replace(/}/g, '').replace(/"/g, '')
})
} else if (this.method.toUpperCase() == 'POST') {
2021-11-19 10:57:45 +08:00
postForDebug(this.path, this.checkedDetail).then(response => {
2021-11-11 23:28:52 +08:00
if (response) {
2021-11-19 10:57:45 +08:00
this.proxyResponseBody = this.syntaxJson(JSON.parse(JSON.stringify(response.data)))
this.proxyResponseHeader = this.syntaxJson(JSON.parse(JSON.stringify(response.headers)))
this.proxyRequestHeader = this.syntaxJson(JSON.parse(JSON.stringify(response.config.headers)))
2021-11-11 23:28:52 +08:00
this.proxyResponseHeader = this.proxyResponseHeader.replace(/{/g, '').replace(/}/g, '').replace(/"/g, '')
this.proxyRequestHeader = this.proxyRequestHeader.replace(/{/g, '').replace(/}/g, '').replace(/"/g, '')
}
})
}
},
// 格式化json
2021-11-19 10:57:45 +08:00
syntaxJson (json) {
2021-11-11 23:28:52 +08:00
if (typeof json != 'string') {
json = JSON.stringify(json, undefined, 2)
}
2021-11-19 10:57:45 +08:00
return json = json.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>')
2021-11-11 23:28:52 +08:00
},
closeDebug () {
2021-11-19 10:57:45 +08:00
this.paramsTableData = []// JSON.parse(JSON.stringify(paramsTableData))
2021-11-11 23:28:52 +08:00
this.proxyResponseBody = ''
this.proxyResponseHeader = ''
this.proxyRequestHeader = ''
this.name = ''
this.path = ''
2021-11-19 10:57:45 +08:00
this.fullPathWithParams = ''
2021-11-11 23:28:52 +08:00
for (const key in this.curGalaxyProxy) {
delete this.curGalaxyProxy[key]
}
},
openDebug () {
if (this.curGalaxyProxy.path) {
this.name = this.curGalaxyProxy.name
2021-11-19 10:57:45 +08:00
this.path = axios.defaults.baseURL + 'interface' + this.curGalaxyProxy.path
this.fullPathWithParams = this.path
2021-11-11 23:28:52 +08:00
this.method = this.curGalaxyProxy.method.toUpperCase()
} else {
this.name = ''
this.path = ''
this.method = 'GET'
}
}
},
mounted () {
// 获取代理名称及路径
this.getGalaxyProxyData()
},
watch: {
showDebug (n, o) {
this.show = n
},
show (n, o) {
this.$emit('update:show-debug', n)
2021-11-19 10:57:45 +08:00
},
path (n, o) {
this.updateUrlParams()
2021-11-11 23:28:52 +08:00
}
}
}
</script>