picgo/static/js/user.js

114 lines
3.6 KiB
JavaScript
Raw Permalink Normal View History

2024-08-06 20:37:34 +08:00
function User() {
2024-08-09 20:30:35 +08:00
this.pageSize = 10
this.page = 1
this.search = ""
2024-08-06 20:37:34 +08:00
}
User.prototype.CreateUser = function () {
// 示例字段配置
let fields = [
2024-08-09 20:30:35 +08:00
{label: '用户名:', name: 'username', type: 'text'},
{label: '密码:', name: 'password', type: 'password'},
{label: '确认密码:', name: 'confirm', type: 'password'}
2024-08-06 20:37:34 +08:00
]
// 初始化表单并显示
2024-08-09 20:30:35 +08:00
$('#add-user').on('click', function () {
2024-08-06 20:37:34 +08:00
let title = '添加用户'
2024-08-09 20:30:35 +08:00
let modal = new Modal(title, fields, function (data) {
2024-08-06 20:37:34 +08:00
console.log('表单提交数据:', data)
let csrfToken = document.getElementsByName("gorilla.csrf.Token")[0].value
console.log('表单提交数据csrfToken', csrfToken)
// 表单
if (!data.username) {
Alert.AlertError("请输入用户名!")
return
}
if (!data.password || !data.confirm) {
Alert.AlertError("请输入密码!")
return
}
if (data.password !== data.confirm) {
Alert.AlertError("请确认密码!")
return
}
$.ajax({
url: '/api/v1/user/create',
method: 'POST',
data: JSON.stringify({
username: data.username,
password: data.password
}),
headers: {
'Content-Type': 'application/json',
"X-CSRF-Token": csrfToken
},
success: function (result) {
if (result.status === 200) {
Alert.AlertSuccessToast(result.message)
2024-08-09 20:30:35 +08:00
// 刷新表格
2024-08-06 20:37:34 +08:00
} else {
Alert.AlertError(result.message)
}
},
error: function (xhr, status, error) {
Alert.AlertError("服务器内部错误!")
console.log(" 状态: ", status, ' 请求失败:', error);
}
})
})
modal.RenderModalForm()
})
}
2024-08-09 20:30:35 +08:00
User.prototype.RefreshTable = function () {
let self = this
let csrfToken = document.getElementsByName("gorilla.csrf.Token")[0].value
let url = `/api/v1/user/page?pageSize=${self.pageSize}&page=${self.page}`
if (!self.search) {
url = `/api/v1/user/page?pageSize=${self.pageSize}&page=${self.page}&search=${self.search}`
}
$.ajax({
url: url,
method: 'POST',
headers: {
'Content-Type': 'application/json',
"X-CSRF-Token": csrfToken
},
success: function (result) {
if (result.status === 200) {
console.log("用户数据: ", result.data)
} else {
Alert.AlertSimpleWarn(result.message)
}
},
error: function (xhr, status, error) {
Alert.AlertError("服务器内部错误!")
console.log(" 状态: ", status, ' 请求失败:', error);
}
})
2024-08-06 20:37:34 +08:00
}
User.prototype.run = function () {
2024-08-09 20:30:35 +08:00
this.RefreshTable()
2024-08-06 20:37:34 +08:00
this.CreateUser()
}
// 构造执行入口
$(function () {
// feather.replace()
// 模板过滤方法
// if (window.template) {
// template.defaults.imports.domainSubstring = function (dateValue) {
// if (dateValue.length > 40 ) {
// return dateValue.substring(0, 37) + "..."
// } else {
// return dateValue
// }
// }
// }
let user = new User()
user.run()
})