pub struct Client { /* private fields */ }Expand description
存储微信小程序的 appid 和 secret
Implementations§
Source§impl Client
impl Client
Sourcepub fn new(app_id: &str, secret: &str) -> Self
pub fn new(app_id: &str, secret: &str) -> Self
ⓘ
use wechat_minapp::Client;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let app_id = "your app id";
let secret = "your app secret";
let client = Client::new(app_id, secret);
Ok(())
}Sourcepub async fn login(&self, code: &str) -> Result<Credential>
pub async fn login(&self, code: &str) -> Result<Credential>
登录凭证校验 https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/user-login/code2Session.html
use wechat_minapp::Client;
use serde::Deserialize;
use crate::{Error, state::AppState};
use actix_web::{Responder, web};
#[derive(Deserialize, Default)]
#[serde(default)]
pub struct Logger {
code: String,
}
pub async fn login(
state: web::Data<AppState>,
logger: web::Json<Logger>,
) -> Result<impl Responder, Error> {
let credential = state.client.login(&logger.code).await?;
Ok(())
}Sourcepub async fn access_token(&self) -> Result<String>
pub async fn access_token(&self) -> Result<String>
ⓘ
use wechat_minapp::Client;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let app_id = "your app id";
let secret = "your app secret";
let client = Client::new(app_id, secret);
let access_token = client.access_token().await?;
Ok(())
}Sourcepub async fn stable_access_token(
&self,
force_refresh: impl Into<Option<bool>> + Clone + Send,
) -> Result<String>
pub async fn stable_access_token( &self, force_refresh: impl Into<Option<bool>> + Clone + Send, ) -> Result<String>
ⓘ
use wechat_minapp::Client;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let app_id = "your app id";
let secret = "your app secret";
let client = Client::new(app_id, secret);
let stable_access_token = client.stable_access_token(Some(true)).await?;
Ok(())
}Source§impl Client
impl Client
Sourcepub async fn check_session_key(
&self,
session_key: &str,
open_id: &str,
) -> Result<()>
pub async fn check_session_key( &self, session_key: &str, open_id: &str, ) -> Result<()>
检查登录态是否过期 https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/user-login/checkSessionKey.html
Sourcepub async fn reset_session_key(
&self,
session_key: &str,
open_id: &str,
) -> Result<Credential>
pub async fn reset_session_key( &self, session_key: &str, open_id: &str, ) -> Result<Credential>
重置用户的 session_key https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/user-login/ResetUserSessionKey.html
Source§impl Client
impl Client
Sourcepub async fn qr_code(&self, args: QrCodeArgs) -> Result<QrCode>
pub async fn qr_code(&self, args: QrCodeArgs) -> Result<QrCode>
ⓘ
use wechat_minapp::{Client,QrCodeArgs};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let app_id = "your app id";
let secret = "your app secret";
let client = Client::new(app_id, secret);
let qr_args = QrCodeArgs::builder().path(&page).build()?;
let buffer = minapp.qr_code(qr_args).await?;
Ok(())
}Source§impl Client
impl Client
Sourcepub async fn get_contact(
&self,
code: &str,
open_id: Option<&str>,
) -> Result<Contact>
pub async fn get_contact( &self, code: &str, open_id: Option<&str>, ) -> Result<Contact>
ⓘ
use wechat_minapp::Client;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let app_id = "your app id";
let secret = "your app secret";
let client = Client::new(app_id, secret);
let access_token = client.get_contace("code","openid").await?;
Ok(())
}Source§impl Client
impl Client
Sourcepub async fn msg_sec_check(&self, args: &Args) -> Result<MsgSecCheckResult>
pub async fn msg_sec_check(&self, args: &Args) -> Result<MsgSecCheckResult>
内容安全检测
§示例
ⓘ
use wechat_minapp::minapp_security::{Args, Scene};
use wechant_minapp::Client;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let app_id = "your app id";
let secret = "your app secret";
let client = Client::new(app_id, secret);
let args = Args::builder()
.content("需要检测的文本内容")
.scene(Scene::Comment)
.openid("user_openid")
.build()?;
let result = client.msg_sec_check(args).await?;
if result.is_pass() {
println!("内容安全,可以发布");
} else if result.needs_review() {
println!("内容需要人工审核");
} else {
println!("内容有风险,建议修改");
}
Ok(())
}Trait Implementations§
Auto Trait Implementations§
impl Freeze for Client
impl !RefUnwindSafe for Client
impl Send for Client
impl Sync for Client
impl Unpin for Client
impl !UnwindSafe for Client
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more