wx_rust_open/api/
wx_open_service.rs1use std::sync::Arc;
24
25use async_trait::async_trait;
26
27use wx_rust_common::bean::result::WxMinishopImageUploadResult;
28use wx_rust_common::error::WxErrorException;
29use wx_rust_common::util::http::{SimpleGetRequestExecutor, SimplePostRequestExecutor};
30
31use crate::api::WxOpenComponentService;
32use crate::bean::message::WxOpenXmlMessage;
33use crate::config::WxOpenConfigStorage;
34use crate::constant::wx_open_constants::ACCESS_TOKEN_KEY_COMPONENT;
35
36#[async_trait]
38pub trait WxOpenService: Send + Sync {
39 fn wx_open_component_service(&self) -> Option<Arc<dyn WxOpenComponentService>> {
45 None
46 }
47
48 fn wx_open_config_storage(&self) -> Arc<dyn WxOpenConfigStorage>;
50
51 fn set_wx_open_config_storage(&self, wx_open_config_storage: Arc<dyn WxOpenConfigStorage>);
53
54 fn http_client(&self) -> &reqwest::Client;
56
57 async fn get(&self, url: &str, query_param: &str) -> Result<String, WxErrorException> {
64 let executor = SimpleGetRequestExecutor::new(self.http_client().clone());
65 crate::api::r#impl::base_wx_open_service_impl::execute_with_retry(
66 self,
67 &executor,
68 url,
69 query_param.to_string(),
70 ACCESS_TOKEN_KEY_COMPONENT,
71 )
72 .await
73 }
74
75 async fn post(&self, url: &str, post_data: &str) -> Result<String, WxErrorException> {
80 let executor = SimplePostRequestExecutor::new(self.http_client().clone());
81 crate::api::r#impl::base_wx_open_service_impl::execute_with_retry(
82 self,
83 &executor,
84 url,
85 post_data.to_string(),
86 ACCESS_TOKEN_KEY_COMPONENT,
87 )
88 .await
89 }
90
91 async fn upload_minishop_media_file(
98 &self,
99 _url: &str,
100 _file_path: &str,
101 ) -> Result<WxMinishopImageUploadResult, WxErrorException> {
102 Err(WxErrorException::from_code(
103 -99,
104 "upload_minishop_media_file 未实现(Wave 1:MinishopUploadRequestExecutor)",
105 ))
106 }
107
108 async fn get_component_access_token(
115 &self,
116 force_refresh: bool,
117 ) -> Result<String, WxErrorException> {
118 match self.wx_open_component_service() {
119 Some(svc) => svc.get_component_access_token(force_refresh).await,
120 None => Err(WxErrorException::from_code(
121 -99,
122 "组件子服务未装配(getWxOpenComponentService 返回 null)",
123 )),
124 }
125 }
126
127 async fn get_pre_auth_code(&self) -> Result<String, WxErrorException> {
130 match self.wx_open_component_service() {
131 Some(svc) => svc.get_pre_auth_code().await,
132 None => Err(WxErrorException::from_code(
133 -99,
134 "组件子服务未装配(getWxOpenComponentService 返回 null)",
135 )),
136 }
137 }
138
139 async fn get_pre_auth_url(&self, redirect_uri: &str) -> Result<String, WxErrorException> {
141 match self.wx_open_component_service() {
142 Some(svc) => svc.get_pre_auth_url(redirect_uri).await,
143 None => Err(WxErrorException::from_code(
144 -99,
145 "组件子服务未装配(getWxOpenComponentService 返回 null)",
146 )),
147 }
148 }
149
150 async fn get_pre_auth_url_with(
153 &self,
154 redirect_uri: &str,
155 auth_type: Option<&str>,
156 biz_appid: Option<&str>,
157 ) -> Result<String, WxErrorException> {
158 match self.wx_open_component_service() {
159 Some(svc) => {
160 svc.get_pre_auth_url_with(redirect_uri, auth_type, biz_appid)
161 .await
162 }
163 None => Err(WxErrorException::from_code(
164 -99,
165 "组件子服务未装配(getWxOpenComponentService 返回 null)",
166 )),
167 }
168 }
169
170 async fn get_mobile_pre_auth_url(
173 &self,
174 redirect_uri: &str,
175 ) -> Result<String, WxErrorException> {
176 match self.wx_open_component_service() {
177 Some(svc) => svc.get_mobile_pre_auth_url(redirect_uri).await,
178 None => Err(WxErrorException::from_code(
179 -99,
180 "组件子服务未装配(getWxOpenComponentService 返回 null)",
181 )),
182 }
183 }
184
185 async fn get_mobile_pre_auth_url_with(
188 &self,
189 redirect_uri: &str,
190 auth_type: Option<&str>,
191 biz_appid: Option<&str>,
192 ) -> Result<String, WxErrorException> {
193 match self.wx_open_component_service() {
194 Some(svc) => {
195 svc.get_mobile_pre_auth_url_with(redirect_uri, auth_type, biz_appid)
196 .await
197 }
198 None => Err(WxErrorException::from_code(
199 -99,
200 "组件子服务未装配(getWxOpenComponentService 返回 null)",
201 )),
202 }
203 }
204
205 async fn get_authorizer_access_token(
210 &self,
211 app_id: &str,
212 force_refresh: bool,
213 ) -> Result<String, WxErrorException> {
214 match self.wx_open_component_service() {
215 Some(svc) => svc.get_authorizer_access_token(app_id, force_refresh).await,
216 None => Err(WxErrorException::from_code(
217 -99,
218 "组件子服务未装配(getWxOpenComponentService 返回 null)",
219 )),
220 }
221 }
222
223 async fn route(&self, message: &WxOpenXmlMessage) -> Result<String, WxErrorException> {
230 match self.wx_open_component_service() {
231 Some(svc) => svc.route(message).await,
232 None => Err(WxErrorException::from_code(
233 -99,
234 "组件子服务未装配(getWxOpenComponentService 返回 null)",
235 )),
236 }
237 }
238}