doc-act-list.vue
4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<template>
<Collapse accordion v-model="openAct" @on-change="actChange">
<Panel :name="act.name" v-for="act in list" :key="act.name">
{{act.name}}({{act.desc}})
<div slot="content" class="act-content" v-if="act.__rendered">
<h2>接口路径</h2>
<div class="path">{{getPath(act)}}</div>
<div v-if="showUrlParams(act)">
<h2>提交参数</h2>
<Table :columns="reqColumns" :data="paramsColumns(act)"></Table>
</div>
<div v-if="act.reqMethod === 'JSON'">
<h2>提交示例</h2>
<json-view :value="paramsJsonColumns(act)" icon-prefix="ivu-icon" @copied="copied"></json-view>
</div>
<div v-if="act.responseInfo.data">
<h2>返回示例</h2>
<json-view :value="act.responseInfo.data" icon-prefix="ivu-icon" @copied="copied"></json-view>
</div>
<div v-if="act.responseInfo.codeDesc.length">
<h2>状态码</h2>
<Table :width="500" :columns="codeColumns" :data="act.responseInfo.codeDesc"></Table>
</div>
</div>
</Panel>
</Collapse>
</template>
<script>
import _ from 'lodash';
import config from 'config';
import JsonView from 'vue-json-viewer';
export default {
name: 'DocActList',
props: {
list: Array
},
data() {
return {
test2: '',
openAct: [],
reqColumns: [{
title: '名称',
key: 'name'
}, {
title: '类型',
key: 'paramType'
}, {
title: '必填',
key: 'required'
}, {
title: '默认值',
key: 'defaultValue'
}, {
title: '描述',
key: 'desc'
}],
resColumns: [{
title: '类型',
key: 'clazz'
}, {
title: '描述',
key: 'desc'
}],
codeColumns: [{
title: '状态码',
key: 'code'
}, {
title: '说明',
key: 'desc'
}],
reqData: [],
resData: []
};
},
methods: {
actChange(open) {
let act = this.list.find(a => a.name === open[0]);
if (act) {
act.__rendered = true;
}
},
getPath(act) {
if (act.reqMethod === 'JSON') {
return config.axiosBaseUrl + act.path;
}
const params = act.paramInfo.map(param => {
return `${param.name}=${param.defaultValue}`;
}).join('&');
if (act.path.indexOf('method') >= 0) {
return `${config.axiosBaseUrl}/?${act.path}&${params}`;
}
return `${config.axiosBaseUrl}/?${params}`;
},
paramsColumns(act) {
return act.paramInfo[0].data ? this.transformParams(act.paramInfo[0].data) : act.paramInfo;
},
paramsJsonColumns(act) {
if (act.paramInfo.length && typeof act.paramInfo[0].data !== 'object') {
let data = {};
act.paramInfo.forEach(param => {
data[param.name] = param.defaultValue;
});
return data;
}
return act.paramInfo[0].data;
},
showUrlParams(act) {
return (act.reqMethod === 'URL' && act.paramInfo.length) ||
(act.reqMethod === 'JSON' && act.paramInfo.length && typeof act.paramInfo[0].data !== 'object');
},
transformParams(data) {
return _.map(data, (v, k) => {
return {
name: k,
paramType: typeof v
};
});
},
copied() {
this.$Message.success('复制成功');
}
},
components: {
JsonView
}
};
</script>
<style lang="scss">
.act-content {
padding: 0 16px;
.path {
font-size: 15px;
}
}
</style>