lnu_elytra/method/
select_course.rs1use 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 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 jxb_ids: &'a str,
35 kch_id: &'a str,
36 qz: &'a str, 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#[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}