ny
昨天 b6f169fe43a2b13f351aefc152374fc7f0bc8cb7
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
 
 
 
package jnpf.message.controller;
 
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import jnpf.base.DictionaryDataApi;
import jnpf.base.controller.SuperController;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import jnpf.base.ActionResult;
 
import jnpf.base.entity.DictionaryDataEntity;
import jnpf.base.vo.PageListVO;
import jnpf.base.vo.PaginationVO;
import jnpf.base.UserInfo;
import jnpf.config.ConfigValueUtil;
import jnpf.constant.MsgCode;
import jnpf.exception.DataException;
import jnpf.message.entity.AccountConfigEntity;
import jnpf.message.model.EmailModel;
import jnpf.message.model.accountconfig.*;
import jnpf.message.service.AccountConfigService;
import jnpf.message.service.SendConfigTemplateService;
 
import jnpf.message.util.EmailUtil;
 
import jnpf.permission.UserApi;
import jnpf.permission.entity.UserEntity;
 
import jnpf.util.third.DingTalkUtil;
import jnpf.util.third.QyWebChatUtil;
import jnpf.util.wxutil.HttpUtil;
import org.springframework.transaction.annotation.Transactional;
import jnpf.util.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
 
import jakarta.validation.Valid;
import java.io.IOException;
import java.util.*;
 
 
/**
 * 账号配置功能
 *
 * @版本: V3.2.0
 * @版权: 引迈信息技术有限公司(https://www.jnpfsoft.com)
 * @作者: JNPF开发平台组
 * @日期: 2022-08-18
 */
@Slf4j
@RestController
@Tag(name = "账号配置功能", description = "AccountConfig")
@RequestMapping("/AccountConfig")
public class AccountConfigController extends SuperController<AccountConfigService, AccountConfigEntity> {
 
    @Autowired
    private UserApi userApi;
    @Autowired
    private AccountConfigService accountConfigService;
    @Autowired
    private SendConfigTemplateService sendConfigTemplateService;
    @Autowired
    private DictionaryDataApi dictionaryDataApi;
 
    /**
     * 列表
     *
     * @param accountConfigPagination 账号配置分页模型
     * @return ignore
     */
    @Operation(summary = "列表")
    @SaCheckPermission(value = {"integrationCenter.sms", "integrationCenter.email", "integrationCenter.weChatMp", "integrationCenter.webhook"}, mode = SaMode.OR)
    @GetMapping
    public ActionResult<PageListVO<AccountConfigListVO>> list(AccountConfigPagination accountConfigPagination) throws IOException {
        List<AccountConfigEntity> list = accountConfigService.getList(accountConfigPagination);
        List<DictionaryDataEntity> smsSendTypeList = dictionaryDataApi.getListByCode("smsSendType");
        List<DictionaryDataEntity> webHookList = dictionaryDataApi.getListByCode("msgWebHookSendType");
        //处理id字段转名称,若无需转或者为空可删除
        UserEntity userEntity;
        List<AccountConfigListVO> listVO = JsonUtil.getJsonToList(list, AccountConfigListVO.class);
        for (AccountConfigListVO accountConfigVO : listVO) {
            //渠道
            if (StringUtil.isNotEmpty(accountConfigVO.getChannel())) {
                smsSendTypeList.stream().filter(t -> accountConfigVO.getChannel().equals(t.getEnCode())).findFirst()
                        .ifPresent(dataTypeEntity -> accountConfigVO.setChannel(dataTypeEntity.getFullName()));
            }
            //webhook类型
            if (StringUtil.isNotEmpty(accountConfigVO.getWebhookType())) {
                webHookList.stream().filter(t -> accountConfigVO.getWebhookType().equals(t.getEnCode())).findFirst()
                        .ifPresent(dataTypeEntity -> accountConfigVO.setWebhookType(dataTypeEntity.getFullName()));
            }
 
            if (StringUtil.isNotEmpty(accountConfigVO.getCreatorUserId())) {
                userEntity = userApi.getInfoById(accountConfigVO.getCreatorUserId());
                if (userEntity != null) {
                    accountConfigVO.setCreatorUser(userEntity.getRealName() + "/" + userEntity.getAccount());
                }
            }
 
//            if(StringUtil.isNotBlank(accountConfigVO.getLastModifyUserId()) && !"null".equals(accountConfigVO.getLastModifyUserId())){
//                userEntity = userService.getInfo(accountConfigVO.getLastModifyUserId());
//                if(userEntity != null){
//                    accountConfigVO.setLastModifyUserId(userEntity.getRealName() + userEntity.getAccount());
//                }
//            }
 
        }
 
        PageListVO vo = new PageListVO();
        vo.setList(listVO);
        PaginationVO page = JsonUtil.getJsonToBean(accountConfigPagination, PaginationVO.class);
        vo.setPagination(page);
        return ActionResult.success(vo);
    }
 
