514 lines
16 KiB
Rust
514 lines
16 KiB
Rust
//! 业务命令:登录 / 列表 / 详情 / 播放 / 互动。
|
|
|
|
use crate::net::{base, edge};
|
|
use crate::session::SessionStore;
|
|
use serde_json::{json, Value};
|
|
use tauri::State;
|
|
|
|
pub struct Api {
|
|
pub session: SessionStore,
|
|
}
|
|
|
|
impl Api {
|
|
pub fn new() -> Self {
|
|
Self {
|
|
session: SessionStore::new(),
|
|
}
|
|
}
|
|
|
|
async fn get(&self, path: &str) -> Result<Value, String> {
|
|
let origin = base();
|
|
let token = self.session.token(&origin);
|
|
edge(&origin, "GET", path, None, token.as_deref()).await
|
|
}
|
|
|
|
async fn authed(&self, method: &str, path: &str, body: Option<Value>) -> Result<Value, String> {
|
|
let origin = base();
|
|
let token = self
|
|
.session
|
|
.token(&origin)
|
|
.ok_or_else(|| "请先登录".to_string())?;
|
|
edge(&origin, method, path, body, Some(&token)).await
|
|
}
|
|
}
|
|
|
|
/// 诊断用:把前端自检结果写到应用数据目录 debug.log
|
|
#[tauri::command]
|
|
pub async fn debug_log(app: tauri::AppHandle, msg: String) -> Result<(), String> {
|
|
use tauri::Manager;
|
|
let dir = app.path().app_data_dir().map_err(|e| e.to_string())?;
|
|
let _ = std::fs::create_dir_all(&dir);
|
|
let path = dir.join("debug.log");
|
|
use std::io::Write;
|
|
if let Ok(mut f) = std::fs::OpenOptions::new()
|
|
.create(true)
|
|
.append(true)
|
|
.open(&path)
|
|
{
|
|
let _ = writeln!(f, "{}", msg);
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
/// 网络自检:系统代理 / DNS / 连通性快照,用于远程定位「打不开」。
|
|
#[tauri::command]
|
|
pub async fn net_diag() -> Result<Value, String> {
|
|
Ok(crate::net::diagnose().await)
|
|
}
|
|
|
|
/* ───────────────────────── 线路(数据源) ───────────────────────── */
|
|
|
|
/// 当前线路 + 固定线路清单(镜像线路另外按需拉取)。
|
|
#[tauri::command]
|
|
pub async fn line_state() -> Result<Value, String> {
|
|
Ok(json!({
|
|
"base": base(),
|
|
"default": crate::net::DEFAULT_BASE,
|
|
"fixed": crate::lines::fixed(),
|
|
}))
|
|
}
|
|
|
|
/// 手动切换线路:立即生效并落盘(下次启动沿用)。
|
|
#[tauri::command]
|
|
pub async fn line_set(state: State<'_, Api>, url: String) -> Result<String, String> {
|
|
let previous = base();
|
|
let next = crate::net::set_base(&url)?;
|
|
if previous != next {
|
|
state.session.clear();
|
|
}
|
|
Ok(next)
|
|
}
|
|
|
|
/// 从地址发布页抓镜像线路(只在用户点开「线路」时调用)。
|
|
#[tauri::command]
|
|
pub async fn line_mirrors() -> Result<Value, String> {
|
|
// 用户手动点「刷新 / 打开线路」时重新读一次系统代理:代理软件开关过之后旧客户端会卡死
|
|
crate::net::refresh_client();
|
|
crate::lines::mirrors().await
|
|
}
|
|
|
|
/// 可达性探测:只报结果,不做自动检查 / 自动切换。
|
|
#[tauri::command]
|
|
pub async fn line_probe(urls: Vec<String>) -> Result<Value, String> {
|
|
crate::net::refresh_client();
|
|
Ok(crate::lines::probe(urls).await)
|
|
}
|
|
|
|
/* ───────────────────────── 更新检查 ───────────────────────── */
|
|
|
|
const REPO: &str = "Minireel/HGhub";
|
|
|
|
fn ver_parts(v: &str) -> Vec<u64> {
|
|
v.trim()
|
|
.trim_start_matches(['v', 'V'])
|
|
.split(['.', '-', '+'])
|
|
.map(|s| s.trim().parse::<u64>().unwrap_or(0))
|
|
.collect()
|
|
}
|
|
|
|
/// 只比较数字段:`v1.2.0` > `1.1.9`;解析不出的段按 0 处理。
|
|
fn is_newer(latest: &str, current: &str) -> bool {
|
|
let (a, b) = (ver_parts(latest), ver_parts(current));
|
|
for i in 0..a.len().max(b.len()) {
|
|
let x = *a.get(i).unwrap_or(&0);
|
|
let y = *b.get(i).unwrap_or(&0);
|
|
if x != y {
|
|
return x > y;
|
|
}
|
|
}
|
|
false
|
|
}
|
|
|
|
/// 对比 GitHub Release 的 tag 与当前版本(未认证 60 次/小时/IP,够用)。
|
|
#[tauri::command]
|
|
pub async fn check_update() -> Result<Value, String> {
|
|
let current = env!("CARGO_PKG_VERSION").to_string();
|
|
let url = format!("https://api.github.com/repos/{REPO}/releases/latest");
|
|
let resp = crate::net::get(
|
|
&url,
|
|
std::time::Duration::from_secs(12),
|
|
Some("application/vnd.github+json"),
|
|
None,
|
|
)
|
|
.await?;
|
|
|
|
let status = resp.status();
|
|
if status.as_u16() == 404 {
|
|
// 还没发过 release
|
|
return Ok(json!({
|
|
"current": current, "latest": "", "has_update": false,
|
|
"url": format!("https://github.com/{REPO}/releases"),
|
|
}));
|
|
}
|
|
if !status.is_success() {
|
|
return Err(format!("GitHub 返回 HTTP {status}"));
|
|
}
|
|
|
|
let v: Value = resp.json().await.map_err(|e| crate::net::err_text(&e))?;
|
|
let tag = v
|
|
.get("tag_name")
|
|
.and_then(|x| x.as_str())
|
|
.unwrap_or("")
|
|
.to_string();
|
|
let html = v
|
|
.get("html_url")
|
|
.and_then(|x| x.as_str())
|
|
.map(|s| s.to_string())
|
|
.unwrap_or_else(|| format!("https://github.com/{REPO}/releases"));
|
|
let notes = v.get("body").and_then(|x| x.as_str()).unwrap_or("");
|
|
|
|
Ok(json!({
|
|
"current": current,
|
|
"latest": tag.trim_start_matches(['v', 'V']).to_string(),
|
|
"tag": tag,
|
|
"has_update": !tag.is_empty() && is_newer(&tag, ¤t),
|
|
"url": html,
|
|
"name": v.get("name").and_then(|x| x.as_str()).unwrap_or(""),
|
|
"published_at": v.get("published_at").and_then(|x| x.as_str()).unwrap_or(""),
|
|
"notes": notes.chars().take(800).collect::<String>(),
|
|
}))
|
|
}
|
|
|
|
/// 用系统默认浏览器打开链接(只允许 http/https)。
|
|
#[tauri::command]
|
|
pub async fn open_url(url: String) -> Result<(), String> {
|
|
if !(url.starts_with("https://") || url.starts_with("http://")) {
|
|
return Err("只允许打开 http(s) 链接".to_string());
|
|
}
|
|
open_external(&url)
|
|
}
|
|
|
|
#[cfg(target_os = "windows")]
|
|
fn open_external(url: &str) -> Result<(), String> {
|
|
use std::os::windows::process::CommandExt;
|
|
std::process::Command::new("cmd")
|
|
.args(["/C", "start", "", url])
|
|
.creation_flags(0x0800_0000) // CREATE_NO_WINDOW,别闪一下黑框
|
|
.spawn()
|
|
.map(|_| ())
|
|
.map_err(|e| e.to_string())
|
|
}
|
|
|
|
#[cfg(target_os = "macos")]
|
|
fn open_external(url: &str) -> Result<(), String> {
|
|
std::process::Command::new("open")
|
|
.arg(url)
|
|
.spawn()
|
|
.map(|_| ())
|
|
.map_err(|e| e.to_string())
|
|
}
|
|
|
|
#[cfg(all(unix, not(any(target_os = "macos", target_os = "android"))))]
|
|
fn open_external(url: &str) -> Result<(), String> {
|
|
std::process::Command::new("xdg-open")
|
|
.arg(url)
|
|
.spawn()
|
|
.map(|_| ())
|
|
.map_err(|e| e.to_string())
|
|
}
|
|
|
|
#[cfg(target_os = "android")]
|
|
fn open_external(_url: &str) -> Result<(), String> {
|
|
Err("移动端请手动在浏览器打开".to_string())
|
|
}
|
|
|
|
/* ───────────────────────── 会话 ───────────────────────── */
|
|
|
|
#[tauri::command]
|
|
pub async fn app_boot(state: State<'_, Api>) -> Result<Value, String> {
|
|
let origin = base();
|
|
Ok(json!({
|
|
"user": state.session.user(&origin),
|
|
"logged_in": state.session.token(&origin).is_some(),
|
|
"base": origin,
|
|
"strict_security": crate::network_policy::strict_security(),
|
|
"cli_relaxed": crate::network_policy::cli_strict_override() == Some(false),
|
|
}))
|
|
}
|
|
|
|
/// 切换严格安全模式(关闭后回退到兼容清洗与网络守卫模式,立即生效并落盘)。
|
|
#[tauri::command]
|
|
pub async fn security_set(strict: bool) -> Result<bool, String> {
|
|
crate::network_policy::set_strict_security(strict);
|
|
crate::net::refresh_client();
|
|
Ok(crate::network_policy::strict_security())
|
|
}
|
|
|
|
#[tauri::command]
|
|
pub async fn api_login(
|
|
state: State<'_, Api>,
|
|
username: String,
|
|
password: String,
|
|
) -> Result<Value, String> {
|
|
let origin = base();
|
|
let data = edge(
|
|
&origin,
|
|
"POST",
|
|
"/api/account/loginByPassword",
|
|
Some(json!({ "username": username, "password": password })),
|
|
None,
|
|
)
|
|
.await?;
|
|
let token = data
|
|
.get("token")
|
|
.and_then(|v| v.as_str())
|
|
.ok_or_else(|| "登录返回缺少 token".to_string())?
|
|
.to_string();
|
|
let user = data.get("user").cloned().unwrap_or(Value::Null);
|
|
if base() != origin {
|
|
return Err("登录期间线路已变化,请重新登录".into());
|
|
}
|
|
state.session.set(&origin, token, user.clone());
|
|
Ok(json!({ "user": user }))
|
|
}
|
|
|
|
#[tauri::command]
|
|
pub async fn api_register(
|
|
state: State<'_, Api>,
|
|
username: String,
|
|
password: String,
|
|
email: String,
|
|
) -> Result<Value, String> {
|
|
let origin = base();
|
|
let mut body = json!({
|
|
"username": username,
|
|
"password": password,
|
|
"confirm_password": password,
|
|
});
|
|
if !email.trim().is_empty() {
|
|
body["email"] = json!(email);
|
|
}
|
|
let data = edge(
|
|
&origin,
|
|
"POST",
|
|
"/api/account/registerByPassword",
|
|
Some(body),
|
|
None,
|
|
)
|
|
.await?;
|
|
let token = data
|
|
.get("token")
|
|
.and_then(|v| v.as_str())
|
|
.ok_or_else(|| "注册返回缺少 token".to_string())?
|
|
.to_string();
|
|
let user = data.get("user").cloned().unwrap_or(Value::Null);
|
|
if base() != origin {
|
|
return Err("注册期间线路已变化,请重新登录".into());
|
|
}
|
|
state.session.set(&origin, token, user.clone());
|
|
Ok(json!({ "user": user }))
|
|
}
|
|
|
|
#[tauri::command]
|
|
pub async fn api_logout(state: State<'_, Api>) -> Result<Value, String> {
|
|
let origin = base();
|
|
let token = state.session.token(&origin);
|
|
let _ = edge(
|
|
&origin,
|
|
"POST",
|
|
"/api/account/logout",
|
|
None,
|
|
token.as_deref(),
|
|
)
|
|
.await;
|
|
state.session.clear();
|
|
Ok(json!({ "ok": true }))
|
|
}
|
|
|
|
#[tauri::command]
|
|
pub async fn api_me(state: State<'_, Api>) -> Result<Value, String> {
|
|
let origin = base();
|
|
let token = state.session.token(&origin);
|
|
let me = edge(&origin, "GET", "/api/account/me", None, token.as_deref()).await?;
|
|
if base() != origin {
|
|
return Err("读取账号期间线路已变化".into());
|
|
}
|
|
if let Some(token) = state.session.token(&origin) {
|
|
state.session.set(&origin, token, me.clone());
|
|
}
|
|
Ok(me)
|
|
}
|
|
|
|
/* ───────────────────────── 列表 ───────────────────────── */
|
|
|
|
#[tauri::command]
|
|
pub async fn api_feed(state: State<'_, Api>, page: u32, size: u32) -> Result<Value, String> {
|
|
state
|
|
.get(&format!("/api/videos?page={page}&size={size}"))
|
|
.await
|
|
}
|
|
|
|
#[tauri::command]
|
|
pub async fn api_category(
|
|
state: State<'_, Api>,
|
|
cate: String,
|
|
page: u32,
|
|
size: u32,
|
|
) -> Result<Value, String> {
|
|
state
|
|
.get(&format!(
|
|
"/api/videos/category/{cate}?page={page}&size={size}"
|
|
))
|
|
.await
|
|
}
|
|
|
|
#[tauri::command]
|
|
pub async fn api_rank(state: State<'_, Api>, kind: String) -> Result<Value, String> {
|
|
let kind = match kind.as_str() {
|
|
"recommend" | "potential" => kind,
|
|
_ => "hot".to_string(),
|
|
};
|
|
state.get(&format!("/api/ranks/{kind}")).await
|
|
}
|
|
|
|
#[tauri::command]
|
|
pub async fn api_search(
|
|
state: State<'_, Api>,
|
|
q: String,
|
|
page: u32,
|
|
size: u32,
|
|
) -> Result<Value, String> {
|
|
let encoded: String = url::form_urlencoded::byte_serialize(q.as_bytes()).collect();
|
|
state
|
|
.get(&format!("/api/search?q={encoded}&page={page}&size={size}"))
|
|
.await
|
|
}
|
|
|
|
#[tauri::command]
|
|
pub async fn api_posts(state: State<'_, Api>, page: u32, size: u32) -> Result<Value, String> {
|
|
state
|
|
.get(&format!("/api/posts?page={page}&size={size}"))
|
|
.await
|
|
}
|
|
|
|
#[tauri::command]
|
|
pub async fn api_post(state: State<'_, Api>, id: i64) -> Result<Value, String> {
|
|
state.get(&format!("/api/posts/{id}")).await
|
|
}
|
|
|
|
/* ───────────────────────── 详情 / 播放 ───────────────────────── */
|
|
|
|
#[tauri::command]
|
|
pub async fn api_video(state: State<'_, Api>, id: i64) -> Result<Value, String> {
|
|
state.get(&format!("/api/videos/{id}")).await
|
|
}
|
|
|
|
#[tauri::command]
|
|
pub async fn api_play(state: State<'_, Api>, id: i64, ep: u32) -> Result<Value, String> {
|
|
state
|
|
.get(&format!("/api/videos/{id}/play?ep={ep}&page_texts=0"))
|
|
.await
|
|
}
|
|
|
|
/* ───────────────────────── 评论 / 互动 ───────────────────────── */
|
|
|
|
#[tauri::command]
|
|
pub async fn api_comments(
|
|
state: State<'_, Api>,
|
|
id: i64,
|
|
ep: u32,
|
|
page: u32,
|
|
size: u32,
|
|
) -> Result<Value, String> {
|
|
state
|
|
.get(&format!(
|
|
"/api/videos/{id}/comments?ep={ep}&page={page}&size={size}"
|
|
))
|
|
.await
|
|
}
|
|
|
|
#[tauri::command]
|
|
pub async fn api_comment_send(
|
|
state: State<'_, Api>,
|
|
id: i64,
|
|
ep: u32,
|
|
text: String,
|
|
reply_id: Option<i64>,
|
|
) -> Result<Value, String> {
|
|
let mut body = json!({ "text": text });
|
|
let path = match reply_id {
|
|
Some(rid) if rid > 0 => {
|
|
body["reply_id"] = json!(rid);
|
|
format!("/api/videos/{id}/comments")
|
|
}
|
|
_ => format!("/api/videos/{id}/comments?ep={ep}"),
|
|
};
|
|
state.authed("POST", &path, Some(body)).await
|
|
}
|
|
|
|
#[tauri::command]
|
|
pub async fn api_like(state: State<'_, Api>, id: i64, ep: u32, on: bool) -> Result<Value, String> {
|
|
let method = if on { "POST" } else { "DELETE" };
|
|
state
|
|
.authed(method, &format!("/api/videos/{id}/like?ep={ep}"), None)
|
|
.await
|
|
}
|
|
|
|
#[tauri::command]
|
|
pub async fn api_collect(state: State<'_, Api>, id: i64, on: bool) -> Result<Value, String> {
|
|
let method = if on { "POST" } else { "DELETE" };
|
|
state
|
|
.authed(method, &format!("/api/videos/{id}/collect"), None)
|
|
.await
|
|
}
|
|
|
|
#[tauri::command]
|
|
pub async fn api_follow(state: State<'_, Api>, uid: i64, on: bool) -> Result<Value, String> {
|
|
let method = if on { "POST" } else { "DELETE" };
|
|
state
|
|
.authed(method, &format!("/api/users/{uid}/follow"), None)
|
|
.await
|
|
}
|
|
|
|
/* ───────────────────────── 本地缓存 ───────────────────────── */
|
|
|
|
#[tauri::command]
|
|
pub async fn cache_clear() -> Result<(), String> {
|
|
crate::media::clear_caches();
|
|
Ok(())
|
|
}
|
|
|
|
/* ───────────────────────── 吃瓜帖子互动 ───────────────────────── */
|
|
|
|
#[tauri::command]
|
|
pub async fn api_post_like(state: State<'_, Api>, id: i64, on: bool) -> Result<Value, String> {
|
|
let method = if on { "POST" } else { "DELETE" };
|
|
state
|
|
.authed(method, &format!("/api/posts/{id}/like"), None)
|
|
.await
|
|
}
|
|
|
|
#[tauri::command]
|
|
pub async fn api_post_collect(state: State<'_, Api>, id: i64, on: bool) -> Result<Value, String> {
|
|
let method = if on { "POST" } else { "DELETE" };
|
|
state
|
|
.authed(method, &format!("/api/posts/{id}/collect"), None)
|
|
.await
|
|
}
|
|
|
|
#[tauri::command]
|
|
pub async fn api_post_comments(
|
|
state: State<'_, Api>,
|
|
id: i64,
|
|
page: u32,
|
|
size: u32,
|
|
) -> Result<Value, String> {
|
|
state
|
|
.get(&format!("/api/posts/{id}/comments?page={page}&size={size}"))
|
|
.await
|
|
}
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::is_newer;
|
|
|
|
#[test]
|
|
fn compares_versions() {
|
|
assert!(is_newer("v1.0.2", "1.0.1"));
|
|
assert!(is_newer("v1.1", "1.0.9"));
|
|
assert!(is_newer("1.0.10", "1.0.9"));
|
|
assert!(!is_newer("v1.0.1", "1.0.1"));
|
|
assert!(!is_newer("v0.9.9", "1.0.1"));
|
|
assert!(!is_newer("", "1.0.1"));
|
|
}
|
|
}
|