wqyblog_api_go/wqyblog_webui/js/edit.js

79 lines
2.2 KiB
JavaScript

$(function () {
if (!Cookie()){
window.location.href = `/index.html`
}
const urlParams = new URLSearchParams(window.location.search);
if (urlParams.get('s') === 'new') {
$('#post_title').val('')
$('#post_content').val('')
}
if (urlParams.get('s') === 'old') {
$.ajax({
url: `${baseUrl}/posts/${urlParams.get('id')}`,
type: 'GET',
contentType: 'application/json',
success: function (data) {
$('#post_title').val(data.title)
$('#post_content').val(data.content)
$('#show').html(md.render(data.content));
}
})
}
$('#post_content').on('input', function () {
// 获取输入框的值
let content = $(this).val()
$('#show').html(md.render(content));
})
$('#save').click(function () {
// 获取输入框的值
let title = $('#post_title').val()
let content = $('#post_content').val()
if (title === '' || content === '') {
alert('请输入标题和内容')
return
}
if (urlParams.get('s') === 'new') {
$.ajax({
url: `${baseUrl}/posts`,
type: 'POST',
data: JSON.stringify({
title: title,
content: content
}),
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', 'Bearer ' + document.cookie.split('token=')[1].split(';')[0])
},
contentType: 'application/json',
success: function (data) {
alert('保存成功')
window.location.href = `/index.html`
},
error: function (data) {
alert('保存失败')
}
})
}
if (urlParams.get('s') === 'old') {
$.ajax({
url: `${baseUrl}/posts/${urlParams.get('id')}`,
type: 'PUT',
data: JSON.stringify({
title: title,
content: content
}),
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', 'Bearer ' + document.cookie.split('token=')[1].split(';')[0])
},
contentType: 'application/json',
success: function (data) {
alert('保存成功')
window.location.href = `/index.html`
},
error: function (data) {
}
})
}
})
})