[dependencies]
wechat-mp-core = "0.1.1"
wechat-mp-auth = "0.1"
# 或聚合包(注意包名)
wechat-miniprogram = "0.1"
wechat-pay = "0.1"
tokio = { version = "1", features = ["rt-multi-thread", "macros"] }
3. 调用登录接口
use wechat_mp_auth::AuthApi;
use wechat_mp_core::{Config, MpClient};
#[tokio::main]
async fn main() -> wechat_mp_core::Result<()> {
let client = MpClient::new(Config::from_env()?)?;
let auth = AuthApi::new(client);
let session = auth.code2session("wx.login 返回的 code").await?;
println!("openid={}", session.openid);
Ok(())
}
更多示例:聚合包、推送、物流、配送、OCR、数据分析、运维、发货、支付
use wechat_miniprogram::{
AnalyticsApi, AuthApi, Config, DeliveryApi, LogisticsApi, MpClient,
NotifyConfig, NotifyHandler, OcrApi, OpsApi,
};
#[tokio::main]
async fn main() -> wechat_miniprogram::Result<()> {
let client = MpClient::new(Config::from_env()?)?;
let auth = AuthApi::new(client.clone());
let session = auth.code2session("js_code").await?;
let _ocr = OcrApi::new(client.clone());
let _analytics = AnalyticsApi::new(client.clone());
let _ops = OpsApi::new(client.clone());
let _logistics = LogisticsApi::new(client.clone());
let _delivery = DeliveryApi::new(client);
let _push = NotifyHandler::new(NotifyConfig::from_env()?);
let _ = session.openid;
Ok(())
}
use wechat_mp_notify::{IncomingMessage, NotifyConfig, NotifyHandler, NotifyQuery};
fn handle_push(body: &str) -> wechat_mp_notify::Result<()> {
let handler = NotifyHandler::new(NotifyConfig::from_env()?);
// GET 验证:handler.verify_url(&query)?
let query = NotifyQuery {
timestamp: Some("1714112445".into()),
nonce: Some("415670741".into()),
msg_signature: Some("046e02f8...".into()),
encrypt_type: Some("aes".into()),
..Default::default()
};
let msg = handler.parse_incoming(&query, body)?;
if let IncomingMessage::Event(ev) = msg {
if ev.is_trade_manage() {
println!("发货管理事件: {} {:?}", ev.event, ev.transaction_id);
}
}
Ok(())
}
use wechat_mp_core::{Config, MpClient};
use wechat_mp_logistics::LogisticsApi;
#[tokio::main]
async fn main() -> wechat_mp_core::Result<()> {
let api = LogisticsApi::new(MpClient::new(Config::from_env()?)?);
let deliveries = api.get_all_delivery().await?;
println!("{} 家快递公司", deliveries.count);
Ok(())
}
use wechat_mp_core::{Config, MpClient};
use wechat_mp_delivery::{DeliveryApi, GetLocalOrderRequest};
#[tokio::main]
async fn main() -> wechat_mp_core::Result<()> {
let api = DeliveryApi::new(MpClient::new(Config::from_env()?)?);
let list = api.get_all_delivery().await?;
let req = GetLocalOrderRequest::new("shopid", "order_001", "app_secret");
let order = api.get_order(&req).await?;
let _ = (list, order);
Ok(())
}
use wechat_mp_core::{Config, MpClient};
use wechat_mp_ocr::{ImageSource, OcrApi};
#[tokio::main]
async fn main() -> wechat_mp_core::Result<()> {
let api = OcrApi::new(MpClient::new(Config::from_env()?)?);
let r = api
.printed_text(ImageSource::url("https://example.com/a.jpg"))
.await?;
println!("{r:?}");
Ok(())
}
use wechat_mp_analytics::{AnalyticsApi, DateRange};
use wechat_mp_core::{Config, MpClient};
#[tokio::main]
async fn main() -> wechat_mp_core::Result<()> {
let api = AnalyticsApi::new(MpClient::new(Config::from_env()?)?);
let day = DateRange::day("20260803");
let trend = api.daily_visit_trend(&day).await?;
let pages = api.visit_page(&day).await?;
let _ = (trend, pages);
Ok(())
}
use wechat_mp_core::{Config, MpClient};
use wechat_mp_ops::{CallbackCheckRequest, OpsApi};
#[tokio::main]
async fn main() -> wechat_mp_core::Result<()> {
let api = OpsApi::new(MpClient::new(Config::from_env()?)?);
let ips = api.get_api_domain_ip().await?;
let quota = api.get_api_quota("/cgi-bin/message/custom/send").await?;
let check = api.callback_check(&CallbackCheckRequest::all_default()).await?;
let _ = (ips, quota, check);
Ok(())
}
use wechat_mp_core::{Config, MpClient};
use wechat_mp_trade::{
now_upload_time, OrderKey, ShippingItem, TradeApi, UploadShippingInfoRequest,
};
#[tokio::main]
async fn main() -> wechat_mp_core::Result<()> {
let trade = TradeApi::new(MpClient::new(Config::from_env()?)?);
let req = UploadShippingInfoRequest::unified_express(
OrderKey::by_transaction_id("微信支付单号"),
ShippingItem::express("运单号", "SF", "商品*1"),
"用户 openid",
now_upload_time(),
);
trade.upload_shipping_info(&req).await?;
Ok(())
}
use wechat_pay::{Config, WechatPayClient};
#[tokio::main]
async fn main() -> wechat_pay::Result<()> {
let _ = dotenvy::dotenv();
let client = WechatPayClient::new(Config::from_env()?)?;
let out_trade_no = format!("T{}", chrono::Utc::now().timestamp_millis());
let params = client
.jsapi_pay("商品描述", &out_trade_no, 1, "用户 openid", "https://your.domain/wechatpay/notify")
.await?;
println!("{params:#?}");
Ok(())
}
use wechat_pay::{Config, TradeBillType, WechatPayClient};
#[tokio::main]
async fn main() -> wechat_pay::Result<()> {
let client = WechatPayClient::new(Config::from_env()?)?;
let bytes = client
.download_trade_bill("2026-08-01", Some(TradeBillType::All), None)
.await?;
std::fs::write("tradebill.csv", bytes).ok();
Ok(())
}