    /**
     * 创建
     *
     * @param accountConfigForm 新建账号配置模型
     * @return ignore
     */
    @Operation(summary = "新建")
    @PostMapping
    @Parameters({
            @Parameter(name = "accountConfigForm", description = "新建账号配置模型")
    })
    @SaCheckPermission(value = {"integrationCenter.sms", "integrationCenter.email", "integrationCenter.weChatMp", "integrationCenter.webhook"}, mode = SaMode.OR)
    @Transactional
    public ActionResult create(@RequestBody @Valid AccountConfigForm accountConfigForm) throws DataException {
        boolean b = accountConfigService.checkForm(accountConfigForm, 0, accountConfigForm.getType(), "");
        if (b) {
            return ActionResult.fail(MsgCode.EXIST002.get());
        }
        boolean c = accountConfigService.checkGzhId(accountConfigForm.getAppKey(), 0, "7", "");
        if ("7".equals(accountConfigForm.getType())) {
            if (c) {
                return ActionResult.fail(MsgCode.FA048.get());
            }
        }
        String mainId = RandomUtil.uuId();
        UserInfo userInfo = UserProvider.getUser();
        AccountConfigEntity entity = JsonUtil.getJsonToBean(accountConfigForm, AccountConfigEntity.class);
        entity.setCreatorTime(DateUtil.getNowDate());
        entity.setCreatorUserId(userInfo.getUserId());
        entity.setId(mainId);
        accountConfigService.save(entity);
        return ActionResult.success(MsgCode.SU001.get());
    }
 
 
    /**
     * 信息
     *
     * @param id 主键
     * @return ignore
     */
    @Operation(summary = "信息")
    @GetMapping("/{id}")
    @Parameters({
            @Parameter(name = "id", description = "主键", required = true)
    })
    @SaCheckPermission(value = {"integrationCenter.sms", "integrationCenter.email", "integrationCenter.weChatMp", "integrationCenter.webhook"}, mode = SaMode.OR)
    public ActionResult<AccountConfigInfoVO> info(@PathVariable("id") String id) {
        AccountConfigEntity entity = accountConfigService.getInfo(id);
        AccountConfigInfoVO vo = JsonUtil.getJsonToBean(entity, AccountConfigInfoVO.class);
        return ActionResult.success(vo);
    }
 
