| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- pub mod user;
- pub mod area;
- pub mod auth;
- pub mod device;
- pub mod flow_task;
- pub mod ability;
- #[cfg(target_arch = "x86_64")]
- #[cfg(target_os = "windows")]
- mod code_helper;
- use rand::Rng;
- use crate::{datasource::Datasource, log, LogLevel::*};
- #[derive(serde::Deserialize)]
- pub struct Ident{
- token: String
- }
- #[derive(serde::Serialize)]
- pub struct JsonBack{
- pub errcode: i16,
- #[serde(skip_serializing_if = "Option::is_none")]
- pub errmsg: Option<String>
- }
- #[derive(serde::Serialize)]
- pub struct DataBack<T>{
- pub errcode: i16,
- #[serde(skip_serializing_if = "Option::is_none")]
- pub errmsg: Option<String>,
- #[serde(skip_serializing_if = "Option::is_none")]
- pub data: Option<T>
- }
- #[derive(serde::Deserialize)]
- pub struct Page{
- page: usize,
- size: usize
- }
- pub fn token_fail() ->axum::Json<JsonBack> {axum::Json(JsonBack{
- errcode: 2000,
- errmsg: Some("鉴权失败: token无效".to_string())
- })}
- pub fn errcode0() ->axum::Json<JsonBack> {axum::Json(JsonBack{
- errcode: 0,
- errmsg: None
- })}
- #[allow(dead_code)]
- pub async fn example(axum::extract::State(_): axum::extract::State<crate::AppState>) -> axum::Json<JsonBack> {
- axum::Json(JsonBack{
- errcode: 0,
- errmsg: None
- })
- }
- pub async fn check_token(state: &crate::AppState, token: String) -> Result<(u64,String),()>{
- state.db_lite.query(
- "select id,mqid from user where token=? and isdelete=0",
- [token.clone()], // 这里不能写作 rusqlite::params![token]
- |r|{Ok((r.get::<usize,u64>(0)?,r.get::<usize,String>(1)?))}).await.map_err(|e| log(Warning,format!("{token},{e}")))
- }
- pub async fn check_openid(state: &crate::AppState, openid: String) -> Result<(u64,String),()>{
- state.db_lite.query(
- "select id,mqid from user where openid=? and isdelete=0",
- [openid], // 这里不能写作 rusqlite::params![token]
- |r|{Ok((r.get::<usize,u64>(0)?,r.get::<usize,String>(1)?))}).await.map_err(|e| println!("{e}"))
- }
- pub fn token(s: usize) -> String{
- rand::rng().sample_iter(&rand::distr::Alphanumeric).take(s).map(char::from).collect()
- }
- pub fn md5(payload: String) -> String{
- format!("{:x}",md5::compute(payload))
- }
- #[cfg(test)]
- mod tests{
- #[test]
- fn test_token(){
- use super::token;
- println!("{}",token(32));
- }
- #[test]
- fn test_md5(){
- use super::md5;
- println!("{}",md5("123456".to_string()));
- }
- }
|