Files

99 lines
3.4 KiB
Lua
Raw Permalink 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阅读源设置:登录。
设备上扫阅文二维码(QQ阅读或起点 App)。浏览器扩展绑定保留:login/check 必须是
book.qq.com 下的 HTTPS,不能指到 passport。书架页未登录也是 200,扩展会立刻回传 Cookie。
扫码、粘贴、扩展三条路都先打 /api/user/homepage,code 不是 0 就不保存。
@module koplugin.book.moonsource_qqreader.setting
--]]
local _ = require("l10n").get("moonsource_qqreader")
local CookieLogin = require("source.cookie_login")
local SOURCE_ID = "qqreader"
local Setting = {}
function Setting.rowIcon()
return "forum"
end
---@return boolean
local function loggedIn()
return require("moonsource_qqreader.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, _("QQ阅读账号标识缺失")); return
end
return CookieLogin.commitAccount(SOURCE_ID, cookie, account.id, cb, current)
end
---@type CookieLoginSpec
local SPEC = {
id = SOURCE_ID,
title = _("QQ阅读账号"),
cookie_title = _("QQ阅读登录 Cookie"),
cookie_hint = _("按键盘 F12,点「网络」,刷新网页,复制请求标头里的整段 Cookie"),
logout_text = _("确定退出 QQ阅读账号?"),
loggedIn = loggedIn,
checkAsync = require("moonsource_qqreader.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 请求头;只收 QQ阅读站点的 Cookie。
---@param payload table
---@return string
function Setting.fromPayload(payload)
return CookieLogin.fromPayload(payload, "qq.com")
end
--- 浏览器扩展登录(features/cookie)。
Setting.browser = {
domain = "book.qq.com",
scope = "cookie",
-- 必须是 domain 下的 HTTPS。未登录书架页不跳 passport,校验在 apply 里打账号接口。
login = "https://book.qq.com/book-shelf",
check = "https://book.qq.com/book-shelf",
icon = "https://imgservices-1252317822.file.myqcloud.com/image/090120210101409/3a5c39e0.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_qqreader.yuewen_qr"):new({
app_id = "1450000220", area_id = "1", return_url = "https://book.qq.com/",
referer = "https://login.book.qq.com/login.html",
title = _("QQ阅读扫码登录"), caption = _("用 QQ阅读或起点 App 扫一扫"),
}, function(cookie) CookieLogin.verify(SPEC, cookie) end):start()
end)
end
---@return table[]
function Setting.rows()
local rows = {
CookieLogin.accountRow(SPEC, { callback = startQR, off_text = _("未登录 · 点此扫码") }),
CookieLogin.browserRow(),
CookieLogin.pasteRow(SPEC),
}
if loggedIn() then rows[#rows + 1] = CookieLogin.logoutRow(SPEC) end
return rows
end
--- 登录失效提示的「重新登录」直达入口(desktop.sync.promptLogin)。
Setting.login = startQR
return Setting