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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
{% extends "base.html" %}
{% block css %}
<style>
/* 按钮悬浮*/
.float-button {
position: fixed;
bottom: 100px;
right: 100px;
z-index: 99;
border: none;
outline: none;
background-color: #1dc5a3;
cursor: pointer;
padding: 15px;
border-radius: 10px;
}
.btn-primary:hover {
color: red;
}
</style>
{% endblock %}
{% block crumbs %}
<div class="row">
<div class="col-md-12">
<ol class="breadcrumb">
<li>
<a href="/yqjr/index">主页</a>
</li>
<li class="active">
<a class="this">基础信息</a>
</li>
<li class="active">
<a class="this-page" href="{% url 'interface' %}">接口管理</a>
</li>
<li class="active">
<a class="">添加接口</a>
</li>
</ol>
</div>
</div>
{% endblock %}
{% block body %}
<div class="row">
<div class="col-md-12">
<div class="panel">
<div class="panel-heading">
<h3 class="panel-title">添加接口</h3>
</div>
<form method="post" class="form-horizontal" action="{% url 'interface_add' %}">
<div class="panel-body">
<div class="form-horizontal">
<div class="form-group">
<label class="col-sm-2 control-label">接口名称</label>
<div class="col-sm-8">
<input type="text" id="if_name" name="if_name" class="form-control" value="{{ name }}">
</div>
{% if name_error %}
<span style="color: red; font-size: 10px;">{{ name_error }}</span>
{% endif %}
</div>
<div class="form-group">
<label class="col-sm-2 control-label">所属项目</label>
<div class="col-sm-8">
<select type="text" id="prj_id" class="form-control" required="required"
name='prj_id'>
{% for prj in prj_list %}
{% ifequal prj.prj_id project_c.prj_id %}
<option value={{ prj.prj_id }} selected>{{ prj.prj_name }}</option>
{% else %}
<option value={{ prj.prj_id }}>{{ prj.prj_name }}</option>
{% endifequal %}
{% endfor %}
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">URL</label>
<div class="col-sm-8">
<input type="text" id="url" name="url" class="form-control" value="{{ url }}">
</div>
{% if url_error %}
<span style="color: red; font-size: 10px;">{{ url_error }}</span>
{% endif %}
</div>
<div class="form-group">
<label class="col-sm-2 control-label">请求方式</label>
{% for item in method_types %}
<div class="col-sm-1 control-label">
<label class="fancy-radio">
{% ifequal item method %}
<input name="method" value={{ item }} type="radio"
checked><span><i></i>{{ item }}</span>
{% else %}
<input name="method" value={{ item }} type="radio"
><span><i></i>{{ item }}</span>
{% endifequal %}
</label>
</div>
{% endfor %}
{% if method_error %}
<span style="color: red; font-size: 10px;">{{ method_error }}</span>
{% endif %}
</div>
<div class="form-group">
<label class="col-sm-2 control-label">数据传输方式</label>
<div class="col-sm-8">
<select type="text" id="data_type" name="data_type" class="form-control">
{% for item in data_types %}
{% ifequal item data_type %}
<option value="{{ item }}" selected>{{ item }}</option>
{% else %}
<option value="{{ item }}">{{ item }}</option>
{% endifequal %}
{% endfor %}
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">接口描述</label>
<div class="col-sm-8">
<textarea name="description" placeholder="请输入内容" class="form-control"
rows="3">{{ description }}</textarea>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">请求header</label>
<div class="col-sm-8">
<button id="add_request_header" class="btn btn-primary" type="button"><i
class="fa fa-plus-square-o"></i> 添加行
</button>
</div>
{% if headers_error %}
<span style="color: red; font-size: 10px;">{{ headers_error }}</span>
{% endif %}
<div class="col-sm-offset-2 col-sm-8">
<table id="request_header" class="table col-sm-10">
<thead>
<tr>
<th>参数名</th>
<th>参数值</th>
<th></th>
</tr>
</thead>
<tbody>
{% if request_headers %}
{% for request_header in request_headers %}
<tr>
<td><input class='form-control' name="request_header_key"
value="{{ request_header.0 }}">
</td>
<td><input class='form-control' name="request_header_value"
value="{{ request_header.1 }}">
</td>
<td>
<button class='btn btn-danger' onclick='del_step(this)'><i
class='fa fa-close'></i> 删除行
</button>
</td>
</tr>
{% endfor %}
{% endif %}
</tbody>
</table>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">请求params</label>
<div class="col-sm-8">
<button id="add_request_params" class="btn btn-primary" type="button"><i
class="fa fa-plus-square-o"></i> 添加行
</button>
</div>
{% if params_error %}
<span style="color: red; font-size: 10px;">{{ params_error }}</span>
{% endif %}
<div class="col-sm-offset-2 col-sm-8">
<table id="request_params" class="table col-sm-10">
<thead>
<tr>
<th>参数名</th>
<th>参数值</th>
<th></th>
</tr>
</thead>
<tbody>
{% if request_params %}
{% for request_param in request_params %}
<tr>
<td><input class='form-control' name="request_params_key"
value="{{ request_param.0 }}">
</td>
<td><input class='form-control' name="request_params_value"
value="{{ request_param.1 }}">
</td>
<td>
<button class='btn btn-danger' onclick='del_step(this)'><i
class='fa fa-close'></i> 删除行
</button>
</td>
</tr>
{% endfor %}
{% endif %}
</tbody>
</table>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">请求body</label>
<div class="col-sm-8">
<button id="add_request_body" class="btn btn-primary" type="button"><i
class="fa fa-plus-square-o"></i> 添加行
</button>
</div>
{% if body_error %}
<span style="color: red; font-size: 10px;">{{ body_error }}</span>
{% endif %}
<div class="col-sm-offset-2 col-sm-8">
<table id="request_body" class="table col-sm-10">
<thead>
<tr>
<th>参数名</th>
<th>参数值</th>
<th></th>
</tr>
</thead>
<tbody>
{% if request_body %}
{% for request_body_item in request_body %}
<tr>
<td><input class='form-control' name="request_body_key"
value="{{ request_body_item.0 }}">
</td>
<td><input class='form-control' name="request_body_value"
value="{{ request_body_item.1 }}">
</td>
<td>
<button class='btn btn-danger' onclick='del_step(this)'><i
class='fa fa-close'></i> 删除行
</button>
</td>
</tr>
{% endfor %}
{% endif %}
</tbody>
</table>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-4 col-sm-6">
<button id="sumbit" class="btn btn-primary float-button" type="submit"><i
class="fa fa-save"></i> 保存
</button>
</div>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
{% endblock %}
{% block javascript %}
<script>
function del_step(obj) {
var tr = obj.parentNode.parentNode;
var tbody = tr.parentNode;
tbody.removeChild(tr);
}
$(document).ready(function () {
//添加请求头参数行
$("#add_request_header").click(function () {
var newRow = "<tr> <td name='var_name' ><input class='form-control' name='request_header_key'></td>" +
"<td name='var_remark' ><input class='form-control' name='request_header_value'></td> " +
"<td> <button class='btn btn-danger' onclick='del_step(this)'><i class='fa fa-close'></i> 删除行</button> </td> </tr>";
$("#request_header tbody").append(newRow);
});
$("#add_request_params").click(function () {
var newRow = "<tr> <td name='var_name'><input class='form-control' name='request_params_key'></td>" +
"<td name='var_remark' ><input class='form-control' name='request_params_value'></td> " +
"<td> <button class='btn btn-danger' onclick='del_step(this)'><i class='fa fa-close'></i> 删除行</button> </td> </tr>";
$("#request_params tbody").append(newRow);
});
$("#add_request_body").click(function () {
var newRow = "<tr> <td name='var_name'><input class='form-control' name='request_body_key'></td>" +
"<td name='var_remark' ><input class='form-control' name='request_body_value'></td>" +
" <td> <button class='btn btn-danger' onclick='del_step(this)'><i class='fa fa-close'></i> 删除行</button> </td> </tr>";
$("#request_body tbody").append(newRow);
});
//删除行
});
$('#get_table_data').click(function () {
var args = {};
$('#request_header tr').each(function (i) { // 遍历 tr
if (i == 0) return true;
var data = new Object();
$(this).find("td[name]").each(function () {
var name = $(this).attr("name");
data[name] = $(this).text();
});
args[i] = data;
});
send_data = JSON.stringify(args);
alert(send_data);
});
// 检查输入框
function check(att) {
strValue = att.replace(/\n/g, '');
if (javaTrim(strValue) == "") {
alert('输入有误,请检查!');
return false;
}
}
function javaTrim(str) {
for (var i = 0; (str.charAt(i) == ' ') && i < str.length; i++) ;
if (i == str.length) return ''; //whole string is space
var newstr = str.substr(i);
for (var i = newstr.length - 1; newstr.charAt(i) == ' ' && i >= 0; i--) ;
newstr = newstr.substr(0, i + 1);
return newstr;
}
</script>
{% endblock %}