Skip to content

Commit 4e502d6

Browse files
committed
优化动态表单,增加缓存,前端自行解析
1 parent 1335ff1 commit 4e502d6

File tree

8 files changed

+125
-44
lines changed

8 files changed

+125
-44
lines changed

hsweb-web-bean/src/main/java/org/hsweb/web/bean/po/module/ModuleMeta.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.hsweb.web.bean.po.module;
22

33
import org.hibernate.validator.constraints.NotBlank;
4+
import org.hsweb.commons.MD5;
45
import org.hsweb.web.bean.po.GenericPo;
56

67
/**
@@ -70,4 +71,14 @@ public String getMeta() {
7071
public void setMeta(String meta) {
7172
this.meta = meta;
7273
}
74+
75+
public String getMd5() {
76+
StringBuilder builder = new StringBuilder();
77+
builder.append(key);
78+
builder.append(moduleId);
79+
builder.append(roleId);
80+
builder.append(meta);
81+
builder.append(status);
82+
return MD5.defaultEncode(builder.toString());
83+
}
7384
}

hsweb-web-controller/src/main/java/org/hsweb/web/controller/GenericController.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333

3434
import java.util.Arrays;
3535

36+
import static org.hsweb.web.core.message.ResponseMessage.*;
37+
3638
/**
3739
* 通用控制器,此控制器实现了通用的增删改查功能
3840
* 需要提供一个实现了{@link GenericService}接口的实现类
@@ -83,7 +85,7 @@ public ResponseMessage list(QueryParam param) {
8385
data = getService().select(param);
8486
else
8587
data = getService().selectPager(param);
86-
return ResponseMessage.ok(data)
88+
return ok(data)
8789
.include(getPOType(), param.getIncludes())
8890
.exclude(getPOType(), param.getExcludes())
8991
.onlyData();
@@ -103,7 +105,7 @@ public ResponseMessage info(@PathVariable("id") PK id) {
103105
PO po = getService().selectByPk(id);
104106
if (po == null)
105107
throw new NotFoundException("data is not found!");
106-
return ResponseMessage.ok(po);
108+
return ok(po);
107109
}
108110

109111

@@ -118,7 +120,7 @@ public ResponseMessage info(@PathVariable("id") PK id) {
118120
@Authorize(action = "R")
119121
public ResponseMessage total(QueryParam param) {
120122
// 获取条件查询
121-
return ResponseMessage.ok(getService().total(param));
123+
return ok(getService().total(param));
122124
}
123125

124126
/**
@@ -134,7 +136,7 @@ public ResponseMessage total(QueryParam param) {
134136
@ResponseStatus(HttpStatus.CREATED)
135137
public ResponseMessage add(@RequestBody PO object) {
136138
PK pk = getService().insert(object);
137-
return ResponseMessage.created(pk);
139+
return created(pk);
138140
}
139141

140142
/**
@@ -151,7 +153,7 @@ public ResponseMessage delete(@PathVariable("id") PK id) {
151153
PO old = getService().selectByPk(id);
152154
assertFound(old, "data is not found!");
153155
getService().delete(id);
154-
return ResponseMessage.ok();
156+
return ok();
155157
}
156158

157159
/**
@@ -172,7 +174,7 @@ public ResponseMessage update(@PathVariable("id") PK id, @RequestBody PO object)
172174
((GenericPo) object).setId(id);
173175
}
174176
int number = getService().update(object);
175-
return ResponseMessage.ok(number);
177+
return ok(number);
176178
}
177179

178180
/**
@@ -194,7 +196,7 @@ public ResponseMessage update(@RequestBody String json) {
194196
} else {
195197
throw new BusinessException("请求数据格式错误!");
196198
}
197-
return ResponseMessage.ok(number);
199+
return ok(number);
198200
}
199201

200202
/**

hsweb-web-controller/src/main/java/org/hsweb/web/controller/form/FormController.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
import java.sql.SQLException;
3434
import java.util.List;
3535

36+
import static org.hsweb.web.core.message.ResponseMessage.*;
37+
import static org.hsweb.web.core.message.ResponseMessage.ok;
38+
3639
/**
3740
* 动态表单控制器,用于管理动态表单
3841
*
@@ -65,15 +68,15 @@ public FormService getService() {
6568
public ResponseMessage latestList(QueryParam param) {
6669
ResponseMessage message;
6770
if (!param.isPaging()) {
68-
message = ResponseMessage.ok(formService.selectLatestList(param));
71+
message = ok(formService.selectLatestList(param));
6972
} else {
7073
param.setPaging(false);
7174
int total = formService.countLatestList(param);
7275
param.rePaging(total);
7376
List<Form> list = formService.selectLatestList(param);
7477
PagerResult<Form> result = new PagerResult<>();
7578
result.setData(list).setTotal(total);
76-
message = ResponseMessage.ok(result);
79+
message = ok(result);
7780
}
7881
message.include(Form.class, param.getIncludes())
7982
.exclude(Form.class, param.getExcludes())
@@ -92,7 +95,7 @@ public ResponseMessage latestList(QueryParam param) {
9295
public ResponseMessage latest(@PathVariable(value = "name") String name) {
9396
Form form = formService.selectLatest(name);
9497
assertFound(form, "表单不存在");
95-
return ResponseMessage.ok(form);
98+
return ok(form);
9699
}
97100

98101
/**
@@ -104,11 +107,16 @@ public ResponseMessage latest(@PathVariable(value = "name") String name) {
104107
* @throws NotFoundException 表单不存在
105108
*/
106109
@RequestMapping(value = "/{name}/{version}", method = RequestMethod.GET)
107-
public ResponseMessage version(@PathVariable(value = "name") String name,
110+
public ResponseMessage selectByVersion(@PathVariable(value = "name") String name,
108111
@PathVariable(value = "version") Integer version) {
109112
Form form = formService.selectByVersion(name, version);
110113
assertFound(form, "表单不存在");
111-
return ResponseMessage.ok(form);
114+
return ok(form);
115+
}
116+
117+
@RequestMapping(value = "/{name}/version", method = RequestMethod.GET)
118+
public ResponseMessage selectVersion(@PathVariable(value = "name") String name) {
119+
return ok(formService.selectDeployedVersion(name));
112120
}
113121

114122
/**
@@ -123,7 +131,7 @@ public ResponseMessage version(@PathVariable(value = "name") String name,
123131
@Authorize(action = "deploy")
124132
public ResponseMessage deploy(@PathVariable("id") String id) throws SQLException {
125133
formService.deploy(id);
126-
return ResponseMessage.ok();
134+
return ok();
127135
}
128136

129137
/**
@@ -137,7 +145,7 @@ public ResponseMessage deploy(@PathVariable("id") String id) throws SQLException
137145
@Authorize(action = "deploy")
138146
public ResponseMessage unDeploy(@PathVariable("id") String id) {
139147
formService.unDeploy(id);
140-
return ResponseMessage.ok();
148+
return ok();
141149
}
142150

143151
/**
@@ -149,7 +157,7 @@ public ResponseMessage unDeploy(@PathVariable("id") String id) {
149157
*/
150158
@RequestMapping(value = "/{name}/html", method = RequestMethod.GET)
151159
public ResponseMessage html(@PathVariable("name") String name) {
152-
return ResponseMessage.ok(formService.createDeployHtml(name));
160+
return ok(formService.createDeployHtml(name));
153161
}
154162

155163
/**
@@ -162,7 +170,7 @@ public ResponseMessage html(@PathVariable("name") String name) {
162170
@RequestMapping(value = "/{id}/new-version", method = RequestMethod.POST)
163171
@ResponseStatus(HttpStatus.CREATED)
164172
public ResponseMessage newVersion(@PathVariable("id") String id) {
165-
return ResponseMessage.created(formService.createNewVersion(id));
173+
return created(formService.createNewVersion(id));
166174
}
167175

168176
/**
@@ -175,7 +183,7 @@ public ResponseMessage newVersion(@PathVariable("id") String id) {
175183
public ResponseMessage using(@PathVariable("name") String name) {
176184
Form form = formService.selectUsing(name);
177185
assertFound(form, "表单不存在");
178-
return ResponseMessage.ok(form).exclude(Form.class, "html");
186+
return ok(form).exclude(Form.class, "html");
179187
}
180188

181189
/**
@@ -187,6 +195,6 @@ public ResponseMessage using(@PathVariable("name") String name) {
187195
*/
188196
@RequestMapping(value = "/{id}/view", method = RequestMethod.GET)
189197
public ResponseMessage view(@PathVariable("id") String id) throws Exception {
190-
return ResponseMessage.ok(formService.createViewHtml(id));
198+
return ok(formService.createViewHtml(id));
191199
}
192200
}

