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

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

109 lines
4.1 KiB
Lua
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
--[[--
起点源设置:登录。
优先在设备上展示阅文二维码,用起点或 QQ阅读 App 扫码;保留浏览器扩展与手动 Cookie 导入。
拿到登录态先拉书架页校验,通过才保存。Cookie 里的 _csrfToken 与请求参数同值。
@module koplugin.book.moonsource_qidian.setting
--]]
local _ = require("l10n").get("moonsource_qidian")
local CookieLogin = require("source.cookie_login")
local SOURCE_ID = "ext.qidian"
local Setting = {}
---@return boolean
local function loggedIn()
return require("moonsource_qidian.client").cookie() ~= nil
end
--- 同账号续期不动同步标记;换号保留本地记录并准备上行。
local function commit(cookie, account, cb, current)
if not account or type(account.id) ~= "string" or not account.id:match("^%d+$") then
cb(false, _("起点账号标识缺失")); return
end
return CookieLogin.commitAccount(SOURCE_ID, cookie, account.id, cb, current)
end
---@type CookieLoginSpec
local SPEC = {
id = SOURCE_ID,
title = _("起点账号"),
cookie_title = _("起点登录 Cookie"),
cookie_hint = _("按键盘 F12,点「网络」,刷新网页,复制请求标头里的整段 Cookie"),
logout_text = _("确定退出起点账号?"),
loggedIn = loggedIn,
checkAsync = require("moonsource_qidian.client").loginCheckAsync,
commit = commit,
clear = function(cfg) cfg.cookie, cfg.account_id = nil, nil end,
}
---@return string, boolean
function Setting.rowStatus()
local on = loggedIn()
return on and _("已登录") or _("未登录 · 点此登录"), on
end
Setting.parseCookie = CookieLogin.parse
--- 浏览器扩展交来的 payload.cookies → Cookie 请求头;只收起点站点的 Cookie。
---@param payload table
---@return string
function Setting.fromPayload(payload)
return CookieLogin.fromPayload(payload, "qidian.com")
end
--- 浏览器扩展登录(features/cookie)。
Setting.browser = {
domain = "m.qidian.com",
scope = "cookie",
-- 扩展要求 login/check 与 domain 同主机。未登录时书架页自己 302 到 passport,登录完回到本站再采集。
login = "https://m.qidian.com/bookshelf/my/",
check = "https://m.qidian.com/bookshelf/my/",
icon = "https://imgservices-1252317822.image.myqcloud.com/coco/s03102023/b7b10602.w663ve.ico",
isAuthorized = function() return CookieLogin.authorized(SPEC) end,
apply = function(payload, cb) return CookieLogin.save(SPEC, Setting.fromPayload(payload), cb) end,
}
local function startQR()
require("ui/network/manager"):runWhenConnected(function()
require("moonsource_qidian.yuewen_qr"):new({
app_id = "13", area_id = "31", return_url = "https://m.qidian.com/",
ticket = "1",
referer = "https://passport.qidian.com/mobile/login",
title = _("起点扫码登录"), caption = _("用 QQ阅读或起点 App 扫一扫"),
}, function(cookie)
-- 不要造假 _csrfToken:loginCheck 会把书架页 Set-Cookie(真 csrf 等)合并后再保存。
CookieLogin.verify(SPEC, cookie)
end):start()
end)
end
---@return table[]
function Setting.rows(plugin)
local rows = {
CookieLogin.accountRow(SPEC, { callback = startQR, off_text = _("未登录 · 点此扫码") }),
function(iw)
return require("ui.themes.widgets.row").widget{
width = iw, chevron = true, kind = "nav", icon = "sms",
title = _("手机号验证码登录"),
callback = function()
require("moonsource_qidian.phone_login").new(function(cookie) CookieLogin.verify(SPEC, cookie) end):start()
end,
}
end,
CookieLogin.browserRow(),
CookieLogin.pasteRow(SPEC),
require("source.review_setting").row(SOURCE_ID, plugin),
require("source.category_setting").row(SOURCE_ID, plugin),
}
if loggedIn() then rows[#rows + 1] = CookieLogin.logoutRow(SPEC) end
return rows
end
--- 登录失效提示的「重新登录」直达入口(desktop.sync.promptLogin)。
Setting.login = startQR
return Setting