刘光辉
13 小时以前 34981c30a78e8bbd7791131059a9210f9928b62c
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
package jnpf.base.controller;
 
import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jnpf.base.ActionResult;
import jnpf.base.SysConfigApi;
import jnpf.base.entity.EmailConfigEntity;
import jnpf.base.entity.SynThirdInfoEntity;
import jnpf.base.entity.SysConfigEntity;
import jnpf.base.model.synthirdinfo.DingTalkModel;
import jnpf.base.model.synthirdinfo.QyWebChatModel;
import jnpf.base.model.systemconfig.EmailTestForm;
import jnpf.base.model.systemconfig.PermissionSwitchModel;
import jnpf.base.model.systemconfig.SocialsSysVo;
import jnpf.base.model.systemconfig.SysConfigModel;
import jnpf.base.service.SynThirdInfoService;
import jnpf.base.service.SysconfigService;
import jnpf.config.ConfigValueUtil;
import jnpf.constant.MsgCode;
import jnpf.database.util.TenantDataSourceUtil;
import jnpf.flowable.WorkFlowApi;
import jnpf.message.util.SynThirdConsts;
import jnpf.model.BaseSystemInfo;
import jnpf.model.SocialsSysConfig;
import jnpf.permission.UserApi;
import jnpf.permission.model.user.form.UserUpAdminForm;
import jnpf.permission.model.user.vo.UserAdminVO;
import jnpf.util.JsonUtil;
import jnpf.util.NoDataSourceBind;
import jnpf.util.RandomUtil;
import jnpf.util.third.DingTalkUtil;
import jnpf.util.third.QyWebChatUtil;
import jnpf.util.wxutil.HttpUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
 
import java.util.*;
 
import static jnpf.constant.JnpfConst.SOCIALS_CONFIG;
 
/**
 * 系统配置
 *
 * @author JNPF开发平台组
 * @version V3.1.0
 * @copyright 引迈信息技术有限公司
 * @date 2019年9月27日 上午9:18
 */
@Tag(name = "系统配置", description = "SysConfig")
@RestController
@RequestMapping("/SysConfig")
@RequiredArgsConstructor
public class SysConfigController extends SuperController<SysconfigService, SysConfigEntity> implements SysConfigApi {
 
   
    private final SysconfigService sysconfigService;
   
    private final  ConfigValueUtil configValueUtil;
   
    private final  UserApi userService;
   
    private final  WorkFlowApi workFlowApi;
   
    private final  SynThirdInfoService synThirdInfoService;
 
    /**
     * 列表
     *
     * @return ignore
     */
    @Operation(summary = "列表")
    @GetMapping
    @SaCheckPermission(value = {"sysConfig.parameter", "sysConfig.strategy"}, mode = SaMode.OR)
    public ActionResult<SysConfigModel> list() {
        List<SysConfigEntity> list = sysconfigService.getList("SysConfig");
        HashMap<String, String> map = new HashMap<>(16);
        for (SysConfigEntity sys : list) {
            map.put(sys.getFkey(), sys.getValue());
        }
        SysConfigModel sysConfigModel = JsonUtil.getJsonToBean(map, SysConfigModel.class);
        return ActionResult.success(sysConfigModel);
    }
 
    /**
     * 获取权限配置开关
     *
     * @return 权限配置开关
     */
    @Operation(summary = "获取权限配置开关")
    @SaCheckLogin
    @GetMapping("/permissionSwitches")
    public ActionResult<PermissionSwitchModel> permissionSwitches() {
        Map<String, String> configMap = new HashMap<>(4);
        for (SysConfigEntity config : sysconfigService.getList("SysConfig")) {
            configMap.put(config.getFkey(), config.getValue());
        }
        PermissionSwitchModel model = new PermissionSwitchModel();
        model.setStandingSwitch(toSwitchValue(configMap.get("standingSwitch")));
        model.setOrgPositionRoleSwitch(toSwitchValue(configMap.get("orgPositionRoleSwitch")));
        model.setOrgPermissionSwitch(toSwitchValue(configMap.get("orgPermissionSwitch")));
        model.setPermissionGroupSwitch(toSwitchValue(configMap.get("permissionGroupSwitch")));
        return ActionResult.success(model);
    }
 