hsweb-web-controller/src/main/java/org/hsweb/web/controller/module/ModuleMetaController.java

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,7 @@
3131

3232
import javax.annotation.Resource;
3333
import java.util.ArrayList;
34-
import java.util.Arrays;
3534
import java.util.List;
36-
import java.util.Optional;
37-
import java.util.stream.Collector;
3835
import java.util.stream.Collectors;
3936

4037
/**
@@ -55,21 +52,33 @@ protected ModuleMetaService getService() {
5552
return moduleMetaService;
5653
}
5754

55+
protected List<String> getRoleId() {
56+
User user = WebUtil.getLoginUser();
57+
List<UserRole> roles = user.getUserRoles();
58+
if (roles == null) roles = new ArrayList<>();
59+
return roles.stream()
60+
.map(UserRole::getRoleId).collect(Collectors.toList());
61+
}
62+
5863
/**
5964
* 查询当前用户持有制定key的所有模块配置定义信息
6065
*
6166
* @param key
6267
* @return {@link ResponseMessage}
6368
*/
64-
@RequestMapping(value = "/{key}/own", method = RequestMethod.GET)
69+
@RequestMapping(value = "/{key}/list", method = RequestMethod.GET)
6570
public ResponseMessage userModuleMeta(@PathVariable String key) {
66-
User user = WebUtil.getLoginUser();
67-
List<UserRole> roles = user.getUserRoles();
68-
if (roles == null) roles = new ArrayList<>();
69-
String[] roleIdList = roles
70-
.stream()
71-
.map(userRole -> userRole.getRoleId())
72-
.collect(Collectors.toList()).toArray(new String[roles.size()]);
73-
return ResponseMessage.ok(getService().selectByKeyAndRoleId(key, roleIdList));
71+
return ResponseMessage.ok(getService().selectByKeyAndRoleId(key, getRoleId()));
72+
}
73+
74+
@RequestMapping(value = "/{key}/md5", method = RequestMethod.GET)
75+
public ResponseMessage getMd5(@PathVariable String key) {
76+
return ResponseMessage.ok(moduleMetaService.selectMD5SingleByKeyAndRoleId(key, getRoleId()));
7477
}
78+
79+
@RequestMapping(value = "/{key}/single", method = RequestMethod.GET)
80+
public ResponseMessage getSingle(@PathVariable String key) {
81+
return ResponseMessage.ok(moduleMetaService.selectSingleByKeyAndRoleId(key, getRoleId()));
82+
}
83+
7584
}

