Skip to main content

bpi_rs/manga/
point_shop.rs

1//! 积分商城
2//!
3//! [查看 API 文档](https://github.com/Yuelioi/bilibili-API-collect/tree/cfc5fddcc8a94b74d91970bb5b4eaeb349addc47/docs/manga/point_shop.md)
4
5use crate::{ BilibiliRequest, BpiClient, BpiError, BpiResponse };
6use serde::{ Deserialize, Serialize };
7
8// ================= 数据结构 =================
9
10#[derive(Debug, Serialize, Clone, Deserialize)]
11pub struct UserPointData {
12    /// 用户当前持有的点数
13    pub point: String,
14}
15
16pub type UserPointResponse = BpiResponse<UserPointData>;
17
18#[derive(Debug, Serialize, Clone, Deserialize)]
19pub struct ProductLimit {
20    /// 限制类型
21    #[serde(rename = "type")]
22    pub limit_type: i32,
23    /// 限制ID
24    pub id: i64,
25    /// 限制标题
26    pub title: String,
27}
28
29#[derive(Debug, Serialize, Clone, Deserialize)]
30pub struct Product {
31    /// 物品ID
32    pub id: i64,
33    /// 物品类型
34    pub r#type: i32,
35    /// 物品名称
36    pub title: String,
37    /// 显示的图像
38    pub image: String,
39    /// 库存总量
40    pub amount: i32,
41    /// 兑换所需点数(原价)
42    pub cost: i32,
43    /// 兑换所需点数(现价)
44    pub real_cost: i32,
45    /// 库存剩余数
46    pub remain_amount: i32,
47    /// 相关漫画ID
48    pub comic_id: i64,
49    /// 限定使用范围(漫画)
50    pub limits: Vec<ProductLimit>,
51    /// 折扣
52    pub discount: i32,
53    /// 产品类型
54    pub product_type: i32,
55    /// 挂件URL
56    pub pendant_url: String,
57    /// 挂件过期时间
58    pub pendant_expire: i32,
59    /// 兑换次数限制
60    pub exchange_limit: i32,
61    /// 地址截止时间
62    pub address_deadline: String,
63    /// 活动类型
64    pub act_type: i32,
65    /// 是否兑换过该物品
66    pub has_exchanged: bool,
67    /// 主优惠券截止时间
68    pub main_coupon_deadline: String,
69    /// 截止时间
70    pub deadline: String,
71    /// 点数
72    pub point: String,
73}
74
75pub type ProductListResponse = BpiResponse<Vec<Product>>;
76
77#[derive(Debug, Clone, Serialize)]
78pub struct ExchangeRequest {
79    /// 物品ID
80    pub product_id: String,
81    /// 兑换个数
82    pub product_num: i32,
83    /// 物品所需点数(现价)
84    pub point: i32,
85}
86
87pub type ExchangeResponse = BpiResponse<serde_json::Value>;
88
89// ================= 实现 =================
90
91impl BpiClient {
92    /// 获取当前持有点数
93    ///
94    /// # 文档
95    /// [查看API文档](https://github.com/SocialSisterYi/bilibili-API-collect/tree/master/docs/manga)
96    pub async fn manga_user_point(&self) -> Result<UserPointResponse, BpiError> {
97        self
98            .post("https://manga.bilibili.com/twirp/pointshop.v1.Pointshop/GetUserPoint")
99            .send_bpi("获取当前持有点数").await
100    }
101
102    /// 获取兑换奖品列表
103    ///
104    /// # 文档
105    /// [查看API文档](https://github.com/SocialSisterYi/bilibili-API-collect/tree/master/docs/manga)
106    pub async fn manga_point_products(&self) -> Result<ProductListResponse, BpiError> {
107        self
108            .post("https://manga.bilibili.com/twirp/pointshop.v1.Pointshop/ListProduct")
109            .send_bpi("获取兑换奖品列表").await
110    }
111
112    /// 兑换物品
113    ///
114    /// # 文档
115    /// [查看API文档](https://github.com/SocialSisterYi/bilibili-API-collect/tree/master/docs/manga)
116    ///
117    /// # 参数
118    ///
119    /// | 名称 | 类型 | 说明 |
120    /// | ---- | ---- | ---- |
121    /// | `product_id` | i64 | 物品 ID |
122    /// | `product_num` | i32 | 兑换数量 |
123    /// | `point` | i32 | 现价所需点数 |
124    pub async fn manga_point_exchange(
125        &self,
126        product_id: i64,
127        product_num: i32,
128        point: i32
129    ) -> Result<ExchangeResponse, BpiError> {
130        let req = ExchangeRequest {
131            product_id: product_id.to_string(),
132            product_num,
133            point,
134        };
135
136        self
137            .post("https://manga.bilibili.com/twirp/pointshop.v1.Pointshop/Exchange")
138            .form(&req)
139            .send_bpi("兑换物品").await
140    }
141}
142
143#[cfg(test)]
144mod tests {
145    use super::*;
146
147    #[tokio::test]
148    async fn test_list_product() -> Result<(), Box<BpiError>> {
149        let bpi = BpiClient::new();
150        let resp = bpi.manga_point_products().await?;
151
152        let data = resp.into_data()?;
153        assert!(!data.is_empty());
154        Ok(())
155    }
156
157    #[tokio::test]
158    async fn test_get_user_point() -> Result<(), Box<BpiError>> {
159        let bpi = BpiClient::new();
160
161        let resp = bpi.manga_user_point().await?;
162        let data = resp.into_data()?;
163
164        tracing::info!("User point: {}", data.point);
165        Ok(())
166    }
167}