use std::collections::HashMap;
use serde_json::{json};
use serde::{Serialize, Deserialize};
use crate::{session::SessionStore, errors::LabraError, wechat::{cryptos::WechatCrypto}, request::RequestType, WechatCommonResponse, LabradorResult};
use crate::wechat::miniapp::method::{MaUserMethod, WechatMaMethod};
use crate::wechat::miniapp::WechatMaClient;
#[derive(Debug, Clone)]
pub struct WechatMaUser<'a, T: SessionStore> {
client: &'a WechatMaClient<T>,
}
#[allow(unused)]
impl<'a, T: SessionStore> WechatMaUser<'a, T> {
#[inline]
pub fn new(client: &WechatMaClient<T>) -> WechatMaUser<T> {
WechatMaUser {
client,
}
}
pub fn decrypt_user_info(&self, session_key: &str, encrypted_data: &str, iv: &str) -> LabradorResult<WechatMaUserResponse> {
let result = WechatCrypto::decrypt_data(session_key, encrypted_data, iv)?;
serde_json::from_str::<WechatMaUserResponse>(&result).map_err(LabraError::from)
}
pub async fn set_user_storage(&self,session_key: &str, openid: &str, kv: &HashMap<String, String>) -> LabradorResult<WechatCommonResponse> {
let mut params = Vec::new();
for (k, v) in kv.into_iter() {
params.push(json!({
"key": k,
"value": v
}));
}
let req = json!({
"kv_list": params
});
let signature = WechatCrypto::create_hmac_sha256_sign(session_key, &req.to_string())?;
self.client.post(WechatMaMethod::User(MaUserMethod::SetUserStorage), vec![("appid".to_string(), self.client.secret.to_string()),
("signature".to_string(), signature),("openid".to_string(), openid.to_string()),("sig_method".to_string(), "hmac_sha256".to_string()),], &req, RequestType::Json).await?.json::<WechatCommonResponse>()
}
pub async fn get_phone_info(&self,code: &str) -> LabradorResult<PhoneInfo> {
let req = json!({
"code": code
});
let v = self.client.post(WechatMaMethod::User(MaUserMethod::GetPhoneNumber), vec![], &req, RequestType::Json).await?.json::<serde_json::Value>()?;
WechatCommonResponse::parse(v)
}
pub async fn decrypt_phone_info(&self, session_key: &str, encrypted_data: &str, iv: &str) -> LabradorResult<PhoneInfo> {
let result = WechatCrypto::decrypt_data(session_key, encrypted_data, iv)?;
serde_json::from_str::<PhoneInfo>(&result).map_err(LabraError::from)
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct WechatMaUserResponse {
pub nick_name: String,
pub gender: u8,
pub language: String,
pub city: String,
pub province: String,
pub country: String,
pub avatar_url: String,
pub union_id: Option<String>,
pub watermark: Option<serde_json::Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PhoneInfo {
pub phone_number: Option<String>,
pub pure_phone_number: Option<String>,
pub country_code: Vec<String>,
}