11 releases (5 stable)

1.1.1 Jan 2, 2025
1.0.3 Dec 2, 2024
0.2.3 Dec 2, 2024
0.2.1 Mar 7, 2024
0.1.0 Oct 6, 2023

#646 in Web programming

Download history 1/week @ 2024-09-18 2/week @ 2024-09-25 607/week @ 2024-11-27 239/week @ 2024-12-04 21/week @ 2024-12-11 257/week @ 2025-01-01

291 downloads per month

MIT license

47KB
938 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–20MB
~267K SLoC