br_addon/
action.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
use std::any::type_name;
use br_fields::Field;
use json::{array, object, JsonValue};
use br_web_server::request::{ContentType, Method, Request};
use crate::{ApiResponse, Tools};
use crate::PLUGIN_TOOLS;

/// 功能接口
pub trait Action {
    /// 功能名称
    fn name(&self) -> String {
        type_name::<Self>().rsplit("::").next().unwrap().to_lowercase()
    }
    /// API 名称
    fn api(&self) -> String {
        let t = type_name::<Self>().split("::").collect::<Vec<&str>>();
        let plugin = t[2].to_lowercase();
        let module = t[3].to_lowercase();
        let action = t[4].to_lowercase();
        format!("{plugin}.{module}.{action}")
    }
    /// 是否需要密钥
    fn token(&self) -> bool { false }
    /// 版本号
    fn version(&self) -> &'static str { "v1" }
    /// 功能标题
    fn title(&self) -> &'static str;
    /// 功能描述
    fn description(&self) -> &'static str { "" }
    /// 归属标签
    fn tags(&self) -> &'static [&'static str] { &[] }

    /// 是否公开
    fn public(&mut self) -> bool { true }
    /// 是否权限组显示
    fn auth(&mut self) -> bool { false }
    /// 接口类型 btn api menu
    fn interface_type(&mut self) -> InterfaceType {
        InterfaceType::API
    }
    /// 允许的请求类型
    fn method(&mut self) -> Method {
        Method::GET
    }
    /// 请求类型
    fn content_type(&mut self) -> ContentType {
        ContentType::Json
    }
    /// 请求参数
    fn params(&mut self) -> JsonValue {
        object! {}
    }
    /// 成功响应
    fn success(&mut self) -> ApiResponse {
        let mut data = object! {};
        data["code"] = br_fields::int::Int::new(true, "code", "编号", 10, 0).example(0.into()).swagger();
        data["message"] = br_fields::str::Str::new(true, "message", "成功消息", 256, "").example("成功".into()).swagger();
        data["data"] = br_fields::text::Json::new(true, "data", "返回数据", object! {}).swagger();
        data["success"] = br_fields::int::Switch::new(true, "success", "成功状态", true).example(true.into()).swagger();
        ApiResponse::success(data, "请求成功")
    }
    /// 失败响应
    fn error(&mut self) -> ApiResponse {
        let mut data = object! {};
        data["code"] = br_fields::int::Int::new(true, "code", "编号", 10, 1000).example(1000.into()).swagger();
        data["message"] = br_fields::str::Str::new(true, "message", "错误消息", 256, "").example("失败".into()).swagger();
        data["data"] = br_fields::text::Json::new(true, "data", "返回数据", object! {}).swagger();
        data["success"] = br_fields::int::Switch::new(true, "success", "成功状态", false).example(false.into()).swagger();
        ApiResponse::error(data, "请求失败")
    }
    /// 外部入口
    fn run(&mut self, mut request: Request) -> Result<ApiResponse, ApiResponse> {
        if self.method().str().to_lowercase() != request.method.str().to_lowercase() {
            return Err(ApiResponse::fail(-1, format!("Request type error: Actual [{}] Expected [{}]", request.method.str(), self.method().str()).as_str()));
        }
        self.check(&mut request.body.content)?;
        let res = self.index(request.clone());
        match res.success {
            true => Ok(res),
            false => Err(res)
        }
    }
    /// 入参验证
    fn check(&mut self, request: &mut JsonValue) -> Result<(), ApiResponse> {
        let params = self.params();
        let req = request.clone();
        for (name, _) in req.entries() {
            if !params.has_key(name) {
                request.remove(name);
            }
        }
        for (name, field) in params.entries() {
            let require = field["require"].as_bool().unwrap_or(false);
            if request.has_key(name) {
                // 判断输入类型
                match field["mode"].as_str().unwrap() {
                    "int" => {
                        if !request[name].is_number() {
                            return Err(ApiResponse::fail(-1, format!("请求参数数据类型错误: 参数 [{}] 数据类型应为[{}]", name, field["mode"]).as_str()));
                        }
                    }
                    "string" => {
                        if !request[name].is_string() {
                            return Err(ApiResponse::fail(-1, format!("请求参数数据类型错误: 参数 [{}] 数据类型应为[{}]", name, field["mode"]).as_str()));
                        }
                        if require && request[name].is_empty() {
                            return Err(ApiResponse::fail(-1, format!("请求参数数据类型错误: 参数 [{}] 不能为空", name).as_str()));
                        }
                    }
                    "switch" => {
                        if !request[name].is_boolean() {
                            return Err(ApiResponse::fail(-1, format!("请求参数数据类型错误: 参数 [{}] 数据类型应为[{}]", name, field["mode"]).as_str()));
                        }
                    }
                    "select" => {
                        if !request[name].is_array() && !request[name].is_string() {
                            return Err(ApiResponse::fail(-1, format!("请求参数数据类型错误: 参数 [{}] 数据类型应为[{}]", name, field["mode"]).as_str()));
                        }
                        let option = field["option"].members().map(|m| m.as_str().unwrap_or("")).collect::<Vec<&str>>().join(",");
                        if request[name].is_string() && !field["option"].contains(request[name].as_str().unwrap_or("")) {
                            return Err(ApiResponse::fail(-1, format!("请求参数选项错误: 参数 [{}] 数据类型应为[{}]之内", name, option).as_str()));
                        }
                        if request[name].is_array() {
                            let res1 = request[name].members().map(|m| m.as_str().unwrap_or("")).collect::<Vec<&str>>();
                            let diff = res1
                                .iter()
                                .filter(|&item| !field["option"].members().map(|m| m.as_str().unwrap_or("")).collect::<Vec<&str>>().contains(item))
                                .collect::<Vec<&&str>>();
                            if !diff.is_empty() {
                                return Err(ApiResponse::fail(-1, format!("请求参数选项错误: 参数 [{}] 数据 {:?} 应为[{:?}]范围之内", name, diff, option).as_str()));
                            }
                        }
                    }
                    "radio" => {
                        if !request[name].is_string() {
                            return Err(ApiResponse::fail(-1, format!("请求参数数据类型错误: 参数 [{}] 数据类型应为[{}] 实际为[{}]", name, field["mode"], request[name]).as_str()));
                        }
                        let option = field["option"].members().map(|m| m.as_str().unwrap_or("")).collect::<Vec<&str>>().join(",");
                        if request[name].is_string() && !field["option"].contains(request[name].as_str().unwrap_or("")) {
                            return Err(ApiResponse::fail(-1, format!("请求参数选项错误: 参数 [{}] 数据 [{}] 应为 [{}] 之一", name, request[name], option).as_str()));
                        }
                    }
                    _ => {
                        // println!("未知类型: {}", field["mode"])
                    }
                }
            } else {
                if require {
                    return Err(ApiResponse::fail(-1, format!("请求参数错误: 参数[{}]必填", name).as_str()));
                }
                request[name] = field["def"].clone();
            }
        }
        Ok(())
    }
    /// 内部入口
    fn index(&mut self, request: Request) -> ApiResponse;
    /// 表格接收参数
    fn params_table_list(&mut self) -> JsonValue {
        let mut params = object! {};
        params["page"] = br_fields::int::Int::new(false, "page", "页数", 10, 1).field();
        params["limit"] = br_fields::int::Int::new(false, "limit", "每页行数", 10, 10).field();
        params["first"] = br_fields::int::Switch::new(false, "first", "首次", true).field();
        params["sorts"] = br_fields::text::Object::new(false, "sorts", "排序", object! {}).field();
        params["where_or"] = br_fields::text::Object::new(false, "where_or", "查询条件", object! {}).field();
        params["where_and"] = br_fields::text::Object::new(false, "where_and", "查询条件", array![]).field();
        params
    }
    /// 列表表格渲染
    #[cfg(any(feature = "sqlite", feature = "mssql", feature = "mysql"))]
    fn table_list(&mut self, request: JsonValue, table_name: &str, fields: JsonValue) -> (i64, JsonValue) {
        let page = request["page"].as_i32().unwrap_or(1);
        let limit = request["limit"].as_i32().unwrap_or(10);
        let _first = request["first"].as_bool().unwrap_or(false);
        let sorts = request["sorts"].clone();
        let where_or = request["where_or"].clone();
        let where_and = request["where_and"].clone();


        let fields_key = fields.entries().map(|(field, _)| field).collect::<Vec<&str>>().join(",");


        let mut tools = self.tools();
        let db = tools.db.table(table_name);

        for (key, value) in where_or.entries() {
            if value.is_empty() {
                continue;
            }
            if value.is_array() {
                db.where_or(key, "between", value.clone());
                db.where_or(key, "in", value.clone());
            } else if value.is_boolean() {
                db.where_or(key, "=", value.clone());
            } else {
                db.where_or(key, "like", format!("%{}%", value).into());
            }
        }

        for value in where_and.members() {
            db.where_and(value[0].as_str().unwrap(), value[1].as_str().unwrap(), value[2].clone());
        }

        let total = (db.clone().count().as_f64().unwrap() / limit as f64).ceil() as i64;
        let mut db_list = db.clone();

        for (key, sort) in sorts.entries() {
            db_list.order(key, sort.as_bool().unwrap());
        }
        let data = db_list.field(fields_key.as_str()).page(page, limit).select();
        (total, data)
    }
    /// 使用工具
    fn tools(&mut self) -> Tools {
        let tools = PLUGIN_TOOLS.lock().unwrap();
        let tools = tools.get("tools").unwrap().clone();
        tools
    }
    /// 按钮信息
    /// * cnd 显示条件 vec[vec["xxx","=","yyy"]]
    fn btn_info(&mut self, btn_type: BtnType, btn_color: BtnColor, title: &str, desc: &str, url: &str, cnd: Vec<JsonValue>) -> JsonValue {
        object! {
            api:self.api().clone(),
            title:if title.is_empty() {self.title()}else{title},
            desc:desc,
            btn_type:btn_type.str(),
            btn_color:btn_color.str(),
            cnd:cnd,
            url:url,
            path:self.api().replace(".","/")
        }
    }
}
/// 接口类型
#[derive(Debug, Clone)]
pub enum InterfaceType {
    API,
    BTN,
    MENU,
}

