Skip to main content

lnu_elytra/method/
select_course.rs

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