    /**
     * 表单信息(详情页)
     *
     * @param id 主键
     * @return ignore
     */
    @Operation(summary = "表单信息(详情页)")
    @GetMapping("/detail/{id}")
    @Parameters({
            @Parameter(name = "id", description = "主键", required = true)
    })
    @SaCheckPermission(value = {"integrationCenter.sms", "integrationCenter.email", "integrationCenter.weChatMp", "integrationCenter.webhook"}, mode = SaMode.OR)
    public ActionResult<AccountConfigInfoVO> detailInfo(@PathVariable("id") String id) {
        AccountConfigEntity entity = accountConfigService.getInfo(id);
        AccountConfigInfoVO vo = JsonUtil.getJsonToBean(entity, AccountConfigInfoVO.class);
        return ActionResult.success(vo);
    }
 
 
    /**
     * 更新
     *
     * @param id                主键
     * @param accountConfigForm 修改账号配置模型
     * @return ignore
     */
    @Operation(summary = "更新")
    @PutMapping("/{id}")
    @Parameters({
            @Parameter(name = "id", description = "主键", required = true),
            @Parameter(name = "accountConfigForm", description = "修改账号配置模型", required = true)
    })
    @SaCheckPermission(value = {"integrationCenter.sms", "integrationCenter.email", "integrationCenter.weChatMp", "integrationCenter.webhook"}, mode = SaMode.OR)
    @Transactional
    public ActionResult update(@PathVariable("id") String id, @RequestBody @Valid AccountConfigForm accountConfigForm) throws DataException {
 
        boolean b = accountConfigService.checkForm(accountConfigForm, 0, accountConfigForm.getType(), accountConfigForm.getId());
        if (b) {
            return ActionResult.fail(MsgCode.EXIST002.get());
        }
        //判断配置是否被引用
        if ("0".equals(String.valueOf(accountConfigForm.getEnabledMark()))) {
            if (sendConfigTemplateService.isUsedAccount(accountConfigForm.getId())) {
                return ActionResult.fail(MsgCode.FA048.get());
            }
        }
        boolean c = accountConfigService.checkGzhId(accountConfigForm.getAppKey(), 0, "7", id);
        if ("7".equals(accountConfigForm.getType())) {
            if (c) {
                return ActionResult.fail(MsgCode.FA048.get());
            }
        }
        //判断配置是否被引用
        if (Objects.equals(0, accountConfigForm.getEnabledMark())) {
            if (sendConfigTemplateService.isUsedAccount(accountConfigForm.getId())) {
                return ActionResult.fail(MsgCode.FA049.get());
            }
        }
        UserInfo userInfo = UserProvider.getUser();
        AccountConfigEntity entity = accountConfigService.getInfo(id);
        if (entity != null) {
            AccountConfigEntity subentity = JsonUtil.getJsonToBean(accountConfigForm, AccountConfigEntity.class);
            subentity.setCreatorTime(entity.getCreatorTime());
            subentity.setCreatorUserId(entity.getCreatorUserId());
            subentity.setLastModifyTime(DateUtil.getNowDate());
            subentity.setLastModifyUserId(userInfo.getUserId());
            boolean b1 = accountConfigService.updateById(subentity);
            if (!b1) {
                return ActionResult.fail(MsgCode.VS405.get());
            }
            return ActionResult.success(MsgCode.SU004.get());
        } else {
            return ActionResult.fail(MsgCode.FA002.get());
        }
    }
 
 
    /**
     * 删除
     *
     * @param id 主键
     * @return ignore
     */
    @Operation(summary = "删除")
    @DeleteMapping("/{id}")
    @Parameters({
            @Parameter(name = "id", description = "主键", required = true)
    })
    @SaCheckPermission(value = {"integrationCenter.sms", "integrationCenter.email", "integrationCenter.weChatMp", "integrationCenter.webhook"}, mode = SaMode.OR)
    @Transactional
    public ActionResult delete(@PathVariable("id") String id) {
        AccountConfigEntity entity = accountConfigService.getInfo(id);
        if (entity != null) {
            //判断是否与消息发送配置关联
            //判断配置是否被引用
            if (sendConfigTemplateService.isUsedAccount(entity.getId())) {
                return ActionResult.fail(MsgCode.FA050.get());
            }
            accountConfigService.delete(entity);
 
        }
        return ActionResult.success(MsgCode.SU003.get());
    }
 
    /**
     * 开启或禁用
     *
     * @param id 主键
     * @return ignore
     */
    @Operation(summary = "开启或禁用")
    @PostMapping("/unable/{id}")
    @Parameters({
            @Parameter(name = "id", description = "主键", required = true)
    })
    @SaCheckPermission(value = {"integrationCenter.sms", "integrationCenter.email", "integrationCenter.weChatMp", "integrationCenter.webhook"}, mode = SaMode.OR)
    @Transactional
    public ActionResult unable(@PathVariable("id") String id) {
        AccountConfigEntity entity = accountConfigService.getInfo(id);
        if (entity != null) {
            if ("1".equals(String.valueOf(entity.getEnabledMark()))) {
                entity.setEnabledMark(0);
                return ActionResult.success(MsgCode.WF027.get());
            }else {
                //判断是否被引用
 
                entity.setEnabledMark(1);
                return ActionResult.success(MsgCode.WF026.get());
            }
        }else {
            return ActionResult.fail(MsgCode.FA007.get());
        }
    }
 
