Skip to content

Commit

Permalink
optimize(backend): 优化存储在数据库的认证信息
Browse files Browse the repository at this point in the history
  • Loading branch information
yansongda committed May 15, 2024
1 parent 6a6a0ed commit 5296aa3
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 13 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## v1.5.2

### optimized

- optimize(backend): 优化存储在数据库的认证信息(#70)

## v1.5.1

### chore
Expand Down
12 changes: 12 additions & 0 deletions src/model/miniprogram/access_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use chrono::{DateTime, Local};
use serde::{Deserialize, Serialize};
use sqlx::types::Json;
use sqlx::FromRow;
use crate::model::wechat::LoginResponse;

#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct AccessToken {
Expand All @@ -17,4 +18,15 @@ pub struct AccessToken {
pub struct AccessTokenData {
pub open_id: String,
pub session_key: String,
pub union_id: Option<String>,
}

impl From<LoginResponse> for AccessTokenData {
fn from(response: LoginResponse) -> Self {
AccessTokenData {
open_id: response.openid.unwrap(),
session_key: response.session_key.unwrap(),
union_id: response.unionid,
}
}
}
2 changes: 1 addition & 1 deletion src/model/wechat.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use serde::Deserialize;

#[derive(Debug, Deserialize)]
#[derive(Debug, Clone, Deserialize)]
pub struct LoginResponse {
pub session_key: Option<String>,
pub unionid: Option<String>,
Expand Down
11 changes: 2 additions & 9 deletions src/service/miniprogram/access_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,14 @@ use crate::service::wechat;
pub async fn login(code: &str) -> Result<AccessToken> {
let wechat_response = wechat::login(code).await?;
let open_id = wechat_response.openid.unwrap();
let session_key = wechat_response.session_key.unwrap();
let user_id = get_login_user_id(open_id.as_str()).await?;

let exist = miniprogram::access_token::fetch_by_user_id(user_id).await;

if exist.is_ok() {
return miniprogram::access_token::update(
exist.unwrap().id,
AccessTokenData {
open_id,
session_key,
},
AccessTokenData::from(wechat_response.clone()),
)
.await;
}
Expand All @@ -26,10 +22,7 @@ pub async fn login(code: &str) -> Result<AccessToken> {
Error::AccessTokenNotFound(_) => {
miniprogram::access_token::insert(
user_id,
AccessTokenData {
open_id,
session_key,
},
AccessTokenData::from(wechat_response.clone()),
)
.await
}
Expand Down
6 changes: 3 additions & 3 deletions src/util/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ pub async fn request(request: Request) -> Result<HttpResponse> {
.unwrap()
});

info!("发送 http 请求 {:?}", request);
info!("请求第三方服务接口 {:?}", request);

let started_at = std::time::Instant::now();
let response = client.execute(request).await.map_err(|e| {
warn!("发送 http 请求失败 {:?}", e);
warn!("请求第三方服务接口失败 {:?}", e);

Error::Http(None)
})?;
Expand All @@ -43,7 +43,7 @@ pub async fn request(request: Request) -> Result<HttpResponse> {
duration: started_at.elapsed().as_secs_f32(),
};

info!("发送 http 请求结果 {:?}", result);
info!("请求第三方服务接口结果 {:?}", result);

Ok(result)
}

0 comments on commit 5296aa3

Please sign in to comment.