front/js/submission.min.js
2024-07-27 00:35:04 +08:00

7 lines
16 KiB
JavaScript
Raw 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.

let client;let s=false;var curemail="";var submitted=0;document.getElementById("loginForm").addEventListener("submit",async function(e){e.preventDefault();const n=document.getElementById("email").value;const t=document.getElementById("password").value;const o=CryptoJS.SHA256(t).toString();const i="user/email.json";const c=`user/${n}/p.json`;try{let e=await client.get(i);let t=JSON.parse(e.content).registeredEmails;if(t.includes(n)){let e=await client.get(c);let t=JSON.parse(e.content);if(t.h[0]===o){s=true;curemail=n;document.getElementById("message").innerText="登录成功!正在加载中...";setTimeout(initializeUserSession,3e3)}else{document.getElementById("message").innerText="密码错误。"}}else{if(confirm("此邮箱没有注册。是否注册?")){t.push(n);const a=JSON.stringify({registeredEmails:t},null,2);await client.put(i,new Blob([a],{type:"application/json"}));const l={h:[o],nickname:"默认昵称",hasAvatar:false};const r=JSON.stringify(l,null,2);await client.put(c,new Blob([r],{type:"application/json"}));s=true;curemail=n;document.getElementById("message").innerText="注册并登录成功!正在加载中...";setTimeout(initializeUserSession,3e3)}}}catch(e){console.error("Error:",e);document.getElementById("message").innerText="发生错误,请稍后重试。"}});function initializeUserSession(){document.getElementById("login").style.display="none";document.getElementById("navContainer").style.display="block";checkAndLoadAvatar();updateUserInfo();checkVerifiedStatus(curemail);checkInvitedStatus(curemail);fetchSubmittedCount(curemail);document.getElementById("userEmail").innerText=curemail}async function f1(){try{const e=await fetchNoCache("https://download.xn--xhq44jb2fzpc.com/upload/json/s.json");const t=await e.json();const n=t.masterKey;const o=CryptoJS.SHA256(n);const i={region:d2(t.encryptedRegion,o),accessKeyId:d2(t.encryptedKeyId,o),accessKeySecret:d2(t.encryptedKeySecret,o),bucket:d2(t.encryptedBucket,o)};client=new OSS(i);console.log("OSS Client Initialized Successfully with decrypted config",client)}catch(e){console.error("Failed to fetch or decrypt OSS config:",e)}}function d2(e,t){e=e.replace(/\s/g,"");const n=CryptoJS.enc.Base64.parse(e);const o=CryptoJS.lib.WordArray.create(n.words.slice(0,4));const i=CryptoJS.lib.WordArray.create(n.words.slice(4));const s=CryptoJS.AES.decrypt({ciphertext:i},t,{iv:o,mode:CryptoJS.mode.CBC,padding:CryptoJS.pad.Pkcs7});return s.toString(CryptoJS.enc.Utf8)}document.addEventListener("DOMContentLoaded",function(){f1().then(()=>{if(client){}else{console.error("Failed to initialize OSS client due to decryption error.")}}).catch(e=>{console.log("Error initializing OSS Client:",e)})});const buttons=document.querySelectorAll(".navButton");buttons.forEach(e=>{e.addEventListener("click",function(){buttons.forEach(e=>e.classList.remove("selected"));this.classList.add("selected")})});function showSubmission(){document.getElementById("submission-area").style.display="block";document.getElementById("myinfo").style.display="none";document.getElementById("mysubmission").style.display="none"}function showMyInfo(){document.getElementById("submission-area").style.display="none";document.getElementById("myinfo").style.display="block";document.getElementById("mysubmission").style.display="none"}function showMySubmissions(){document.getElementById("submission-area").style.display="none";document.getElementById("myinfo").style.display="none";fetchSubmissionData(curemail,submitted);document.getElementById("mysubmission").style.display="block"}async function updateUserInfo(){if(!s){console.log("User is not logged in.");return}const e=`https://download.xn--xhq44jb2fzpc.com/user/${curemail}/p.json`;try{const t=await fetchNoCache(e);if(t.ok){const n=await t.json();if(n&&n.nickname){document.getElementById("nickname").innerText=n.nickname;console.log("Nickname has been successfully updated.")}else{document.getElementById("nickname").innerText="Default Nickname";console.log("Default nickname set due to missing 'nickname' field in response.")}}else{throw new Error("p.json not found")}}catch(e){console.error("Error loading p.json:",e);document.getElementById("nickname").innerText="Default Nickname"}}async function checkAndLoadAvatar(){if(!s){console.log("User is not logged in.");return}const e=`https://download.xn--xhq44jb2fzpc.com/user/${curemail}/p.json`;const t=`https://download.xn--xhq44jb2fzpc.com/user/${curemail}/avatar`;const n="https://download.xn--xhq44jb2fzpc.com/avatar/default.png";try{const i=await fetchNoCache(e);if(i.ok){const c=await i.json();if(c.hasAvatar){var o=t+"?t="+Date.now();document.getElementById("myinfoavatar").src=o;console.log("Custom avatar loaded.")}else{document.getElementById("myinfoavatar").src=n;console.log("Default avatar loaded due to `hasAvatar` being false.")}}else{throw new Error("p.json not found")}}catch(e){document.getElementById("myinfoavatar").src=n;console.error("Error loading p.json. Using default avatar:",e)}}function showTooltip(e,t,n){const o=document.getElementById("tooltip");const i=document.getElementById("tooltip-title");const s=document.getElementById("tooltip-content");i.textContent=t;s.textContent=n;o.classList.add("show")}function hideTooltip(){const e=document.getElementById("tooltip");e.classList.remove("show")}document.body.addEventListener("click",e=>{const t=document.getElementById("tooltip");const n=document.getElementById("verified-icon");const o=document.getElementById("invited-icon");if(!n.contains(e.target)&&!o.contains(e.target)&&!t.contains(e.target)){hideTooltip()}});async function checkVerifiedStatus(e){if(!e){console.log("Email not provided, skipping verified status check.");return}const t="https://download.xn--xhq44jb2fzpc.com/upload/verified-email/verified-email.json";const n=document.getElementById("verified-icon");const o="认证作者";try{const i=await fetchNoCache(t);if(i.ok){const s=await i.json();if(s.includes(e)){n.style.display="inline-flex";console.log("Email is verified.");n.addEventListener("mouseenter",()=>fetchVerificationDetails(e,n));n.addEventListener("mouseleave",hideTooltip);n.addEventListener("click",()=>fetchVerificationDetails(e,n))}else{console.log("Email is not verified.")}}else{throw new Error("Failed to fetch verified-email.json")}}catch(e){console.error("Error loading or parsing verified-email.json:",e)}}async function fetchVerificationDetails(e,t){const n=`https://download.xn--xhq44jb2fzpc.com/user/${e}/verified.json`;try{const o=await fetchNoCache(n);if(o.ok){const i=await o.json();showTooltip(t,"认证作者",i.description)}else if(o.status===404){showTooltip(t,"认证作者","本作者为经过网站认证的优质内容分享者。")}}catch(e){console.error("Error fetching verification details:",e);showTooltip(t,"认证作者","本作者为经过网站认证的优质内容分享者。")}}async function checkInvitedStatus(e){if(!e){console.log("Email not provided, skipping invited author status check.");return}const t="https://download.xn--xhq44jb2fzpc.com/upload/invited-email/invited-email.json";const n=document.getElementById("invited-icon");try{const o=await fetchNoCache(t);if(o.ok){const i=await o.json();if(i.includes(e)){n.style.display="inline-flex";console.log("Email is an invited author.");n.addEventListener("mouseenter",()=>fetchInvitedInfo(e,n));n.addEventListener("mouseleave",hideTooltip);n.addEventListener("click",()=>fetchInvitedInfo(e,n))}else{console.log("Email is not an invited author.")}}else{throw new Error("Failed to fetch invited-email.json")}}catch(e){console.error("Error loading or parsing invited-email.json:",e)}}async function fetchInvitedInfo(e,t){const n=`https://download.xn--xhq44jb2fzpc.com/user/${e}/invited.json`;try{const o=await fetchNoCache(n);if(o.ok){const i=await o.json();showTooltip(t,"特邀作者",i.description||"无详细信息")}else{showTooltip(t,"特邀作者","无法获取信息")}}catch(e){console.error("Error fetching invited info:",e);showTooltip(t,"特邀作者","无法加载信息")}}async function editNickname(){if(!s){alert("您尚未登录,请登录后再尝试修改昵称。");return}const e=prompt("请输入新的昵称:");if(e===null){return}if(e.trim()===""){alert("昵称不能为空!");return}const t=`https://download.xn--xhq44jb2fzpc.com/user/${curemail}/p.json`;try{const n=await fetchNoCache(t);if(n.ok){const o=await n.json();o.nickname=e;const i=new Blob([JSON.stringify(o)],{type:"application/json"});await client.put(`user/${curemail}/p.json`,i);console.log("p.json has been successfully updated with new nickname.");setTimeout(()=>{document.getElementById("nickname").innerText=e;console.log("Nickname has been updated on the page.")},1e3)}else{throw new Error("Failed to fetch p.json")}}catch(e){console.error("Error updating nickname:",e)}}document.getElementById("editNicknameBtn").addEventListener("click",editNickname);async function uploadAvatar(e){var t=e.target.files[0];if(t&&t.size<=1048576){if(s===true){var n=`user/${curemail}/p.json`;var o=`user/${curemail}/avatar`;try{const i=await fetchNoCache(`https://download.xn--xhq44jb2fzpc.com/${n}`);let e;if(i.ok){e=await i.json()}else{e={h:[hashedPassword],nickname:"默认昵称",hasAvatar:false}}await client.put(o,t);console.log("Avatar has been successfully uploaded.");if(!e.hasAvatar){e.hasAvatar=true;const c=new Blob([JSON.stringify(e)],{type:"application/json"});await client.put(n,c);console.log("p.json has been successfully uploaded.");setTimeout(()=>{document.getElementById("myinfoavatar").src=`https://download.xn--xhq44jb2fzpc.com/${o}`;console.log("myinfoavatar's src has been updated to the new avatar.")},2e3)}else{setTimeout(()=>{document.getElementById("myinfoavatar").src+="?"+(new Date).getTime();console.log("Forced reload of the existing avatar.")},2e3)}}catch(e){console.error("Failed to upload new avatar or update p.json:",e)}}else{alert("You are not logged in, please log in before attempting to upload an avatar.")}}else{alert("头像必须小于 1MB")}}document.querySelector(".overlay").addEventListener("click",function(){document.getElementById("fileInput").click()});const input=document.getElementById("input");const preview=document.getElementById("preview");input.addEventListener("input",()=>{const e=input.value;preview.innerHTML=marked.parse(e)});preview.innerHTML=marked.parse("");async function fetchSubmittedCount(e){const t=`https://download.xn--xhq44jb2fzpc.com/upload/${e}/submitted.json`;try{const n=await fetchNoCache(t);if(n.ok){const o=await n.json();submitted=o;console.log("Submitted count updated:",submitted)}else{throw new Error("File not found or access error")}}catch(e){submitted=0;console.log("Error fetching submitted count:",e)}}async function fetchSubmissionData(t,n){let o=`
## 投稿记录
| 标题 | 板块 | 审核状态 | 审核备注 |
|------|------|------|------|`;for(let e=1;e<=n;e++){const i=`https://download.xn--xhq44jb2fzpc.com/upload/${t}/${e}/status.json`;try{const s=await fetchNoCache(i);if(s.ok){const c=await s.json();const{title:a,section:l,status:r,note:d,link:u}=c;let e=a||"无标题";if(r==="已通过"&&u){e=`<a href="${u}" target="_blank">${e}</a>`}o+=`
| ${e} | ${l||"无板块"} | ${r||"无状态"} | ${d||""} |`}else{console.error(`无法获取 ${i}: ${s.status} ${s.statusText}`)}}catch(e){console.error(`请求 ${i} 时发生错误:`,e)}}if(n===0){o+=`
| 没有投稿记录 | | | |`}document.getElementById("mysubmission").innerHTML=marked.parse(o)}function validateFiles(t){const e=10;const n=2*1024*1024;const o=20*1024*1024;const i=t.target.files;if(i.length>e){alert(`您只能选择最多 ${e} 个文件。`);t.target.value="";return}let s=false;for(let e=0;e<i.length;e++){const c=i[e];const a=c.name;const l=c.size;const r=a.split(".").pop().toLowerCase();if(r==="zip"||r==="rar"){if(s){alert(`您只能上传一个压缩文件。`);t.target.value="";return}s=true;if(l>o){alert(`压缩文件 "${a}" 超过了大小限制20MB`);t.target.value="";return}}else{if(l>n){alert(`文件 "${a}" 超过了大小限制2MB`);t.target.value="";return}}}if(s&&i.length>1){alert("上传压缩文件时,只能上传一个文件。");t.target.value="";return}}function getBeijingTime(){const e=new Date;const t=e.getTimezoneOffset()*6e4;const n=8*60*60*1e3;const o=new Date(e.getTime()+t+n);return o.toLocaleString("zh-CN",{timeZone:"Asia/Shanghai"})}function displayBeijingTime(){const e=document.getElementById("beijing-time");e.textContent=getBeijingTime()}window.onload=function(){displayBeijingTime()};async function uploadImage(t){const e=t.target.files[0];const n=document.querySelector("input[name='email']").value.trim();if(!s){alert("您还没有登录,请登录后再上传图片!");return}if(!e){alert("请先选择图片文件!");return}if(e.size>2*1024*1024){alert("图片文件大小不能超过2MB");t.target.value="";return}const o=submitted+1;const i=`upload/${n}/${o}/postimg/${e.name}`;try{const c=await client.put(i,e);const a=`https://download.xn--xhq44jb2fzpc.com/${i}`;const l=`<img src="${a}" alt="自定义图片文字" width="150">`;document.getElementById("imageUrl").innerText=l;document.getElementById("imagePreview").src=a;document.getElementById("imagePreview").style.display="block";document.getElementById("copyButton").style.display="inline-block"}catch(e){console.error("图片上传失败:",e);alert("图片上传失败!");t.target.value=""}}function copyImageUrl(){const e=document.getElementById("imageUrl").innerText;const t=document.createElement("textarea");t.value=e;document.body.appendChild(t);t.select();try{const n=document.execCommand("copy");const o=n?"标签已复制到剪贴板!请直接粘贴到 markdown 编辑区中,并根据预览效果调整大小。":"复制失败!";alert(o)}catch(e){alert("复制失败!",e)}document.body.removeChild(t)}document.getElementById("SubmitButton").onclick=function(){const f=document.getElementById("section").value;const y=document.querySelector("input[name='wp']").value.trim();const g=document.querySelector("input[name='wppassword']").value.trim();const e=document.querySelector("input[name='note']").value.trim();const h=document.getElementById("input").value.trim();if(!confirm("请仔细检查后提交,多次提交无关内容将被禁止访问网站!")){return}t();async function t(){if(!s){alert("您还未登录,请登录后再进行投稿。");return}const t=curemail;const n=document.querySelector("input[name='title']").value.trim();if(!n){alert("请填写内容的标题。");return}const o=submitted+1;try{const i=await fetch(`https://download.xn--xhq44jb2fzpc.com/user/${t}/p.json`);if(!i.ok){throw new Error("无法加载用户数据")}const c=await i.json();const a=c.nickname||"未知昵称";const e="";const l="";const r=`时间:${getBeijingTime()}\n内容标题:${n}\n邮箱:${t}\n板块:${f}\n昵称:${a}\n备注:${e}\n网盘外链:${y}\n网盘提取密码:${g}\n资源展示页:\n${h}`;const d=`upload/${t}/${o}/userinfo.txt`;const u=`upload/${t}/${o}/status.json`;const m={title:n,status:"审核中",section:f,note:e,link:l};await client.put(d,new Blob([r],{type:"text/plain"}));await client.put(u,new Blob([JSON.stringify(m)],{type:"application/json"}));const p=document.getElementById("filePicker").files;for(let e=0;e<p.length;e++){await client.put(`upload/${t}/${o}/files/${p[e].name}`,p[e])}submitted+=1;await client.put(`upload/${t}/submitted.json`,new Blob([JSON.stringify(submitted)],{type:"application/json"}));window.location.href="/submissionsuccess/"}catch(e){console.error("上传失败:",e);alert("投稿失败,请重试!")}}};document.getElementById("SaveDraft").addEventListener("click",async()=>{if(!s){alert("非法操作!请先登录。");return}if(confirm("确认保存草稿吗?如您之前有草稿内容,此操作会覆盖前一次的草稿内容。")){const e=document.getElementById("input").value;const t=submitted+1;const n=`upload/${curemail}/${t}/draft.json`;const o=new Blob([JSON.stringify({content:e},null,2)],{type:"application/json"});try{await client.put(n,o);alert("草稿已保存!")}catch(e){console.error("保存草稿时出错:",e);alert("保存草稿失败,请稍后再试。")}}});document.getElementById("LoadDraft").addEventListener("click",async()=>{if(!s){alert("非法操作!请先登录。");return}const e=submitted+1;const t=`https://download.xn--xhq44jb2fzpc.com/upload/${curemail}/${e}/draft.json`;try{const n=await fetchNoCache(t);if(!n.ok){if(n.status===404){alert("没有草稿记录!")}else{throw new Error("无法加载草稿内容")}}else{const o=await n.json();if(confirm("此操作会覆盖您当前的输入内容,确认加载草稿吗?")){document.getElementById("input").value=o.content;preview.innerHTML=marked.parse(o.content);alert("草稿已加载!")}}}catch(e){console.error("加载草稿时出错:",e);alert("加载草稿失败,请稍后再试。")}});