import * as md5 from "blueimp-md5"; import {hideMessage, showMessage} from "../dialog/message"; import {Constants} from "../constants"; import {fetchPost} from "../util/fetch"; import {repos} from "./repos"; import {confirmDialog} from "../dialog/confirmDialog"; import {hasClosestByClassName} from "../protyle/util/hasClosest"; import {getEventName, isIPad} from "../protyle/util/compatibility"; import {processSync} from "../dialog/processSystem"; import {needSubscribe} from "../util/needSubscribe"; import {syncGuide} from "../sync/syncGuide"; import {hideElements} from "../protyle/ui/hideElements"; import {getCloudURL, getIndexURL} from "./util/about"; const genSVGBG = () => { let html = ""; const svgs: string[] = []; document.querySelectorAll("body > svg > defs > symbol").forEach((item) => { svgs.push(item.id); }); Array.from({length: 45}, () => { const index = Math.floor(Math.random() * svgs.length); html += ``; svgs.splice(index, 1); }); return `
${html}
`; }; export const account = { element: undefined as Element, genHTML: (onlyPayHTML = false) => { const hideIPad = isIPad() ? " fn__none" : ""; const payHTML = ` ${window.siyuan.languages[window.siyuan.user?.userSiYuanOneTimePayStatus === 1 ? "account4" : "account1"]}
${window.siyuan.languages.freeSub}
${window.siyuan.languages.sponsor}
${window.siyuan.languages.accountSupport1}
${window.siyuan.languages.accountSupport2}
`; if (onlyPayHTML) { return `
${genSVGBG()}
${payHTML}
${genSVGBG()}
`; } if (window.siyuan.user) { let userTitlesHTML = ""; if (window.siyuan.user.userTitles.length > 0) { userTitlesHTML = '
'; window.siyuan.user.userTitles.forEach((item) => { userTitlesHTML += `
${item.icon} ${item.name}
`; }); userTitlesHTML += "
"; } let subscriptionHTML = ""; let activeSubscriptionHTML = `
`; if (window.siyuan.user.userSiYuanProExpireTime === -1) { // 终身会员 activeSubscriptionHTML = ""; subscriptionHTML = `
${Constants.SIYUAN_IMAGE_VIP}${window.siyuan.languages.account12}
`; } else if (window.siyuan.user.userSiYuanProExpireTime > 0) { // 订阅中 const renewHTML = `
${window.siyuan.languages.account6} ${Math.max(0, Math.floor((window.siyuan.user.userSiYuanProExpireTime - new Date().getTime()) / 1000 / 60 / 60 / 24))} ${window.siyuan.languages.day} ${window.siyuan.languages.clickMeToRenew}
`; if (window.siyuan.user.userSiYuanOneTimePayStatus === 1) { subscriptionHTML = `
${window.siyuan.languages.onepay}
`; } if (window.siyuan.user.userSiYuanSubscriptionPlan === 2) { // 订阅试用 subscriptionHTML += `
${window.siyuan.languages.account3}
${renewHTML}
`; } else { subscriptionHTML += `
${window.siyuan.languages.account10}
${renewHTML}`; } } else { if (window.siyuan.user.userSiYuanOneTimePayStatus === 1) { subscriptionHTML = `
${window.siyuan.languages.onepay}
${payHTML}`; } else { subscriptionHTML = payHTML; } } return `
${subscriptionHTML}
${activeSubscriptionHTML}
`; } return `
${genSVGBG()}
${payHTML}
${genSVGBG()}
`; }, bindEvent: (element: Element) => { const trialSubElement = element.querySelector("#trialSub"); if (trialSubElement) { trialSubElement.addEventListener("click", () => { fetchPost("/api/account/startFreeTrial", {}, () => { element.querySelector("#refresh").dispatchEvent(new Event("click")); }); }); } const agreeLoginElement = element.querySelector("#agreeLogin") as HTMLInputElement; const userNameElement = element.querySelector("#userName") as HTMLInputElement; if (!userNameElement) { const refreshElement = element.querySelector("#refresh"); refreshElement.addEventListener("click", () => { const svgElement = refreshElement.firstElementChild; if (svgElement.classList.contains("fn__rotate")) { return; } svgElement.classList.add("fn__rotate"); fetchPost("/api/setting/getCloudUser", { token: window.siyuan.user.userToken, }, response => { window.siyuan.user = response.data; element.innerHTML = account.genHTML(); account.bindEvent(element); showMessage(window.siyuan.languages.refreshUser, 3000); account.onSetaccount(); processSync(); }); }); element.querySelector("#logout").addEventListener("click", () => { fetchPost("/api/setting/logoutCloudUser", {}, () => { fetchPost("/api/setting/getCloudUser", {}, response => { window.siyuan.user = response.data; element.innerHTML = account.genHTML(); account.bindEvent(element); account.onSetaccount(); processSync(); }); }); }); element.querySelector("#deactivateUser").addEventListener(getEventName(), () => { confirmDialog("⚠️ " + window.siyuan.languages.deactivateUser, window.siyuan.languages.deactivateUserTip, () => { fetchPost("/api/account/deactivate", {}, () => { window.siyuan.user = null; element.innerHTML = account.genHTML(); account.bindEvent(element); account.onSetaccount(); processSync(); }); }); }); element.querySelectorAll("input[type='checkbox']").forEach(item => { item.addEventListener("change", () => { fetchPost("/api/setting/setAccount", { displayTitle: (element.querySelector("#displayTitle") as HTMLInputElement).checked, displayVIP: (element.querySelector("#displayVIP") as HTMLInputElement).checked, }, (response) => { window.siyuan.config.account.displayTitle = response.data.displayTitle; window.siyuan.config.account.displayVIP = response.data.displayVIP; account.onSetaccount(); }); }); }); const activationCodeElement = element.querySelector("#activationCode"); activationCodeElement.addEventListener("click", () => { const activationCodeInput = (activationCodeElement.previousElementSibling as HTMLInputElement); fetchPost("/api/account/checkActivationcode", {data: activationCodeInput.value}, (response) => { if (0 !== response.code) { activationCodeInput.value = ""; } confirmDialog(window.siyuan.languages.activationCode, response.msg, () => { if (response.code === 0) { fetchPost("/api/account/useActivationcode", {data: (activationCodeElement.previousElementSibling as HTMLInputElement).value}, () => { refreshElement.dispatchEvent(new CustomEvent("click")); }); } }); }); }); return; } const userPasswordElement = element.querySelector("#userPassword") as HTMLInputElement; const captchaImgElement = element.querySelector("#captchaImg") as HTMLInputElement; const captchaElement = element.querySelector("#captcha") as HTMLInputElement; const twofactorAuthCodeElement = element.querySelector("#twofactorAuthCode") as HTMLInputElement; const loginBtnElement = element.querySelector("#login") as HTMLButtonElement; const login2BtnElement = element.querySelector("#login2") as HTMLButtonElement; agreeLoginElement.addEventListener("click", () => { if (agreeLoginElement.checked) { loginBtnElement.removeAttribute("disabled"); } else { loginBtnElement.setAttribute("disabled", "disabled"); } }); userNameElement.focus(); userNameElement.addEventListener("keydown", (event) => { if (event.isComposing) { event.preventDefault(); return; } if (event.key === "Enter") { loginBtnElement.click(); event.preventDefault(); } }); twofactorAuthCodeElement.addEventListener("keydown", (event) => { if (event.isComposing) { event.preventDefault(); return; } if (event.key === "Enter") { login2BtnElement.click(); event.preventDefault(); } }); captchaElement.addEventListener("keydown", (event) => { if (event.isComposing) { event.preventDefault(); return; } if (event.key === "Enter") { loginBtnElement.click(); event.preventDefault(); } }); userPasswordElement.addEventListener("keydown", (event) => { if (event.isComposing) { event.preventDefault(); return; } if (event.key === "Enter") { loginBtnElement.click(); event.preventDefault(); } }); let token: string; let needCaptcha: string; captchaImgElement.addEventListener("click", () => { captchaImgElement.setAttribute("src", getCloudURL("captcha") + `/login?needCaptcha=${needCaptcha}&t=${new Date().getTime()}`); }); const cloudRegionElement = element.querySelector("#cloudRegion") as HTMLSelectElement; cloudRegionElement.addEventListener("change", () => { window.siyuan.config.cloudRegion = parseInt(cloudRegionElement.value); element.querySelector(".config-account__center--text").innerHTML = account.genHTML(true); element.querySelector("#form1").lastElementChild.innerHTML = `${window.siyuan.languages.forgetPassword} ${window.siyuan.languages.register}`; }); loginBtnElement.addEventListener("click", () => { fetchPost("/api/account/login", { userName: userNameElement.value.replace(/(^\s*)|(\s*$)/g, ""), userPassword: md5(userPasswordElement.value), captcha: captchaElement.value.replace(/(^\s*)|(\s*$)/g, ""), cloudRegion: window.siyuan.config.cloudRegion, }, (data) => { let messageId; if (data.code === 1) { messageId = showMessage(data.msg); if (data.data.needCaptcha) { // 验证码 needCaptcha = data.data.needCaptcha; captchaElement.parentElement.classList.remove("fn__none"); captchaElement.previousElementSibling.setAttribute("src", getCloudURL("captcha") + `/login?needCaptcha=${data.data.needCaptcha}`); captchaElement.value = ""; return; } return; } if (data.code === 10) { // 两步验证 element.querySelector("#form1").classList.add("fn__none"); element.querySelector("#form2").classList.remove("fn__none"); twofactorAuthCodeElement.focus(); token = data.data.token; return; } hideMessage(messageId); fetchPost("/api/setting/getCloudUser", { token: data.data.token, }, response => { account._afterLogin(response, element); }); }); }); login2BtnElement.addEventListener("click", () => { fetchPost("/api/setting/login2faCloudUser", { code: twofactorAuthCodeElement.value, token, }, response => { fetchPost("/api/setting/getCloudUser", { token: response.data.token, }, userResponse => { account._afterLogin(userResponse, element); }); }); }); }, _afterLogin(userResponse: IWebSocketData, element: Element) { window.siyuan.user = userResponse.data; processSync(); element.innerHTML = account.genHTML(); account.bindEvent(element); account.onSetaccount(); if (element.getAttribute("data-action") === "go-repos") { if (needSubscribe("") && 0 === window.siyuan.config.sync.provider) { const dialogElement = hasClosestByClassName(element, "b3-dialog--open"); if (dialogElement) { dialogElement.querySelector('.b3-tab-bar [data-name="repos"]').dispatchEvent(new CustomEvent("click")); element.removeAttribute("data-action"); } } else { hideElements(["dialog"]); syncGuide(); } } }, onSetaccount() { if (repos.element) { repos.element.innerHTML = ""; } if (window.siyuan.config.system.container === "ios") { return; } let html = ""; if (window.siyuan.config.account.displayVIP) { if (window.siyuan.user) { if (window.siyuan.user.userSiYuanProExpireTime === -1) { // 终身会员 html = `
${Constants.SIYUAN_IMAGE_VIP}
`; } else if (window.siyuan.user.userSiYuanProExpireTime > 0) { // 订阅中 if (window.siyuan.user.userSiYuanSubscriptionPlan === 2) { // 试用订阅 html = `
`; } else { // 正常订阅 html = `
`; } } else if (window.siyuan.user.userSiYuanSubscriptionStatus === -1) { // 未订阅 html = `
`; } if (window.siyuan.user.userSiYuanOneTimePayStatus === 1) { // 一次性付费功能特性 html += `
`; } } else { // 未登录 html = `
`; } } if (window.siyuan.config.account.displayTitle && window.siyuan.user) { window.siyuan.user.userTitles.forEach(item => { html += `
${item.icon}
`; }); } document.getElementById("toolbarVIP").innerHTML = html; } };