-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
309 lines (285 loc) · 10.5 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>PSU CSSA</title>
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<style media="screen">
body {
padding: 15px;
}
header {
margin-bottom: 30px;
}
.query-bar {
margin-bottom: 20px;
}
.query-bar label, .query-bar input {
margin-right: 8px;
}
</style>
<!-- 其中 client_id 需替换为您应用的 ClientID -->
<script src="https://cloud.minapp.com/custom-userdash/auth/:2ab8ed842c8a220c9bb1/"></script>
</head>
<body>
<header>
<h1>PSU CSSA后台运营</h1>
<h4>活动报名数据表</h4>
</header>
<div class="content-body">
<form class="form-inline query-bar">
<label for="uid">微信号</label>
<input type="text" class="form-control" id="bookName" placeholder="微信 ID">
<label for="phoneNumber">活动名称</label>
<input type="text" class="form-control" id="index" placeholder="活动名称">
<button type="button" class="btn btn-primary" onclick="queryRecord()">查询</button>
<button type="button" class="btn btn-primary ml-2" onclick="recordId = '';clearEditForm()" data-toggle="modal" data-target="#editModal">新建</button>
</form>
<table class="table" style="font-size: 14px;">
<thead class="thead-light">
<tr>
<th>微信号</th>
<th>姓名</th>
<th>年龄</th>
<th>活动名称</th>
<!--th>行政区</th>
<th>地址</th>
<th>邮政编码</th>
<th>联系人</th>
<th>联系方式</th>
<th>状态</th>
<th>创建时间</th>
<th>操作</th-->
</tr>
</thead>
<tbody id="table-body">
</tbody>
</table>
</div>
<div class="modal" tabindex="-1" role="dialog" id="editModal">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">信息管理</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form id='editForm'>
<div class="form-group">
<label for="country">微信号</label>
<input type="text" class="form-control" id="bookName" name="bookName">
</div>
<div class="form-group">
<label for="country">姓名</label>
<input type="text" class="form-control" id="Name" name="Name">
</div>
<div class="form-group">
<label for="province">年龄</label>
<input type="text" class="form-control" id="Age" name="Age">
</div>
<div class="form-group">
<label for="city">活动名称</label>
<input type="text" class="form-control" id="index" name="index">
</div>
<div class="form-group">
<label for="district">队伍(选填)</label>
<input type="text" class="form-control" id="Team" name="Team">
</div>
<!--div class="form-group">
<label for="address">地址</label>
<input type="text" class="form-control" id="address" name="address">
</div>
<div class="form-group">
<label for="address">邮政编码</label>
<input type="text" class="form-control" id="zipcode" name="zipcode">
</div>
<div class="form-group">
<label for="contact">联系人</label>
<input type="text" class="form-control" id="contact" name="contact">
</div>
<div class="form-group">
<label for="phone">联系方式</label>
<input type="text" class="form-control" id="phone" name="phone">
</div-->
<div class="form-group">
<label for="status">状态</label>
<select class="form-control" id="status" name="status">
<option value="0">有效</option>
<option value="1">过期</option>
</select>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" onclick="submitModal()">确定</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
</div>
</div>
</div>
</div>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdn.bootcss.com/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://cdn.bootcss.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<script type="text/javascript">
// 配置对象
var config = {
clientId: '2ab8ed842c8a220c9bb1', // 应用 ClientID,可在管理后台设置模块找到
tableId: '40995', // 数据表 ID
}
var client = axios.create({
baseURL: 'https://cloud.minapp.com/userve/v1/',// user dash api 的请求地址
withCredentials: true // 必须手动开启为 true,允许跨域请求发送身份凭证信息
})
var records = []
var recordId = ''
var statusMap = {
'0': '有效',
'1': '过期'
}
var formatDate = function (timestamp) {
return new Date(timestamp * 1000).toLocaleDateString().replace(/\//g, '-')
}
// 渲染数据表格
//<td><a href="#editModal" data-toggle="modal" onclick="fillForm('${d.id}')">编辑</a> <a href="javascript:;" onclick="deleteRecord('${d.id}')">删除</a></td>
var renderTable = function (data) {
var tableHtml = ''
for (var i = 0; i < data.length; i++) {
var d = data[i]
tableHtml += `<tr>
<td>${d.bookName}</td>
<td>${d.Name}</td>
<td>${d.Age}</td>
<td>${d.index}</td>
<td>${d.Team}</td>
<td>${statusMap[d.status]}</td>
<td>${formatDate(d.created_at)}</td>
</tr>`
}
return tableHtml
}
var getRecords = function (offset, where) {
var _offset = offset || 0
var _where = where || {}
client.get(`table/${config.tableId}/record/`, {
params: {
order_by: '-updated_at',
limit: 50,
offset: _offset,
where: _where
}
}).then(function(res) {
var data = res.data.objects
records = data
var html = renderTable(data)
document.querySelector('#table-body').innerHTML = html
})
}
var deleteRecord = function (recordId) {
client.delete(`table/${config.tableId}/record/${recordId}/`).then(function(res) {
if (res.status === 204) {
getRecords()
} else {
throw Error(res)
}
}).catch(function(err) {
console.log(err)
})
}
var updateRecord = function (recordId, data) {
var _data = data || {}
return client.put(`table/${config.tableId}/record/${recordId}/`, _data)
}
var addRecord = function (data) {
return client.post(`table/${config.tableId}/record/`, data)
}
// 将数据填充到编辑 modal
var fillForm = function (id) {
recordId = id // 将 id 赋值给 recordId,保存当前编辑数据的 id
var editRecord = records.find(function(r) {
return r.id == id
})
var eles = document.querySelector('#editForm').elements
for (var i = 0; i < eles.length; i++) {
eles[i].value = editRecord[eles[i].name]
}
}
// 清空编辑 modal
var clearEditForm = function () {
var eles = document.querySelector('#editForm').elements
for (var i = 0; i < eles.length; i++) {
eles[i].value = ''
}
}
// 查询数据
var queryRecord = function () {
var bookName = document.querySelector('#bookName').value
var index = document.querySelector('#index').value
var queryData = {} // 构造查询数据时要注意数据类型必须与知晓云后台设置的数据类型一致,如本 demo uid 必须为整型
if (bookName && !index) {
queryData = {
"bookName": {"$eq": bookName}
}
} else if (!bookName && index) {
queryData = {
"index": {"$eq": index}
}
} else if (bookName && index) {
queryData = {
"$and": [
{
"bookName": {"$eq": bookName}
}, {
"index": {"$eq": index}
}
]
}
}
getRecords(0, JSON.stringify(queryData))
}
var submitModal = function () {
var isEdit = !!recordId // 利用 recordId 判断编辑、新建状态
var data = {}
var eles = document.querySelector('#editForm').elements
for (var i = 0; i < eles.length; i++) {
var value = ''
if (eles[i].name === 'bookName') {
value = parseInt(eles[i].value)
} else {
value = eles[i].value
}
Object.assign(data, {
[eles[i].name]: value
})
}
// 区分编辑、新建状态调用不同接口。更新数据成功后重新拉取最新的数据列表
if (isEdit) {
updateRecord(recordId, data).then(function(res) {
if (res.status === 200 ) {
getRecords()
$('#editModal').modal('hide')
} else {
throw Error(res)
}
}).catch(function(err) {
console.log(err)
})
} else {
addRecord(data).then(function(res) {
if (res.status === 201 ) {
getRecords()
$('#editModal').modal('hide')
} else {
throw Error(res)
}
}).catch(function(err) {
console.log(err)
})
}
}
getRecords()
</script>
</body>
</html>