Skip to content

Commit

Permalink
Merge pull request #9 from wslongchen/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
wslongchen authored Sep 9, 2022
2 parents 909553a + 471d4f2 commit 9b52db6
Show file tree
Hide file tree
Showing 12 changed files with 814 additions and 124 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "labrador"
version = "0.1.10"
version = "0.1.11"
authors = ["mrpan <1049058427@qq.com>"]
edition = "2018"
description = "Labrador - Mini thirdpart client for rust."
Expand Down
98 changes: 98 additions & 0 deletions src/wechat/cp/api/agent.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
use serde::{Serialize, Deserialize};
use serde_json::Value;

use crate::{session::SessionStore, request::{RequestType}, WechatCommonResponse, LabradorResult, WechatCpClient};
use crate::wechat::cp::method::{CpAgentMethod, WechatCpMethod};

/// 管理企业号应用
#[derive(Debug, Clone)]
pub struct WechatCpAgent<'a, T: SessionStore> {
client: &'a WechatCpClient<T>,
}

#[allow(unused)]
impl<'a, T: SessionStore> WechatCpAgent<'a, T> {

#[inline]
pub fn new(client: &WechatCpClient<T>) -> WechatCpAgent<T> {
WechatCpAgent {
client,
}
}

/// <pre>
/// 获取企业号应用信息
/// 该API用于获取企业号某个应用的基本信息,包括头像、昵称、帐号类型、认证类型、可见范围等信息
/// 详情请见: https://work.weixin.qq.com/api/doc#10087
/// </pre>
pub async fn get(&self, agent_id: i32) -> LabradorResult<WechatCpAgentInfo> {
let v = self.client.get(WechatCpMethod::Agent(CpAgentMethod::Get(agent_id)), vec![], RequestType::Json).await?.json::<Value>()?;
WechatCommonResponse::parse::<WechatCpAgentInfo>(v)
}

/// <pre>
/// 设置应用.
/// 仅企业可调用,可设置当前凭证对应的应用;第三方不可调用。
/// 详情请见: https://work.weixin.qq.com/api/doc#10088
/// </pre>
pub async fn set(&self, req: WechatCpAgentInfo) -> LabradorResult<WechatCommonResponse> {
self.client.post(WechatCpMethod::Agent(CpAgentMethod::Set), vec![], req,RequestType::Json).await?.json::<WechatCommonResponse>()
}

/// <pre>
/// 获取应用列表.
/// 企业仅可获取当前凭证对应的应用;第三方仅可获取被授权的应用。
/// 详情请见: https://work.weixin.qq.com/api/doc#11214
/// </pre>
pub async fn list(&self) -> LabradorResult<WechatCpAgentListResponse> {
let v = self.client.get(WechatCpMethod::Agent(CpAgentMethod::List), vec![],RequestType::Json).await?.json::<Value>()?;
WechatCommonResponse::parse::<WechatCpAgentListResponse>(v)
}
}

//----------------------------------------------------------------------------------------------------------------------------
/// 企业号应用信息
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct WechatCpAgentInfo {
pub agentid: Option<i32>,
pub name: Option<String>,
pub square_logo_url: Option<String>,
pub logo_mediaid: Option<String>,
pub description: Option<String>,
pub allow_userinfos: Option<Users>,
pub allow_partys: Option<Parties>,
pub allow_tags: Option<Tags>,
pub close: Option<i32>,
pub redirect_domain: Option<String>,
pub report_location_flag: Option<i32>,
pub isreportenter: Option<i32>,
pub home_url: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Users {
pub user: Option<Vec<User>>,
}


#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Parties {
pub partyid: Option<Vec<i64>>,
}


#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Tags {
pub tagid: Option<Vec<i64>>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct User {
pub userid: Option<String>,
}


#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct WechatCpAgentListResponse {
pub agentlist: Option<Vec<WechatCpAgentInfo>>,
}
101 changes: 101 additions & 0 deletions src/wechat/cp/api/department.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
use serde::{Serialize, Deserialize};
use serde_json::{Value};

use crate::{session::SessionStore, request::{RequestType}, WechatCommonResponse, LabradorResult, WechatCpClient};
use crate::wechat::cp::method::{CpDepartmentMethod, WechatCpMethod};

/// 部门管理
#[derive(Debug, Clone)]
pub struct WechatCpDepartment<'a, T: SessionStore> {
client: &'a WechatCpClient<T>,
}

