From 0c4268a194a9abbe5398b74704489a6b5f237d94 Mon Sep 17 00:00:00 2001 From: hstyi Date: Mon, 16 Jun 2025 09:13:24 +0800 Subject: [PATCH] feat: support account registration --- .../app/termora/account/LoginServerDialog.kt | 48 ++++++++++++++++++- src/main/resources/i18n/messages.properties | 2 + .../resources/i18n/messages_zh_CN.properties | 2 + .../resources/i18n/messages_zh_TW.properties | 2 + 4 files changed, 53 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/app/termora/account/LoginServerDialog.kt b/src/main/kotlin/app/termora/account/LoginServerDialog.kt index 0e06512..75d0293 100644 --- a/src/main/kotlin/app/termora/account/LoginServerDialog.kt +++ b/src/main/kotlin/app/termora/account/LoginServerDialog.kt @@ -11,8 +11,13 @@ import com.jgoodies.forms.builder.FormBuilder import com.jgoodies.forms.layout.FormLayout import kotlinx.coroutines.* import kotlinx.coroutines.swing.Swing +import kotlinx.serialization.json.JsonObject +import kotlinx.serialization.json.boolean +import kotlinx.serialization.json.jsonPrimitive +import okhttp3.Request import org.apache.commons.lang3.StringUtils import org.jdesktop.swingx.JXHyperlink +import org.slf4j.LoggerFactory import java.awt.Component import java.awt.Dimension import java.awt.Window @@ -27,6 +32,10 @@ import kotlin.math.max import kotlin.time.Duration.Companion.milliseconds class LoginServerDialog(owner: Window) : DialogWrapper(owner) { + companion object { + private val log = LoggerFactory.getLogger(LoginServerDialog::class.java) + } + private val serverComboBox = OutlineComboBox() private val usernameTextField = OutlineTextField() private val passwordField = OutlinePasswordField() @@ -144,6 +153,39 @@ class LoginServerDialog(owner: Window) : DialogWrapper(owner) { } } + val registerAction = object : AnAction(I18n.getString("termora.settings.account.register")) { + override fun actionPerformed(evt: AnActionEvent) { + val server = serverComboBox.selectedItem as Server? + if (server == null) { + serverComboBox.outline = FlatClientProperties.OUTLINE_ERROR + serverComboBox.requestFocusInWindow() + return + } + + try { + val text = AccountHttp.execute( + AccountHttp.client, Request.Builder() + .get().url("${server.server}/v1/client/system").build() + ) + val json = runCatching { ohMyJson.decodeFromString(text) }.getOrNull() + val allowRegister = json?.get("register")?.jsonPrimitive?.boolean ?: false + if (allowRegister.not()) { + throw IllegalStateException(I18n.getString("termora.settings.account.not-support-register")) + } + Application.browse(URI.create("${server.server}/v1/client/redirect?to=register&from=${Application.getName()}")) + } catch (e: Exception) { + if (log.isErrorEnabled) { + log.error(e.message, e) + } + OptionPane.showMessageDialog( + dialog, + e.message ?: I18n.getString("termora.settings.account.not-support-register"), + messageType = JOptionPane.ERROR_MESSAGE + ) + } + } + } + fun refreshButton() { if (serverComboBox.selectedItem == singaporeServer || serverComboBox.selectedItem == chinaServer || serverComboBox.itemCount < 1) { @@ -172,13 +214,17 @@ class LoginServerDialog(owner: Window) : DialogWrapper(owner) { }) + val registerLink = JXHyperlink(registerAction) + registerLink.isFocusable = false + return FormBuilder.create().layout(layout).debug(false).padding("0dlu, $FORM_MARGIN, 0dlu, $FORM_MARGIN") .add("${I18n.getString("termora.settings.account.server")}:").xy(1, rows) .add(serverComboBox).xy(3, rows) .add(newServer).xy(5, rows).apply { rows += step } .add("${I18n.getString("termora.settings.account")}:").xy(1, rows) - .add(usernameTextField).xy(3, rows).apply { rows += step } + .add(usernameTextField).xy(3, rows) + .add(registerLink).xy(5, rows).apply { rows += step } .add("${I18n.getString("termora.new-host.general.password")}:").xy(1, rows) .add(passwordField).xy(3, rows).apply { rows += step } .build() diff --git a/src/main/resources/i18n/messages.properties b/src/main/resources/i18n/messages.properties index a84b6f5..f4410ac 100644 --- a/src/main/resources/i18n/messages.properties +++ b/src/main/resources/i18n/messages.properties @@ -115,6 +115,8 @@ termora.settings.plugin.manage-plugin-repository=Manage Plugin Repository... termora.settings.plugin.install-from-disk-warning={0} plugin will have access to all your data. Are you sure you want to install it? termora.settings.account=Account +termora.settings.account.register=Register +termora.settings.account.not-support-register=This server does not support account registration termora.settings.account.login=Log in termora.settings.account.server=Server termora.settings.account.locally=locally diff --git a/src/main/resources/i18n/messages_zh_CN.properties b/src/main/resources/i18n/messages_zh_CN.properties index 27bf6b0..8f8e081 100644 --- a/src/main/resources/i18n/messages_zh_CN.properties +++ b/src/main/resources/i18n/messages_zh_CN.properties @@ -123,6 +123,8 @@ termora.settings.plugin.install-from-disk-warning={0} 插件可以访问 termora.settings.account=账号 termora.settings.account.login=登录 +termora.settings.account.register=注册 +termora.settings.account.not-support-register=该服务器不支持注册账号 termora.settings.account.server=服务器 termora.settings.account.locally=本地的 termora.settings.account.lifetime=长期 diff --git a/src/main/resources/i18n/messages_zh_TW.properties b/src/main/resources/i18n/messages_zh_TW.properties index cff4b2b..5f1c338 100644 --- a/src/main/resources/i18n/messages_zh_TW.properties +++ b/src/main/resources/i18n/messages_zh_TW.properties @@ -134,6 +134,8 @@ termora.settings.plugin.install-from-disk-warning={0} 插件可以存取 termora.settings.account=帳號 termora.settings.account.login=登入 +termora.settings.account.register=註冊 +termora.settings.account.not-support-register=此伺服器不支援註冊帳號 termora.settings.account.server=伺服器 termora.settings.account.locally=本地的 termora.settings.account.lifetime=長期