对接微信小程序和小游戏登录
This commit is contained in:
parent
c2d249d72b
commit
0d9647535f
@ -1,3 +1,4 @@
|
|||||||
pub mod account;
|
pub mod account;
|
||||||
pub mod feedback;
|
pub mod feedback;
|
||||||
pub mod pageable;
|
pub mod pageable;
|
||||||
|
pub mod social_wx;
|
11
domain/src/dto/social_wx.rs
Normal file
11
domain/src/dto/social_wx.rs
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use validator::Validate;
|
||||||
|
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize, Validate)]
|
||||||
|
pub struct WxMinAppLogin {
|
||||||
|
/// 微信code
|
||||||
|
#[validate(required(message = "ValidateWxMinAppLoginCodeRequired"), length(min = 1, message = "ValidateWxMinAppLoginCodeRequired"))]
|
||||||
|
pub code: Option<String>,
|
||||||
|
}
|
2
i18n.csv
2
i18n.csv
@ -15,3 +15,5 @@ ValidatePageablePageSizeRequired,invalid quantity per page,每页数量无效
|
|||||||
BadRequest,bad request,无效请求
|
BadRequest,bad request,无效请求
|
||||||
InvalidParams,invalid params,无效参数
|
InvalidParams,invalid params,无效参数
|
||||||
FailedGetWxAaccessToken,Failed to get WeChat access_token,获取微信access_token失败
|
FailedGetWxAaccessToken,Failed to get WeChat access_token,获取微信access_token失败
|
||||||
|
FailedWeChatLogin,Failed to check WeChat login code,微信登录失败
|
||||||
|
ValidateWxMinAppLoginCodeRequired,login code is required,微信登陆code不能为空
|
|
@ -33,9 +33,13 @@ pub enum MessageId {
|
|||||||
ValidatePageablePageRequired,
|
ValidatePageablePageRequired,
|
||||||
/// 每页数量无效
|
/// 每页数量无效
|
||||||
ValidatePageablePageSizeRequired,
|
ValidatePageablePageSizeRequired,
|
||||||
|
/// 无效的微信code
|
||||||
|
ValidateWxMinAppLoginCodeRequired,
|
||||||
|
|
||||||
/// social begin
|
/// social begin
|
||||||
|
|
||||||
/// 获取微信access_token失败
|
/// 获取微信access_token失败
|
||||||
FailedGetWxAaccessToken,
|
FailedGetWxAaccessToken,
|
||||||
|
/// 微信登录失败,校验code失败
|
||||||
|
FailedWeChatLogin,
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,8 @@ use crate::{cache::account_cache::LOGIN_CACHE, config, context::Context, model::
|
|||||||
const WHITE_LIST: &[(&str, &str)] = &[
|
const WHITE_LIST: &[(&str, &str)] = &[
|
||||||
("POST", "/account/sys"),
|
("POST", "/account/sys"),
|
||||||
("POST", "/account/google"),
|
("POST", "/account/google"),
|
||||||
("GET", "/wechat/access_token")
|
("GET", "/wechat/access_token"),
|
||||||
|
("POST", "/wechat/code_2_session"),
|
||||||
];
|
];
|
||||||
|
|
||||||
/// 认证中间件,包括网络请求白名单、token验证、登录缓存
|
/// 认证中间件,包括网络请求白名单、token验证、登录缓存
|
||||||
|
@ -47,20 +47,32 @@ pub struct WeChatBaseResult {
|
|||||||
pub errmsg: String,
|
pub errmsg: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Debug, Clone)]
|
/// 微信登录登录
|
||||||
|
///
|
||||||
|
/// 登录成功
|
||||||
|
/// ```
|
||||||
|
/// {
|
||||||
|
/// "openid": "odbV75XGs-Lwj0CmOxwIXjdDfVEY",
|
||||||
|
/// "session_key": "iM6cs8nhw0VtAty16RjswQ==",
|
||||||
|
/// "unionid": null,
|
||||||
|
/// "errcode": null,
|
||||||
|
/// "errmsg": null
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
#[derive(Deserialize, Serialize, Debug, Clone, Responsable)]
|
||||||
pub struct MiniAppLoginResult {
|
pub struct MiniAppLoginResult {
|
||||||
// 用户唯一标识
|
// 用户唯一标识
|
||||||
pub openid: String,
|
pub openid: Option<String>,
|
||||||
// 会话密钥
|
// 会话密钥
|
||||||
pub session_key: String,
|
pub session_key: Option<String>,
|
||||||
// 用户在开放平台的唯一标识符,若当前小程序已绑定到微信开放平台账号下会返回
|
// 用户在开放平台的唯一标识符,若当前小程序已绑定到微信开放平台账号下会返回
|
||||||
// 如果开发者拥有多个移动应用、网站应用、和公众账号(包括小程序),可通过 UnionID 来区分用户的唯一性
|
// 如果开发者拥有多个移动应用、网站应用、和公众账号(包括小程序),可通过 UnionID 来区分用户的唯一性
|
||||||
// https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/union-id.html
|
// https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/union-id.html
|
||||||
pub unionid: String,
|
pub unionid: Option<String>,
|
||||||
// 错误码
|
// 错误码
|
||||||
pub errcode: i64,
|
pub errcode: Option<i64>,
|
||||||
// 错误信息
|
// 错误信息
|
||||||
pub errmsg: String,
|
pub errmsg: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for WechatSocial {
|
impl Default for WechatSocial {
|
||||||
@ -122,18 +134,26 @@ impl WechatSocial {
|
|||||||
))
|
))
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let result: MiniAppLoginResult = response.json().await.unwrap();
|
let result: MiniAppLoginResult = match response.json().await {
|
||||||
if result.errcode != 0 {
|
Ok(result) => result,
|
||||||
|
Err(err) => {
|
||||||
|
tracing::error!("微信登录失败,err:{:?}", err);
|
||||||
|
return Err(Box::new(ResErr::social(format!("微信登录失败,err:{}", err))));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
if let Some(errcode) = result.errcode {
|
||||||
|
if errcode != 0 {
|
||||||
tracing::error!(
|
tracing::error!(
|
||||||
"微信登录失败,errcode:{},errmsg:{}",
|
"微信登录失败,errcode:{:?},errmsg:{:?}",
|
||||||
result.errcode,
|
result.errcode,
|
||||||
result.errmsg
|
result.errmsg
|
||||||
);
|
);
|
||||||
return Err(Box::new(ResErr::social(format!(
|
return Err(Box::new(ResErr::social(format!(
|
||||||
"微信登录失败,errcode:{},errmsg:{}",
|
"微信登录失败,errcode:{:?},errmsg:{:?}",
|
||||||
result.errcode, result.errmsg
|
result.errcode, result.errmsg
|
||||||
))));
|
))));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
Ok(result)
|
Ok(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,4 +17,5 @@ pub fn init() -> Router {
|
|||||||
.typed_route(feedback_controller::get_feedback_list)
|
.typed_route(feedback_controller::get_feedback_list)
|
||||||
// 微信相关路由
|
// 微信相关路由
|
||||||
.typed_route(social_wx_controller::get_wechat_access_token)
|
.typed_route(social_wx_controller::get_wechat_access_token)
|
||||||
|
.typed_route(social_wx_controller::code_2_session)
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
use library::{context::Context, model::response::ResResult, social::wechat::WeChatAccessToken};
|
use domain::dto::social_wx::WxMinAppLogin;
|
||||||
use macros::get;
|
use library::{context::Context, extractor::body_extractor::JsonBody, model::response::ResResult, social::wechat::{MiniAppLoginResult, WeChatAccessToken}};
|
||||||
|
use macros::{get, post};
|
||||||
|
|
||||||
use crate::service::social_wx_service;
|
use crate::service::social_wx_service;
|
||||||
|
|
||||||
@ -7,3 +8,11 @@ use crate::service::social_wx_service;
|
|||||||
pub async fn get_wechat_access_token(context: Context) -> ResResult<WeChatAccessToken> {
|
pub async fn get_wechat_access_token(context: Context) -> ResResult<WeChatAccessToken> {
|
||||||
social_wx_service::get_wechat_access_token(context).await
|
social_wx_service::get_wechat_access_token(context).await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[post("/wechat/code_2_session")]
|
||||||
|
pub async fn code_2_session(
|
||||||
|
context: Context,
|
||||||
|
JsonBody(mini_app_login): JsonBody<WxMinAppLogin>
|
||||||
|
) -> ResResult<MiniAppLoginResult> {
|
||||||
|
social_wx_service::code_2_session(context, mini_app_login.code.unwrap()).await
|
||||||
|
}
|
@ -1,6 +1,9 @@
|
|||||||
use i18n::{message, message_ids::MessageId};
|
use i18n::{message, message_ids::MessageId};
|
||||||
use library::{context::Context, model::response::{ResErr, ResResult}, social::wechat::{WeChatAccessToken, WECHAT_SOCIAL}};
|
use library::{
|
||||||
|
context::Context,
|
||||||
|
model::response::{ResErr, ResResult},
|
||||||
|
social::wechat::{MiniAppLoginResult, WeChatAccessToken, WECHAT_SOCIAL},
|
||||||
|
};
|
||||||
|
|
||||||
pub async fn get_wechat_access_token(context: Context) -> ResResult<WeChatAccessToken> {
|
pub async fn get_wechat_access_token(context: Context) -> ResResult<WeChatAccessToken> {
|
||||||
let lang_tag = context.get_lang_tag();
|
let lang_tag = context.get_lang_tag();
|
||||||
@ -8,8 +11,26 @@ pub async fn get_wechat_access_token(context: Context) -> ResResult<WeChatAccess
|
|||||||
Ok(access_token) => access_token,
|
Ok(access_token) => access_token,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
tracing::error!("获取微信access_token失败,err:{}", err);
|
tracing::error!("获取微信access_token失败,err:{}", err);
|
||||||
return Err(ResErr::service(message!(lang_tag, MessageId::FailedGetWxAaccessToken)));
|
return Err(ResErr::service(message!(
|
||||||
|
lang_tag,
|
||||||
|
MessageId::FailedGetWxAaccessToken
|
||||||
|
)));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
Ok(access_token)
|
Ok(access_token)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn code_2_session(context: Context, code: String) -> ResResult<MiniAppLoginResult> {
|
||||||
|
let lang_tag = context.get_lang_tag();
|
||||||
|
let result = match WECHAT_SOCIAL.code_2_session(&code).await {
|
||||||
|
Ok(result) => result,
|
||||||
|
Err(err) => {
|
||||||
|
tracing::error!("微信登录失败,err:{}", err);
|
||||||
|
return Err(ResErr::service(message!(
|
||||||
|
lang_tag,
|
||||||
|
MessageId::FailedWeChatLogin
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Ok(result)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user