Skip to content

headironc/open-wechat

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

open-wechat

WeChat

A rust sdk for wechat miniprogram open api

Usage

Get access token

use open_wechat::{
    client::Client, 
    credential::{GenericAccessToken, GetAccessToken}
};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let app_id = "your app id";
    let app_secret = "your app secret";

    let client = Client::new(app_id, app_secret);

    let access_token = GenericAccessToken::new(self.client.clone()).await?;

    Ok(())
}

Get stable access token

use open_wechat::{
    client::Client, 
    credential::{GenericAccessToken, GetStableAccessToken}
};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let app_id = "your app id";
    let app_secret = "your app secret";

    let client = Client::new(app_id, app_secret);

    let stable_access_token = GenericAccessToken::new(self.client.clone(), None).await?;

    Ok(())
}

Code to session

use axum::{extract::State, response::IntoResponse, Json};
use open_wechat::client::Client;
use serde::Deserialize;

use crate::Result;

#[derive(Deserialize, Default)]
#[serde(default)]
pub(crate) struct Logger {
    code: String,
}

pub(crate) async fn login(
    State(client): State<Client>,
    Json(logger): Json<Logger>,
) -> Result<impl IntoResponse> {
    let credential = client.login(&logger.code).await?;

    Ok(())
}

Decrypt data

use axum::{extract::State, response::IntoResponse, Json};
use open_wechat::client::Client;
use serde::Deserialize;

use crate::Result;

#[derive(Deserialize, Default)]
pub(crate) struct EncryptedPayload {
    code: String,
    encrypted_data: String,
    iv: String,
}

pub(crate) async fn decrypt(
    State(client): State<Client>,
    Json(payload): Json<EncryptedPayload>,
) -> Result<impl IntoResponse> {
    let credential = client.login(&payload.code).await?;

    let user = credential.decrypt(&payload.encrypted_data, &payload.iv)?;

    Ok(())
}

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages