function openFileChooser() { var input = document.createElement('input'); input.type = 'file'; input.accept = 'image/*'; input.onchange = function () { var file = input.files[0]; var reader = new FileReader(); reader.readAsDataURL(file); reader.onload = function () { var image = new Image(); image.src = reader.result; image.onload = function () { var canvas = document.createElement('canvas'); var context = canvas.getContext('2d'); var size = Math.min(image.width, image.height); canvas.width = size; canvas.height = size; context.beginPath(); context.arc(size / 2, size / 2, size / 2, 0, Math.PI * 2, true); context.closePath(); context.clip(); context.drawImage(image, (size - image.width) / 2, (size - image.height) / 2, image.width, image.height); var avatar = document.getElementById('avatar'); avatar.style.backgroundImage = 'url(' + canvas.toDataURL() + ')'; document.getElementById('initial').textContent = ''; var formData = new FormData(); canvas.toBlob(function (blob) { formData.append('file', blob); var xhr = new XMLHttpRequest(); xhr.open('POST', `/upload_avatar/${username}`, true); xhr.onload = function () { console.log('File uploaded successfully!'); }; xhr.send(formData); }, 'image/jpeg', 0.8); }; }; var avatar = document.getElementById('avatar'); avatar.style.backgroundImage = 'url(' + URL.createObjectURL(file) + ')'; document.getElementById('initial').textContent = ''; }; input.click(); } function like(postId) { const likeBtn = document.querySelector(`#like-${postId}`); const scrollPos = window.scrollY; if (likeBtn.classList.contains('active')) { // 取消点赞 likeBtn.classList.remove('active'); likeBtn.innerHTML = ` ${parseInt(likeBtn.textContent) - 1} 个赞同`; fetch(`/like/${postId}`, { method: 'DELETE' }).then(() => { // 更新容器里面的内容 /*location.reload(); window.scrollTo(0, scrollPos); */ }); } else { // 点赞 likeBtn.classList.add('active'); likeBtn.innerHTML = ` ${parseInt(likeBtn.textContent) + 1} 个赞同`; fetch(`/like/${postId}`, { method: 'POST' }).then(() => { // 更新容器里面的内容 /*location.reload(); window.scrollTo(0, scrollPos); */ }); } } function collect(postId) { const collectBtn = document.querySelector(`#collect-${postId}`); const scrollPos = window.scrollY; if (collectBtn.classList.contains('active')) { // 取消收藏 collectBtn.classList.remove('active'); collectBtn.innerHTML = ` ${parseInt(collectBtn.textContent) - 1} 个收藏`; fetch(`/collect/${postId}`, { method: 'DELETE' }).then(() => { // 页面刷新且位置不变 /*location.reload(); window.scrollTo(0, scrollPos); */ }); } else { // 收藏 collectBtn.classList.add('active'); collectBtn.innerHTML = ` ${parseInt(collectBtn.textContent) + 1} 个收藏`; fetch(`/collect/${postId}`, { method: 'POST' }).then(() => { // 页面刷新且位置不变 /*location.reload(); window.scrollTo(0, scrollPos); */ }); } } function like1(postId) { const likeBtn = document.querySelector(`#like1-${postId}`); const scrollPos = window.scrollY; if (likeBtn.classList.contains('active')) { // 取消点赞 likeBtn.classList.remove('active'); likeBtn.innerHTML = ` ${parseInt(likeBtn.textContent) - 1} 个赞同`; fetch(`/like/${postId}`, { method: 'DELETE' }).then(() => { // 更新容器里面的内容 /*location.reload(); window.scrollTo(0, scrollPos); */ }); } else { // 点赞 likeBtn.classList.add('active'); likeBtn.innerHTML = ` ${parseInt(likeBtn.textContent) + 1} 个赞同`; fetch(`/like/${postId}`, { method: 'POST' }).then(() => { // 更新容器里面的内容 /*location.reload(); window.scrollTo(0, scrollPos); */ }); } } function collect1(postId) { const collectBtn = document.querySelector(`#collect1-${postId}`); const scrollPos = window.scrollY; if (collectBtn.classList.contains('active')) { // 取消收藏 collectBtn.classList.remove('active'); collectBtn.innerHTML = ` ${parseInt(collectBtn.textContent) - 1} 个收藏`; fetch(`/collect/${postId}`, { method: 'DELETE' }).then(() => { // 页面刷新且位置不变 /*location.reload(); window.scrollTo(0, scrollPos); */ }); } else { // 收藏 collectBtn.classList.add('active'); collectBtn.innerHTML = ` ${parseInt(collectBtn.textContent) + 1} 个收藏`; fetch(`/collect/${postId}`, { method: 'POST' }).then(() => { // 页面刷新且位置不变 /*location.reload(); window.scrollTo(0, scrollPos); */ }); } } // $(document).ready(function() { //浏览器自带消息框 // $(".cancel_datebtn").click(function(event) { // event.preventDefault(); // 阻止表单的默认行为 // if (confirm("确定要取消预约吗?")) { // $(this).closest("form").submit(); // 手动提交表单 // } // }); // }); $(document).ready(function() { //自定义消息框 $(".cancel_datebtn").click(function(event) { event.preventDefault(); // 阻止表单的默认行为 $('#confirmModal').modal('show'); // 显示模态框 }); $('#confirmCancel').click(function() { $('#confirmModal').modal('hide'); // 隐藏模态框 $("#cancel_app").submit(); // 手动提交表单 }); }); //$(document).ready(function() { // setInterval(function() { // $.ajax({ // url: "/query_call", // type: "POST", // success: function(response) { // console.log(response); // }, // error: function(xhr) { // console.log(xhr); // } // }); // }, 5000); // 每隔 5 秒执行一次 //}); $(document).ready(function() { // 向后端查询用户是否有预约 $.ajax({ url: "/query_appointment", type: "POST", success: function(response) { if (response.hasAppointment) { // 如果用户有预约,则每隔 5 秒查询是否有呼叫 setInterval(function() { $.ajax({ url: "/query_call", type: "POST", success: function(response) { console.log(response); }, error: function(xhr) { console.log(xhr); } }); }, 5000); } }, error: function(xhr) { console.log(xhr); } }); });