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
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;
    /// 动作api
    fn api(&mut self) -> &'static str;

    /// 支持模式
    /// org 企业模式
    /// admin 平台模式
    /// user 普通模式
    fn mode(&mut self) -> Vec<&'static str> {
        return vec![];
    }
    /// 菜单
    fn menu(&mut self) -> bool { return false; }
    /// 菜单 icon
    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 }
    /// API描述
    fn describe(&mut self) -> &'static str { return ""; }
    /// 是否公开
    fn public(&mut self) -> bool { true }
    /// 是否权限组显示
    fn auth(&mut self) -> bool { true }
    /// 接口类型 btn api
    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()
    }
    /// 获取共享消息通道
    /// * org_org 接收的企业
    /// * user_user 接收的用户
    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()
    }
    /// 获取菜单信息
    /// * title 菜单名称
    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
    }
    /// 获取按钮信息
    /// * title 菜单名称
    /// * mode 按钮模式
    /// * color 按钮颜色
    /// * match_condition 显示条件
    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;
    }
    /// 获取跳转按钮信息
    /// * title 菜单名称
    /// * mode 按钮模式
    /// * color 按钮颜色
    /// * match_condition 显示条件
    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;
    }
    /// 获取自定义页面按钮信息
    /// * title 菜单名称
    /// * mode 按钮模式
    /// * color 按钮颜色
    /// * match_condition 显示条件
    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");
    }
}