304 lines
12 KiB
Lua
304 lines
12 KiB
Lua
--[[--
|
|
LinguoLand 账号。邮箱密码、邮箱验证码、手机短信在设备上填写。
|
|
浏览器扩展读 read.linguoland.com 的 localStorage,不读 Cookie。
|
|
|
|
@module moonsource_linguoland.setting
|
|
--]]
|
|
|
|
local _ = require("l10n").get("moonsource_linguoland")
|
|
local CookieLogin = require("source.cookie_login")
|
|
|
|
local SOURCE_ID = "linguoland"
|
|
local Setting = {}
|
|
local LOGIN_URL = "https://read.linguoland.com/login"
|
|
|
|
local function client()
|
|
return require("moonsource_linguoland.client")
|
|
end
|
|
|
|
local function loggedIn()
|
|
return client().isLoggedIn()
|
|
end
|
|
|
|
local function clearAccount(cfg)
|
|
cfg.token, cfg.refresh_token, cfg.account_id = nil, nil, nil
|
|
end
|
|
|
|
--- 浏览器扩展登录:令牌在 localStorage,不在 Cookie。
|
|
Setting.browser = {
|
|
domain = "read.linguoland.com",
|
|
scope = "local",
|
|
login = LOGIN_URL,
|
|
check = "https://read.linguoland.com/shelf",
|
|
icon = "https://read.linguoland.com/icon.png",
|
|
isAuthorized = function()
|
|
return loggedIn() and not require("source.auth").expired(SOURCE_ID)
|
|
end,
|
|
apply = function(payload, cb)
|
|
local storage = payload["local"]
|
|
local access = type(storage) == "table" and storage["linguoland.access_token"] or nil
|
|
local refresh = type(storage) == "table" and storage["linguoland.refresh_token"] or nil
|
|
if type(access) ~= "string" or access == "" then
|
|
cb(false, _("没有读到登录令牌"))
|
|
return
|
|
end
|
|
if type(refresh) ~= "string" or refresh == "" then refresh = nil end
|
|
client().commitSession(access, refresh, cb)
|
|
end,
|
|
}
|
|
|
|
local function connected()
|
|
if require("ui/network/manager"):isConnected() then return true end
|
|
require("ui.themes.widgets.toast"):new{ text = _("网络不可用,请先连接 Wi-Fi"), timeout = 3 }:show()
|
|
return false
|
|
end
|
|
|
|
---@param access string|nil
|
|
---@param refresh string|nil
|
|
---@param err string|nil
|
|
---@param waiting table|nil
|
|
---@param dialog table|nil
|
|
---@param closed fun(): boolean
|
|
local function finishLogin(access, refresh, err, waiting, dialog, closed)
|
|
local Toast = require("ui.themes.widgets.toast")
|
|
if not access then
|
|
if waiting then waiting:close() end
|
|
if not closed() then Toast:new{ text = err or _("登录失败"), timeout = 3 }:show() end
|
|
return
|
|
end
|
|
client().commitSession(access, refresh, function(ok, message)
|
|
if waiting then waiting:close() end
|
|
if closed() then return end
|
|
if not ok then Toast:new{ text = message or _("登录失败"), timeout = 3 }:show(); return end
|
|
if dialog then dialog:close() end
|
|
Toast:new{ text = _("登录成功"), timeout = 2 }:show()
|
|
end)
|
|
end
|
|
|
|
---@param title string
|
|
---@param text string
|
|
---@return table
|
|
local function waitingDialog(title, text)
|
|
return require("ui.themes.widgets.progress_dialog"):new{
|
|
title = title, text = text, dismissable = false,
|
|
}
|
|
end
|
|
|
|
local function showPasswordLogin()
|
|
local Toast = require("ui.themes.widgets.toast")
|
|
local closed, job, waiting, dialog = false, nil, nil, nil
|
|
dialog = require("ui.themes.widgets.input"):new{
|
|
title = _("LinguoLand 账号"),
|
|
keep_open = true,
|
|
fields = {
|
|
{ label = _("邮箱"), text = "" },
|
|
{ label = _("密码"), text = "", text_type = "password" },
|
|
},
|
|
ok_text = _("登录"),
|
|
close_callback = function()
|
|
closed = true
|
|
if job and job.cancel then job:cancel() end
|
|
if waiting then waiting:close(); waiting = nil end
|
|
end,
|
|
callback = function(email, password)
|
|
email = require("utils.text").trim(email)
|
|
if email == "" then Toast:new{ text = _("请填写邮箱"), timeout = 3 }:show(); return end
|
|
if password == "" then Toast:new{ text = _("请填写密码"), timeout = 3 }:show(); return end
|
|
if not connected() then return end
|
|
waiting = waitingDialog(_("LinguoLand 账号"), _("正在登录…"))
|
|
waiting:show()
|
|
job = client().loginAsync(email, password, function(access, refresh, err)
|
|
job = nil
|
|
finishLogin(access, refresh, err, waiting, dialog, function() return closed end)
|
|
waiting = nil
|
|
end)
|
|
end,
|
|
}
|
|
dialog:show()
|
|
end
|
|
|
|
local function showEmailCodeLogin()
|
|
local Toast = require("ui.themes.widgets.toast")
|
|
local closed, job, waiting, dialog = false, nil, nil, nil
|
|
dialog = require("ui.themes.widgets.input"):new{
|
|
title = _("邮箱验证码登录"),
|
|
keep_open = true,
|
|
fields = {
|
|
{ label = _("邮箱"), text = "" },
|
|
{ label = _("验证码"), text = "", input_type = "number" },
|
|
},
|
|
ok_text = _("登录"),
|
|
close_callback = function()
|
|
closed = true
|
|
if job and job.cancel then job:cancel() end
|
|
if waiting then waiting:close(); waiting = nil end
|
|
end,
|
|
actions = {
|
|
{ text = _("发送验证码"), callback = function(email)
|
|
email = require("utils.text").trim(email)
|
|
if email == "" then Toast:new{ text = _("请填写邮箱"), timeout = 3 }:show(); return end
|
|
if not connected() then return end
|
|
if job and job.cancel then job:cancel() end
|
|
job = client().sendEmailCodeAsync(email, function(ok, err)
|
|
job = nil
|
|
if closed then return end
|
|
Toast:new{ text = ok and _("验证码已发送") or (err or _("登录失败")), timeout = 3 }:show()
|
|
end)
|
|
end },
|
|
},
|
|
callback = function(email, code)
|
|
email = require("utils.text").trim(email)
|
|
code = require("utils.text").trim(code)
|
|
if email == "" then Toast:new{ text = _("请填写邮箱"), timeout = 3 }:show(); return end
|
|
if code == "" then Toast:new{ text = _("请先填写验证码"), timeout = 3 }:show(); return end
|
|
if not connected() then return end
|
|
waiting = waitingDialog(_("LinguoLand 账号"), _("正在登录…"))
|
|
waiting:show()
|
|
if job and job.cancel then job:cancel() end
|
|
job = client().loginWithEmailCodeAsync(email, code, function(access, refresh, err)
|
|
job = nil
|
|
finishLogin(access, refresh, err, waiting, dialog, function() return closed end)
|
|
waiting = nil
|
|
end)
|
|
end,
|
|
}
|
|
dialog:show()
|
|
end
|
|
|
|
local function validPhone(phone)
|
|
return phone:match("^1[3-9]%d%d%d%d%d%d%d%d%d$") ~= nil
|
|
end
|
|
|
|
local function showSmsLogin()
|
|
local Toast = require("ui.themes.widgets.toast")
|
|
local closed, job, waiting, dialog = false, nil, nil, nil
|
|
dialog = require("ui.themes.widgets.input"):new{
|
|
title = _("手机验证码登录"),
|
|
keep_open = true,
|
|
fields = {
|
|
{ label = _("手机号"), text = "", input_type = "number" },
|
|
{ label = _("验证码"), text = "", input_type = "number" },
|
|
},
|
|
ok_text = _("登录"),
|
|
close_callback = function()
|
|
closed = true
|
|
if job and job.cancel then job:cancel() end
|
|
if waiting then waiting:close(); waiting = nil end
|
|
end,
|
|
actions = {
|
|
{ text = _("发送验证码"), callback = function(phone)
|
|
phone = phone:gsub("%D", "")
|
|
if not validPhone(phone) then
|
|
Toast:new{ text = _("请填写正确的手机号"), timeout = 3 }:show()
|
|
return
|
|
end
|
|
if not connected() then return end
|
|
if job and job.cancel then job:cancel() end
|
|
job = client().sendSmsCodeAsync(phone, function(ok, err)
|
|
job = nil
|
|
if closed then return end
|
|
Toast:new{ text = ok and _("验证码已发送") or (err or _("登录失败")), timeout = 3 }:show()
|
|
end)
|
|
end },
|
|
},
|
|
callback = function(phone, code)
|
|
phone = phone:gsub("%D", "")
|
|
code = require("utils.text").trim(code)
|
|
if not validPhone(phone) then
|
|
Toast:new{ text = _("请填写正确的手机号"), timeout = 3 }:show()
|
|
return
|
|
end
|
|
if code == "" then Toast:new{ text = _("请先填写验证码"), timeout = 3 }:show(); return end
|
|
if not connected() then return end
|
|
waiting = waitingDialog(_("LinguoLand 账号"), _("正在登录…"))
|
|
waiting:show()
|
|
if job and job.cancel then job:cancel() end
|
|
job = client().loginWithSmsCodeAsync(phone, code, function(access, refresh, err)
|
|
job = nil
|
|
finishLogin(access, refresh, err, waiting, dialog, function() return closed end)
|
|
waiting = nil
|
|
end)
|
|
end,
|
|
}
|
|
dialog:show()
|
|
end
|
|
|
|
local function showTokenLogin()
|
|
require("ui.themes.widgets.input"):new{
|
|
title = _("粘贴登录令牌"),
|
|
fields = {
|
|
{ text = "", hint = _("按 F12,点「应用」里的本地存储,复制 linguoland.access_token") },
|
|
{ text = "", hint = _("linguoland.refresh_token,没有就留空") },
|
|
},
|
|
ok_text = _("登录"),
|
|
callback = function(access, refresh)
|
|
access = require("utils.text").trim(access or "")
|
|
refresh = require("utils.text").trim(refresh or "")
|
|
if access == "" then
|
|
require("ui.themes.widgets.toast"):new{ text = _("没有读到登录令牌"), timeout = 3 }:show()
|
|
return
|
|
end
|
|
if not connected() then return end
|
|
local waiting = waitingDialog(_("LinguoLand 账号"), _("正在校验登录…"))
|
|
waiting:show()
|
|
client().commitSession(access, refresh ~= "" and refresh or nil, function(ok, err)
|
|
waiting:close()
|
|
require("ui.themes.widgets.toast"):new{
|
|
text = ok and _("登录成功") or (err or _("登录失败")), timeout = ok and 2 or 3,
|
|
}:show()
|
|
end)
|
|
end,
|
|
}:show()
|
|
end
|
|
|
|
function Setting.rowStatus()
|
|
local on = loggedIn()
|
|
return on and _("已登录") or _("未登录 · 点此登录"), on
|
|
end
|
|
|
|
function Setting.rows()
|
|
local Row = require("ui.themes.widgets.row")
|
|
local rows = {
|
|
function(iw)
|
|
local status, status_on = Setting.rowStatus()
|
|
return Row.widget({
|
|
width = iw, chevron = true, kind = "nav", icon = "account_circle",
|
|
title = _("LinguoLand 账号"), subtitle = _("邮箱密码登录"),
|
|
status = status, status_on = status_on, callback = showPasswordLogin,
|
|
})
|
|
end,
|
|
function(iw)
|
|
return Row.widget({
|
|
width = iw, chevron = true, kind = "nav", icon = "mail",
|
|
title = _("邮箱验证码登录"), callback = showEmailCodeLogin,
|
|
})
|
|
end,
|
|
function(iw)
|
|
return Row.widget({
|
|
width = iw, chevron = true, kind = "nav", icon = "sms",
|
|
title = _("手机验证码登录"), callback = showSmsLogin,
|
|
})
|
|
end,
|
|
CookieLogin.browserRow(),
|
|
function(iw)
|
|
return Row.widget({
|
|
width = iw, chevron = true, kind = "nav", icon = "key",
|
|
title = _("粘贴登录令牌"), callback = showTokenLogin,
|
|
})
|
|
end,
|
|
}
|
|
if loggedIn() then
|
|
rows[#rows + 1] = require("source.logout").row(SOURCE_ID, _("确定退出 LinguoLand 账号?"), function()
|
|
local settings = require("utils.settings")
|
|
local cfg = settings.getSource(SOURCE_ID)
|
|
clearAccount(cfg)
|
|
settings.saveSource(SOURCE_ID, cfg)
|
|
end)
|
|
end
|
|
return rows
|
|
end
|
|
|
|
Setting.login = showPasswordLogin
|
|
|
|
return Setting
|