use json::{array, JsonValue, object};
use crate::{BtnColor, BtnMode, NotifyMode, PLUGIN_TOOLS, PLUGIN_WS_RX, PLUGIN_WS_TX, Response, Tools};
use crate::model::{ModelTemp};
pub trait Action {
fn author(&mut self) -> &'static str {
return "Blue Rain";
}
fn title(&mut self) -> &'static str;
fn api(&mut self) -> &'static str;
fn mode(&mut self) -> Vec<&'static str> {
return vec![];
}
fn menu(&mut self) -> bool { return false; }
fn icon(&mut self) -> &'static str { return ""; }
fn menu_sort(&mut self) -> usize { return 0; }
fn tags(&mut self) -> Vec<&'static str> { return vec![]; }
fn is_openapi(&mut self) -> bool { false }
fn token(&mut self) -> bool { true }
fn describe(&mut self) -> &'static str { return ""; }
fn public(&mut self) -> bool { true }
fn auth(&mut self) -> bool { true }
fn interface_type(&mut self) -> &'static str {
""
}
fn dependent(&mut self) -> Vec<&'static str> {
return vec![];
}
fn params(&mut self) -> JsonValue { object! {} }
fn _check(&mut self, mut request: JsonValue) -> (bool, String, JsonValue) {
let params = self.params();
let mut new_request = object! {};
for (key, field) in params.entries() {
match field["require"].as_bool().unwrap() {
true => {
if request[key].is_null() && !field["def"].is_empty() {
request[key] = field["def"].clone().into();
}
if request[key].is_null() {
return (false, format!("缺少参数 {}:{} 错误API:{}", key, field["title"], self.api()), request);
}
if request[key].is_string() && request[key] == "" {
return (false, format!("请填写【{}】内容 错误API:{}", field["title"], self.api()), request);
}
}
false => {
if request[key].is_null() {
request[key] = field["def"].clone().into();
}
if request[key] == "" || request[key].is_empty() {
request[key] = field["def"].clone().into();
}
if request[key] == JsonValue::from("null") {
request[key] = field["def"].clone().into();
}
}
}
new_request[key] = request[key].clone();
}
return (true, "验证通过".to_string(), new_request);
}
fn run(&mut self, header: JsonValue, request: JsonValue) -> Response {
let (state, msg, request) = self._check(request.clone());
if !state {
return self.fail(&*msg);
}
return self.index(header.clone(), request.clone());
}
fn index(&mut self, header: JsonValue, request: JsonValue) -> Response;
fn success(&mut self, data: JsonValue, msg: &str) -> Response {
Response::success(data, msg).clone()
}
fn fail(&mut self, msg: &str) -> Response {
Response::fail(msg).clone()
}
fn notify(&mut self, msg: &str, caption: &str, mode: NotifyMode, timeout: usize, path: &str) -> Response {
Response::notify(msg, caption, mode, timeout, path).clone()
}
fn login(&mut self, msg: &str) -> Response {
Response::login(msg).clone()
}
fn download(&mut self, filename: &str) -> Response {
Response::download(filename).clone()
}
fn redirect(&mut self, url: &str) -> Response {
Response::redirect(url).clone()
}
fn tools(&mut self) -> Tools {
PLUGIN_TOOLS.lock().unwrap().get("tools").unwrap().clone()
}
fn ws_send(&mut self, api: &str, mode: &str, org_org: &str, user_user: &str, msg: JsonValue) -> bool {
let tx = PLUGIN_WS_TX.lock().unwrap().get("wss").unwrap().clone();
let send = object! {
api:api.clone(),
mode:mode.clone(),
org_org:org_org.clone(),
user_user:user_user.clone(),
msg:msg.clone()
};
match tx.send(send) {
Ok(_) => true,
Err(_) => false
}
}
fn ws_rx(&self) -> JsonValue {
PLUGIN_WS_RX.lock().unwrap().get("wss").unwrap().clone().lock().unwrap().recv().unwrap()
}
fn menu_info(&mut self, title: &str) -> JsonValue {
let apis: Vec<&str> = self.api().split(".").collect();
let info = object! {
title:if title=="" {self.title()}else{title},
api:self.api(),
addon:apis[0],
model:apis[1],
action:apis[2],
icon:self.icon(),
path:format!("/{}",self.api().replace(".","/")),
auth:self.auth(),
tags:self.tags()
};
info
}
fn btn_info_data(&mut self, title: &str) -> JsonValue {
let mut params = array![];
for (_field, item) in self.params().entries() {
params.push(item.clone()).unwrap();
}
let apis: Vec<&str> = self.api().split(".").collect();
let info = object! {
title:if title=="" {self.title()}else{title},
api:self.api(),
addon:apis[0],
model:apis[1],
action:apis[2],
icon:self.icon(),
path:format!("/{}",self.api().replace(".","/")),
auth:self.auth(),
params:params
};
info
}
fn btn_info(&mut self, title: &str, mode: BtnMode, color: BtnColor, match_condition: Vec<Vec<&str>>) -> JsonValue {
let mut btn = object! {};
if title.is_empty() {
btn["title"] = self.title().into();
} else {
btn["title"] = title.into();
}
btn["api"] = self.api().into();
let mut params = array![];
for (_, item) in self.params().entries() {
params.push(item.clone()).unwrap();
}
btn["params"] = JsonValue::from(params);
btn["color"] = JsonValue::from(color.from());
btn["icon"] = JsonValue::from(self.icon());
btn["mode"] = JsonValue::from(mode.from());
btn["match_condition"] = match_condition.into();
return btn;
}
fn btn_info_to(&mut self, title: &str, path: &str, mode: BtnMode, color: BtnColor, match_condition: Vec<Vec<&str>>) -> JsonValue {
let mut btn = object! {};
if title.is_empty() {
btn["title"] = self.title().into();
} else {
btn["title"] = title.into();
}
btn["api"] = self.api().into();
let mut params = array![];
for (_, item) in self.params().entries() {
params.push(item.clone()).unwrap();
}
if path == "" {
btn["path"] = format!("/{}", self.api().replace(".", "/")).into();
} else {
btn["path"] = JsonValue::from(path);
}
btn["params"] = JsonValue::from(params);
btn["color"] = JsonValue::from(color.from());
btn["mode"] = JsonValue::from(mode.from());
btn["match_condition"] = match_condition.into();
return btn;
}
fn btn_custom_info(&mut self, title: &str) -> JsonValue {
let mut btn = object! {};
if title.is_empty() {
btn["title"] = self.title().into();
} else {
btn["title"] = title.into();
}
btn["api"] = self.api().into();
let mut params = array![];
for (_, item) in self.params().entries() {
params.push(item.clone()).unwrap();
}
btn["params"] = JsonValue::from(params);
btn["auth"] = self.auth().into();
return btn;
}
}
pub struct ActionTemp {
pub model: ModelTemp,
}
impl Action for ActionTemp {
fn title(&mut self) -> &'static str {
"测试api"
}
fn api(&mut self) -> &'static str { "addon_temp.model_temp.action_temp" }
fn index(&mut self, _header: JsonValue, _request: JsonValue) -> Response {
return self.fail("无效API");
}
}