diff --git a/Cargo.lock b/Cargo.lock index bbb924d..01d313c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1121,9 +1121,11 @@ dependencies = [ "http", "http-body", "http-body-util", + "hyper", "i18n", "jsonwebtoken", "lazy_static", + "macro", "moka", "once_cell", "reqwest", diff --git a/app.toml b/app.toml index 14ecf13..42067b0 100644 --- a/app.toml +++ b/app.toml @@ -20,5 +20,5 @@ expires = 1800 refresh_expires = 3600 [social.wechat] -app_id = "wx0d0b0b0b0b0b0b0b" -app_secret = "0d0b0b0b0d0b0b0b0d0b0b0b0b0b0b0b" +app_id = "wxf0547aa24593a446" +app_secret = "e4a29192f9220d4d8c9eab37f8385c37" diff --git a/i18n.csv b/i18n.csv index e28b6c4..83e16d3 100644 --- a/i18n.csv +++ b/i18n.csv @@ -13,4 +13,5 @@ ValidateAccountLangTagRequired,lang tag is required,用户语言标识不能为 ValidatePageablePageRequired,invalid page number,页码无效 ValidatePageablePageSizeRequired,invalid quantity per page,每页数量无效 BadRequest,bad request,无效请求 -InvalidParams,invalid params,无效参数 \ No newline at end of file +InvalidParams,invalid params,无效参数 +FailedGetWxAaccessToken,Failed to get WeChat access_token,获取微信access_token失败 \ No newline at end of file diff --git a/i18n/src/message_ids.rs b/i18n/src/message_ids.rs index 71f0a39..7bd32c2 100644 --- a/i18n/src/message_ids.rs +++ b/i18n/src/message_ids.rs @@ -2,21 +2,40 @@ use strum_macros::{AsRefStr, Display, EnumIter, EnumString}; #[derive(Debug, EnumIter, EnumString, Display, PartialEq, AsRefStr)] pub enum MessageId { + /// 无效请求 BadRequest, + /// 系统内部错误 ServerInternalError, Hello, + /// 账户已禁用 AccountDisabled, + /// 账户无权限 AccountNoPermission, + /// 用户名或密码错误 IncorrectUsernameOrPassword, + /// 无效令牌 InvalidToken, + /// 无效参数 InvalidParams, + /// 反馈内容不能为空 ValidateFeedbackContentRequired, + /// 用户名称不能为空 ValidateAccountNameRequired, + /// 密码不能为空 ValidateAccountPasswordRequired, + /// 用户ID Token不能为空 ValidateAccountIdTokenRequired, + /// 用户语言标识不能为空 ValidateAccountLangTagRequired, + /// 页码无效 ValidatePageablePageRequired, + /// 每页数量无效 ValidatePageablePageSizeRequired, + + /// social begin + + /// 获取微信access_token失败 + FailedGetWxAaccessToken, } diff --git a/library/Cargo.toml b/library/Cargo.toml index 5260f26..270ad37 100644 --- a/library/Cargo.toml +++ b/library/Cargo.toml @@ -34,6 +34,8 @@ hex-literal = { workspace = true } tokio-cron-scheduler = { workspace = true } tower-http = { workspace = true, features = ["trace"] } tower = { workspace = true } +hyper = { workspace = true } domain = { path = "../domain" } -i18n = { path = "../i18n" } \ No newline at end of file +i18n = { path = "../i18n" } +macro = { path = "../macro" } \ No newline at end of file diff --git a/library/src/middleware/req_ctx.rs b/library/src/middleware/req_ctx.rs index 7c6d1d4..97fb07b 100644 --- a/library/src/middleware/req_ctx.rs +++ b/library/src/middleware/req_ctx.rs @@ -9,7 +9,8 @@ use crate::{cache::account_cache::LOGIN_CACHE, config, context::Context, model:: const WHITE_LIST: &[(&str, &str)] = &[ ("POST", "/account/sys"), - ("POST", "/account/google") + ("POST", "/account/google"), + ("GET", "/wechat/access_token") ]; /// 认证中间件,包括网络请求白名单、token验证、登录缓存 diff --git a/library/src/social/wechat.rs b/library/src/social/wechat.rs index a7922bc..8ac15ee 100644 --- a/library/src/social/wechat.rs +++ b/library/src/social/wechat.rs @@ -4,7 +4,8 @@ use chrono::Utc; use futures_util::lock::Mutex; use hmac::{Hmac, Mac}; use lazy_static::lazy_static; -use serde::Deserialize; +use macros::Responsable; +use serde::{Deserialize, Serialize}; use sha2::Sha256; use crate::{ @@ -15,7 +16,14 @@ use crate::{ use super::SocialResult; lazy_static! { - pub static ref WECHAT_SOCIAL: WechatSocial = WechatSocial::default(); + pub static ref WECHAT_SOCIAL: WechatSocial = { + let wechat_option = &config!().social.wechat; + WechatSocial { + app_id: wechat_option.app_id.to_owned(), + app_secret: wechat_option.app_secret.to_owned(), + ..Default::default() + } + }; } #[derive(Debug, Clone)] @@ -25,7 +33,7 @@ pub struct WechatSocial { access_token: Arc>, } -#[derive(Deserialize, Debug, Clone, Default)] +#[derive(Deserialize, Serialize, Debug, Clone, Default, Responsable)] pub struct WeChatAccessToken { pub access_token: String, pub expires_in: i64, @@ -79,8 +87,7 @@ impl WechatSocial { /// /// https://developers.weixin.qq.com/minigame/dev/api-backend/open-api/access-token/auth.getAccessToken.html /// https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/mp-access-token/getAccessToken.html - #[allow(dead_code)] - async fn fetch_and_parse_wechat_access_token(&self) -> SocialResult { + pub async fn get_access_token(&self) -> SocialResult { let mut wechat_access_token = self.access_token.lock().await; if wechat_access_token.access_token.is_empty() && wechat_access_token.expires_in < Utc::now().timestamp() diff --git a/server/src/controller/mod.rs b/server/src/controller/mod.rs index 87770fa..f2742d5 100644 --- a/server/src/controller/mod.rs +++ b/server/src/controller/mod.rs @@ -3,6 +3,7 @@ use library::typed_router::TypedRouter; pub mod account_controller; pub mod feedback_controller; +pub mod social_wx_controller; pub fn init() -> Router { Router::new() @@ -14,4 +15,6 @@ pub fn init() -> Router { .typed_route(feedback_controller::add_feedback) .typed_route(feedback_controller::get_feedback_list_by_page) .typed_route(feedback_controller::get_feedback_list) + // 微信相关路由 + .typed_route(social_wx_controller::get_wechat_access_token) } diff --git a/server/src/controller/social_wx_controller.rs b/server/src/controller/social_wx_controller.rs new file mode 100644 index 0000000..9b7ce69 --- /dev/null +++ b/server/src/controller/social_wx_controller.rs @@ -0,0 +1,9 @@ +use library::{context::Context, model::response::ResResult, social::wechat::WeChatAccessToken}; +use macros::get; + +use crate::service::social_wx_service; + +#[get("/wechat/access_token")] +pub async fn get_wechat_access_token(context: Context) -> ResResult { + social_wx_service::get_wechat_access_token(context).await +} \ No newline at end of file diff --git a/server/src/service/mod.rs b/server/src/service/mod.rs index 1301dbe..e51550d 100644 --- a/server/src/service/mod.rs +++ b/server/src/service/mod.rs @@ -1,3 +1,4 @@ pub mod account_service; pub mod feedback_service; pub mod sys_account_service; +pub mod social_wx_service; diff --git a/server/src/service/social_service.rs b/server/src/service/social_service.rs deleted file mode 100644 index 96ea1ab..0000000 --- a/server/src/service/social_service.rs +++ /dev/null @@ -1,2 +0,0 @@ -/// 用于接收各个服务平台的回调、消息推送 - diff --git a/server/src/service/social_wx_service.rs b/server/src/service/social_wx_service.rs new file mode 100644 index 0000000..14e61be --- /dev/null +++ b/server/src/service/social_wx_service.rs @@ -0,0 +1,15 @@ +use i18n::{message, message_ids::MessageId}; +use library::{context::Context, model::response::{ResErr, ResResult}, social::wechat::{WeChatAccessToken, WECHAT_SOCIAL}}; + + +pub async fn get_wechat_access_token(context: Context) -> ResResult { + let lang_tag = context.get_lang_tag(); + let access_token = match WECHAT_SOCIAL.get_access_token().await { + Ok(access_token) => access_token, + Err(err) => { + tracing::error!("获取微信access_token失败,err:{}", err); + return Err(ResErr::service(message!(lang_tag, MessageId::FailedGetWxAaccessToken))); + } + }; + Ok(access_token) +} \ No newline at end of file