    private int toSwitchValue(String value) {
        return Objects.equals(value, "1") ? 1 : 0;
    }
 
    @Override
    @PostMapping("/updateById")
    public void updateById(@RequestBody SysConfigEntity configEntity) {
        sysconfigService.updateById(configEntity);
    }
 
    @Override
    @GetMapping("/getList")
    public List<SysConfigEntity> getList(@RequestParam String department) {
        LambdaQueryWrapper<SysConfigEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysConfigEntity::getCategory, SOCIALS_CONFIG);
        wrapper.eq(SysConfigEntity::getFkey, department);
        return sysconfigService.list(wrapper);
    }
 
    @Override
    @GetMapping("/getSocialsConfig")
    public SocialsSysConfig getSocialsConfig() {
        return sysconfigService.getSocialsConfig();
    }
 
    @Override
    @PostMapping("/create")
    public void create(SysConfigEntity sysConfigEntity) {
        sysconfigService.save(sysConfigEntity);
    }
 
    /**
     * 保存设置
     *
     * @param sysConfigModel 系统配置模型
     * @return ignore
     */
    @Operation(summary = "更新系统配置")
    @Parameter(name = "sysConfigModel", description = "系统模型", required = true)
    @SaCheckPermission(value = {"sysConfig.parameter", "sysConfig.strategy"}, mode = SaMode.OR)
    @PutMapping
    public ActionResult<Object> save(@RequestBody SysConfigModel sysConfigModel) {
        if (Objects.nonNull(sysConfigModel.getVerificationCodeNumber())) {
            if (sysConfigModel.getVerificationCodeNumber() > 6) {
                return ActionResult.fail(MsgCode.SYS029.get());
            }
            if (sysConfigModel.getVerificationCodeNumber() < 3) {
                return ActionResult.fail(MsgCode.SYS030.get());
            }
        }
        String flowTodo = sysconfigService.getValueByKey("flowTodo");
        if (ObjectUtil.equals(flowTodo, "1") && ObjectUtil.equals(sysConfigModel.getFlowTodo(), 0) && workFlowApi.checkTodo()) {
                return ActionResult.fail(MsgCode.WF141.get());
            }
 
        String flowSign = sysconfigService.getValueByKey("flowSign");
        if (ObjectUtil.equals(flowSign, "1") && ObjectUtil.equals(sysConfigModel.getFlowSign(), 0) && workFlowApi.checkSign()) {
                return ActionResult.fail(MsgCode.WF138.get());
            }
 
        if (sysConfigModel.getAddSignLevel() > 6 || sysConfigModel.getAddSignLevel() < 1) {
            return ActionResult.fail("加签层级的范围不在1-6之间");
        }
        List<SysConfigEntity> entitys = new ArrayList<>();
        Map<String, Object> map = JsonUtil.entityToMap(sysConfigModel);
        map.put("isLog", "1");
        map.put("sysTheme", "1");
        map.put("pageSize", "30");
        map.put("lastLoginTime", 1);
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            SysConfigEntity entity = new SysConfigEntity();
            entity.setId(RandomUtil.uuId());
            entity.setFkey(entry.getKey());
            entity.setValue(String.valueOf(entry.getValue()));
            entitys.add(entity);
        }
        sysconfigService.save(entitys);
        return ActionResult.success(MsgCode.SU005.get());
    }
 
    @Operation(summary = "获取第三方配置")
    @SaCheckPermission(value = {"sysConfig.parameter", "integrationCenter.dingTalk"}, mode = SaMode.OR)
    @GetMapping("/socials")
    public ActionResult<SocialsSysVo> getSocials() {
        SocialsSysConfig socialsConfig = sysconfigService.getSocialsConfig();
        SocialsSysVo vo = JsonUtil.getJsonToBean(socialsConfig, SocialsSysVo.class);
        List<SynThirdInfoEntity> qySynList = synThirdInfoService.getList(SynThirdConsts.THIRD_TYPE_QY, SynThirdConsts.DATA_TYPE_ORG);
        List<SynThirdInfoEntity> dingSynList = synThirdInfoService.getList(SynThirdConsts.THIRD_TYPE_DING, SynThirdConsts.DATA_TYPE_ORG);
        if (CollUtil.isNotEmpty(qySynList)) {
            vo.setQyhDisabled(true);
        }
        if (CollUtil.isNotEmpty(dingSynList)) {
            vo.setDingDisabled(true);
        }
        return ActionResult.success(vo);
    }
 
    @Operation(summary = "更新第三方配置")
    @Parameter(name = "SocialsSysConfig", description = "第三方参数模型", required = true)
    @SaCheckPermission(value = {"sysConfig.parameter", "integrationCenter.dingTalk"}, mode = SaMode.OR)
    @PutMapping("/socials")
    public ActionResult<Object> saveSocials(@RequestBody SocialsSysConfig sysConfigModel) {
        List<SysConfigEntity> entitys = new ArrayList<>();
        Map<String, Object> map = JsonUtil.entityToMap(sysConfigModel);
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            SysConfigEntity entity = new SysConfigEntity();
            entity.setId(RandomUtil.uuId());
            entity.setFkey(entry.getKey());
            entity.setValue(String.valueOf(entry.getValue()));
            entity.setCategory("SocialsConfig");
            entitys.add(entity);
        }
        sysconfigService.saveSocials(entitys);
        return ActionResult.success(MsgCode.SU005.get());
    }
 
    /**
     * 邮箱账户密码验证
     *
     * @param emailTestForm 邮箱测试模型
     * @return ignore
     */
    @Operation(summary = "邮箱连接测试")
    @Parameter(name = "emailTestForm", description = "邮箱测试模型", required = true)
    @SaCheckPermission(value = {"sysConfig.parameter", "sysConfig.strategy"}, mode = SaMode.OR)
    @PostMapping("/Email/Test")
    public ActionResult<Object> checkLogin(@RequestBody EmailTestForm emailTestForm) {
        EmailConfigEntity entity = JsonUtil.getJsonToBean(emailTestForm, EmailConfigEntity.class);
        entity.setEmailSsl(Integer.valueOf(emailTestForm.getSsl()));
        String result = sysconfigService.checkLogin(entity);
        if ("true".equals(result)) {
            return ActionResult.success(MsgCode.SU017.get());
        } else {
            return ActionResult.fail(result);
        }
    }
 
 
    //=====================================测试企业微信、钉钉的连接=====================================
 
    /**
     * 测试企业微信配置的连接功能
     *
     * @param type           0-发送消息,1-同步组织
     * @param qyWebChatModel 企业微信模型
     * @return ignore
     */
    @Operation(summary = "测试企业微信配置的连接")
     @Parameter(name = "type", description = "0-发送消息,1-同步组织", required = true)
    @Parameter(name = "qyWebChatModel", description = "企业微信模型", required = true)
    @SaCheckPermission("sysConfig.parameter")
    @PostMapping("{type}/testQyWebChatConnect")
    public ActionResult<Object> testQyWebChatConnect(@PathVariable("type") String type, @RequestBody @Valid QyWebChatModel qyWebChatModel) {
        JSONObject retMsg ;
        // 测试发送消息、组织同步的连接
        String corpId = qyWebChatModel.getQyhCorpId();
        String agentSecret = qyWebChatModel.getQyhAgentSecret();
        String corpSecret = qyWebChatModel.getQyhCorpSecret();
        // 测试发送消息的连接
        if ("0".equals(type)) {
            retMsg = QyWebChatUtil.getAccessToken(corpId, agentSecret);
            if (HttpUtil.isWxError(retMsg)) {
                return ActionResult.fail(MsgCode.SYS031.get(retMsg.getString("errmsg")));
            }
            return ActionResult.success(MsgCode.SYS032.get());
        } else if ("1".equals(type)) {
            retMsg = QyWebChatUtil.getAccessToken(corpId, corpSecret);
            if (HttpUtil.isWxError(retMsg)) {
                return ActionResult.fail(MsgCode.SYS033.get(retMsg.getString("errmsg")));
            }
            return ActionResult.success(MsgCode.SYS037.get());
        }
        return ActionResult.fail(MsgCode.SYS035.get());
    }
 
    /**
     * 测试钉钉配置的连接功能
     *
     * @param dingTalkModel 钉钉模板
     * @return ignore
     */
    @Operation(summary = "测试钉钉配置的连接")
    @SaCheckPermission("sysConfig.parameter")
    @PostMapping("/testDingTalkConnect")
    public ActionResult<Object> testDingTalkConnect(@RequestBody @Valid DingTalkModel dingTalkModel) {
        JSONObject retMsg ;
        // 测试钉钉配置的连接
        String appKey = dingTalkModel.getDingSynAppKey();
        String appSecret = dingTalkModel.getDingSynAppSecret();
 
        // 测试钉钉的连接
        retMsg = DingTalkUtil.getAccessToken(appKey, appSecret);
        if (Boolean.FALSE.equals(retMsg.getBoolean("code"))) {
            return ActionResult.fail(MsgCode.SYS036.get(retMsg.getString("error")));
        }
 
        return ActionResult.success(MsgCode.SYS037.get());
    }
 
    /**
     * 获取管理员集合
     *
     * @return
     */
    @Operation(summary = "获取管理员集合")
    @SaCheckPermission("sysConfig.parameter")
    @GetMapping("/getAdminList")
    public ActionResult<List<UserAdminVO>> getAdminList() {
        List<UserAdminVO> admins = JsonUtil.getJsonToList(userService.getAdminList(), UserAdminVO.class);
        return ActionResult.success(admins);
    }
 
    /**
     * 获取管理员集合
     *
     * @param userUpAdminForm 超级管理员设置表单参数
     * @return
     */
    @Operation(summary = "获取管理员集合")
    @Parameter(name = "userUpAdminForm", description = "超级管理员设置表单参数", required = true)
    @SaCheckPermission("sysConfig.parameter")
    @PutMapping("/setAdminList")
    public ActionResult<String> setAdminList(@RequestBody UserUpAdminForm userUpAdminForm) {
        userService.setAdminListByIds(userUpAdminForm.getAdminIds());
        return ActionResult.success(MsgCode.SU004.get());
    }
 
    //+++++++++++++++++++++++++++++++++++以下为api接口
 
    /**
     * 获取BaseSystemInfo
     *
     * @return
     */
    @Override
    @NoDataSourceBind
    @GetMapping("/getInfo")
    public BaseSystemInfo getSysInfo(@RequestParam("tenantId") String tenantId) {
        //判断是否为多租户
        if (configValueUtil.isMultiTenancy()) {
            TenantDataSourceUtil.switchTenant(tenantId);
        }
        return sysconfigService.getSysInfo();
    }
 
    @Override
    @GetMapping("/getSysConfigInfo")
    public BaseSystemInfo getSysConfigInfo() {
        return sysconfigService.getSysInfo();
    }
 
    @Override
    @GetMapping("/getValueByKey")
    public String getValueByKey(@RequestParam("keyStr") String keyStr) {
        return sysconfigService.getValueByKey(keyStr);
    }
 
    @Override
    @NoDataSourceBind
    @GetMapping("/getSysConfigInfoByType")
    public List<SysConfigEntity> getSysConfigInfoByType(@RequestParam("type") String type, @RequestParam("tenantId") String tenantId) {
        //判断是否为多租户
        if (configValueUtil.isMultiTenancy()) {
            TenantDataSourceUtil.switchTenant(tenantId);
        }
        return sysconfigService.getList(type);
    }
}