user表role改为role_id;后台管理用户增删改查封装成component
This commit is contained in:
@@ -28,5 +28,14 @@
|
||||
<artifactId>spring-beans</artifactId>
|
||||
<version>5.3.22</version>
|
||||
</dependency>
|
||||
<!-- 分页 -->
|
||||
<dependency>
|
||||
<groupId>com.github.pagehelper</groupId>
|
||||
<artifactId>pagehelper-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.fastjson2</groupId>
|
||||
<artifactId>fastjson2</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.cxyxiaomo.epp.common.enums;
|
||||
|
||||
public enum EditType {
|
||||
PLAIN_TEXT("plainText"),
|
||||
INPUT("input"),
|
||||
SELECT("select");
|
||||
|
||||
private final String value;
|
||||
|
||||
private EditType(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.cxyxiaomo.epp.common.enums;
|
||||
|
||||
public enum SearchType {
|
||||
CAN_NOT_SEARCH(null),
|
||||
INPUT("input"),
|
||||
SELECT("select");
|
||||
|
||||
private final String value;
|
||||
|
||||
private SearchType(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.cxyxiaomo.epp.common.pojo;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
// 数据库关系映射
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@Accessors(chain = true) // 链式写法
|
||||
// 微服务必须要实现Serializable
|
||||
public class Role implements Serializable {
|
||||
private Integer id;
|
||||
private String roleName;
|
||||
}
|
||||
@@ -20,7 +20,7 @@ public class User implements Serializable {
|
||||
private String realname;
|
||||
private String idNumber;
|
||||
private String phoneNumber;
|
||||
private Integer role;
|
||||
private Integer roleId;
|
||||
private String buildingId;
|
||||
private String doorplate;
|
||||
private String permission;
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.cxyxiaomo.epp.common.query;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public class PageQuery {
|
||||
|
||||
private int pageIndex = 1; // 第几页
|
||||
private int pageSize = 10; // 每页几条数据
|
||||
|
||||
public void setPageIndex(int page) {
|
||||
if (page < 1) {
|
||||
page = 1;
|
||||
}
|
||||
this.pageIndex = page;
|
||||
}
|
||||
|
||||
public void setPageSize(int pageSize) {
|
||||
if (pageSize < 1) {
|
||||
pageSize = 1;
|
||||
}
|
||||
if (pageSize > 20) {
|
||||
pageSize = 20;
|
||||
}
|
||||
this.pageSize = pageSize;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.cxyxiaomo.epp.common.utils;
|
||||
|
||||
import com.alibaba.fastjson2.JSONArray;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.cxyxiaomo.epp.common.enums.EditType;
|
||||
import com.cxyxiaomo.epp.common.enums.SearchType;
|
||||
|
||||
public class PageTableFieldBuilder {
|
||||
|
||||
private JSONArray columns;
|
||||
|
||||
public static PageTableFieldBuilder create() {
|
||||
PageTableFieldBuilder builder = new PageTableFieldBuilder();
|
||||
builder.columns = new JSONArray();
|
||||
return builder;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param field 对应 POJO 中的属性名称 <br>
|
||||
* 用于新增/修改弹窗 <br>
|
||||
* @param prop 显示的字段名 如果需要翻译(例如roleId->roleName)则填写翻译后的字段 <br>
|
||||
* 用于渲染表格时指定显示列 <br>
|
||||
* @param label 列的显示名称 <br>
|
||||
* @param canSearch 该字段能否在表格上方被筛选 <br>
|
||||
* @param searchType 该筛选字段显示为什么类型 <br>
|
||||
* @param canEdit 该字段能否在新增/修改弹窗中被修改 <br>
|
||||
* @param editType 新增/修改弹窗中该字段显示为什么类型 <br>
|
||||
* @param defaultWhenAdd 新增弹窗中的默认值 <br>
|
||||
* @return
|
||||
*/
|
||||
public PageTableFieldBuilder add(String field, String prop, String label,
|
||||
Boolean canSearch, SearchType searchType,
|
||||
Boolean canEdit, EditType editType, Object defaultWhenAdd) {
|
||||
JSONObject jsonObject = new JSONObject(2);
|
||||
// 表格数据
|
||||
jsonObject.put("prop", prop);
|
||||
jsonObject.put("label", label);
|
||||
|
||||
// 上方筛选条件
|
||||
jsonObject.put("canSearch", canSearch);
|
||||
jsonObject.put("searchType", searchType.getValue());
|
||||
|
||||
// 新增、修改弹窗
|
||||
jsonObject.put("field", field);
|
||||
jsonObject.put("canEdit", canEdit);
|
||||
jsonObject.put("editType", editType.getValue());
|
||||
|
||||
// 新增弹窗 字段默认值
|
||||
jsonObject.put("default", defaultWhenAdd);
|
||||
|
||||
columns.add(jsonObject);
|
||||
return this;
|
||||
}
|
||||
|
||||
public JSONArray build() {
|
||||
return columns;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.cxyxiaomo.epp.common.utils;
|
||||
|
||||
import com.alibaba.fastjson2.JSONArray;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class PageTableFieldMapperBuilder {
|
||||
|
||||
private JSONArray columns;
|
||||
|
||||
public static PageTableFieldMapperBuilder create() {
|
||||
PageTableFieldMapperBuilder builder = new PageTableFieldMapperBuilder();
|
||||
builder.columns = new JSONArray();
|
||||
return builder;
|
||||
}
|
||||
|
||||
// public PageTableFieldMapperBuilder add(String prop, String label, List mapper) {
|
||||
// if (mapper == null || mapper.size() == 0) {
|
||||
// return this;
|
||||
// }
|
||||
// JSONObject jsonObject = new JSONObject(2);
|
||||
// jsonObject.put("key", prop);
|
||||
// jsonObject.put("value", label);
|
||||
// jsonObject.put("mapper", mapper);
|
||||
// columns.add(jsonObject);
|
||||
// return this;
|
||||
// }
|
||||
|
||||
public PageTableFieldMapperBuilder add(String prop, String label, HashMap mapper) {
|
||||
JSONObject jsonObject = new JSONObject(2);
|
||||
jsonObject.put("key", prop);
|
||||
jsonObject.put("value", label);
|
||||
jsonObject.put("mapper", mapper);
|
||||
columns.add(jsonObject);
|
||||
return this;
|
||||
}
|
||||
|
||||
public JSONArray build() {
|
||||
return columns;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.cxyxiaomo.epp.common.utils;
|
||||
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class PageUtils {
|
||||
|
||||
/**
|
||||
* 转换PageInfo中的List
|
||||
*
|
||||
* @param pageInfoPo 原来的pageInfo
|
||||
* @param convert 转换方式
|
||||
* @param <P> 原来的类型
|
||||
* @param <V> 转换后的类型
|
||||
* @return 转换后的pageInfo
|
||||
*/
|
||||
public static <P, V> PageInfo<V> convert(PageInfo<P> pageInfoPo, Function<P, V> convert) {
|
||||
//视图pageInfo
|
||||
PageInfo<V> vPageInfo = new PageInfo<>();
|
||||
//copy属性
|
||||
BeanUtils.copyProperties(pageInfoPo, vPageInfo);
|
||||
//转化
|
||||
List<V> vList = pageInfoPo.getList().stream().map(convert).collect(Collectors.toList());
|
||||
//赋值
|
||||
vPageInfo.setList(vList);
|
||||
return vPageInfo;
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,8 @@ import org.springframework.beans.BeanUtils;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
// 数据库关系映射
|
||||
|
||||
@@ -21,7 +23,7 @@ public class UserVO implements Serializable {
|
||||
private String realname;
|
||||
private String idNumber;
|
||||
private String phoneNumber;
|
||||
private Integer role;
|
||||
private Integer roleId;
|
||||
private String buildingId;
|
||||
private String doorplate;
|
||||
private String permission;
|
||||
@@ -35,4 +37,12 @@ public class UserVO implements Serializable {
|
||||
BeanUtils.copyProperties(user, userVO);
|
||||
return userVO;
|
||||
}
|
||||
|
||||
public static List<UserVO> convertFrom(List<User> userList) {
|
||||
if (userList == null) {
|
||||
return null;
|
||||
}
|
||||
List<UserVO> userVOList = userList.stream().map(UserVO::convertFrom).collect(Collectors.toList());
|
||||
return userVOList;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -82,6 +82,16 @@
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 分页 -->
|
||||
<dependency>
|
||||
<groupId>com.github.pagehelper</groupId>
|
||||
<artifactId>pagehelper-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.fastjson2</groupId>
|
||||
<artifactId>fastjson2</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 密码 Hash 加密 -->
|
||||
<dependency>
|
||||
<groupId>commons-codec</groupId>
|
||||
@@ -89,11 +99,10 @@
|
||||
</dependency>
|
||||
|
||||
<!-- 热部署 -->
|
||||
<!--<dependency>-->
|
||||
<!-- <groupId>org.springframework.boot</groupId>-->
|
||||
<!-- <artifactId>spring-boot-devtools</artifactId>-->
|
||||
<!-- <version>2.7.5</version>-->
|
||||
<!--</dependency>-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-devtools</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
||||
@@ -1,25 +1,37 @@
|
||||
package com.cxyxiaomo.epp.user.controller;
|
||||
|
||||
import com.alibaba.fastjson2.JSONArray;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.cxyxiaomo.epp.common.enums.EditType;
|
||||
import com.cxyxiaomo.epp.common.enums.SearchType;
|
||||
import com.cxyxiaomo.epp.common.pojo.Role;
|
||||
import com.cxyxiaomo.epp.common.pojo.User;
|
||||
import com.cxyxiaomo.epp.common.query.PageQuery;
|
||||
import com.cxyxiaomo.epp.common.response.Res;
|
||||
import com.cxyxiaomo.epp.common.utils.PageTableFieldBuilder;
|
||||
import com.cxyxiaomo.epp.common.utils.PageTableFieldMapperBuilder;
|
||||
import com.cxyxiaomo.epp.common.vo.UserVO;
|
||||
import com.cxyxiaomo.epp.user.service.UserServiceImpl;
|
||||
import com.cxyxiaomo.epp.user.service.RoleService;
|
||||
import com.cxyxiaomo.epp.user.service.UserService;
|
||||
import com.github.pagehelper.PageHelper;
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/user")
|
||||
public class UserController {
|
||||
|
||||
@Autowired
|
||||
private UserServiceImpl userService;
|
||||
private UserService userService;
|
||||
|
||||
@Autowired
|
||||
private RoleService roleService;
|
||||
|
||||
/**
|
||||
* 用户登录
|
||||
@@ -58,6 +70,71 @@ public class UserController {
|
||||
return userService.getUserById(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户列表
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/manage/getUserList")
|
||||
@ResponseBody
|
||||
public Res getUserList(PageQuery pageQuery, UserVO userVO) {
|
||||
|
||||
// 查询分页数据
|
||||
PageHelper.startPage(pageQuery.getPageIndex(), pageQuery.getPageSize());
|
||||
List<User> userList = userService.getUserList(userVO);
|
||||
PageInfo<User> userPageInfo = new PageInfo<>(userList);
|
||||
List<User> list = userPageInfo.getList();
|
||||
List<UserVO> voList = UserVO.convertFrom(list);
|
||||
|
||||
// 指定前端表格显示列
|
||||
JSONArray columns = PageTableFieldBuilder.create()
|
||||
.add("username", "username", "用户名",
|
||||
true, SearchType.INPUT,
|
||||
false, EditType.PLAIN_TEXT, null)
|
||||
.add("roleId", "roleName", "角色",
|
||||
true, SearchType.SELECT,
|
||||
true, EditType.SELECT, 0)
|
||||
.add("realname", "realname", "真实姓名",
|
||||
true, SearchType.INPUT,
|
||||
true, EditType.INPUT, "")
|
||||
.add("phoneNumber", "phoneNumber", "电话",
|
||||
true, SearchType.INPUT,
|
||||
true, EditType.INPUT, "")
|
||||
.add("idNumber", "idNumber", "身份证号",
|
||||
true, SearchType.INPUT,
|
||||
true, EditType.INPUT, "")
|
||||
.add("buildingId", "buildingId", "门栋/单元号",
|
||||
true, SearchType.INPUT,
|
||||
true, EditType.INPUT, "")
|
||||
.add("doorplate", "doorplate", "门牌号",
|
||||
true, SearchType.INPUT,
|
||||
true, EditType.INPUT, "")
|
||||
.build();
|
||||
|
||||
// 指定需要翻译的字段
|
||||
// role -> roleName
|
||||
List<Role> roleList = roleService.getRoleList();
|
||||
HashMap roleMap = new HashMap();
|
||||
for (Role role : roleList) {
|
||||
roleMap.put(role.getId(), role.getRoleName());
|
||||
}
|
||||
// build
|
||||
JSONArray fieldMapper = PageTableFieldMapperBuilder.create()
|
||||
.add("roleId", "roleName", roleMap)
|
||||
.build();
|
||||
|
||||
// 拼装返回结果
|
||||
JSONObject map = new JSONObject(2);
|
||||
map.put("total", userPageInfo.getTotal());
|
||||
map.put("list", voList);
|
||||
map.put("columns", columns);
|
||||
map.put("fieldMapper", fieldMapper);
|
||||
|
||||
// 返回结果
|
||||
return Res.success(map);
|
||||
}
|
||||
|
||||
|
||||
@RequestMapping("/person")
|
||||
@ResponseBody
|
||||
public User person(String username) {
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.cxyxiaomo.epp.user.dao;
|
||||
|
||||
import com.cxyxiaomo.epp.common.pojo.Role;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
@Repository
|
||||
public interface RoleDao {
|
||||
|
||||
public List<Role> getRoleList();
|
||||
}
|
||||
@@ -1,9 +1,12 @@
|
||||
package com.cxyxiaomo.epp.user.dao;
|
||||
|
||||
import com.cxyxiaomo.epp.common.pojo.User;
|
||||
import com.cxyxiaomo.epp.common.vo.UserVO;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
@Repository
|
||||
public interface UserDao {
|
||||
@@ -13,4 +16,6 @@ public interface UserDao {
|
||||
public User getUserById(Long id);
|
||||
|
||||
User getUserByUsername(String username);
|
||||
|
||||
public List<User> getUserList(UserVO userVO);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.cxyxiaomo.epp.user.service;
|
||||
|
||||
import com.cxyxiaomo.epp.common.pojo.Role;
|
||||
import com.cxyxiaomo.epp.user.dao.RoleDao;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class RoleService {
|
||||
|
||||
@Autowired
|
||||
private RoleDao roleDao;
|
||||
|
||||
public List<Role> getRoleList() {
|
||||
List<Role> roleList = roleDao.getRoleList();
|
||||
return roleList;
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,29 @@
|
||||
package com.cxyxiaomo.epp.user.service;
|
||||
|
||||
import com.cxyxiaomo.epp.common.pojo.User;
|
||||
import com.cxyxiaomo.epp.common.vo.UserVO;
|
||||
import com.cxyxiaomo.epp.user.dao.UserDao;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
public interface UserService {
|
||||
import java.util.List;
|
||||
|
||||
User getUserByUsername(String username);
|
||||
@Service
|
||||
public class UserService {
|
||||
|
||||
User getUserById(Long id);
|
||||
@Autowired
|
||||
private UserDao userDao;
|
||||
|
||||
public User getUserByUsername(String username) {
|
||||
return userDao.getUserByUsername(username);
|
||||
}
|
||||
|
||||
public User getUserById(Long id) {
|
||||
return userDao.getUserById(id);
|
||||
}
|
||||
|
||||
public List<User> getUserList(UserVO userVO) {
|
||||
List<User> userList = userDao.getUserList(userVO);
|
||||
return userList;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,23 +0,0 @@
|
||||
package com.cxyxiaomo.epp.user.service;
|
||||
|
||||
import com.cxyxiaomo.epp.common.pojo.User;
|
||||
import com.cxyxiaomo.epp.user.dao.UserDao;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class UserServiceImpl implements UserService {
|
||||
|
||||
@Autowired
|
||||
private UserDao userDao;
|
||||
|
||||
@Override
|
||||
public User getUserByUsername(String username) {
|
||||
return userDao.getUserByUsername(username);
|
||||
}
|
||||
|
||||
@Override
|
||||
public User getUserById(Long id) {
|
||||
return userDao.getUserById(id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.cxyxiaomo.epp.user.dao.RoleDao">
|
||||
<select id="getRoleList" resultType="com.cxyxiaomo.epp.common.pojo.Role">
|
||||
select * from role
|
||||
</select>
|
||||
</mapper>
|
||||
@@ -8,10 +8,39 @@
|
||||
VALUES (#{username}, #{password})
|
||||
</insert>
|
||||
<select id="getUserById" parameterType="java.lang.Long" resultType="com.cxyxiaomo.epp.common.pojo.User">
|
||||
SELECT * FROM user
|
||||
SELECT *
|
||||
FROM user
|
||||
WHERE id = #{id}
|
||||
</select>
|
||||
<select id="getUserByUsername" resultType="com.cxyxiaomo.epp.common.pojo.User">
|
||||
select * from user where username = #{username}
|
||||
select *
|
||||
from user
|
||||
where username = #{username}
|
||||
</select>
|
||||
<select id="getUserList" resultType="com.cxyxiaomo.epp.common.pojo.User">
|
||||
select *
|
||||
from user
|
||||
where 1 = 1
|
||||
<if test="roleId != null">
|
||||
AND role_id = #{roleId}
|
||||
</if>
|
||||
<if test="username != null && username != ''">
|
||||
AND username LIKE concat('%',#{username,jdbcType=VARCHAR},'%')
|
||||
</if>
|
||||
<if test="realname != null && realname != ''">
|
||||
AND realname LIKE concat('%',#{realname,jdbcType=VARCHAR},'%')
|
||||
</if>
|
||||
<if test="idNumber != null && idNumber != ''">
|
||||
AND id_number LIKE concat('%',#{idNumber,jdbcType=VARCHAR},'%')
|
||||
</if>
|
||||
<if test="phoneNumber != null && phoneNumber != ''">
|
||||
AND phone_number LIKE concat('%',#{phoneNumber,jdbcType=VARCHAR},'%')
|
||||
</if>
|
||||
<if test="buildingId != null && buildingId != ''">
|
||||
AND building_id LIKE concat('%',#{buildingId,jdbcType=VARCHAR},'%')
|
||||
</if>
|
||||
<if test="doorplate != null && doorplate != ''">
|
||||
AND doorplate LIKE concat('%',#{doorplate,jdbcType=VARCHAR},'%')
|
||||
</if>
|
||||
</select>
|
||||
</mapper>
|
||||
|
||||
@@ -135,6 +135,13 @@
|
||||
<version>2.7.6</version>
|
||||
</dependency>
|
||||
|
||||
<!-- 分页 -->
|
||||
<dependency>
|
||||
<groupId>com.github.pagehelper</groupId>
|
||||
<artifactId>pagehelper-spring-boot-starter</artifactId>
|
||||
<version>1.4.6</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Fastjson -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.fastjson2</groupId>
|
||||
|
||||
Reference in New Issue
Block a user