Files
moonsource-jjwxc/moonsource-jjwxc.koplugin/moonsource_jjwxc/setting.lua
T
GarfieldandCopilot 2bc5c24ea7 ✨ 初始提交:月读第三方书源插件集合
每个书源一个独立插件目录,含微信读书公众号订阅、书架导入等完整功能。

Co-authored-by: Copilot <[email protected]>
2026-11-16 12:08:03 +08:00

243 lines
11 KiB
Lua

--[[--
晋江源设置:登录。
晋江手机网页的登录要过数美验证,墨水屏上做不了;登录协议有两套(protocols/web.lua、protocols/app.lua),只用其中一个:
1. 网页 Cookie:远程管理里的「浏览器登录」(浏览器扩展交 Cookie),或在电脑浏览器登录后把 Cookie 请求头粘贴进来,
先拉一次收藏页校验,通过才保存。能同步收藏、取消收藏、读已购 VIP 章。
2. App 账号:设备上直接输账号密码(新设备要手机 / 邮箱验证码),只保存账号与 token,token 失效后重新登录。
能同步书架、读已购 VIP 章。
登录成功即切到该协议;两套都登录过时可在设置里改用哪一个。
@module koplugin.book.moonsource_jjwxc.setting
--]]
local _ = require("l10n").get("moonsource_jjwxc")
local CookieLogin = require("source.cookie_login")
local SOURCE_ID = "ext.jjwxc"
local Setting = {}
---@return boolean
local function loggedIn()
return require("moonsource_jjwxc.protocols").current() ~= nil
end
---@return string, boolean
function Setting.rowStatus()
local on = loggedIn()
return on and _("已登录") or _("未登录 · 点此登录"), on
end
---@type CookieLoginSpec
local SPEC = {
id = SOURCE_ID,
title = _("晋江账号"),
cookie_title = _("晋江登录 Cookie"),
cookie_hint = _("按键盘 F12,点「网络」,刷新网页,复制请求标头里的整段 Cookie"),
logout_text = _("确定退出晋江账号?"),
loggedIn = loggedIn,
-- 拉一次收藏页校验 Cookie;通过即切到网页协议。
checkAsync = function(cookie, cb)
return require("moonsource_jjwxc.protocols.web").favAsync(cookie, function(rows, err) cb(rows ~= nil, err) end)
end,
commit = function(cookie, _account, cb)
local Settings = require("utils.settings")
local cfg = Settings.getSource(SOURCE_ID)
if (cfg.cookie or cfg.token) and cfg.cookie ~= cookie then
local cleared = require("source.logout").clear(SOURCE_ID, function()
cfg.cookie, cfg.token, cfg.account, cfg.password, cfg.protocol = nil, nil, nil, nil, nil
Settings.saveSource(SOURCE_ID)
end)
if not cleared then return cb(false, _("准备本地同步失败")) end
end
cfg.cookie, cfg.protocol = cookie, "web"
Settings.saveSource(SOURCE_ID)
require("source.auth").ok(SOURCE_ID)
require("source.registry").reload()
cb(true)
end,
clear = function(cfg) cfg.cookie, cfg.token, cfg.account, cfg.password, cfg.protocol = nil, nil, nil, nil, nil end,
}
Setting.parseCookie = CookieLogin.parse
--- 浏览器扩展交来的 payload.cookies → Cookie 请求头;只收晋江站点的 Cookie。
---@param payload table
---@return string
function Setting.fromPayload(payload)
return CookieLogin.fromPayload(payload, "jjwxc.com")
end
--- 浏览器扩展登录(features/cookie)。
Setting.browser = {
domain = "m.jjwxc.com",
scope = "cookie",
login = "https://m.jjwxc.com/my/login",
check = "https://m.jjwxc.com/my/fav",
icon = "https://m-static.jjwxc.net/images/wap/logo2.png",
isAuthorized = function() return CookieLogin.authorized(SPEC) end,
apply = function(payload, cb) return CookieLogin.save(SPEC, Setting.fromPayload(payload), cb) end,
}
local showAccountLogin
---@param plugin table|nil
---@return table[]
function Setting.rows(plugin)
local Row = require("ui.themes.widgets.row")
local rows = {
CookieLogin.accountRow(SPEC, { callback = function() showAccountLogin() end,
off_text = _("未登录 · 点此用账号密码") }),
CookieLogin.browserRow(),
CookieLogin.pasteRow(SPEC),
require("source.review_setting").row(SOURCE_ID, plugin),
}
if not loggedIn() then return rows end
local Protocols = require("moonsource_jjwxc.protocols")
rows[#rows + 1] = function(iw)
local current = Protocols.current()
return Row.widget({ width = iw, chevron = true, kind = "nav", icon = "swap_horiz", title = _("登录协议"),
status = current.label(), status_on = true,
callback = function()
local items = {}
for _idx, protocol in ipairs(Protocols.list) do
items[#items + 1] = {
text = protocol.label() .. (protocol.available() and "" or _("(未登录)")),
variant = protocol == current and "primary" or "normal",
enabled = protocol.available(),
callback = function()
Protocols.select(protocol.id)
require("source.registry").reload()
end,
}
end
require("ui.themes.widgets.sheet"):new{ title = _("登录协议"), items = items }:show()
end })
end
rows[#rows + 1] = CookieLogin.logoutRow(SPEC)
return rows
end
---@param account string
---@param password string
---@param token string
local function saveToken(account, password, token)
require("moonsource_jjwxc.protocols.app").save(account, password, token)
require("moonsource_jjwxc.protocols").select("app")
require("source.auth").ok(SOURCE_ID)
require("source.registry").reload()
end
local showLogin
--- 设备验证:选手机或邮箱收验证码,填了再登录一次。
---@param account string
---@param password string
---@param message string
local function showVerify(account, password, message)
local Toast = require("ui.themes.widgets.toast")
local Token = require("moonsource_jjwxc.protocols.app")
local checktype = "phone"
local function send(kind)
checktype = kind
Token.sendCodeAsync(account, kind, function(msg, err)
Toast:new{ text = msg or err, timeout = 3 }:show()
end)
end
require("ui.themes.widgets.input"):new{
title = _("晋江设备验证"),
fields = { { label = message, text = "", hint = _("验证码"), input_type = "number" } },
ok_text = _("验证并登录"),
actions = {
{ text = _("发到手机"), callback = function() send("phone") end },
{ text = _("发到邮箱"), callback = function() send("email") end },
},
callback = function(code)
code = (code or ""):gsub("%s", "")
if code == "" then
Toast:new{ text = _("请先填写验证码"), timeout = 3 }:show()
return
end
showLogin(account, password, { checktype = checktype, code = code })
end,
}:show()
end
--- 账号密码登录;verify 非空表示带设备验证码重试。
---@param account string
---@param password string
---@param verify { checktype: string, code: string }|nil
function showLogin(account, password, verify)
local Toast = require("ui.themes.widgets.toast")
if not require("ui/network/manager"):isConnected() then
Toast:new{ text = _("未联网"), timeout = 3 }:show()
return
end
local cfg = require("utils.settings").getSource(SOURCE_ID)
local previous_cookie, previous_token = cfg.cookie, cfg.token
local current = require("source.auth").beginLogin(SOURCE_ID)
local cancelled, job = false, nil
local waiting = require("ui.themes.widgets.progress_dialog"):new{
title = _("晋江账号"), text = _("正在登录…"), dismissable = false,
on_cancel = function()
cancelled = true
if job then job.cancel() end
end,
}
waiting:show()
job = require("moonsource_jjwxc.protocols.app").loginAsync(account, password, verify, function(token, err, kind)
if cancelled then return end
waiting:close()
if not current() or cfg.cookie ~= previous_cookie or cfg.token ~= previous_token then
Toast:new{ text = _("账号已变化,请重新登录"), timeout = 3 }:show()
elseif token then
if cfg.cookie or (cfg.token and cfg.account ~= account) then
local cleared = require("source.logout").clear(SOURCE_ID, function()
cfg.cookie, cfg.token, cfg.account, cfg.password, cfg.protocol = nil, nil, nil, nil, nil
require("utils.settings").saveSource(SOURCE_ID)
end)
if not cleared then Toast:new{ text = _("准备本地同步失败"), timeout = 3 }:show(); return end
end
saveToken(account, password, token)
Toast:new{ text = _("登录成功"), timeout = 2 }:show()
elseif kind == "verify" then
showVerify(account, password, err)
else
Toast:new{ text = err, timeout = 3 }:show()
end
end)
end
function showAccountLogin()
require("ui.themes.widgets.input"):new{
title = _("晋江账号登录"),
fields = {
{ label = _("账号"), text = "", hint = _("笔名 / 邮箱 / 手机号") },
{ label = _("密码"), text = "", text_type = "password" },
},
ok_text = _("登录"),
callback = function(account, password)
account = (account or ""):gsub("^%s+", ""):gsub("%s+$", "")
if account == "" or (password or "") == "" then
require("ui.themes.widgets.toast"):new{ text = _("账号或密码不能为空"), timeout = 3 }:show()
return
end
showLogin(account, password)
end,
}:show()
end
--- 登录失效时的操作说明:账号密码和浏览器 Cookie 两条都写清。
---@return string
function Setting.expired()
return _("你在晋江用的是哪种登录,就做哪种。") .. "\n\n"
.. _("账号和密码:点这个提示上的「重新登录」。没有这个按钮的话,打开「设置 → 书库 → 账号 → 晋江文学城 → 晋江账号」。填笔名、邮箱或手机号,再填密码,点「登录」。要验证码就点「发到手机」或「发到邮箱」,把验证码填进去,再点「验证并登录」。")
.. "\n\n" .. require("source.cookie_login").relogin(_("晋江文学城"), "https://m.jjwxc.com/my/login")
.. "\n\n" .. _("不想装扩展、平时用的是网页 Cookie:打开「设置 → 书库 → 账号 → 晋江文学城 → 晋江账号」,点「用 Cookie 登录」。Cookie 这样复制:电脑登录后按 F12,点「网络」,刷新,点任意一条请求,复制「请求标头」里的整段 Cookie,贴进去点「登录」。")
end
--- 登录失效提示的「重新登录」直达入口(desktop.sync.promptLogin)。
Setting.login = showAccountLogin
return Setting