#[allow(unused)]
impl<'a, T: SessionStore> WechatCpDepartment<'a, T> {

#[inline]
pub fn new(client: &WechatCpClient<T>) -> WechatCpDepartment<T> {
WechatCpDepartment {
client,
}
}

/// <pre>
/// 部门管理接口 - 创建部门.
/// 最多支持创建500个部门
/// 详情请见: https://work.weixin.qq.com/api/doc#90000/90135/90205
/// </pre>
pub async fn create(&self, req: WechatCpDepartInfo) -> LabradorResult<i64> {

let v = self.client.post(WechatCpMethod::Department(CpDepartmentMethod::Create), vec![], req, RequestType::Json).await?.json::<Value>()?;
let v = WechatCommonResponse::parse::<Value>(v)?;
let tag_id = v["id"].as_i64().unwrap_or_default();
Ok(tag_id)
}

/// <pre>
/// 部门管理接口 - 获取子部门ID列表.
/// 详情请见: https://developer.work.weixin.qq.com/document/path/95350
/// </pre>
pub async fn simple_list(&self, id: Option<i64>) -> LabradorResult<WechatCpDepartSimpleResponse> {
let mut query = vec![];
if let Some(id) = id {
query.push(("id".to_string(), id.to_string()));
}
let v = self.client.get(WechatCpMethod::Department(CpDepartmentMethod::SimpleList), query, RequestType::Json).await?.json::<Value>()?;
WechatCommonResponse::parse::<WechatCpDepartSimpleResponse>(v)
}


/// <pre>
/// 部门管理接口 - 获取部门列表.
/// 详情请见: https://work.weixin.qq.com/api/doc#90000/90135/90208
/// </pre>
pub async fn list(&self, id: Option<i64>) -> LabradorResult<WechatCpDepartResponse> {
let mut query = vec![];
if let Some(id) = id {
query.push(("id".to_string(), id.to_string()));
}
let v = self.client.get(WechatCpMethod::Department(CpDepartmentMethod::List), query, RequestType::Json).await?.json::<Value>()?;
WechatCommonResponse::parse::<WechatCpDepartResponse>(v)
}

/// <pre>
/// 部门管理接口 - 更新部门.
/// 详情请见: https://work.weixin.qq.com/api/doc#90000/90135/90206
/// 如果id为0(未部门),1(黑名单),2(星标组),或者不存在的id,微信会返回系统繁忙的错误
/// </pre>
pub async fn update(&self, req: WechatCpDepartInfo) -> LabradorResult<WechatCommonResponse> {
self.client.post(WechatCpMethod::Department(CpDepartmentMethod::Update), vec![], req, RequestType::Json).await?.json::<WechatCommonResponse>()
}

/// <pre>
/// 部门管理接口 - 删除部门.
/// 详情请见: https://work.weixin.qq.com/api/doc#90000/90135/90207
/// 应用须拥有指定部门的管理权限
/// </pre>
pub async fn delete(&self, depart_id: i64) -> LabradorResult<WechatCommonResponse> {
self.client.get(WechatCpMethod::Department(CpDepartmentMethod::Delete(depart_id)), vec![], RequestType::Json).await?.json::<WechatCommonResponse>()
}
}

//----------------------------------------------------------------------------------------------------------------------------
/// 企业微信的部门
#[derive(Debug, Clone,Serialize, Deserialize)]
pub struct WechatCpDepartInfo {
pub id: Option<i32>,
pub name: Option<String>,
pub en_name: Option<String>,
pub parentid: Option<i32>,
pub order: Option<i32>,
}

#[derive(Debug, Clone,Serialize, Deserialize)]
pub struct WechatCpDepartResponse {
pub department: Vec<WechatCpDepartInfo>,
}

#[derive(Debug, Clone,Serialize, Deserialize)]
pub struct WechatCpDepartSimpleResponse {
pub department_id: Vec<WechatCpDepartInfo>,
}
8 changes: 8 additions & 0 deletions src/wechat/cp/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ mod external_contact;
mod menu;
mod group_robot;
mod message;
mod department;
mod agent;
mod tag;
mod user;

// 企业微信

Expand All @@ -15,3 +19,7 @@ pub use self::external_contact::*;
pub use self::menu::*;
pub use self::group_robot::*;
pub use self::message::*;
pub use self::department::*;
pub use self::agent::*;
pub use self::tag::*;
pub use self::user::*;
Loading

0 comments on commit 9b52db6

Please sign in to comment.