wechat_minapp/user/mod.rs
1//! 微信小程序用户信息模块
2//!
3//! 该模块提供了获取和处理微信小程序用户信息的功能,包括用户基本信息和手机号信息。
4//!
5//! # 登录信息
6//!
7//! ```no_run
8//! use wechat_minapp::client::WechatMinappSDK;
9//! use wechat_minapp::user::{User, Contact};
10//!
11//! #[tokio::main]
12//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
13//! let client = WechatMinappSDK::new("app_id", "secret");
14//! let user = User::new(client);
15//! let code = "0816abc123def456";
16//! let credential = user.login(code).await?;
17//!
18//! println!("用户OpenID: {}", credential.open_id());
19//! println!("会话密钥: {}", credential.session_key());
20//!
21//! Ok(())
22//! }
23//! ```
24//!
25//! 解析用户基本信息(需要前端传递加密数据)
26//! ```no_run
27//! use wechat_minapp::client::WechatMinappSDK;
28//! use wechat_minapp::user::{User, Contact};
29//!
30//! #[tokio::main]
31//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
32//! let client = WechatMinappSDK::new("app_id", "secret");
33//! let user = User::new(client);
34//! let code = "0816abc123def456";
35//! let credential = user.login(code).await?;
36//! let info = credential.decrypt(&encrypted_data, &iv)?;
37//! println!("昵称: {}", info.nickname());
38//! println!("性别: {}", info.gender());
39//! println!("地区: {}-{}-{}", info.country(), info.province(), info.city());
40//! println!("头像: {}", info.avatar());
41//! println!("AppID: {}", info.app_id());
42//! println!("时间戳: {}", info.timestamp());
43//!
44//! Ok(())
45//! }
46//! ```
47//!
48//! 获取用户手机号
49//! ```no_run
50//! use wechat_minapp::client::WechatMinappSDK;
51//! use wechat_minapp::user::{User, Contact};
52//!
53//! #[tokio::main]
54//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
55//! let client = WechatMinappSDK::new("app_id", "secret");
56//! let user = User::new(client);
57//! let code = "0816abc123def456";
58//! let contact = user.get_contact(code, None).await?;
59//! let info = credential.decrypt(&encrypted_data, &iv)?;
60//! println!("用户手机号: {}", contact.phone_number());
61//!
62//! Ok(())
63//! }
64//! ```
65//!
66mod credential;
67mod user_info;
68use crate::client::WechatMinappSDK;
69
70pub struct User {
71 pub client: WechatMinappSDK,
72}
73
74impl User {
75 pub fn new(client: WechatMinappSDK) -> Self {
76 User { client }
77 }
78}