Skip to main content

fishpi_sdk/client/
profession.rs

1//! 职业成长域方法。
2
3use serde_json::json;
4
5use super::core::FishPi;
6use crate::domain::profession::{
7    ProfessionDetail, ProfessionModuleVisibility, ProfessionPrivacyPreset,
8    ProfessionProfilePosition, ProfessionRanking, ProfessionRecordsPage, ProfessionSettings,
9    PublicProfessionProfile,
10};
11use crate::utils::{build_http_path, error::Error};
12use crate::wire::profession::{
13    WireProfessionDetail, WireProfessionRanking, WireProfessionRecordsPage, WireProfessionSettings,
14    WirePublicProfessionProfile,
15};
16
17/// 职业成长域访问器。
18pub struct ProfessionApi(pub(crate) FishPi);
19
20impl FishPi {
21    /// 职业成长域。
22    pub fn profession(&self) -> ProfessionApi {
23        ProfessionApi(self.clone())
24    }
25}
26
27impl ProfessionApi {
28    /// 读取当前登录用户的主职业、累计职业、可选职业和隐私设置。
29    pub async fn me(&self) -> Result<ProfessionSettings, Error> {
30        let wire: WireProfessionSettings = self
31            .0
32            .get_data(&self.0.with_key("api/profession/me"))
33            .await?;
34        Ok(wire.into())
35    }
36
37    /// 读取指定职业的当前等级、等级路线、稀有度和贡献统计。
38    pub async fn detail(
39        &self,
40        profession_id: impl AsRef<str>,
41        dark: bool,
42    ) -> Result<ProfessionDetail, Error> {
43        let path = build_http_path(
44            &format!("api/profession/me/{}/detail", profession_id.as_ref()),
45            &[
46                ("dark", dark.to_string()),
47                ("apiKey", self.0.api_key().to_string()),
48            ],
49        );
50        let wire: WireProfessionDetail = self.0.get_data(&path).await?;
51        Ok(wire.into())
52    }
53
54    /// 读取指定职业最近 90 天的经验变化记录。
55    pub async fn records(
56        &self,
57        profession_id: impl AsRef<str>,
58        before_occurred_at: Option<i64>,
59        before_effect_id: Option<&str>,
60        limit: u32,
61    ) -> Result<ProfessionRecordsPage, Error> {
62        let limit = limit.clamp(1, 100);
63        let mut params = vec![
64            ("limit", limit.to_string()),
65            ("apiKey", self.0.api_key().to_string()),
66        ];
67        if let Some(v) = before_occurred_at {
68            params.push(("beforeOccurredAt", v.to_string()));
69        }
70        if let Some(v) = before_effect_id.filter(|v| !v.trim().is_empty()) {
71            params.push(("beforeEffectId", v.to_string()));
72        }
73        let path = build_http_path(
74            &format!("api/profession/me/{}/records", profession_id.as_ref()),
75            &params,
76        );
77        let wire: WireProfessionRecordsPage = self.0.get_data(&path).await?;
78        Ok(wire.into())
79    }
80
81    /// 设置主职业,其他职业的经验和等级不会清除。
82    pub async fn set_primary(
83        &self,
84        profession_id: impl AsRef<str>,
85    ) -> Result<ProfessionSettings, Error> {
86        let body = json!({ "professionId": profession_id.as_ref() });
87        let wire: WireProfessionSettings = self
88            .0
89            .post_page_data("api/profession/me/primary", &body)
90            .await?;
91        Ok(wire.into())
92    }
93
94    /// 结束首次选择提示。
95    pub async fn skip_first_prompt(&self) -> Result<(), Error> {
96        let _: serde_json::Value = self
97            .0
98            .post_data("api/profession/me/skip", &json!({}))
99            .await?;
100        Ok(())
101    }
102
103    /// 更新职业隐私。
104    pub async fn update_privacy(
105        &self,
106        preset: ProfessionPrivacyPreset,
107        module_visibility: Option<ProfessionModuleVisibility>,
108    ) -> Result<ProfessionSettings, Error> {
109        let mut body = json!({ "preset": preset.as_str() });
110        if let Some(v) = module_visibility
111            && let Some(map) = body.as_object_mut()
112        {
113            map.insert(
114                "moduleVisibility".to_string(),
115                json!({
116                    "primaryProfession": v.primary_profession,
117                    "allProfessions": v.all_professions,
118                    "experience": v.experience,
119                    "rarity": v.rarity,
120                    "majorContributions": v.major_contributions,
121                    "contributionStats": v.contribution_stats,
122                    "activityFeed": v.activity_feed,
123                    "levelHistory": v.level_history,
124                    "rewards": v.rewards,
125                    "ranking": v.ranking,
126                }),
127            );
128        }
129        let wire: WireProfessionSettings = self
130            .0
131            .post_page_data("api/profession/me/privacy", &body)
132            .await?;
133        Ok(wire.into())
134    }
135
136    /// 按用户隐私设置读取公开职业资料。
137    pub async fn public_profile(
138        &self,
139        user_name: impl AsRef<str>,
140        position: ProfessionProfilePosition,
141        dark: bool,
142    ) -> Result<PublicProfessionProfile, Error> {
143        let path = build_http_path(
144            &format!("api/user/{}/profession", user_name.as_ref()),
145            &[
146                ("position", position.as_str().to_string()),
147                ("dark", dark.to_string()),
148                ("apiKey", self.0.api_key().to_string()),
149            ],
150        );
151        let wire: WirePublicProfessionProfile = self.0.get_data(&path).await?;
152        Ok(wire.into())
153    }
154
155    /// 读取指定职业的公开榜单。
156    pub async fn ranking(
157        &self,
158        profession_id: Option<&str>,
159        dark: bool,
160    ) -> Result<ProfessionRanking, Error> {
161        let mut params = vec![
162            ("dark", dark.to_string()),
163            ("apiKey", self.0.api_key().to_string()),
164        ];
165        if let Some(id) = profession_id.filter(|v| !v.trim().is_empty()) {
166            params.push(("professionId", id.to_string()));
167        }
168        let path = build_http_path("api/profession/ranking", &params);
169        let wire: WireProfessionRanking = self.0.get_data(&path).await?;
170        Ok(wire.into())
171    }
172}