use serde::{Serialize, Deserialize};
use crate::{session::SessionStore, request::{RequestType}, WechatCommonResponse, LabradorResult};
use crate::wechat::miniapp::constants::{APPID, AUTHORIZATION_CODE, GRANT_TYPE, JS_CODE, SECRET};
use crate::wechat::miniapp::method::WechatMaMethod;
use crate::wechat::miniapp::WechatMaClient;
#[derive(Debug, Clone)]
pub struct WechatMaCodeSession<'a, T: SessionStore> {
client: &'a WechatMaClient<T>,
}
#[allow(unused)]
impl<'a, T: SessionStore> WechatMaCodeSession<'a, T> {
#[inline]
pub fn new(client: &WechatMaClient<T>) -> WechatMaCodeSession<T> {
WechatMaCodeSession {
client,
}
}
pub async fn jscode_2_session(&self, code: &str) -> LabradorResult<JsCodeSession> {
let v = self.client.get(WechatMaMethod::CodeSession, vec![
(GRANT_TYPE.to_string(), AUTHORIZATION_CODE.to_string()),
(JS_CODE.to_string(), code.to_string()),
(APPID.to_string(), self.client.appid.to_string()),
(SECRET.to_string(), self.client.secret.to_string()),
], RequestType::Json).await?.json::<serde_json::Value>()?;
WechatCommonResponse::parse::<JsCodeSession>(v)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct JsCodeSession {
pub openid: String,
pub session_key: String,
pub unionid: Option<String>,
}