-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy pathUploadToYapi.java
More file actions
171 lines (162 loc) · 8.51 KB
/
UploadToYapi.java
File metadata and controls
171 lines (162 loc) · 8.51 KB
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
package com.qbb.interaction;
import com.google.common.base.Strings;
import com.intellij.notification.NotificationDisplayType;
import com.intellij.notification.NotificationGroup;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.psi.PsiFile;
import com.qbb.builder.BuildJsonForDubbo;
import com.qbb.builder.BuildJsonForYapi;
import com.qbb.component.ConfigPersistence;
import com.qbb.constant.ProjectTypeConstant;
import com.qbb.constant.YapiConstant;
import com.qbb.dto.*;
import com.qbb.upload.UploadYapi;
import org.apache.commons.lang3.RegExUtils;
import org.apache.commons.lang3.StringUtils;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* @description: 入口
* @author: chengsheng@qbb6.com
* @date: 2019/5/15
*/
public class UploadToYapi extends AnAction {
private static NotificationGroup notificationGroup;
static {
notificationGroup = new NotificationGroup("Java2Json.NotificationGroup", NotificationDisplayType.BALLOON, true);
}
@Override
public void actionPerformed(AnActionEvent e) {
Editor editor = (Editor) e.getDataContext().getData(CommonDataKeys.EDITOR);
Project project = editor.getProject();
String projectToken = null;
String projectId = null;
String yapiUrl = null;
String projectType = null;
String returnClass = null;
String attachUpload = null;
// 获取配置
try {
final java.util.List<ConfigDTO> configs = ServiceManager.getService(ConfigPersistence.class).getConfigs();
if(configs == null || configs.size() == 0){
Messages.showErrorDialog("请先去配置界面配置yapi配置","获取配置失败!");
return;
}
PsiFile psiFile = e.getDataContext().getData(CommonDataKeys.PSI_FILE);
String virtualFile = psiFile.getVirtualFile().getPath();
final List<ConfigDTO> collect = configs.stream()
.filter(it -> {
if (!it.getProjectName().equals(project.getName())) {
return false;
}
String str = (File.separator + it.getProjectName() + File.separator) + (it.getModuleName().equals(it.getProjectName()) ? "" : (it.getModuleName() + File.separator));
if (StringUtils.contains(str, "\\")) {
str = RegExUtils.replaceAll(str, "\\\\", "/");
}
return virtualFile.contains(str);
}).collect(Collectors.toList());
if (collect.isEmpty()) {
Messages.showErrorDialog("没有找到对应的yapi配置,请在菜单 > Preferences > Other setting > YapiUpload 添加", "Error");
return;
}
final ConfigDTO configDTO = collect.get(0);
projectToken = configDTO.getProjectToken();
projectId = configDTO.getProjectId();
yapiUrl = configDTO.getYapiUrl();
projectType = configDTO.getProjectType();
} catch (Exception e2) {
Messages.showErrorDialog("获取配置失败,异常: " + e2.getMessage(),"获取配置失败!");
return;
}
// // 配置校验
// if (Strings.isNullOrEmpty(projectToken) || Strings.isNullOrEmpty(projectId) || Strings.isNullOrEmpty(yapiUrl) || Strings.isNullOrEmpty(projectType)) {
// Messages.showErrorDialog("请在项目的.idea目录下的misc.xml中配置[projectToken,projectId,yapiUrl,projectType] " ,"获取配置失败!");
// return;
// }
// 判断项目类型
if (ProjectTypeConstant.dubbo.equals(projectType)) {
// 获得dubbo需上传的接口列表 参数对象
ArrayList<YapiDubboDTO> yapiDubboDTOs = new BuildJsonForDubbo().actionPerformedList(e);
if (yapiDubboDTOs != null) {
for (YapiDubboDTO yapiDubboDTO : yapiDubboDTOs) {
YapiSaveParam yapiSaveParam = new YapiSaveParam(projectToken, yapiDubboDTO.getTitle(), yapiDubboDTO.getPath(), yapiDubboDTO.getParams(), yapiDubboDTO.getResponse(), Integer.valueOf(projectId), yapiUrl, yapiDubboDTO.getDesc());
yapiSaveParam.setStatus(yapiDubboDTO.getStatus());
if (!Strings.isNullOrEmpty(yapiDubboDTO.getMenu())) {
yapiSaveParam.setMenu(yapiDubboDTO.getMenu());
} else {
yapiSaveParam.setMenu(YapiConstant.menu);
}
try {
// 上传
YapiResponse yapiResponse = new UploadYapi().uploadSave(yapiSaveParam, null, project.getBasePath());
if (yapiResponse.getErrcode() != 0) {
Messages.showErrorDialog("上传失败!异常: " + yapiResponse.getErrmsg(),"上传失败!");
} else {
String url = yapiUrl + "/project/" + projectId + "/interface/api/cat_" + yapiResponse.getCatId();
this.setClipboard(url);
Messages.showInfoMessage("上传成功!接口文档url地址: " + url,"上传成功!");
}
} catch (Exception e1) {
Messages.showErrorDialog("上传失败!异常: " + e1,"上传失败!");
}
}
}
} else if (ProjectTypeConstant.api.equals(projectType)) {
//获得api 需上传的接口列表 参数对象
ArrayList<YapiApiDTO> yapiApiDTOS = new BuildJsonForYapi().actionPerformedList(e, attachUpload, returnClass);
if (yapiApiDTOS != null) {
for (YapiApiDTO yapiApiDTO : yapiApiDTOS) {
YapiSaveParam yapiSaveParam = new YapiSaveParam(projectToken, yapiApiDTO.getTitle(), yapiApiDTO.getPath(), yapiApiDTO.getParams(), yapiApiDTO.getRequestBody(), yapiApiDTO.getResponse(), Integer.valueOf(projectId), yapiUrl, true, yapiApiDTO.getMethod(), yapiApiDTO.getDesc(), yapiApiDTO.getHeader());
yapiSaveParam.setReq_body_form(yapiApiDTO.getReq_body_form());
yapiSaveParam.setReq_body_type(yapiApiDTO.getReq_body_type());
yapiSaveParam.setReq_params(yapiApiDTO.getReq_params());
yapiSaveParam.setStatus(yapiApiDTO.getStatus());
if (!Strings.isNullOrEmpty(yapiApiDTO.getMenu())) {
yapiSaveParam.setMenu(yapiApiDTO.getMenu());
} else {
yapiSaveParam.setMenu(YapiConstant.menu);
}
try {
// 上传
YapiResponse yapiResponse = new UploadYapi().uploadSave(yapiSaveParam, attachUpload, project.getBasePath());
if (yapiResponse.getErrcode() != 0) {
Messages.showInfoMessage("上传失败,原因: " + yapiResponse.getErrmsg(),"上传失败!");
} else {
String url = yapiUrl + "/project/" + projectId + "/interface/api/cat_" + yapiResponse.getCatId();
this.setClipboard(url);
Messages.showInfoMessage("上传成功!接口文档url地址: " + url,"上传成功!");
}
} catch (Exception e1) {
Messages.showErrorDialog("上传失败!异常: " + e1,"上传失败!");
}
}
}
}
}
/**
* @description: 设置到剪切板
* @param: [content]
* @return: void
* @author: chengsheng@qbb6.com
* @date: 2019/7/3
*/
private void setClipboard(String content) {
//获取系统剪切板
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
//构建String数据类型
StringSelection selection = new StringSelection(content);
//添加文本到系统剪切板
clipboard.setContents(selection, null);
}
}