Skip to main content

lnu_elytra/method/
select_course.rs

1use serde::{Deserialize, Serialize};
2
3use crate::{
4    Client,
5    error::{Error, R},
6    utils::{
7        ToJson,
8        macros::{debug, error, info, trace, warn},
9    },
10};
11
12impl Client {
13    /// 选课接口
14    pub async fn select_course(
15        &self,
16        course_id: &str,
17        course_do_id: &str,
18    ) -> R<SelectCourseResponse> {
19        info!("执行选课");
20
21        let xh = self
22            .stores
23            .get("xh_id")
24            .ok_or(Error::MissingField("xh_id"))?;
25
26        if xh.len() < 8 {
27            error!("xh_id {} 无法提取选课参数", xh);
28            return Err(Error::InvalidXhId);
29        }
30
31        #[derive(Serialize, Debug)]
32        struct SelectCouresData<'a> {
33            // 选课需要的参数
34            jxb_ids: &'a str,
35            kch_id: &'a str,
36            qz: &'a str, // 0 定值
37            njdm_id: &'a str,
38            zyh_id: &'a str,
39        }
40
41        trace!("发送请求选课");
42
43        let res = self
44            .post(&Client::SELECT_COURSE_URL)
45            .form(&SelectCouresData {
46                jxb_ids: course_do_id,
47                kch_id: course_id,
48                qz: "0",
49                njdm_id: &xh[0..4],
50                zyh_id: &xh[4..8],
51            })
52            .send()
53            .await?;
54
55        let res = res.jsonr::<SelectCourseResponse>().await?;
56
57        if res.is_success() {
58            info!("选课成功");
59        } else {
60            warn!("选课失败: {}", res.msg().unwrap_or("未知错误"));
61        }
62
63        debug!("选课结果: {:?}", res);
64
65        Ok(res)
66    }
67}
68
69/// { flag: "1", msg: None }
70///
71/// { flag: "0", msg: Some("对不起,当前未开放选课!") }
72///
73/// { flag: "0", msg: Some("选课频率过高,请稍后重试!") }
74///
75/// { flag: "0", msg: Some("一门课程只能选一个教学班,不可再选!") }
76///
77/// { flag: "0", msg: Some("超过体育分项本学期本专业最高选课门次限制,不可选!") }
78#[cfg_attr(
79    feature = "__pyo3",
80    cfg_attr(test, pyo3_stub_gen::derive::gen_stub_pyclass),
81    pyo3::pyclass(get_all)
82)]
83#[derive(Deserialize, Debug)]
84pub struct SelectCourseResponse {
85    pub flag: String,
86    pub msg: Option<String>,
87}
88
89impl SelectCourseResponse {
90    pub fn is_success(&self) -> bool {
91        self.flag == "1"
92    }
93
94    pub fn msg(&self) -> Option<&str> {
95        self.msg.as_deref()
96    }
97}