picgo/static/js/user.js
2024-08-09 20:30:35 +08:00

114 lines
3.6 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

function User() {
this.pageSize = 10
this.page = 1
this.search = ""
}
User.prototype.CreateUser = function () {
// 示例字段配置
let fields = [
{label: '用户名:', name: 'username', type: 'text'},
{label: '密码:', name: 'password', type: 'password'},
{label: '确认密码:', name: 'confirm', type: 'password'}
]
// 初始化表单并显示
$('#add-user').on('click', function () {
let title = '添加用户'
let modal = new Modal(title, fields, function (data) {
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)
// 刷新表格
} else {
Alert.AlertError(result.message)
}
},
error: function (xhr, status, error) {
Alert.AlertError("服务器内部错误!")
console.log(" 状态: ", status, ' 请求失败:', error);
}
})
})
modal.RenderModalForm()
})
}
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);
}
})
}
User.prototype.run = function () {
this.RefreshTable()
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()
})