7 lines
14 KiB
JavaScript
7 lines
14 KiB
JavaScript
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 a="user/email.json";const i=`user/${n}/p.json`;try{let e=await client.get(a);let t=JSON.parse(e.content).registeredEmails;if(t.includes(n)){let e=await client.get(i);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 c=JSON.stringify({registeredEmails:t},null,2);await client.put(a,new Blob([c],{type:"application/json"}));const l={h:[o],nickname:"默认昵称",hasAvatar:false};const r=JSON.stringify(l,null,2);await client.put(i,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();checkInvitedStatus();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 a={region:d2(t.encryptedRegion,o),accessKeyId:d2(t.encryptedKeyId,o),accessKeySecret:d2(t.encryptedKeySecret,o),bucket:d2(t.encryptedBucket,o)};client=new OSS(a);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 a=CryptoJS.lib.WordArray.create(n.words.slice(4));const i=CryptoJS.AES.decrypt({ciphertext:a},t,{iv:o,mode:CryptoJS.mode.CBC,padding:CryptoJS.pad.Pkcs7});return i.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 a=await fetchNoCache(e);if(a.ok){const i=await a.json();if(i.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)}}async function checkVerifiedStatus(){const e=curemail;const t="https://download.xn--xhq44jb2fzpc.com/upload/verified-email/verified-email.json";try{const n=await fetchNoCache(t);const o=await n.json();if(o.includes(e)){document.getElementById("verified-icon").style.display="inline-flex";console.log("Email is verified.")}else{console.log("Email is not verified.")}}catch(e){console.error("Error loading or parsing verified-email.json:",e)}}async function checkInvitedStatus(){const e=curemail;const t="https://download.xn--xhq44jb2fzpc.com/upload/invited-email/invited-email.json";try{const n=await fetchNoCache(t);const o=await n.json();if(o.includes(e)){document.getElementById("invited-icon").style.display="inline-flex";console.log("Email is an invited author.")}else{console.log("Email is not an invited author.")}}catch(e){console.error("Error loading or parsing invited-email.json:",e)}}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 a=new Blob([JSON.stringify(o)],{type:"application/json"});await client.put(`user/${curemail}/p.json`,a);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 a=await fetchNoCache(`https://download.xn--xhq44jb2fzpc.com/${n}`);let e;if(a.ok){e=await a.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 i=new Blob([JSON.stringify(e)],{type:"application/json"});await client.put(n,i);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 a=`https://download.xn--xhq44jb2fzpc.com/upload/${t}/${e}/status.json`;try{const i=await fetchNoCache(a);if(i.ok){const s=await i.json();const{title:c,section:l,status:r,note:d,link:u}=s;let e=c||"无标题";if(r==="已通过"&&u){e=`<a href="${u}" target="_blank">${e}</a>`}o+=`
|
||
| ${e} | ${l||"无板块"} | ${r||"无状态"} | ${d||""} |`}else{console.error(`无法获取 ${a}: ${i.status} ${i.statusText}`)}}catch(e){console.error(`请求 ${a} 时发生错误:`,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 a=t.target.files;if(a.length>e){alert(`您只能选择最多 ${e} 个文件。`);t.target.value="";return}let i=false;for(let e=0;e<a.length;e++){const s=a[e];const c=s.name;const l=s.size;const r=c.split(".").pop().toLowerCase();if(r==="zip"||r==="rar"){if(i){alert(`您只能上传一个压缩文件。`);t.target.value="";return}i=true;if(l>o){alert(`压缩文件 "${c}" 超过了大小限制(20MB)。`);t.target.value="";return}}else{if(l>n){alert(`文件 "${c}" 超过了大小限制(2MB)。`);t.target.value="";return}}}if(i&&a.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 a=`upload/${n}/${o}/postimg/${e.name}`;try{const i=await client.put(a,e);const c=`https://download.xn--xhq44jb2fzpc.com/${a}`;const l=`<img src="${c}" alt="自定义图片文字" width="150">`;document.getElementById("imageUrl").innerText=l;document.getElementById("imagePreview").src=c;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 a=await fetch(`https://download.xn--xhq44jb2fzpc.com/user/${t}/p.json`);if(!a.ok){throw new Error("无法加载用户数据")}const i=await a.json();const c=i.nickname||"未知昵称";const e="";const l="";const r=`时间:${getBeijingTime()}\n内容标题:${n}\n邮箱:${t}\n板块:${f}\n昵称:${c}\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("加载草稿失败,请稍后再试。")}}); |