添加微信获取access token接口
This commit is contained in:
parent
f003467909
commit
c2d249d72b
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -1121,9 +1121,11 @@ dependencies = [
|
||||
"http",
|
||||
"http-body",
|
||||
"http-body-util",
|
||||
"hyper",
|
||||
"i18n",
|
||||
"jsonwebtoken",
|
||||
"lazy_static",
|
||||
"macro",
|
||||
"moka",
|
||||
"once_cell",
|
||||
"reqwest",
|
||||
|
4
app.toml
4
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"
|
||||
|
3
i18n.csv
3
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,无效参数
|
||||
InvalidParams,invalid params,无效参数
|
||||
FailedGetWxAaccessToken,Failed to get WeChat access_token,获取微信access_token失败
|
|
@ -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,
|
||||
}
|
||||
|
@ -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" }
|
||||
i18n = { path = "../i18n" }
|
||||
macro = { path = "../macro" }
|
@ -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验证、登录缓存
|
||||
|
@ -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<Mutex<WeChatAccessToken>>,
|
||||
}
|
||||
|
||||
#[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<WeChatAccessToken> {
|
||||
pub async fn get_access_token(&self) -> SocialResult<WeChatAccessToken> {
|
||||
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()
|
||||
|
@ -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)
|
||||
}
|
||||
|
9
server/src/controller/social_wx_controller.rs
Normal file
9
server/src/controller/social_wx_controller.rs
Normal file
@ -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<WeChatAccessToken> {
|
||||
social_wx_service::get_wechat_access_token(context).await
|
||||
}
|
@ -1,3 +1,4 @@
|
||||
pub mod account_service;
|
||||
pub mod feedback_service;
|
||||
pub mod sys_account_service;
|
||||
pub mod social_wx_service;
|
||||
|
@ -1,2 +0,0 @@
|
||||
/// 用于接收各个服务平台的回调、消息推送
|
||||
|
15
server/src/service/social_wx_service.rs
Normal file
15
server/src/service/social_wx_service.rs
Normal file
@ -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<WeChatAccessToken> {
|
||||
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)
|
||||
}
|
Loading…
Reference in New Issue
Block a user