Skip to content

Commit

Permalink
Merge pull request #17 from wslongchen/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
wslongchen authored Sep 18, 2022
2 parents be9be19 + 045f9dc commit 7dd1652
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 15 deletions.
21 changes: 7 additions & 14 deletions src/wechat/cp/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub enum WechatCpMethod {
GetAdminInfo,
GetOrder,
GetOrderList,
GetCustomizedAuthUrl,
Media(CpMediaMethod),
Tag(CpTagMethod),
Agent(CpAgentMethod),
Expand All @@ -32,7 +33,7 @@ pub enum WechatCpMethod {
Message(CpMessageMethod),
ExternalContact(CpExternalContactMethod),
/// 自定义方法
Custom{ need_token: bool, method_url: String }
Custom { need_token: bool, method_url: String },
}

impl RequestMethod for WechatCpMethod {
Expand All @@ -56,10 +57,11 @@ impl RequestMethod for WechatCpMethod {
WechatCpMethod::JsCode2Session => String::from("/cgi-bin/miniprogram/jscode2session"),
WechatCpMethod::GetCallbackIp => String::from("/cgi-bin/getcallbackip"),
WechatCpMethod::GetAgentConfigTicket => String::from("/cgi-bin/ticket/get?&type=agent_config"),
WechatCpMethod::GetCustomizedAuthUrl => String::from("/cgi-bin/service/get_customized_auth_url"),
WechatCpMethod::Media(v) => v.get_method(),
WechatCpMethod::ExternalContact(v) => v.get_method(),
WechatCpMethod::Oauth2(v) => v.get_method(),
WechatCpMethod::Custom{ method_url, .. } => method_url.to_string(),
WechatCpMethod::Custom { method_url, .. } => method_url.to_string(),
WechatCpMethod::Menu(v) => v.get_method(),
WechatCpMethod::Message(v) => v.get_method(),
WechatCpMethod::Tag(v) => v.get_method(),
Expand All @@ -73,11 +75,10 @@ impl RequestMethod for WechatCpMethod {

#[allow(unused)]
impl WechatCpMethod {

pub fn need_token(&self) -> bool {
match self {
WechatCpMethod::Custom{ need_token, .. } => *need_token,
WechatCpMethod::AccessToken => false,
WechatCpMethod::Custom { need_token, .. } => *need_token,
WechatCpMethod::AccessToken | WechatCpMethod::GetCustomizedAuthUrl => false,
_ => true,
}
}
Expand Down Expand Up @@ -141,7 +142,6 @@ impl CpTagMethod {
}



#[allow(unused)]
#[derive(Debug, PartialEq, Clone)]
pub enum CpAgentMethod {
Expand All @@ -162,8 +162,6 @@ impl CpAgentMethod {
}




#[allow(unused)]
#[derive(Debug, PartialEq, Clone)]
pub enum CpLicenseMethod {
Expand Down Expand Up @@ -226,8 +224,6 @@ impl CpMenuMethod {
}




#[allow(unused)]
#[derive(Debug, PartialEq, Clone)]
pub enum CpUserMethod {
Expand Down Expand Up @@ -272,8 +268,6 @@ impl CpUserMethod {
}




#[allow(unused)]
#[derive(Debug, PartialEq, Clone)]
pub enum CpDepartmentMethod {
Expand All @@ -300,8 +294,6 @@ impl CpDepartmentMethod {
}




#[allow(unused)]
#[derive(Debug, PartialEq, Clone)]
pub enum CpMessageMethod {
Expand Down Expand Up @@ -348,6 +340,7 @@ impl CpOauth2Method {
}
}
}

#[allow(unused)]
#[derive(Debug, PartialEq, Clone)]
pub enum CpExternalContactMethod {
Expand Down
44 changes: 44 additions & 0 deletions src/wechat/cp/tp/agent.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};

use crate::{LabradorResult, request::RequestType, session::SessionStore, WechatCommonResponse, WechatCpTpClient};
use crate::wechat::cp::constants::PROVIDER_ACCESS_TOKEN;
use crate::wechat::cp::method::WechatCpMethod;

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

#[allow(unused)]
impl<'a, T: SessionStore> WechatCpTpAgent<'a, T> {
#[inline]
pub fn new(client: &WechatCpTpClient<T>) -> WechatCpTpAgent<T> {
WechatCpTpAgent {
client,
}
}

/// <pre>
/// 获取带参授权链接
/// 该API用于获取代开发自建应用授权链接,用于生成带参临时二维码。
/// 详情请见: https://developer.work.weixin.qq.com/document/path/95436
/// </pre>
pub async fn get_customized_auth_url(&self, state: &str, templateid_list: &Vec<&str>) -> LabradorResult<WechatCpTpProxyResponse> {
let req = json!({"state":state,"templateid_list":templateid_list});
let access_token = self.client.get_wechat_provider_token().await?;
let query = vec![(PROVIDER_ACCESS_TOKEN.to_string(), access_token)];
let v = self.client.post(WechatCpMethod::GetCustomizedAuthUrl, query, req, RequestType::Json).await?.json::<Value>()?;
WechatCommonResponse::parse::<WechatCpTpProxyResponse>(v)
}
}

//----------------------------------------------------------------------------------------------------------------------------

/// 应用代开发 获取带参授权链接返回结构体
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WechatCpTpProxyResponse {
pub qrcode_url: String,
pub expires_in: i32,
}
3 changes: 2 additions & 1 deletion src/wechat/cp/tp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ mod media;
mod department;
mod user;
mod order;
mod agent;

pub use tag::*;
pub use license::*;
pub use media::*;
pub use department::*;
pub use user::*;
pub use order::*;
pub use agent::*;


/// 企业微信第三方应用API
Expand Down Expand Up @@ -324,7 +326,6 @@ impl<T: SessionStore> WechatCpTpClient<T> {
let timestamp = current_timestamp();
let expires_at: i64 = session.get(&expires_key, Some(timestamp))?.unwrap_or_default();
if expires_at <= timestamp {
let suite_ticket = self.get_suite_ticket()?;
let req = json!({
"corpid": self.corp_id,
"provider_secret": self.provider_secret,
Expand Down

0 comments on commit 7dd1652

Please sign in to comment.