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