编辑器模块(笔记)初版
This commit is contained in:
1 parent
7e65825d3e
commit
ad987111a3
66 files changed
+17470
-283
No files matched your search
@@ -32,6 +32,9 @@ pub use fnconnect::{extract_fn_id, resolve_base_url};
|
||||
use conn::{generate_device_id, sha256_hex};
|
||||
use proxy::{ProxyCfg, ProxyShared};
|
||||
|
||||
/// 登录接口路径(同时用于失败留痕)。
|
||||
const LOGIN_PATH: &str = "/music/api/v1/user/password-login";
|
||||
|
||||
/// 一条飞牛音乐连接(持久化在 `MusicSettings`)。
|
||||
#[derive(Serialize, Deserialize, Clone, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
@@ -151,6 +154,33 @@ fn slot_client(slot: &Mutex<Option<ClientSlot>>, insecure: bool, streaming: bool
|
||||
g.as_ref().unwrap().client.clone()
|
||||
}
|
||||
|
||||
/// 服务端错误 → 画面上要展示的提示。
|
||||
///
|
||||
/// 飞牛音乐失败时常常只回一句笼统的 `msg`(如 `unknown error`),单看它判断不出
|
||||
/// 该重试、该重新登录,还是该去 NAS 上修服务;这里按官方错误码表补上结论与处理动作,
|
||||
/// 让提示本身就能指导操作。HTTP 状态码与错误码附在括号里,便于对日志。
|
||||
///
|
||||
/// 只有取不到 `data` 的失败才会走到这里,因此不需要区分调用方。
|
||||
fn error_text(status: StatusCode, code: i64, msg: &str) -> String {
|
||||
let head = match code {
|
||||
100001 => "飞牛 NAS 的音乐服务内部异常,可在 NAS 上重启「飞牛音乐」应用后重试",
|
||||
100002 => "请求与 NAS 端音乐服务版本不匹配",
|
||||
100003 | 100004 => "当前账号没有权限",
|
||||
100005 => "目标不存在(可能已被删除)",
|
||||
110002 => "NAS 端音乐应用尚未初始化",
|
||||
140001 => "NAS 正在重建搜索索引,请稍后再试",
|
||||
_ => {
|
||||
let m = msg.trim();
|
||||
if m.is_empty() {
|
||||
"飞牛音乐请求失败"
|
||||
} else {
|
||||
m
|
||||
}
|
||||
}
|
||||
};
|
||||
format!("{head}(HTTP {} / code {code})", status.as_u16())
|
||||
}
|
||||
|
||||
impl Feiniu {
|
||||
/// 设置缓存根目录({app_data}/music/cache),应用启动时调用一次。
|
||||
pub fn set_cache_root(&mut self, app_data_dir: &Path) {
|
||||
@@ -310,7 +340,7 @@ impl Feiniu {
|
||||
.relay;
|
||||
let mut rb = self
|
||||
.client(insecure)
|
||||
.post(format!("{base}/music/api/v1/user/password-login"))
|
||||
.post(format!("{base}{LOGIN_PATH}"))
|
||||
.json(&body);
|
||||
// 中继链路:登录请求也需带 mode=relay,否则网关 302 回登录页
|
||||
if let Some(cookie) = Self::auth_cookie("", relay) {
|
||||
@@ -328,20 +358,24 @@ impl Feiniu {
|
||||
format!("{kind}: {e}")
|
||||
})?;
|
||||
let status = resp.status();
|
||||
let b: Value = resp
|
||||
.json()
|
||||
.await
|
||||
.unwrap_or_else(|_| json!({ "code": status.as_u16() }));
|
||||
let raw = resp.text().await.unwrap_or_default();
|
||||
let b: Value = serde_json::from_str(&raw).unwrap_or_else(|_| json!({ "code": status.as_u16() }));
|
||||
let code = b["code"].as_i64().unwrap_or(i64::from(status.as_u16()));
|
||||
if code != 0 {
|
||||
let msg = b["msg"].as_str().unwrap_or("");
|
||||
// 服务端对部分失败只给一句笼统的 msg(如 `unknown error`),
|
||||
// 不带状态码/错误码/原始响应就无从定位,这里一次性留全。
|
||||
crate::logger::log_error(
|
||||
"feiniu",
|
||||
&format!(
|
||||
"POST {LOGIN_PATH} 失败: HTTP {status} code={code} body={}",
|
||||
raw.chars().take(400).collect::<String>()
|
||||
),
|
||||
);
|
||||
if code == 120001 {
|
||||
return Err("用户名或密码错误".into());
|
||||
}
|
||||
let msg = b["msg"]
|
||||
.as_str()
|
||||
.map(|s| s.to_string())
|
||||
.unwrap_or_else(|| format!("HTTP {status}"));
|
||||
return Err(msg);
|
||||
return Err(error_text(status, code, msg));
|
||||
}
|
||||
let token = b["data"]["userToken"]
|
||||
.as_str()
|
||||
@@ -515,20 +549,30 @@ impl Feiniu {
|
||||
}
|
||||
let resp = rb.send().await.map_err(|e| format!("请求失败: {e}"))?;
|
||||
let status = resp.status();
|
||||
let body: Value = resp
|
||||
.json()
|
||||
.await
|
||||
.unwrap_or_else(|_| json!({ "code": status.as_u16() }));
|
||||
let raw = resp.text().await.unwrap_or_default();
|
||||
let body: Value =
|
||||
serde_json::from_str(&raw).unwrap_or_else(|_| json!({ "code": status.as_u16() }));
|
||||
let code = body["code"].as_i64().unwrap_or(i64::from(status.as_u16()));
|
||||
if code != 0 {
|
||||
if status == StatusCode::UNAUTHORIZED || status == StatusCode::FORBIDDEN {
|
||||
let msg = body["msg"].as_str().unwrap_or("");
|
||||
// 服务端对部分失败只给一句笼统的 msg(如 `unknown error`),
|
||||
// 不带状态码/错误码/原始响应就无从定位,这里一次性留全。
|
||||
crate::logger::log_error(
|
||||
"feiniu",
|
||||
&format!(
|
||||
"GET {path} 失败: HTTP {status} code={code} body={}",
|
||||
raw.chars().take(400).collect::<String>()
|
||||
),
|
||||
);
|
||||
// HTTP 401/403 与 120001(Unauthorized)都表示「这把会话不能用了」,
|
||||
// 需重新登录——不该混进「NAS 服务异常」的提示里。
|
||||
if status == StatusCode::UNAUTHORIZED
|
||||
|| status == StatusCode::FORBIDDEN
|
||||
|| code == 120001
|
||||
{
|
||||
return Err("登录已过期,请重新登录".into());
|
||||
}
|
||||
let msg = body["msg"]
|
||||
.as_str()
|
||||
.map(|s| s.to_string())
|
||||
.unwrap_or_else(|| format!("请求失败(HTTP {status})"));
|
||||
return Err(msg);
|
||||
return Err(error_text(status, code, msg));
|
||||
}
|
||||
Ok(body["data"].clone())
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user