hsweb-web-service/hsweb-web-service-api/src/main/java/org/hsweb/web/service/form/FormService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ public interface FormService extends GenericService<Form, String> {
9999
*
100100
* @param id 表单ID
101101
* @return html 字符串
102-
* @
103102
*/
103+
@Deprecated
104104
String createViewHtml(String id);
105105

106106
/**
@@ -112,5 +112,5 @@ public interface FormService extends GenericService<Form, String> {
112112
*/
113113
Form selectUsing(String name);
114114

115-
115+
int selectDeployedVersion(String name);
116116
}

hsweb-web-service/hsweb-web-service-api/src/main/java/org/hsweb/web/service/module/ModuleMetaService.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,13 @@
66

77
import java.util.List;
88

9-
/**
10-
* Created by zhouhao on 16-5-10.
11-
*/
129
public interface ModuleMetaService extends GenericService<ModuleMeta, String> {
1310

14-
default List<ModuleMeta> selectByKeyAndRoleId(String key, String... roleId) {
15-
QueryParam queryParam = new QueryParam();
16-
queryParam.where("key", key).and("role_id$IN", roleId);
17-
return this.select(queryParam);
18-
}
11+
List<ModuleMeta> selectByKeyAndRoleId(String key, List<String> roleId);
12+
13+
ModuleMeta selectSingleByKeyAndRoleId(String key, List<String> roleId);
14+
15+
String selectMD5SingleByKeyAndRoleId(String key, List<String> roleId);
1916

2017
default List<ModuleMeta> selectByKey(String key) {
2118
QueryParam queryParam = new QueryParam();

hsweb-web-service/hsweb-web-service-simple/src/main/java/org/hsweb/web/service/impl/form/FormServiceImpl.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ public Form selectLatest(String name) {
146146
@Caching(evict = {
147147
@CacheEvict(value = {CACHE_KEY + ".deploy"}, key = "'deploy.'+target.selectByPk(#formId).getName()+'.html'"),
148148
@CacheEvict(value = {CACHE_KEY + ".deploy"}, key = "'deploy.'+target.selectByPk(#formId).getName()"),
149-
@CacheEvict(value = {CACHE_KEY}, key = "'using.'+target.selectByPk(#formId).getName()")
149+
@CacheEvict(value = {CACHE_KEY}, key = "'using.'+target.selectByPk(#formId).getName()"),
150+
@CacheEvict(value = {CACHE_KEY}, key = "'deploy.'+target.selectByPk(#formId).getName()+'.version'")
150151
})
151152
public void deploy(String formId) throws SQLException {
152153
Form old = this.selectByPk(formId);
@@ -175,7 +176,8 @@ public void deploy(String formId) throws SQLException {
175176
@Caching(evict = {
176177
@CacheEvict(value = {CACHE_KEY + ".deploy"}, key = "'deploy.'+target.selectByPk(#formId).getName()+'.html'"),
177178
@CacheEvict(value = {CACHE_KEY + ".deploy"}, key = "'deploy.'+target.selectByPk(#formId).getName()"),
178-
@CacheEvict(value = {CACHE_KEY}, key = "'using.'+target.selectByPk(#formId).getName()")
179+
@CacheEvict(value = {CACHE_KEY}, key = "'using.'+target.selectByPk(#formId).getName()"),
180+
@CacheEvict(value = {CACHE_KEY}, key = "'deploy.'+target.selectByPk(#formId).getName()+'.version'")
179181
})
180182
public void unDeploy(String formId) {
181183
Form old = this.selectByPk(formId);
@@ -217,4 +219,10 @@ public String createViewHtml(String formId) {
217219
public Form selectUsing(String name) {
218220
return formMapper.selectUsing(name);
219221
}
222+
223+
@Override
224+
@Cacheable(value = CACHE_KEY, key = "'deploy.'+#name+'.version'")
225+
public int selectDeployedVersion(String name) {
226+
return selectDeployed(name).getRelease();
227+
}
220228
}

0 commit comments

Comments
 (0)