Skip to main content

WechatSdk

Struct WechatSdk 

Source
pub struct WechatSdk { /* private fields */ }
Expand description

微信 SDK 客户端 — 对齐 PHP EasyWeChat\Factory::officialAccount()

接收 WechatConfigWechatHttpTransport,提供 OAuth2 / 用户 / 模板消息 / JS-SDK 签名 / 二维码等核心 API。

§用法

use std::sync::Arc;
use sz_rust_auth_facade::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

Source

pub fn new( config: WechatConfig, transport: Arc<dyn WechatHttpTransport>, ) -> WechatSdk

创建微信 SDK 客户端

§参数
  • config: 微信配置
  • transport: HTTP 传输实现(业务方注入 reqwest / hyper / etc.)
Source

pub fn config(&self) -> &WechatConfig

获取配置引用

Source

pub fn get_authorize_url(&self, scope: &str, state: &str) -> String

构造 OAuth2 授权 URL — 对齐 EasyWeChat::officialAccount()->oauth->redirect()

拼接 https://open.weixin.qq.com/connect/oauth2/authorize 与查询参数:

  • appid
  • redirect_uri(来自 oauth_redirect_uri,未配置时为空字符串)
  • response_type=code
  • scope
  • state

URL 末尾追加 #wechat_redirect 锚点。

Source

pub fn get_user_by_code(&self, code: &str) -> Result<WechatUser, WechatError>

通过 code 换取用户 — 对齐 EasyWeChat::officialAccount()->oauth->userFromCode()

工作流程:

  1. GET https://api.weixin.qq.com/sns/oauth2/access_token 用 code 换取 access_token + openid
  2. 调用 WechatSdk::get_user_info 获取用户信息
§参数
  • code: OAuth2 回调携带的授权码
§返回

成功返回 WechatUser,失败返回 WechatError

Source

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: 用户 openid
  • access_token: OAuth2 access_token(来自 sns/oauth2/access_token
§返回

成功返回 WechatUser,失败返回 WechatError

Source

pub fn verify_signature( &self, signature: &str, timestamp: &str, nonce: &str, token: &str, ) -> bool

验证签名 — 对齐 EasyWeChat::officialAccount()->server->verifySignature()

计算 SHA1(token + timestamp + nonce) 并与 signature 比较。

§参数
  • signature: 待验证的签名(微信回调携带)
  • timestamp: 时间戳
  • nonce: 随机数
  • token: 公众号后台配置的 Token
§返回

签名匹配返回 true,否则返回 false

Source

pub fn send_template_message( &self, touser: &str, template_id: &str, data: &Value, ) -> Result<(), WechatError>

发送模板消息 — 对齐 EasyWeChat::officialAccount()->template_message->send()

工作流程:

  1. GET https://api.weixin.qq.com/cgi-bin/token 获取 access_token
  2. POST https://api.weixin.qq.com/cgi-bin/message/template/send 发送模板消息
§参数
  • touser: 接收者 openid
  • template_id: 模板 ID
  • data: 模板数据(JSON 值)
§返回

成功返回 Ok(()),失败返回 WechatError

Source

pub fn generate_jsapi_signature( &self, url: &str, noncestr: &str, timestamp: i64, jsapi_ticket: &str, ) -> String

生成 JS-SDK 签名 — 对齐 EasyWeChat::officialAccount()->jssdk->getSignature()

计算 SHA1(jsapi_ticket=xxx&noncestr=xxx&timestamp=xxx&url=xxx) 并返回十六进制字符串。

§参数
  • url: 当前页面 URL(不含 # 锚点)
  • noncestr: 随机字符串
  • timestamp: 时间戳(秒)
  • jsapi_ticket: jsapi_ticket(通过 cgi-bin/ticket/get 获取)
§返回

40 字符的 SHA1 十六进制签名。

Source

pub fn get_qrcode_url(&self, scene_str: &str) -> Result<String, WechatError>

获取带参数二维码 URL — 对齐 EasyWeChat::officialAccount()->qrcode->forever()

工作流程:

  1. GET https://api.weixin.qq.com/cgi-bin/token 获取 access_token
  2. POST https://api.weixin.qq.com/cgi-bin/qrcode/create 创建永久二维码 ticket
  3. 拼接 https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=xxx 返回
§参数
  • scene_str: 场景值(字符串,用于永久二维码)
§返回

成功返回二维码图片 URL,失败返回 WechatError

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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 more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more