use std::sync::{Arc, Weak};
use async_trait::async_trait;
use wx_rust_common::error::WxErrorException;
use wx_rust_miniapp::api::WxMaService;
use crate::api::WxOpenMaPrivacyService;
use crate::api::WxOpenService;
use crate::api::r#impl::WxOpenMaService;
use crate::api::r#impl::base_wx_open_service_impl;
use crate::bean::ApplyPrivacyInterface;
use crate::bean::ApplyPrivacyInterfaceResult;
use crate::bean::GetPrivacyInterfaceResult;
use crate::bean::GetPrivacySettingResult;
use crate::bean::SetPrivacySetting;
use crate::enums::url_ma_domain::{
ma_privacy_apply_interface_url, ma_privacy_get_interface_url, ma_privacy_get_url,
ma_privacy_set_url,
};
pub struct WxOpenMaPrivacyServiceImpl {
wx_open_service: Weak<dyn WxOpenService>,
app_id: String,
}
impl WxOpenMaPrivacyServiceImpl {
pub fn new(wx_open_service: Arc<dyn WxOpenService>, app_id: String) -> Self {
Self {
wx_open_service: Arc::downgrade(&wx_open_service),
app_id,
}
}
pub fn app_id(&self) -> &str {
&self.app_id
}
fn svc(&self) -> Result<Arc<dyn WxOpenService>, WxErrorException> {
self.wx_open_service
.upgrade()
.ok_or_else(|| WxErrorException::from_code(-99, "门面服务已被释放"))
}
fn ma_service(&self) -> Result<Arc<dyn WxMaService>, WxErrorException> {
let svc = self.svc()?;
let component = svc.wx_open_component_service().ok_or_else(|| {
WxErrorException::from_code(
-99,
"组件子服务未装配(getWxOpenComponentService 返回 null)",
)
})?;
let any = component
.get_wx_ma_service_by_appid(&self.app_id)
.ok_or_else(|| WxErrorException::from_code(-99, "getWxMaServiceByAppid 返回 None"))?;
let ma = any.downcast::<WxOpenMaService>().map_err(|_| {
WxErrorException::from_code(-99, "代 ma 服务 downcast 失败(缓存类型不匹配)")
})?;
Ok(ma as Arc<dyn WxMaService>)
}
}
#[async_trait]
impl WxOpenMaPrivacyService for WxOpenMaPrivacyServiceImpl {
async fn get_privacy_setting(
&self,
privacy_ver: Option<i32>,
) -> Result<GetPrivacySettingResult, WxErrorException> {
let svc = self.svc()?;
let config = svc.wx_open_config_storage();
let ma = self.ma_service()?;
let mut body = serde_json::Map::new();
if let Some(ver) = privacy_ver {
body.insert("privacy_ver".to_string(), serde_json::json!(ver));
}
let body = serde_json::Value::Object(body);
let response = ma
.post(&ma_privacy_get_url(config.as_ref()), &body.to_string())
.await?;
let response = base_wx_open_service_impl::normalize_errcode(&response)?;
serde_json::from_str(&response).map_err(|e| WxErrorException::Serde(e.to_string()))
}
async fn set_privacy_setting(&self, dto: &SetPrivacySetting) -> Result<(), WxErrorException> {
let svc = self.svc()?;
let config = svc.wx_open_config_storage();
let ma = self.ma_service()?;
let body =
serde_json::to_string(dto).map_err(|e| WxErrorException::Serde(e.to_string()))?;
ma.post(&ma_privacy_set_url(config.as_ref()), &body).await?;
Ok(())
}
async fn get_privacy_interface(&self) -> Result<GetPrivacyInterfaceResult, WxErrorException> {
let svc = self.svc()?;
let config = svc.wx_open_config_storage();
let ma = self.ma_service()?;
let response = ma
.get(&ma_privacy_get_interface_url(config.as_ref()), "")
.await?;
let response = base_wx_open_service_impl::normalize_errcode(&response)?;
serde_json::from_str(&response).map_err(|e| WxErrorException::Serde(e.to_string()))
}
async fn apply_privacy_interface(
&self,
dto: &ApplyPrivacyInterface,
) -> Result<ApplyPrivacyInterfaceResult, WxErrorException> {
let svc = self.svc()?;
let config = svc.wx_open_config_storage();
let ma = self.ma_service()?;
let body =
serde_json::to_string(dto).map_err(|e| WxErrorException::Serde(e.to_string()))?;
let response = ma
.post(&ma_privacy_apply_interface_url(config.as_ref()), &body)
.await?;
let response = base_wx_open_service_impl::normalize_errcode(&response)?;
serde_json::from_str(&response).map_err(|e| WxErrorException::Serde(e.to_string()))
}
}