    /**
     * 复制
     *
     * @param id 主键
     * @return ignore
     */
    @Operation(summary = "复制")
    @PostMapping("/{id}/Actions/Copy")
    @Parameters({
            @Parameter(name = "id", description = "主键", required = true)
    })
    @SaCheckPermission(value = {"integrationCenter.sms", "integrationCenter.email", "integrationCenter.weChatMp", "integrationCenter.webhook"}, mode = SaMode.OR)
    @Transactional
    public ActionResult copy(@PathVariable("id") String id) {
        UserInfo userInfo = UserProvider.getUser();
        AccountConfigEntity entity = accountConfigService.getInfo(id);
        if (entity != null) {
            entity.setEnabledMark(0);
            String copyNum = UUID.randomUUID().toString().substring(0, 5);
            entity.setFullName(entity.getFullName() + ".副本" + copyNum);
            entity.setEnCode(entity.getEnCode() + copyNum);
            entity.setCreatorTime(DateUtil.getNowDate());
            entity.setCreatorUserId(userInfo.getUserId());
            if ("7".equals(entity.getType())) {
                entity.setAppKey(entity.getAppKey() + "副本" + copyNum);
            }
            entity.setLastModifyTime(null);
            entity.setLastModifyUserId(null);
            entity.setId(RandomUtil.uuId());
            AccountConfigEntity copyEntity = JsonUtil.getJsonToBean(entity, AccountConfigEntity.class);
            if(copyEntity.getEnCode().length()>50 || copyEntity.getFullName().length()>50){
                return ActionResult.fail(MsgCode.PRI006.get());
            }
            accountConfigService.create(copyEntity);
            return ActionResult.success(MsgCode.SU007.get());
        }else {
            return ActionResult.fail(MsgCode.FA004.get());
        }
    }
 
 
    /**
     * 测试发送邮件
     *
     * @param accountConfigForm 账号测试模型
     * @return ignore
     */
    @Operation(summary = "测试发送邮箱")
    @PostMapping("/testSendMail")
    @Parameters({
            @Parameter(name = "accountConfigForm", description = "账号测试模型", required = true)
    })
    @SaCheckPermission(value = {"integrationCenter.sms", "integrationCenter.email", "integrationCenter.weChatMp", "integrationCenter.webhook"}, mode = SaMode.OR)
    @Transactional
    public ActionResult testSendMail(@RequestBody @Valid AccountConfigForm accountConfigForm) {
        List<String> toMails = accountConfigForm.getTestSendEmail();
        // 获取邮箱配置
        Map<String, String> objModel = new HashMap<>();
        objModel.put("emailSmtpHost", accountConfigForm.getSmtpServer());
        objModel.put("emailSmtpPort", accountConfigForm.getSmtpPort().toString());
        objModel.put("emailSenderName", accountConfigForm.getAddressorName());
        objModel.put("emailAccount", accountConfigForm.getSmtpUser());
        objModel.put("emailPassword", accountConfigForm.getSmtpPassword());
        objModel.put("emailSsl", accountConfigForm.getSslLink() == 1 ? "true" : "false");
 
 
        EmailModel emailModel = JsonUtil.getJsonToBean(objModel, EmailModel.class);
        StringBuilder toUserMail = new StringBuilder();
        StringBuilder nullUserInfo = new StringBuilder();
        String userEmailAll = "";
        String userEmail = "";
        String userName = "";
 
        // 相关参数验证
        if (StringUtil.isEmpty(emailModel.getEmailSmtpHost())) {
            return ActionResult.fail(MsgCode.MSERR101.get());
        } else if (StringUtil.isEmpty(emailModel.getEmailSmtpPort())) {
            return ActionResult.fail(MsgCode.MSERR101.get());
        } else if (StringUtil.isEmpty(emailModel.getEmailAccount())) {
            return ActionResult.fail(MsgCode.MSERR102.get());
        } else if (StringUtil.isEmpty(emailModel.getEmailPassword())) {
            return ActionResult.fail(MsgCode.MSERR103.get());
        } else if (toMails == null || toMails.size() < 1) {
            return ActionResult.fail(MsgCode.MSERR104.get());
        } else {
            // 设置邮件标题
            emailModel.setEmailTitle(accountConfigForm.getTestEmailTitle());
            // 设置邮件内容
            String content = accountConfigForm.getTestEmailContent();
            emailModel.setEmailContent(content);
 
            // 获取收件人的邮箱地址、创建消息用户实体
            for (String userId : toMails) {
                UserEntity userEntity = userApi.getInfoById(userId);
                if (userEntity != null) {
                    userEmail = StringUtil.isEmpty(userEntity.getEmail()) ? "" : userEntity.getEmail();
                    userName = userEntity.getRealName();
                }
                if (StringUtil.isNotBlank(userEmail) && !"null".equals(userEmail)) {
                    //校验用户邮箱格式
                    if(!isEmail(userEmail)){
                        return ActionResult.fail(MsgCode.MSERR105.get(userName));
                    }
                    toUserMail = toUserMail.append(",").append(userName).append("<").append(userEmail).append(">");
                } else {
                    return ActionResult.fail(MsgCode.MSERR106.get(userName));
                }
            }
            // 处理接收人员的邮箱信息串并验证
            userEmailAll = toUserMail.toString();
            if (StringUtil.isNotEmpty(userEmailAll)) {
                userEmailAll = userEmailAll.substring(1);
            }
            if (StringUtil.isEmpty(userEmailAll)) {
                return ActionResult.fail(MsgCode.MSERR107.get());
            } else {
                // 设置接收人员
                emailModel.setEmailToUsers(userEmailAll);
                // 发送邮件
                JSONObject retJson = EmailUtil.sendMail(emailModel);
                if (!retJson.getBoolean("code")) {
                    return ActionResult.fail(MsgCode.MSERR108.get(retJson.get("error")));
                }
            }
        }
        return ActionResult.success(MsgCode.MSERR111.get());
    }
 
