ruipay/wechat/cp/api/
department.rs

1use serde::{Serialize, Deserialize};
2use serde_json::{Value};
3
4use crate::{session::SessionStore, request::{RequestType}, WechatCommonResponse, LabradorResult, WechatCpClient};
5use crate::wechat::cp::method::{CpDepartmentMethod, WechatCpMethod};
6
7/// 部门管理
8#[derive(Debug, Clone)]
9pub struct WechatCpDepartment<'a, T: SessionStore> {
10    client: &'a WechatCpClient<T>,
11}
12
13#[allow(unused)]
14impl<'a, T: SessionStore> WechatCpDepartment<'a, T> {
15
16    #[inline]
17    pub fn new(client: &WechatCpClient<T>) -> WechatCpDepartment<T> {
18        WechatCpDepartment {
19            client,
20        }
21    }
22
23    /// <pre>
24    /// 部门管理接口 - 创建部门.
25    /// 最多支持创建500个部门
26    /// 详情请见: https://work.weixin.qq.com/api/doc#90000/90135/90205
27    /// </pre>
28    pub async fn create(&self, req: WechatCpDepartInfo) -> LabradorResult<i64> {
29
30        let v = self.client.post(WechatCpMethod::Department(CpDepartmentMethod::Create), vec![], req, RequestType::Json).await?.json::<Value>()?;
31        let v = WechatCommonResponse::parse::<Value>(v)?;
32        let tag_id = v["id"].as_i64().unwrap_or_default();
33        Ok(tag_id)
34    }
35
36    /// <pre>
37    /// 部门管理接口 - 获取子部门ID列表.
38    /// 详情请见: https://developer.work.weixin.qq.com/document/path/95350
39    /// </pre>
40    pub async fn simple_list(&self, id: Option<i64>) -> LabradorResult<WechatCpDepartSimpleResponse> {
41        let mut query = vec![];
42        if let Some(id) = id {
43            query.push(("id".to_string(), id.to_string()));
44        }
45        let v = self.client.get(WechatCpMethod::Department(CpDepartmentMethod::SimpleList), query, RequestType::Json).await?.json::<Value>()?;
46        WechatCommonResponse::parse::<WechatCpDepartSimpleResponse>(v)
47    }
48
49
50    /// <pre>
51    /// 部门管理接口 - 获取部门列表.
52    /// 详情请见: https://work.weixin.qq.com/api/doc#90000/90135/90208
53    /// </pre>
54    pub async fn list(&self, id: Option<i64>) -> LabradorResult<WechatCpDepartResponse> {
55        let mut query = vec![];
56        if let Some(id) = id {
57            query.push(("id".to_string(), id.to_string()));
58        }
59        let v = self.client.get(WechatCpMethod::Department(CpDepartmentMethod::List), query, RequestType::Json).await?.json::<Value>()?;
60        WechatCommonResponse::parse::<WechatCpDepartResponse>(v)
61    }
62
63    /// <pre>
64    /// 部门管理接口 - 更新部门.
65    /// 详情请见: https://work.weixin.qq.com/api/doc#90000/90135/90206
66    /// 如果id为0(未部门),1(黑名单),2(星标组),或者不存在的id,微信会返回系统繁忙的错误
67    /// </pre>
68    pub async fn update(&self, req: WechatCpDepartInfo) -> LabradorResult<WechatCommonResponse> {
69        self.client.post(WechatCpMethod::Department(CpDepartmentMethod::Update), vec![], req, RequestType::Json).await?.json::<WechatCommonResponse>()
70    }
71
72    /// <pre>
73    /// 部门管理接口 - 删除部门.
74    /// 详情请见: https://work.weixin.qq.com/api/doc#90000/90135/90207
75    /// 应用须拥有指定部门的管理权限
76    /// </pre>
77    pub async fn delete(&self, depart_id: i64) -> LabradorResult<WechatCommonResponse> {
78        self.client.get(WechatCpMethod::Department(CpDepartmentMethod::Delete(depart_id)), vec![], RequestType::Json).await?.json::<WechatCommonResponse>()
79    }
80}
81
82//----------------------------------------------------------------------------------------------------------------------------
83/// 企业微信的部门
84#[derive(Debug, Clone,Serialize, Deserialize)]
85pub struct WechatCpDepartInfo {
86    pub id: Option<i32>,
87    pub name: Option<String>,
88    pub en_name: Option<String>,
89    pub parentid: Option<i32>,
90    pub order: Option<i32>,
91}
92
93#[derive(Debug, Clone,Serialize, Deserialize)]
94pub struct WechatCpDepartResponse {
95    pub department: Vec<WechatCpDepartInfo>,
96}
97
98#[derive(Debug, Clone,Serialize, Deserialize)]
99pub struct WechatCpDepartSimpleResponse {
100    pub department_id: Vec<WechatCpDepartInfo>,
101}