use crate::{session::SessionStore, errors::LabraError, request::{RequestType}, LabradorResult, WechatCommonResponse};
use bytes::Bytes;
use serde::{Serialize, Deserialize};
use crate::wechat::miniapp::method::{MaQrCodeMethod, WechatMaMethod};
use crate::wechat::miniapp::WechatMaClient;
#[derive(Debug, Clone)]
pub struct WechatMaQrcode<'a, T: SessionStore> {
client: &'a WechatMaClient<T>,
}
#[allow(unused)]
impl<'a, T: SessionStore> WechatMaQrcode<'a, T> {
#[inline]
pub fn new(client: &WechatMaClient<T>) -> WechatMaQrcode<T> {
WechatMaQrcode {
client,
}
}
pub async fn create_qrcode<D: Serialize>(&self, path: &str, width: Option<i32>) -> LabradorResult<Bytes> {
let width = width.unwrap_or(430);
let mini_qr_code = QRCodeRequest {
width,
path: path.to_string()
};
let result = self.client.post(WechatMaMethod::QrCode(MaQrCodeMethod::CreateWxaQrCode), vec![], &mini_qr_code, RequestType::Json).await?.bytes()?;
let res_str = String::from_utf8(result.to_vec()).unwrap_or_default();
match WechatCommonResponse::from_str(&res_str) {
Ok(r) => {
return Err(LabraError::ClientError { errcode: r.errcode.to_owned().unwrap_or_default().to_string(), errmsg: r.errmsg.to_owned().unwrap_or_default()})
}
Err(err) => { }
};
Ok(result)
}
pub async fn get_unlimited_qrcode(&mut self, scene: &str, page: &str) -> LabradorResult<Bytes> {
let mini_qr_code = MiniQRCodeRequest {
scene: scene.to_owned(),
page: page.to_owned(),
};
let result = self.client.post(WechatMaMethod::QrCode(MaQrCodeMethod::GetWxaCodeUnlimit), vec![], &mini_qr_code, RequestType::Json).await?.bytes()?;
let res_str = String::from_utf8(result.to_vec()).unwrap_or_default();
match WechatCommonResponse::from_str(&res_str) {
Ok(r) => {
return Err(LabraError::ClientError { errcode: r.errcode.to_owned().unwrap_or_default().to_string(), errmsg: r.errmsg.to_owned().unwrap_or_default()})
}
Err(err) => { }
};
Ok(result)
}
pub async fn get_qrcode(&mut self, path: &str, width: Option<i32>) -> LabradorResult<Bytes> {
let width = width.unwrap_or(430);
let mini_qr_code = QRCodeRequest {
width,
path: path.to_string()
};
let result = self.client.post(WechatMaMethod::QrCode(MaQrCodeMethod::GetWxaCodeUnlimit), vec![], &mini_qr_code, RequestType::Json).await?.bytes()?;
let res_str = String::from_utf8(result.to_vec()).unwrap_or_default();
match WechatCommonResponse::from_str(&res_str) {
Ok(r) => {
return Err(LabraError::ClientError { errcode: r.errcode.to_owned().unwrap_or_default().to_string(), errmsg: r.errmsg.to_owned().unwrap_or_default()})
}
Err(err) => { }
};
Ok(result)
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MiniQRCodeRequest {
scene: String,
page: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QRCodeRequest {
width: i32,
path: String,
}