书城搜索与筛选、详情、卷列表、整卷 EPUB 下载与缓存、收藏书架同步。 支持邮箱密码、浏览器授权、粘贴 Cookie 登录;下载要求 UA 与登录时一致, 会话 UA 随 Cookie 一起保存。
162 lines
5.7 KiB
Lua
162 lines
5.7 KiB
Lua
--[[--
|
||
Kmoe 账号:邮箱密码登录、浏览器授权、粘贴 Cookie 三种方式,最后都只保存校验通过的 Cookie、数字账号和登录时的 UA,不存密码。
|
||
|
||
下载接口要求 UA 与登录时一致,所以每种方式都带上自己的 UA:密码登录用插件默认 UA(存 nil),
|
||
浏览器授权用扩展回传的 `payload.ua`,粘贴 Cookie 用用户一并填的浏览器 UA。
|
||
|
||
@module moonsource_kmoe.setting
|
||
--]]
|
||
|
||
local CookieLogin = require("source.cookie_login")
|
||
local _ = require("l10n").get("moonsource_kmoe")
|
||
|
||
local SOURCE_ID = "kmoe"
|
||
local Setting = {}
|
||
|
||
local function loggedIn()
|
||
local cfg = require("utils.settings").getSource(SOURCE_ID)
|
||
return type(cfg.cookie) == "string" and cfg.cookie ~= ""
|
||
end
|
||
|
||
---@type CookieLoginSpec
|
||
local SPEC = {
|
||
id = SOURCE_ID,
|
||
title = _("Kmoe 账号"),
|
||
cookie_title = _("Kmoe 登录 Cookie"),
|
||
cookie_hint = _("在 mox.moe 登录后,复制请求标头里的完整 Cookie"),
|
||
logout_text = _("确定退出 Kmoe 账号?"),
|
||
loggedIn = loggedIn,
|
||
checkAsync = function(cookie, cb)
|
||
return require("moonsource_kmoe.client").accountAsync(cookie, cb)
|
||
end,
|
||
---@param account { id: string } checkAsync 成功时必带
|
||
commit = function(cookie, account, cb, current)
|
||
return CookieLogin.commitAccount(SOURCE_ID, cookie, account.id, cb, current)
|
||
end,
|
||
clear = function(cfg) cfg.cookie, cfg.account_id, cfg.ua = nil, nil, nil end,
|
||
}
|
||
|
||
--- 带登录 UA 的 SPEC:UA 先写进配置再提交 Cookie(提交里会重载源),提交失败就还原。
|
||
---@param ua string|nil nil 为插件默认 UA
|
||
---@return CookieLoginSpec
|
||
local function withUA(ua)
|
||
return setmetatable({
|
||
commit = function(cookie, account, cb, current)
|
||
local Settings = require("utils.settings")
|
||
local cfg = Settings.getSource(SOURCE_ID)
|
||
local previous = cfg.ua
|
||
cfg.ua = ua
|
||
return CookieLogin.commitAccount(SOURCE_ID, cookie, account.id, function(ok, err)
|
||
if not ok then
|
||
cfg.ua = previous
|
||
Settings.saveSource(SOURCE_ID, cfg)
|
||
end
|
||
cb(ok, err)
|
||
end, current)
|
||
end,
|
||
}, { __index = SPEC })
|
||
end
|
||
|
||
---@param s any
|
||
---@return string|nil
|
||
local function nonEmpty(s)
|
||
s = type(s) == "string" and s:match("^%s*(.-)%s*$") or ""
|
||
return s ~= "" and s or nil
|
||
end
|
||
|
||
---@param email string
|
||
---@param password string
|
||
local function doLogin(email, password)
|
||
local Toast = require("ui.themes.widgets.toast")
|
||
if email == "" or password == "" then
|
||
Toast:new{ text = _("请输入邮箱和密码"), timeout = 3 }:show()
|
||
return
|
||
end
|
||
require("ui/network/manager"):runWhenConnected(function()
|
||
local job
|
||
local waiting = require("ui.themes.widgets.progress_dialog"):new{
|
||
title = SPEC.title, text = _("正在登录…"), dismissable = false,
|
||
on_cancel = function() if job then job.cancel() end end,
|
||
}
|
||
waiting:show()
|
||
local function done(ok, err)
|
||
waiting:close()
|
||
Toast:new{ text = ok and _("登录成功") or err or _("登录失败"), timeout = ok and 2 or 3 }:show()
|
||
end
|
||
job = require("moonsource_kmoe.client").loginAsync(email, password, function(cookie, err)
|
||
if not cookie then done(false, err); return end
|
||
job = CookieLogin.save(withUA(nil), cookie, done)
|
||
end)
|
||
end)
|
||
end
|
||
|
||
local function showLogin()
|
||
require("ui.themes.widgets.input"):new{
|
||
title = SPEC.title,
|
||
fields = {
|
||
{ text = "", hint = _("邮箱") },
|
||
{ text = "", hint = _("密码"), text_type = "password" },
|
||
},
|
||
ok_text = _("登录"),
|
||
callback = function(email, password) doLogin(email or "", password or "") end,
|
||
}:show()
|
||
end
|
||
|
||
local function showPaste()
|
||
require("ui.themes.widgets.input"):new{
|
||
title = SPEC.cookie_title,
|
||
fields = {
|
||
{ text = "", hint = SPEC.cookie_hint },
|
||
{ text = "", hint = _("同一浏览器的 User-Agent(选填,不填不能下载)") },
|
||
},
|
||
ok_text = _("登录"),
|
||
callback = function(value, ua)
|
||
local cookie = CookieLogin.parse(value or "")
|
||
if cookie == "" then
|
||
require("ui.themes.widgets.toast"):new{ text = _("没有读到 Cookie"), timeout = 3 }:show()
|
||
return
|
||
end
|
||
CookieLogin.verify(withUA(nonEmpty(ua)), cookie)
|
||
end,
|
||
}:show()
|
||
end
|
||
|
||
function Setting.rowIcon()
|
||
return "photo_library"
|
||
end
|
||
|
||
function Setting.rowStatus()
|
||
local on = CookieLogin.authorized(SPEC)
|
||
return on and _("已登录") or _("未登录 · 点此登录"), on
|
||
end
|
||
|
||
Setting.browser = {
|
||
domain = "mox.moe",
|
||
scope = "cookie",
|
||
login = "https://mox.moe/login.php",
|
||
check = "https://mox.moe/my.php",
|
||
icon = "https://mox.moe/favicon.ico",
|
||
isAuthorized = function() return CookieLogin.authorized(SPEC) end,
|
||
apply = function(payload, cb)
|
||
return CookieLogin.save(withUA(nonEmpty(payload.ua)), CookieLogin.fromPayload(payload, "mox.moe"), cb)
|
||
end,
|
||
}
|
||
|
||
function Setting.rows()
|
||
local rows = {
|
||
CookieLogin.accountRow(SPEC, { callback = showLogin }),
|
||
CookieLogin.browserRow(),
|
||
function(iw)
|
||
return require("ui.themes.widgets.row").widget({ width = iw, chevron = true, kind = "nav",
|
||
icon = "content_paste", title = _("粘贴 Cookie"), callback = showPaste })
|
||
end,
|
||
}
|
||
if loggedIn() then rows[#rows + 1] = CookieLogin.logoutRow(SPEC) end
|
||
return rows
|
||
end
|
||
|
||
--- 登录失效提示的「重新登录」直达入口(desktop.sync.promptLogin)。
|
||
Setting.login = showLogin
|
||
|
||
return Setting
|