bpi_rs/manga/
point_shop.rs

1//! 积分商城
2//!
3//! https://github.com/SocialSisterYi/bilibili-API-collect/blob/master/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    /// 文档: https://github.com/SocialSisterYi/bilibili-API-collect/tree/master/docs/manga
95    pub async fn manga_user_point(&self) -> Result<UserPointResponse, BpiError> {
96        self
97            .post("https://manga.bilibili.com/twirp/pointshop.v1.Pointshop/GetUserPoint")
98            .send_bpi("获取当前持有点数").await
99    }
100
101    /// 获取兑换奖品列表
102    ///
103    /// 文档: https://github.com/SocialSisterYi/bilibili-API-collect/tree/master/docs/manga
104    pub async fn manga_point_products(&self) -> Result<ProductListResponse, BpiError> {
105        self
106            .post("https://manga.bilibili.com/twirp/pointshop.v1.Pointshop/ListProduct")
107            .send_bpi("获取兑换奖品列表").await
108    }
109
110    /// 兑换物品
111    ///
112    /// 文档: https://github.com/SocialSisterYi/bilibili-API-collect/tree/master/docs/manga
113    ///
114    /// 参数
115    ///
116    /// | 名称 | 类型 | 说明 |
117    /// | ---- | ---- | ---- |
118    /// | `product_id` | i64 | 物品 ID |
119    /// | `product_num` | i32 | 兑换数量 |
120    /// | `point` | i32 | 现价所需点数 |
121    pub async fn manga_point_exchange(
122        &self,
123        product_id: i64,
124        product_num: i32,
125        point: i32
126    ) -> Result<ExchangeResponse, BpiError> {
127        let req = ExchangeRequest {
128            product_id: product_id.to_string(),
129            product_num,
130            point,
131        };
132
133        self
134            .post("https://manga.bilibili.com/twirp/pointshop.v1.Pointshop/Exchange")
135            .form(&req)
136            .send_bpi("兑换物品").await
137    }
138}
139
140#[cfg(test)]
141mod tests {
142    use super::*;
143
144    #[tokio::test]
145    async fn test_list_product() -> Result<(), Box<BpiError>> {
146        let bpi = BpiClient::new();
147        let resp = bpi.manga_point_products().await?;
148
149        let data = resp.into_data()?;
150        assert!(!data.is_empty());
151        Ok(())
152    }
153
154    #[tokio::test]
155    async fn test_get_user_point() -> Result<(), Box<BpiError>> {
156        let bpi = BpiClient::new();
157
158        let resp = bpi.manga_user_point().await?;
159        let data = resp.into_data()?;
160
161        tracing::info!("User point: {}", data.point);
162        Ok(())
163    }
164}