    /**
     * 测试企业微信配置的连接功能
     *
     * @param accountConfigForm 账号测试模型
     * @return ignore
     */
    @Operation(summary = "测试企业微信配置的连接功能")
    @Parameters({
            @Parameter(name = "accountConfigForm", description = "账号测试模型", required = true)
    })
    @SaCheckPermission(value = {"integrationCenter.sms", "integrationCenter.email", "integrationCenter.weChatMp", "integrationCenter.webhook"}, mode = SaMode.OR)
    @PostMapping("/testQyWebChatConnect")
    public ActionResult testQyWebChatConnect(@RequestBody @Valid AccountConfigForm accountConfigForm) {
        JSONObject retMsg = new JSONObject();
        // 测试发送消息、组织同步的连接
        //企业微信企业id
        String corpId = accountConfigForm.getEnterpriseId();
        //企业微信应用secret
        String agentSecret = accountConfigForm.getAppSecret();
//        String corpSecret = testAccountConfigForm.getQyhCorpSecret();
        // 测试发送消息的连接
        retMsg = QyWebChatUtil.getAccessToken(corpId, agentSecret);
        if (HttpUtil.isWxError(retMsg)) {
            return ActionResult.fail(MsgCode.MSERR110.get(retMsg.getString("errmsg")));
        }
        return ActionResult.success(MsgCode.MSERR109.get());
    }
 
    /**
     * 测试钉钉配置的连接功能
     *
     * @param accountConfigForm 账号测试模型
     * @return ignore
     */
    @Operation(summary = "测试钉钉配置的连接功能")
    @Parameters({
            @Parameter(name = "accountConfigForm", description = "账号测试模型", required = true)
    })
    @SaCheckPermission(value = {"integrationCenter.sms", "integrationCenter.email", "integrationCenter.weChatMp", "integrationCenter.webhook"}, mode = SaMode.OR)
    @PostMapping("/testDingTalkConnect")
    public ActionResult testDingTalkConnect(@RequestBody @Valid AccountConfigForm accountConfigForm) {
        JSONObject retMsg = new JSONObject();
        // 测试钉钉配置的连接
        String appKey = accountConfigForm.getAppId();
        String appSecret = accountConfigForm.getAppSecret();
        ///
//        String agentId = dingTalkModel.getDingAgentId();
        // 测试钉钉的连接
        retMsg = DingTalkUtil.getAccessToken(appKey, appSecret);
        if (!retMsg.getBoolean("code")) {
            return ActionResult.fail(MsgCode.MSERR110.get(retMsg.getString("error")));
        }
        return ActionResult.success(MsgCode.MSERR109.get());
    }
 
    public boolean isEmail(String email) {
        String EMAIL_REGEX = "^[\\w-_\\.+]*[\\w-_\\.]\\@([\\w]+\\.)+[\\w]+[\\w]$";
        Boolean b = email.matches(EMAIL_REGEX);
        return b;
    }
}