代码优化,优化工具类,尽量都切换成hutool

This commit is contained in:
zsvg
2022-11-20 17:45:15 +08:00
parent d823f1aaa0
commit e94b90ccfa
12 changed files with 410 additions and 0 deletions
@@ -0,0 +1,23 @@
package vboot.app.my.demo.cate;
import lombok.Getter;
import lombok.Setter;
import vboot.core.common.mvc.pojo.Ztree;
import java.util.Date;
@Getter
@Setter
public class MyDemoCateVo extends Ztree {
private Date crtim;
private String crman;
private Date uptim;
private String upman;
private String notes;
}
@@ -0,0 +1,29 @@
package vboot.core.common.exception.base;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import vboot.core.common.utils.extra.SpringUtils;
/**
* 获取i18n资源文件
*
* @author Lion Li
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class MessageUtils {
private static final MessageSource MESSAGE_SOURCE = SpringUtils.getBean(MessageSource.class);
/**
* 根据消息键和参数 获取消息 委托给spring messageSource
*
* @param code 消息键
* @param args 参数
* @return 获取国际化翻译值
*/
public static String message(String code, Object... args) {
return MESSAGE_SOURCE.getMessage(code, args, LocaleContextHolder.getLocale());
}
}
@@ -0,0 +1,14 @@
package vboot.core.common.mvc.pojo;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
@Data
public class ZidNamePid {
private String id;
private String name;
private String pid;
}
@@ -0,0 +1,11 @@
package vboot.core.common.mvc.pojo;
import lombok.Data;
@Data
public class ZidOrnum {
private String id;
private Integer ornum;
}
@@ -0,0 +1,65 @@
package vboot.core.common.utils.extra;
import cn.hutool.extra.spring.SpringUtil;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.stereotype.Component;
/**
* spring工具类
*
* @author Lion Li
*/
@Component
public final class SpringUtils extends SpringUtil {
/**
* 如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true
*
* @param name
* @return boolean
*/
public static boolean containsBean(String name) {
return getBeanFactory().containsBean(name);
}
/**
* 判断以给定名字注册的bean定义是一个singleton还是一个prototype。
* 如果与给定名字相应的bean定义没有被找到,将会抛出一个异常(NoSuchBeanDefinitionException)
*
* @param name
* @return boolean
*/
public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
return getBeanFactory().isSingleton(name);
}
/**
* @param name
* @return Class 注册对象的类型
*/
public static Class<?> getType(String name) throws NoSuchBeanDefinitionException {
return getBeanFactory().getType(name);
}
/**
* 如果给定的bean名字在bean定义中有别名,则返回这些别名
*
* @param name
*/
public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {
return getBeanFactory().getAliases(name);
}
/**
* 获取aop代理对象
*
* @param invoker
* @return
*/
@SuppressWarnings("unchecked")
public static <T> T getAopProxy(T invoker) {
return (T) AopContext.currentProxy();
}
}
@@ -0,0 +1,39 @@
package vboot.core.common.utils.lang;
import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.SecureUtil;
import java.util.Random;
public class IdUtils {
public static String getUID()
{
return IdUtil.getSnowflake().nextIdStr();
}
public static String getRSID(int length) {
String val = "";
Random random = new Random();
//参数length,表示生成几位随机数
for(int i = 0; i < length; i++) {
String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
//输出字母还是数字
if( "char".equalsIgnoreCase(charOrNum) ) {
//输出是大写字母还是小写字母
// int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
val += (char)(random.nextInt(26) + 97);
} else if( "num".equalsIgnoreCase(charOrNum) ) {
val += String.valueOf(random.nextInt(10));
}
}
return val;
}
public static String getRSID4() { return getRSID(4); }
public static String getRSID8() {
return getRSID(8);
}
}
@@ -0,0 +1,11 @@
package vboot.core.common.utils.lang;
import cn.hutool.crypto.SecureUtil;
public class SecureUtils {
public static String passwordEncrypt(String password){
return SecureUtil.md5("abc" + password + "xyz");
}
}
@@ -0,0 +1,85 @@
package vboot.core.common.utils.lang;
import vboot.core.common.mvc.pojo.Ztree;
import java.util.ArrayList;
import java.util.List;
public class TreeUtils {
//高性能转换,但是要求顶层有pid,这里假设为0
// public static List<Ztree> buildTree(List<Ztree> pidList) {
// Map<String, List<Ztree>> pidListMap = pidList.stream().collect(Collectors.groupingBy(Ztree::getPid));
// pidList.stream().forEach(item -> item.setChildren(pidListMap.get(item.getId())));
// //返回结果也改为返回顶层节点的list
// return pidListMap.get("0");
// }
public static List<Ztree> build(List<Ztree> nodes) {
List<Ztree> list = new ArrayList<>();
for (Ztree node : nodes) {
if (node.getPid() == null) {
list.add(findZtreeChildrenByTier(node, nodes));
} else {
boolean flag = false;
for (Ztree node2 : nodes) {
if (node.getPid().equals(node2.getId())) {
flag = true;
break;
}
}
if (!flag) {
list.add(findZtreeChildrenByTier(node, nodes));
}
}
}
return list;
}
//递归查找子节点
private static Ztree findZtreeChildrenByTier(Ztree node, List<Ztree> nodes) {
for (Ztree item : nodes) {
if (node.getId().equals(item.getPid())) {
if (node.getChildren() == null) {
node.setChildren(new ArrayList<>());
}
node.getChildren().add(findZtreeChildrenByTier(item, nodes));
}
}
return node;
}
public static <T extends Ztree> List<T> buildEtree(List<T> nodes) {
List<T> list = new ArrayList<>();
for (T node : nodes) {
if (node.getPid() == null) {
list.add(findEtreeChildrenByTier(node, nodes));
} else {
boolean flag = false;
for (T node2 : nodes) {
if (node.getPid().equals(node2.getId())) {
flag = true;
break;
}
}
if (!flag) {
list.add(findEtreeChildrenByTier(node, nodes));
}
}
}
return list;
}
private static <T extends Ztree> T findEtreeChildrenByTier(T node, List<T> nodes) {
for (T item : nodes) {
if (node.getId().equals(item.getPid())) {
if (node.getChildren() == null) {
node.setChildren(new ArrayList<>());
}
node.getChildren().add(findEtreeChildrenByTier(item, nodes));
}
}
return node;
}
}
@@ -0,0 +1,28 @@
package vboot.core.framework.security.pojo;
import lombok.Data;
@Data
public class UserVo {
private String id;//用户ID
private String name;//用户姓名
private String usnam;//用户名
private String monum;//手机号
private String label;//账号标签
private String type;//账号类型
private String depid;//部门id,协同用户则是公司id
private String depna;//部门名称,协同用户则是公司名称
private String relog;//前台通知是否查看过的标记
private String avimg;//头像
}
@@ -0,0 +1,74 @@
package vboot.core.module.gen.user;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import vboot.core.common.mvc.api.R;
import vboot.core.common.mvc.dao.JdbcDao;
import vboot.core.common.mvc.dao.Sqler;
import vboot.core.common.mvc.pojo.Ztree;
import vboot.core.common.utils.web.XuserUtil;
import vboot.core.framework.security.pojo.Zuser;
import vboot.core.module.ass.oss.main.AssOssMainService;
import vboot.core.module.ass.oss.main.Zfile;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("gen/user")
public class GenUserApi {
//查询角色树
@GetMapping("info")
public R getInfo(Authentication auth) {
Zuser zuser = (Zuser) auth.getPrincipal();
if("org".equals(zuser.getLabel())){
Sqler sqler = new Sqler("t.*","sys_org_user");
sqler.addEqual("t.id",zuser.getId());
Map<String, Object> map = jdbcDao.findMap(sqler);
return R.ok(map);
}else{
return R.ok();
}
}
@PostMapping("upload")
@ApiOperation("头像上传")
public R upload(@RequestParam(value = "file", required = false) MultipartFile file) throws IOException, NoSuchAlgorithmException {
Zfile zfile = service.uploadFile(file);
String sql="update sys_org_user set avsrc=?,avimg=? where id=?";
jdbcDao.update(sql,zfile.getId(),zfile.getId(),XuserUtil.getUserId());
return R.ok(zfile);
}
@PostMapping("clip")
@ApiOperation("头像上传")
public R clip(@RequestParam(value = "file", required = false) MultipartFile file) throws IOException, NoSuchAlgorithmException {
Zfile zfile = service.uploadFile(file);
String sql="update sys_org_user set avimg=? where id=?";
jdbcDao.update(sql,zfile.getId(),XuserUtil.getUserId());
return R.ok(zfile);
}
// @PostMapping("clip")
// @ApiOperation("头像裁剪")
// public R clip(String img) {
// String sql="update sys_org_user set avimg=? where id=?";
// jdbcDao.update(sql,img,XuserUtil.getUserId());
// return R.ok();
// }
@Autowired
private AssOssMainService service;
@Autowired
private JdbcDao jdbcDao;
}
@@ -0,0 +1,10 @@
package vboot.core.module.sys.api.main;
import lombok.Data;
@Data
public class SysApiMainVo {
private String id;
private String pid;
}
@@ -0,0 +1,21 @@
package vboot.core.module.sys.org.dept;
import lombok.Getter;
import lombok.Setter;
import vboot.core.common.mvc.pojo.Ztree;
import java.util.Date;
@Getter
@Setter
public class SysOrgDeptVo extends Ztree {
private Date crtim;
private Date uptim;
private String notes;
private Integer ornum;
}