pub struct WechatSdk { /* private fields */ }Expand description
微信 SDK 客户端 — 对齐 PHP EasyWeChat\Factory::officialAccount()
接收 WechatConfig 和 WechatHttpTransport,提供 OAuth2 / 用户 / 模板消息 /
JS-SDK 签名 / 二维码等核心 API。
§用法
use std::sync::Arc;
use sz_rust_core::wechat::{
MemoryWechatHttpTransport, WechatAppType, WechatConfig, WechatSdk,
};
let config = WechatConfig::new(
WechatAppType::OfficialAccount,
"wx-app-id",
"wx-app-secret",
)
.with_oauth_redirect_uri("https://example.com/oauth/callback");
let sdk = WechatSdk::new(config, Arc::new(MemoryWechatHttpTransport::new()));
let url = sdk.get_authorize_url("snsapi_userinfo", "state123");Implementations§
Source§impl WechatSdk
impl WechatSdk
Sourcepub fn new(
config: WechatConfig,
transport: Arc<dyn WechatHttpTransport>,
) -> Self
pub fn new( config: WechatConfig, transport: Arc<dyn WechatHttpTransport>, ) -> Self
Sourcepub fn config(&self) -> &WechatConfig
pub fn config(&self) -> &WechatConfig
获取配置引用
构造 OAuth2 授权 URL — 对齐 EasyWeChat::officialAccount()->oauth->redirect()
拼接 https://open.weixin.qq.com/connect/oauth2/authorize 与查询参数:
appidredirect_uri(来自oauth_redirect_uri,未配置时为空字符串)response_type=codescopestate
URL 末尾追加 #wechat_redirect 锚点。
Sourcepub fn get_user_by_code(&self, code: &str) -> Result<WechatUser, WechatError>
pub fn get_user_by_code(&self, code: &str) -> Result<WechatUser, WechatError>
通过 code 换取用户 — 对齐 EasyWeChat::officialAccount()->oauth->userFromCode()
工作流程:
- GET
https://api.weixin.qq.com/sns/oauth2/access_token用 code 换取 access_token + openid - 调用
WechatSdk::get_user_info获取用户信息
§参数
code: OAuth2 回调携带的授权码
§返回
成功返回 WechatUser,失败返回 WechatError。
Sourcepub fn get_user_info(
&self,
openid: &str,
access_token: &str,
) -> Result<WechatUser, WechatError>
pub fn get_user_info( &self, openid: &str, access_token: &str, ) -> Result<WechatUser, WechatError>
获取用户信息 — 对齐 EasyWeChat::officialAccount()->user->get()
GET https://api.weixin.qq.com/sns/userinfo?access_token=xxx&openid=xxx,
解析 JSON 响应为 WechatUser。
§参数
openid: 用户 openidaccess_token: OAuth2 access_token(来自sns/oauth2/access_token)
§返回
成功返回 WechatUser,失败返回 WechatError。
Sourcepub fn verify_signature(
&self,
signature: &str,
timestamp: &str,
nonce: &str,
token: &str,
) -> bool
pub fn verify_signature( &self, signature: &str, timestamp: &str, nonce: &str, token: &str, ) -> bool
Sourcepub fn send_template_message(
&self,
touser: &str,
template_id: &str,
data: &Value,
) -> Result<(), WechatError>
pub fn send_template_message( &self, touser: &str, template_id: &str, data: &Value, ) -> Result<(), WechatError>
发送模板消息 — 对齐 EasyWeChat::officialAccount()->template_message->send()
工作流程:
- GET
https://api.weixin.qq.com/cgi-bin/token获取 access_token - POST
https://api.weixin.qq.com/cgi-bin/message/template/send发送模板消息
§参数
touser: 接收者 openidtemplate_id: 模板 IDdata: 模板数据(JSON 值)
§返回
成功返回 Ok(()),失败返回 WechatError。
Sourcepub fn generate_jsapi_signature(
&self,
url: &str,
noncestr: &str,
timestamp: i64,
jsapi_ticket: &str,
) -> String
pub fn generate_jsapi_signature( &self, url: &str, noncestr: &str, timestamp: i64, jsapi_ticket: &str, ) -> String
Sourcepub fn get_qrcode_url(&self, scene_str: &str) -> Result<String, WechatError>
pub fn get_qrcode_url(&self, scene_str: &str) -> Result<String, WechatError>
获取带参数二维码 URL — 对齐 EasyWeChat::officialAccount()->qrcode->forever()
工作流程:
- GET
https://api.weixin.qq.com/cgi-bin/token获取 access_token - POST
https://api.weixin.qq.com/cgi-bin/qrcode/create创建永久二维码 ticket - 拼接
https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=xxx返回
§参数
scene_str: 场景值(字符串,用于永久二维码)
§返回
成功返回二维码图片 URL,失败返回 WechatError。
Auto Trait Implementations§
impl !RefUnwindSafe for WechatSdk
impl !UnwindSafe for WechatSdk
impl Freeze for WechatSdk
impl Send for WechatSdk
impl Sync for WechatSdk
impl Unpin for WechatSdk
impl UnsafeUnpin for WechatSdk
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.