<script>
// 通用的分享窗口打开方法
function openShareWindow(url, width = 750, height = 600) {
const dualScreenLeft = window.screenLeft !== undefined ? window.screenLeft : window.screenX;
const dualScreenTop = window.screenTop !== undefined ? window.screenTop : window.screenY;
const systemZoom = window.innerWidth / window.screen.availWidth;
const left = (window.screen.availWidth - width) / 2 / systemZoom + dualScreenLeft;
const top = (window.screen.availHeight - height) / 2 / systemZoom + dualScreenTop;
const features = `scrollbars=yes,width=${width},height=${height},top=${top},left=${left}`;
window.open(url, 'share', features);
}
// // 添加自定义Toast提示函数
function showToast(message, type = 'success') {
// 创建toast元素
const toast = document.createElement('div');
toast.className = `custom-toast ${type}`;
// 设置内容
toast.innerHTML = `
<div class="toast-content">
<span class="toast-icon">${type === 'success' ? '✓' : '!'}</span>
<span class="toast-message">${message}</span>
</div>
`;
// 添加到页面
document.body.appendChild(toast);
// 添加显示类名
setTimeout(() => toast.classList.add('show'), 10);
// 自动移除
setTimeout(() => {
toast.classList.remove('show');
setTimeout(() => toast.remove(), 300);
}, 2000);
}
// 分享到 LinkedIn
function shareToLinkedIn() {
const url = encodeURIComponent(window.location.href);
const title = encodeURIComponent(JSON.parse(`{$solution.title|json_encode}`));
const summary = encodeURIComponent(JSON.parse(`{$solution.description|json_encode}`));
const source = encodeURIComponent("安东在线服务");
const shareUrl = 'https://www.linkedin.com/sharing/share-offsite/?' +
'url=' + url +
'&title=' + title +
'&summary=' + summary +
'&source=' + source;
// 直接在当前窗口打开
window.location.href = shareUrl;
}
// 分享到 Twitter
function shareToTwitter() {
const url = encodeURIComponent(window.location.href);
const title = encodeURIComponent(JSON.parse(`{$solution.title|json_encode}`));
const shareUrl = 'https://twitter.com/intent/tweet?url=' + url + '&text=' + title;
// 直接在当前窗口打开
window.location.href = shareUrl;
}
// 分享到微信 //
function copyToWechat() {
const url = window.location.href; // 获取当前网页链接
// 备用方法:创建一个临时输入元素来复制链接
const tempInput = document.createElement('input');
tempInput.value = url;
document.body.appendChild(tempInput);
tempInput.select();
try {
document.execCommand('copy');
showToast('链接已复制,请到微信中粘贴');
} catch (err) {
console.error('复制失败:', err);
showToast('复制失败,请手动复制');
}
document.body.removeChild(tempInput);
}
function shareToWhatsApp() {
const title = encodeURIComponent(JSON.parse(`{$solution.title|json_encode}`));
const url = encodeURIComponent(window.location.href);
const text = encodeURIComponent(document.title);
const shareUrl = 'https://api.whatsapp.com/send?text=' + text + '%20' + title + '%20' + url;
window.location.href = shareUrl;
}
</script>