# WeChat SDK for Rust 🚀
[](https://crates.io/crates/wechat-sdk)
[](https://docs.rs/wechat-sdk)
[](https://opensource.org/licenses/MIT)
[](https://opensource.org/licenses/Apache-2.0)
一个功能完整、类型安全、易于使用的Rust微信开发SDK,支持微信公众号和小程序APIs。
## ✨ 特性
- 🏗️ **模块化架构**:独立的公众号和小程序模块,按需使用
- 🔒 **类型安全**:完整的类型定义,编译期错误检查
- ⚡ **异步优先**:基于Tokio的异步实现,支持高并发
- 🛡️ **错误处理**:统一的错误类型和优雅的错误处理
- 🔧 **易于配置**:多种配置方式,支持环境变量和配置文件
- 📖 **丰富文档**:完整的API文档和使用示例
- 🧪 **测试覆盖**:全面的单元测试和集成测试
## 🚀 快速开始
### 添加依赖
```toml
[dependencies]
wechat-sdk = "0.1.0"
tokio = { version = "1.0", features = ["full"] }
```
### 基础使用
```rust
use wechat_sdk::WeChat;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// 创建微信客户端
let client = WeChat::builder()
.app_id("your_app_id")
.app_secret("your_app_secret")
.build()?;
// 发送公众号消息
client.official()
.message()
.to("user_openid")
.text("Hello World!")
.send().await?;
// 小程序登录验证
let session = client.miniapp()
.auth()
.code_to_session("js_code")
.call().await?;
println!("用户OpenID: {}", session.openid);
Ok(())
}
```
## 📦 功能模块
| **wechat-core** | 核心功能 | - |
| **wechat-official** | 公众号API | `official` |
| **wechat-miniapp** | 小程序API | `miniapp` |
| **wechat-crypto** | 加密解密 | - |
| **wechat-types** | 类型定义 | - |
### 按需使用
```toml
# 只使用公众号功能
wechat-sdk = { version = "0.1.0", default-features = false, features = ["official"] }
# 只使用小程序功能
wechat-sdk = { version = "0.1.0", default-features = false, features = ["miniapp"] }
# 添加缓存支持
wechat-sdk = { version = "0.1.0", features = ["cache"] }
```
## 🏗️ 架构设计
```
┌─────────────────────────────────────────┐
│ 用户API层 (Public API) │
├─────────────────────────────────────────┤
│ 业务逻辑层 (Business Logic) │
├─────────────────────────────────────────┤
│ 协议层 (Protocol Layer) │
├─────────────────────────────────────────┤
│ 传输层 (Transport Layer) │
└─────────────────────────────────────────┘
```
## 📚 文档
- [快速开始指南](./docs/quickstart.md)
- [API使用文档](./docs/api-reference.md)
- [配置指南](./docs/configuration.md)
- [最佳实践](./docs/best-practices.md)
- [示例代码](./examples/)
## 🌟 使用场景
### 公众号开发
- ✅ 消息接收和自动回复
- ✅ 自定义菜单管理
- ✅ 用户管理和标签操作
- ✅ 模板消息推送
- ✅ 素材管理
- ✅ 网页授权
### 小程序开发
- ✅ 登录凭证校验
- ✅ 用户信息解密
- ✅ 手机号获取
- ✅ 订阅消息推送
- ✅ 数据统计
### Web框架集成
- 🌐 **Axum** - 现代异步Web框架
- 🌐 **Actix-web** - 高性能Actor模型框架
- 🌐 **Warp** - 轻量级Web框架
- 🌐 **Rocket** - 类型安全Web框架
## 📖 示例
查看 [examples](./examples/) 目录获取更多示例:
- [基础使用](./examples/src/basic_usage.rs)
- [公众号消息处理](./examples/src/official_message.rs)
- [小程序认证](./examples/src/miniapp_auth.rs)
- [Axum集成](./examples/src/axum_integration.rs)
- [配置管理](./examples/src/config_management.rs)
- [最佳实践](./examples/src/best_practices.rs)
### 运行示例
```bash
# 设置环境变量
export WECHAT_APP_ID="your_app_id"
export WECHAT_APP_SECRET="your_app_secret"
# 运行基础示例
cargo run --example basic_usage
# 运行Web服务器示例
cargo run --example axum_integration --features web-axum
```
## 🛠️ 开发环境
### 环境要求
- Rust 1.70+
- Tokio runtime
### 构建项目
```bash
git clone https://github.com/your-username/wechat-sdk-rust.git
cd wechat-sdk-rust
cargo build
```
### 运行测试
```bash
# 运行所有测试
cargo test
# 运行特定模块测试
cargo test --package wechat-core
cargo test --package wechat-official
cargo test --package wechat-miniapp
```
## 🤝 贡献
欢迎贡献代码!请阅读 [CONTRIBUTING.md](./CONTRIBUTING.md) 了解贡献指南。
### 开发流程
1. Fork 项目
2. 创建特性分支 (`git checkout -b feature/amazing-feature`)
3. 提交更改 (`git commit -m 'Add amazing feature'`)
4. 推送分支 (`git push origin feature/amazing-feature`)
5. 创建 Pull Request
## 📄 许可证
本项目采用 MIT 或 Apache-2.0 双许可证。详见:
- [MIT License](./LICENSE-MIT)
- [Apache License 2.0](./LICENSE-APACHE)
## 🙏 致谢
感谢所有贡献者和以下项目的灵感:
- [wechat4u](https://github.com/nodeWechat/wechat4u) - Node.js微信SDK
- [wechat-rs](https://github.com/messense/wechat-rs) - 早期Rust微信SDK
- [reqwest](https://github.com/seanmonstar/reqwest) - HTTP客户端库
- [tokio](https://github.com/tokio-rs/tokio) - 异步运行时
## 🔗 相关链接
- [微信公众平台](https://mp.weixin.qq.com/)
- [微信开放平台](https://open.weixin.qq.com/)
- [微信小程序官方文档](https://developers.weixin.qq.com/miniprogram/dev/framework/)
- [Rust官方网站](https://www.rust-lang.org/)
## 📞 支持
如果你在使用过程中遇到问题:
1. 查看 [文档](./docs/)
2. 搜索已有 [Issues](https://github.com/your-username/wechat-sdk-rust/issues)
3. 创建新 [Issue](https://github.com/your-username/wechat-sdk-rust/issues/new)
---
<div align="center">
**用Rust构建更好的微信应用 🦀💪**
</div>