Skip to content

Commit

Permalink
feat: impl sned image
Browse files Browse the repository at this point in the history
  • Loading branch information
lich0821 committed Feb 5, 2024
1 parent 156b57c commit d8ef3e7
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
45 changes: 42 additions & 3 deletions src-tauri/src/endpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ use warp::{
};

use crate::wcferry::{
wcf::{DbNames, DbTable, DbTables, MsgTypes, RpcContact, RpcContacts, TextMsg, UserInfo},
wcf::{
DbNames, DbTable, DbTables, MsgTypes, PathMsg, RpcContact, RpcContacts, TextMsg, UserInfo,
},
WeChat,
};

Expand Down Expand Up @@ -46,9 +48,9 @@ pub fn get_routes(

#[derive(OpenApi)]
#[openapi(
paths(is_login, get_self_wxid, get_user_info, get_contacts, get_dbs, get_tables, get_msg_types, refresh_pyq, send_text),
paths(is_login, get_self_wxid, get_user_info, get_contacts, get_dbs, get_tables, get_msg_types, refresh_pyq, send_text, send_image),
components(schemas(
ApiResponse<bool>, ApiResponse<String>, UserInfo, RpcContacts, RpcContact, DbNames, DbTables, DbTable, MsgTypes, TextMsg
ApiResponse<bool>, ApiResponse<String>, UserInfo, RpcContacts, RpcContact, DbNames, DbTables, DbTable, MsgTypes, TextMsg, PathMsg
)),
tags((name = "WCF", description = "玩微信的接口"))
)]
Expand Down Expand Up @@ -140,6 +142,16 @@ pub fn get_routes(
.and_then(send_text)
}

fn sendimage(
wechat: Arc<Mutex<WeChat>>,
) -> impl Filter<Extract = (impl Reply,), Error = Rejection> + Clone {
warp::path!("image")
.and(warp::post())
.and(warp::body::json())
.and(warp::any().map(move || wechat.clone()))
.and_then(send_image)
}

api_doc
.or(swagger_ui)
.or(islogin(wechat.clone()))
Expand All @@ -151,6 +163,7 @@ pub fn get_routes(
.or(msgtypes(wechat.clone()))
.or(pyq(wechat.clone()))
.or(sendtext(wechat.clone()))
.or(sendimage(wechat.clone()))
}

async fn serve_swagger(
Expand Down Expand Up @@ -414,3 +427,29 @@ pub async fn send_text(text: TextMsg, wechat: Arc<Mutex<WeChat>>) -> Result<Json
};
Ok(warp::reply::json(&rsp))
}

#[utoipa::path(
post,
tag = "WCF",
path = "/image",
request_body = PathMsg,
responses(
(status = 200, body = ApiResponseBool, description = "发送图片消息")
)
)]
pub async fn send_image(image: PathMsg, wechat: Arc<Mutex<WeChat>>) -> Result<Json, Infallible> {
let wechat = wechat.lock().unwrap();
let rsp = match wechat.clone().send_image(image) {
Ok(status) => ApiResponse {
status: 0,
error: None,
data: Some(status),
},
Err(error) => ApiResponse {
status: 1,
error: Some(error.to_string()),
data: None,
},
};
Ok(warp::reply::json(&rsp))
}
23 changes: 23 additions & 0 deletions src-tauri/src/wcferry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,4 +514,27 @@ impl WeChat {
}
};
}

pub fn send_image(self, img: wcf::PathMsg) -> Result<bool, Box<dyn std::error::Error>> {
let req = wcf::Request {
func: wcf::Functions::FuncSendImg.into(),
msg: Some(wcf::request::Msg::File(img)),
};
let rsp = match self.send_cmd(req) {
Ok(res) => res,
Err(e) => {
error!("发送图片消息命令发送失败: {}", e);
return Err("发送图片消息命令发送失败".into());
}
};

match rsp.unwrap() {
wcf::response::Msg::Status(status) => {
return Ok(status == 0);
}
_ => {
return Err("发送图片消息失败".into());
}
};
}
}

0 comments on commit d8ef3e7

Please sign in to comment.