Struct OfficialAccount

Source
pub struct OfficialAccount { /* private fields */ }

Implementations§

Source§

impl OfficialAccount

Source

pub fn get_redirect_url( &self, redirect_uri: String, scope: String, state: Option<String>, ) -> String

Source

pub async fn get_oauth2_token( &self, code: String, ) -> Result<AccessTokenResponse, Box<dyn Error>>

Exchanges the given authorization code for an access token using the WeChat API

This function constructs a request to the WeChat OAuth2 API endpoint with the provided authorization code, app ID, and app secret, and processes the response to retrieve an access token.

§Arguments
  • code - A String containing the authorization code received from the WeChat authorization server.
§Returns
  • A Result containing an AccessTokenResponse on success, or a boxed error on failure.
§Errors
  • Returns an error if the HTTP request fails or returns a non-success status, or if the response cannot be deserialized into an AccessTokenResponse.
Source

pub async fn get_userinfo( &self, openid: String, ) -> Result<UserInfoResponse, Box<dyn Error>>

Retrieves user information from WeChat API using the provided openid.

This function fetches the access token from the Redis database associated with the given openid, constructs a request to the WeChat API to get user information, and processes the response.

§Arguments
  • openid - A String representing the user’s unique identifier in WeChat.
§Returns
  • A Result containing a UserInfoResponse on success, or a boxed error on failure.
§Errors
  • Returns an error if the access token is not found in Redis, if the HTTP request fails or returns a non-success status, or if the response cannot be deserialized into a UserInfoResponse.
Source§

impl OfficialAccount

Source

pub async fn delete_menu(&self) -> Result<String, Box<dyn Error>>

Deletes all custom menus for the official account

§Returns
  • A Result containing a String with the value "ok" on success, or a boxed error on failure.
§Errors
  • Returns an error if the HTTP request fails or returns a non-success status, or if the response cannot be deserialized into a BasicResponse.
Source§

impl OfficialAccount

Source

pub async fn get_qr_ticket( &self, req: TicketRequest, ) -> Result<TicketResponse, Box<dyn Error>>

Create a QR code ticket for the official account

This function creates a QR code ticket for the official account and returns a string representing the ticket.

§Errors
  • Returns an error if the HTTP request fails or returns a non-success status, or if the response cannot be deserialized into a String.
Source§

impl OfficialAccount

Source§

impl OfficialAccount

Source

pub async fn request_validate(&self, query: WechatQuery) -> bool

Checks the signature of the WeChat request to ensure it is sent by WeChat.

§Arguments
  • query - A WechatQuery containing the timestamp, nonce, and signature.
§Returns
  • true if the signature matches, false otherwise.
Source§

impl OfficialAccount

Source

pub async fn token(&self) -> Result<String, Box<dyn Error>>

Retrieves the access token for the official account

This function attempts to fetch the access token from the Redis database. If not found, it requests a new token from the WeChat API using the application ID and secret, then stores the token in Redis for future use.

§Returns
  • A Result containing the access token as a String on success, or a boxed error on failure.
§Errors
  • Returns an error if the Redis operation fails, if the HTTP request fails, or if the response cannot be deserialized into a TokenResponse.
Source§

impl OfficialAccount

Source

pub async fn get_user_by_open_id( &self, open_id: &str, ) -> Result<UserInfoResponse, Box<dyn Error>>

Source§

impl OfficialAccount

Source

pub fn new(conf: Config, redis_url: String) -> Self

Creates a new instance of the OfficialAccount struct.

§Arguments
  • appid - The appid of the WeChat application.
  • app_secret - The app secret of the WeChat application.
  • cfg - The URL of the Redis database connection string.
§Returns

A new instance of the OfficialAccount struct.

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<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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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
Source§

impl<T> ErasedDestructor for T
where T: 'static,