impl InterfaceType {
    pub fn str(self) -> String {
        match self {
            InterfaceType::API => "api",
            InterfaceType::BTN => "btn",
            InterfaceType::MENU => "menu"
        }.to_string()
    }
}


/// 按钮类型
#[derive(Debug, Clone)]
pub enum BtnType {
    /// 表单
    Form,
    /// 表单下载
    FormDownload,
    /// 外部跳转
    Url,
    /// api请求
    Api,
    /// 下载
    Download,
    /// 内部跳转
    Path,
}

impl BtnType {
    fn str(self) -> String {
        match self {
            BtnType::Form => "form",
            BtnType::FormDownload => "form_download",
            BtnType::Api => "api",
            BtnType::Download => "download",
            BtnType::Url => "url",
            BtnType::Path => "path",
        }.to_string()
    }
}
/// 按钮颜色
#[derive(Debug, Clone)]
pub enum BtnColor {
    Primary,
    Red,
    Blue,
    Yellow,
    Green,
}

impl BtnColor {
    fn str(self) -> String {
        match self {
            BtnColor::Primary => "primary",
            BtnColor::Red => "negative",
            BtnColor::Blue => "info",
            BtnColor::Yellow => "warning",
            BtnColor::Green => "positive",
        }.to_string()
    }
}