|
|
@@ -1,15 +1,23 @@
|
|
|
package cn.reghao.devops.manager.notification.controller;
|
|
|
|
|
|
import cn.reghao.devops.manager.notification.db.repository.EmailAccountRepository;
|
|
|
+import cn.reghao.devops.manager.notification.db.repository.NotifyReceiverRepository;
|
|
|
+import cn.reghao.devops.manager.notification.model.NotifyType;
|
|
|
import cn.reghao.devops.manager.notification.model.po.EmailAccount;
|
|
|
+import cn.reghao.devops.manager.notification.model.po.NotifyReceiver;
|
|
|
import cn.reghao.devops.manager.util.db.PageSort;
|
|
|
+import cn.reghao.jutil.jdk.result.WebResult;
|
|
|
+import cn.reghao.jutil.jdk.string.StringRegexp;
|
|
|
import io.swagger.annotations.Api;
|
|
|
import org.springframework.data.domain.Page;
|
|
|
import org.springframework.data.domain.PageRequest;
|
|
|
+import org.springframework.http.MediaType;
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
import org.springframework.ui.Model;
|
|
|
-import org.springframework.web.bind.annotation.GetMapping;
|
|
|
-import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.validation.annotation.Validated;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
|
|
|
/**
|
|
|
* @author reghao
|
|
|
@@ -19,14 +27,17 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
@Controller
|
|
|
@RequestMapping("/sys/notify/email")
|
|
|
public class EmailAccountController {
|
|
|
+ private final NotifyReceiverRepository notifyReceiverRepository;
|
|
|
private final EmailAccountRepository emailAccountRepository;
|
|
|
|
|
|
- public EmailAccountController(EmailAccountRepository emailAccountRepository) {
|
|
|
+ public EmailAccountController(NotifyReceiverRepository notifyReceiverRepository,
|
|
|
+ EmailAccountRepository emailAccountRepository) {
|
|
|
+ this.notifyReceiverRepository = notifyReceiverRepository;
|
|
|
this.emailAccountRepository = emailAccountRepository;
|
|
|
}
|
|
|
|
|
|
@GetMapping("")
|
|
|
- public String emailIndex(Model model) throws Exception {
|
|
|
+ public String emailIndex(Model model) {
|
|
|
PageRequest pageRequest = PageSort.pageRequest();
|
|
|
Page<EmailAccount> page = emailAccountRepository.findAll(pageRequest);
|
|
|
page.getContent().forEach(emailAccount -> {
|
|
|
@@ -39,13 +50,51 @@ public class EmailAccountController {
|
|
|
}
|
|
|
|
|
|
@GetMapping("/add")
|
|
|
- public String emailAdd(Model model) throws Exception {
|
|
|
+ public String emailAdd(Model model) {
|
|
|
return "/sys/notify/emailadd";
|
|
|
}
|
|
|
|
|
|
- @GetMapping("/edit")
|
|
|
- public String emailEdit(Model model) throws Exception {
|
|
|
- model.addAttribute("emailAccount", null);
|
|
|
- return "/sys/notify/emailedit";
|
|
|
+ @PostMapping(value = "/save", produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
+ @ResponseBody
|
|
|
+ public String saveReceiver(@Validated EmailAccount emailAccount) {
|
|
|
+ String email = emailAccount.getUsername();
|
|
|
+ boolean matched = StringRegexp.matchEmail(email);
|
|
|
+ if (!matched) {
|
|
|
+ return WebResult.failWithMsg("邮箱地址格式不正确, 请检查");
|
|
|
+ }
|
|
|
+
|
|
|
+ List<EmailAccount> list = emailAccountRepository.findAll();
|
|
|
+ if (!list.isEmpty()) {
|
|
|
+ EmailAccount emailAccount1 = list.get(0);
|
|
|
+ emailAccount1.setSmtp(emailAccount.getSmtp());
|
|
|
+ emailAccount1.setUsername(emailAccount.getUsername());
|
|
|
+ emailAccount1.setPassword(emailAccount.getPassword());
|
|
|
+ emailAccount1.setPersonal(emailAccount.getPersonal());
|
|
|
+ emailAccountRepository.save(emailAccount1);
|
|
|
+ } else {
|
|
|
+ emailAccountRepository.save(emailAccount);
|
|
|
+ }
|
|
|
+
|
|
|
+ return WebResult.success();
|
|
|
+ }
|
|
|
+
|
|
|
+ @GetMapping("/edit/{username}")
|
|
|
+ public String emailEdit(@PathVariable("username") String username, Model model) throws Exception {
|
|
|
+ List<EmailAccount> list = emailAccountRepository.findAll();
|
|
|
+ model.addAttribute("emailAccount", list.get(0));
|
|
|
+ return "/sys/notify/emailadd";
|
|
|
+ }
|
|
|
+
|
|
|
+ @DeleteMapping(value = "/delete/{username}", produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
+ @ResponseBody
|
|
|
+ public String deleteEmail(@PathVariable("username") String username) {
|
|
|
+ NotifyReceiver notifyReceiver = notifyReceiverRepository.findByType(NotifyType.email.name());
|
|
|
+ if (notifyReceiver != null) {
|
|
|
+ return WebResult.failWithMsg("系统中存在接收通知的邮箱地址, 不能删除发送通知的邮箱");
|
|
|
+ }
|
|
|
+
|
|
|
+ List<EmailAccount> list = emailAccountRepository.findAll();
|
|
|
+ emailAccountRepository.delete(list.get(0));
|
|
|
+ return WebResult.success();
|
|
|
}
|
|
|
}
|