77 lines
1.9 KiB
JavaScript
77 lines
1.9 KiB
JavaScript
$(document).ready(function() {
|
|
loadProfile();
|
|
|
|
$('#profile-form').submit(function(e) {
|
|
e.preventDefault();
|
|
updateProfile();
|
|
});
|
|
|
|
$('#password-form').submit(function(e) {
|
|
e.preventDefault();
|
|
changePassword();
|
|
});
|
|
});
|
|
|
|
function loadProfile() {
|
|
$.ajax({
|
|
url: '/api/users/profile',
|
|
method: 'GET',
|
|
headers: {
|
|
'Authorization': 'Bearer ' + localStorage.getItem('access_token')
|
|
},
|
|
success: function(data) {
|
|
$('#username').val(data.username);
|
|
$('#email').val(data.email);
|
|
$('#uid').val(data.uid);
|
|
},
|
|
error: handleAjaxError
|
|
});
|
|
}
|
|
|
|
function updateProfile() {
|
|
$.ajax({
|
|
url: '/api/users/profile',
|
|
method: 'PUT',
|
|
headers: {
|
|
'Authorization': 'Bearer ' + localStorage.getItem('access_token')
|
|
},
|
|
contentType: 'application/json',
|
|
data: JSON.stringify({
|
|
email: $('#email').val(),
|
|
uid: $('#uid').val()
|
|
}),
|
|
success: function(data) {
|
|
alert(data.message);
|
|
},
|
|
error: handleAjaxError
|
|
});
|
|
}
|
|
|
|
function changePassword() {
|
|
const newPassword = $('#new-password').val();
|
|
const confirmPassword = $('#confirm-password').val();
|
|
|
|
if (newPassword !== confirmPassword) {
|
|
alert('两次密码不一致');
|
|
return;
|
|
}
|
|
|
|
$.ajax({
|
|
url: '/api/users/change-password',
|
|
method: 'POST',
|
|
headers: {
|
|
'Authorization': 'Bearer ' + localStorage.getItem('access_token')
|
|
},
|
|
contentType: 'application/json',
|
|
data: JSON.stringify({
|
|
old_password: $('#old-password').val(),
|
|
new_password: newPassword
|
|
}),
|
|
success: function(data) {
|
|
alert(data.message);
|
|
$('#password-form')[0].reset();
|
|
},
|
|
error: handleAjaxError
|
|
});
|
|
}
|