9 releases (3 stable)

new 1.0.3 Dec 2, 2024
0.2.3 Dec 2, 2024
0.2.1 Mar 7, 2024
0.2.0 Jan 16, 2024
0.1.0 Oct 6, 2023

#914 in Web programming

Download history 2/week @ 2024-09-19 1/week @ 2024-09-26 674/week @ 2024-11-28

674 downloads per month

MIT license

37KB
728 lines

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(())
}

Dependencies

~8–19MB
~261K SLoC