init project v1.0.0
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
package com.central;
|
||||
|
||||
import com.central.common.annotation.EnableLoginArgResolver;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
*/
|
||||
@EnableLoginArgResolver
|
||||
@EnableDiscoveryClient
|
||||
@EnableFeignClients
|
||||
@EnableTransactionManagement
|
||||
@SpringBootApplication
|
||||
public class UserCenterApp {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(UserCenterApp.class, args);
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package com.central.user.config;
|
||||
|
||||
import com.central.common.config.DefaultAsycTaskConfig;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
* 线程池配置、启用异步
|
||||
* @Async quartz 需要使用
|
||||
*/
|
||||
@Configuration
|
||||
public class AsycTaskExecutorConfig extends DefaultAsycTaskConfig {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.central.user.config;
|
||||
|
||||
import com.central.common.exception.DefaultExceptionAdvice;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
* @date 2018/12/22
|
||||
*/
|
||||
@ControllerAdvice
|
||||
public class ExceptionAdvice extends DefaultExceptionAdvice {
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.central.user.config;
|
||||
|
||||
import com.baomidou.mybatisplus.core.injector.ISqlInjector;
|
||||
import com.baomidou.mybatisplus.extension.injector.LogicSqlInjector;
|
||||
import com.central.db.config.DefaultMybatisPlusConfig;
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
* @date 2018/12/10
|
||||
*/
|
||||
@Configuration
|
||||
@MapperScan({"com.central.user.mapper*"})
|
||||
public class MybatisPlusConfig extends DefaultMybatisPlusConfig {
|
||||
@Bean
|
||||
public ISqlInjector sqlInjector(){
|
||||
return new LogicSqlInjector();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.central.user.config;
|
||||
|
||||
import com.central.common.config.DefaultPasswordConfig;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
* @date 2019/1/2
|
||||
*/
|
||||
@Configuration
|
||||
public class PasswordConfig extends DefaultPasswordConfig {
|
||||
}
|
||||
+175
@@ -0,0 +1,175 @@
|
||||
package com.central.user.controller;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import com.central.common.annotation.LoginUser;
|
||||
import com.central.common.constant.CommonConstant;
|
||||
import com.central.common.model.*;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang.ObjectUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import com.central.user.service.ISysMenuService;
|
||||
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
*/
|
||||
@RestController
|
||||
@Api(tags = "菜单模块api")
|
||||
@Slf4j
|
||||
@RequestMapping("/menus")
|
||||
public class SysMenuController {
|
||||
@Autowired
|
||||
private ISysMenuService menuService;
|
||||
|
||||
/**
|
||||
* 两层循环实现建树
|
||||
*
|
||||
* @param sysMenus
|
||||
* @return
|
||||
*/
|
||||
public static List<SysMenu> treeBuilder(List<SysMenu> sysMenus) {
|
||||
List<SysMenu> menus = new ArrayList<>();
|
||||
for (SysMenu sysMenu : sysMenus) {
|
||||
if (ObjectUtils.equals(-1L, sysMenu.getParentId())) {
|
||||
menus.add(sysMenu);
|
||||
}
|
||||
for (SysMenu menu : sysMenus) {
|
||||
if (menu.getParentId().equals(sysMenu.getId())) {
|
||||
if (sysMenu.getSubMenus() == null) {
|
||||
sysMenu.setSubMenus(new ArrayList<>());
|
||||
}
|
||||
sysMenu.getSubMenus().add(menu);
|
||||
}
|
||||
}
|
||||
}
|
||||
return menus;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除菜单
|
||||
*
|
||||
* @param id
|
||||
*/
|
||||
@ApiOperation(value = "删除菜单")
|
||||
@DeleteMapping("/{id}")
|
||||
public Result delete(@PathVariable Long id) {
|
||||
try {
|
||||
menuService.removeById(id);
|
||||
return Result.succeed("操作成功");
|
||||
} catch (Exception ex) {
|
||||
log.error("memu-delete-error", ex);
|
||||
return Result.failed("操作失败");
|
||||
}
|
||||
}
|
||||
|
||||
@ApiOperation(value = "根据roleId获取对应的菜单")
|
||||
@GetMapping("/{roleId}/menus")
|
||||
public List<Map<String, Object>> findMenusByRoleId(@PathVariable Long roleId) {
|
||||
Set<Long> roleIds = new HashSet<Long>() {{
|
||||
add(roleId);
|
||||
}};
|
||||
//获取该角色对应的菜单
|
||||
List<SysMenu> roleMenus = menuService.findByRoles(roleIds);
|
||||
//全部的菜单列表
|
||||
List<SysMenu> allMenus = menuService.findAll();
|
||||
List<Map<String, Object>> authTrees = new ArrayList<>();
|
||||
|
||||
Map<Long, SysMenu> roleMenusMap = roleMenus.stream().collect(Collectors.toMap(SysMenu::getId, SysMenu -> SysMenu));
|
||||
|
||||
for (SysMenu sysMenu : allMenus) {
|
||||
Map<String, Object> authTree = new HashMap<>();
|
||||
authTree.put("id", sysMenu.getId());
|
||||
authTree.put("name", sysMenu.getName());
|
||||
authTree.put("pId", sysMenu.getParentId());
|
||||
authTree.put("open", true);
|
||||
authTree.put("checked", false);
|
||||
if (roleMenusMap.get(sysMenu.getId()) != null) {
|
||||
authTree.put("checked", true);
|
||||
}
|
||||
authTrees.add(authTree);
|
||||
}
|
||||
return authTrees;
|
||||
}
|
||||
|
||||
@ApiOperation(value = "根据roleCodes获取对应的权限")
|
||||
@SuppressWarnings("unchecked")
|
||||
@Cacheable(value = "menu", key ="#roleCodes")
|
||||
@GetMapping("/{roleCodes}")
|
||||
public List<SysMenu> findMenuByRoles(@PathVariable String roleCodes) {
|
||||
List<SysMenu> result = null;
|
||||
if (StringUtils.isNotEmpty(roleCodes)) {
|
||||
Set<String> roleSet = (Set<String>)Convert.toCollection(HashSet.class, String.class, roleCodes);
|
||||
result = menuService.findByRoleCodes(roleSet, CommonConstant.PERMISSION);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 给角色分配菜单
|
||||
*/
|
||||
@ApiOperation(value = "角色分配菜单")
|
||||
@PostMapping("/granted")
|
||||
public Result setMenuToRole(@RequestBody SysMenu sysMenu) {
|
||||
menuService.setMenuToRole(sysMenu.getRoleId(), sysMenu.getMenuIds());
|
||||
return Result.succeed("操作成功");
|
||||
}
|
||||
|
||||
@ApiOperation(value = "查询所有菜单")
|
||||
@GetMapping("/findAlls")
|
||||
public PageResult<SysMenu> findAlls() {
|
||||
List<SysMenu> list = menuService.findAll();
|
||||
return PageResult.<SysMenu>builder().data(list).code(0).count((long) list.size()).build();
|
||||
}
|
||||
|
||||
@ApiOperation(value = "获取菜单以及顶级菜单")
|
||||
@GetMapping("/findOnes")
|
||||
public PageResult<SysMenu> findOnes() {
|
||||
List<SysMenu> list = menuService.findOnes();
|
||||
return PageResult.<SysMenu>builder().data(list).code(0).count((long) list.size()).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加菜单 或者 更新
|
||||
*
|
||||
* @param menu
|
||||
* @return
|
||||
*/
|
||||
@ApiOperation(value = "新增菜单")
|
||||
@PostMapping("saveOrUpdate")
|
||||
public Result saveOrUpdate(@RequestBody SysMenu menu) {
|
||||
try {
|
||||
menuService.saveOrUpdate(menu);
|
||||
return Result.succeed("操作成功");
|
||||
} catch (Exception ex) {
|
||||
log.error("memu-saveOrUpdate-error", ex);
|
||||
return Result.failed("操作失败");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 当前登录用户的菜单
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/current")
|
||||
@ApiOperation(value = "查询当前用户菜单")
|
||||
public List<SysMenu> findMyMenu(@LoginUser SysUser user) {
|
||||
List<SysRole> roles = user.getRoles();
|
||||
if (CollectionUtil.isEmpty(roles)) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
List<SysMenu> menus = menuService.findByRoleCodes(roles.parallelStream().map(SysRole::getCode).collect(Collectors.toSet()), CommonConstant.MENU);
|
||||
List<SysMenu> sysMenus = treeBuilder(menus);
|
||||
return sysMenus;
|
||||
}
|
||||
}
|
||||
+76
@@ -0,0 +1,76 @@
|
||||
package com.central.user.controller;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.central.common.model.PageResult;
|
||||
import com.central.common.model.Result;
|
||||
import com.central.common.model.SysRole;
|
||||
import com.central.user.service.ISysRoleService;
|
||||
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
* 角色管理
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@Api(tags = "角色模块api")
|
||||
public class SysRoleController {
|
||||
@Autowired
|
||||
private ISysRoleService sysRoleService;
|
||||
|
||||
/**
|
||||
* 后台管理查询角色
|
||||
* @param params
|
||||
* @return
|
||||
*/
|
||||
@ApiOperation(value = "后台管理查询角色")
|
||||
@GetMapping("/roles")
|
||||
public PageResult<SysRole> findRoles(@RequestParam Map<String, Object> params) {
|
||||
return sysRoleService.findRoles(params);
|
||||
}
|
||||
|
||||
/**
|
||||
* 角色新增或者更新
|
||||
*
|
||||
* @param sysRole
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/roles/saveOrUpdate")
|
||||
public Result saveOrUpdate(@RequestBody SysRole sysRole) {
|
||||
return sysRoleService.saveOrUpdateRole(sysRole);
|
||||
}
|
||||
|
||||
/**
|
||||
* 后台管理删除角色
|
||||
* delete /role/1
|
||||
*
|
||||
* @param id
|
||||
*/
|
||||
@ApiOperation(value = "后台管理删除角色")
|
||||
@DeleteMapping("/roles/{id}")
|
||||
public Result deleteRole(@PathVariable Long id) {
|
||||
try {
|
||||
if (id == 1L) {
|
||||
return Result.failed("管理员不可以删除");
|
||||
}
|
||||
sysRoleService.deleteRole(id);
|
||||
return Result.succeed("操作成功");
|
||||
} catch (Exception e) {
|
||||
log.error("role-deleteRole-error", e);
|
||||
return Result.failed("操作失败");
|
||||
}
|
||||
}
|
||||
}
|
||||
+266
@@ -0,0 +1,266 @@
|
||||
package com.central.user.controller;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import com.central.common.annotation.LoginUser;
|
||||
import com.central.common.constant.CommonConstant;
|
||||
import com.central.common.model.*;
|
||||
import com.central.common.utils.ExcelUtil;
|
||||
import com.central.user.model.SysUserExcel;
|
||||
import org.apache.commons.collections4.MapUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.cache.annotation.CacheEvict;
|
||||
import org.springframework.cache.annotation.CachePut;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import com.central.common.utils.SysUserUtil;
|
||||
import com.central.user.service.ISysUserService;
|
||||
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiImplicitParam;
|
||||
import io.swagger.annotations.ApiImplicitParams;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
* 用户
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RefreshScope
|
||||
@Api(tags = "用户模块api")
|
||||
public class SysUserController {
|
||||
@Autowired
|
||||
private ISysUserService appUserService;
|
||||
|
||||
/**
|
||||
* 当前登录用户 LoginAppUser
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@ApiOperation(value = "根据access_token当前登录用户")
|
||||
@GetMapping("/users/current")
|
||||
public LoginAppUser getLoginAppUser(@LoginUser(isFull = true) SysUser user) {
|
||||
return SysUserUtil.getLoginAppUser(user);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询用户实体对象SysUser
|
||||
*/
|
||||
@GetMapping(value = "/users/name/{username}")
|
||||
@ApiOperation(value = "根据用户名查询用户实体")
|
||||
@Cacheable(value = "user", key = "#username")
|
||||
public SysUser selectByUsername(@PathVariable String username) {
|
||||
return appUserService.selectByUsername(username);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询用户登录对象LoginAppUser
|
||||
*/
|
||||
@GetMapping(value = "/users-anon/login", params = "username")
|
||||
@ApiOperation(value = "根据用户名查询用户")
|
||||
public LoginAppUser findByUsername(String username) {
|
||||
return appUserService.findByUsername(username);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过手机号查询用户、角色信息
|
||||
*
|
||||
* @param mobile 手机号
|
||||
*/
|
||||
@GetMapping(value = "/users-anon/mobile", params = "mobile")
|
||||
@ApiOperation(value = "根据手机号查询用户")
|
||||
public SysUser findByMobile(String mobile) {
|
||||
return appUserService.findByMobile(mobile);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据OpenId查询用户信息
|
||||
*
|
||||
* @param openId openId
|
||||
*/
|
||||
@GetMapping(value = "/users-anon/openId", params = "openId")
|
||||
@ApiOperation(value = "根据OpenId查询用户")
|
||||
public SysUser findByOpenId(String openId) {
|
||||
return appUserService.findByOpenId(openId);
|
||||
}
|
||||
|
||||
@GetMapping("/users/{id}")
|
||||
public SysUser findUserById(@PathVariable Long id) {
|
||||
return appUserService.getById(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 管理后台修改用户
|
||||
*
|
||||
* @param sysUser
|
||||
*/
|
||||
@PutMapping("/users")
|
||||
@CachePut(value = "user", key = "#sysUser.username")
|
||||
public void updateSysUser(@RequestBody SysUser sysUser) {
|
||||
appUserService.updateById(sysUser);
|
||||
}
|
||||
|
||||
/**
|
||||
* 管理后台给用户分配角色
|
||||
*
|
||||
* @param id
|
||||
* @param roleIds
|
||||
*/
|
||||
@PostMapping("/users/{id}/roles")
|
||||
public void setRoleToUser(@PathVariable Long id, @RequestBody Set<Long> roleIds) {
|
||||
appUserService.setRoleToUser(id, roleIds);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户的角色
|
||||
*
|
||||
* @param
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/users/{id}/roles")
|
||||
public List<SysRole> findRolesByUserId(@PathVariable Long id) {
|
||||
return appUserService.findRolesByUserId(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户查询
|
||||
*
|
||||
* @param params
|
||||
* @return
|
||||
*/
|
||||
@ApiOperation(value = "用户查询列表")
|
||||
@ApiImplicitParams({
|
||||
@ApiImplicitParam(name = "page", value = "分页起始位置", required = true, dataType = "Integer"),
|
||||
@ApiImplicitParam(name = "limit", value = "分页结束位置", required = true, dataType = "Integer")
|
||||
})
|
||||
@GetMapping("/users")
|
||||
public PageResult<SysUser> findUsers(@RequestParam Map<String, Object> params) {
|
||||
return appUserService.findUsers(params);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改用户状态
|
||||
*
|
||||
* @param params
|
||||
* @return
|
||||
*/
|
||||
@ApiOperation(value = "修改用户状态")
|
||||
@GetMapping("/users/updateEnabled")
|
||||
@ApiImplicitParams({
|
||||
@ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "Integer"),
|
||||
@ApiImplicitParam(name = "enabled", value = "是否启用", required = true, dataType = "Boolean")
|
||||
})
|
||||
public Result updateEnabled(@RequestParam Map<String, Object> params) {
|
||||
Long id = MapUtils.getLong(params, "id");
|
||||
if (checkAdmin(id)) {
|
||||
return Result.failed("超级管理员不给予修改");
|
||||
}
|
||||
return appUserService.updateEnabled(params);
|
||||
}
|
||||
|
||||
/**
|
||||
* 管理后台,给用户重置密码
|
||||
*
|
||||
* @param id
|
||||
*/
|
||||
@PutMapping(value = "/users/{id}/password")
|
||||
public Result resetPassword(@PathVariable Long id) {
|
||||
if (checkAdmin(id)) {
|
||||
return Result.failed("超级管理员不给予修改");
|
||||
}
|
||||
appUserService.updatePassword(id, null, null);
|
||||
return Result.succeed("重置成功");
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户自己修改密码
|
||||
*/
|
||||
@PutMapping(value = "/users/password")
|
||||
public Result resetPassword(@RequestBody SysUser sysUser) {
|
||||
if (checkAdmin(sysUser.getId())) {
|
||||
return Result.failed("超级管理员不给予修改");
|
||||
}
|
||||
appUserService.updatePassword(sysUser.getId(), sysUser.getOldPassword(), sysUser.getNewPassword());
|
||||
return Result.succeed("重置成功");
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除用户
|
||||
*
|
||||
* @param id
|
||||
*/
|
||||
@DeleteMapping(value = "/users/{id}")
|
||||
public Result delete(@PathVariable Long id) {
|
||||
if (checkAdmin(id)) {
|
||||
return Result.failed("超级管理员不给予修改");
|
||||
}
|
||||
appUserService.delUser(id);
|
||||
return Result.succeed("删除成功");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 新增or更新
|
||||
*
|
||||
* @param sysUser
|
||||
* @return
|
||||
*/
|
||||
@CacheEvict(value = "user", key = "#sysUser.username")
|
||||
@PostMapping("/users/saveOrUpdate")
|
||||
public Result saveOrUpdate(@RequestBody SysUser sysUser) {
|
||||
return appUserService.saveOrUpdateUser(sysUser);
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出excel
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/users/export")
|
||||
public void exportUser(@RequestParam Map<String, Object> params, HttpServletResponse response) throws IOException {
|
||||
List<SysUserExcel> result = appUserService.findAllUsers(params);
|
||||
//导出操作
|
||||
ExcelUtil.exportExcel(result, null, "用户", SysUserExcel.class, "test", response);
|
||||
}
|
||||
|
||||
@PostMapping(value = "/users/import")
|
||||
public Result importExcl(@RequestParam("file") MultipartFile excl) throws Exception {
|
||||
int rowNum = 0;
|
||||
if(!excl.isEmpty()) {
|
||||
List<SysUserExcel> list = ExcelUtil.importExcel(excl, 0, 1, SysUserExcel.class);
|
||||
rowNum = list.size();
|
||||
if (rowNum > 0) {
|
||||
List<SysUser> users = new ArrayList<>(rowNum);
|
||||
list.forEach(u -> {
|
||||
SysUser user = new SysUser();
|
||||
BeanUtil.copyProperties(u, user);
|
||||
user.setPassword(CommonConstant.DEF_USER_PASSWORD);
|
||||
user.setType(UserType.BACKEND.name());
|
||||
users.add(user);
|
||||
});
|
||||
appUserService.saveUsers(users);
|
||||
}
|
||||
}
|
||||
return Result.succeed("导入数据成功,一共【"+rowNum+"】行");
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否超级管理员
|
||||
*/
|
||||
private boolean checkAdmin(long id) {
|
||||
return id == 1L;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.central.user.mapper;
|
||||
|
||||
import com.central.db.mapper.SuperMapper;
|
||||
import com.central.common.model.SysMenu;
|
||||
|
||||
/**
|
||||
* 菜单
|
||||
*
|
||||
* @author zlt
|
||||
*/
|
||||
public interface SysMenuMapper extends SuperMapper<SysMenu> {
|
||||
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package com.central.user.mapper;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.ibatis.annotations.Delete;
|
||||
import org.apache.ibatis.annotations.Insert;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Options;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
import org.apache.ibatis.annotations.Update;
|
||||
|
||||
import com.central.common.model.SysPermission;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
* 权限
|
||||
*/
|
||||
@Mapper
|
||||
public interface SysPermissionMapper {
|
||||
|
||||
@Options(useGeneratedKeys = true, keyProperty = "id")
|
||||
@Insert("insert into sys_permission(permission, name, createTime, updateTime) values(#{permission}, #{name}, #{createTime}, #{createTime})")
|
||||
int save(SysPermission sysPermission);
|
||||
|
||||
@Update("update sys_permission t set t.name = #{name}, t.permission = #{permission}, t.updateTime = #{updateTime} where t.id = #{id}")
|
||||
int update(SysPermission sysPermission);
|
||||
|
||||
@Delete("delete from sys_permission where id = #{id}")
|
||||
int delete(Long id);
|
||||
|
||||
@Select("select * from sys_permission t where t.id = #{id}")
|
||||
SysPermission findById(Long id);
|
||||
|
||||
@Select("select * from sys_permission t where t.permission = #{permission}")
|
||||
SysPermission findByPermission(String permission);
|
||||
|
||||
int count(Map<String, Object> params);
|
||||
|
||||
List<SysPermission> findList(Map<String, Object> params);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.central.user.mapper;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.central.db.mapper.SuperMapper;
|
||||
|
||||
import com.central.common.model.SysRole;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
* 角色
|
||||
*/
|
||||
public interface SysRoleMapper extends SuperMapper<SysRole> {
|
||||
List<SysRole> findList(Page<SysRole> page, @Param("r") Map<String, Object> params);
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.central.user.mapper;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.ibatis.annotations.Insert;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import com.central.common.model.SysMenu;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
* 角色菜单
|
||||
*/
|
||||
@Mapper
|
||||
public interface SysRoleMenuMapper {
|
||||
@Insert("insert into sys_role_menu(role_id, menu_id) values(#{roleId}, #{menuId})")
|
||||
int save(@Param("roleId") Long roleId, @Param("menuId") Long menuId);
|
||||
|
||||
int delete(@Param("roleId") Long roleId, @Param("menuId") Long menuId);
|
||||
|
||||
List<SysMenu> findMenusByRoleIds(@Param("roleIds") Set<Long> roleIds, @Param("type") Integer type);
|
||||
|
||||
List<SysMenu> findMenusByRoleCodes(@Param("roleCodes") Set<String> roleCodes, @Param("type") Integer type);
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package com.central.user.mapper;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.ibatis.annotations.Insert;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import com.central.common.model.SysPermission;
|
||||
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
* 角色权限关系
|
||||
*/
|
||||
@Mapper
|
||||
public interface SysRolePermissionMapper {
|
||||
|
||||
@Insert("insert into sys_role_permission(roleId, permissionId) values(#{roleId}, #{permissionId})")
|
||||
int saveRolePermission(@Param("roleId") Long roleId, @Param("permissionId") Long permissionId);
|
||||
|
||||
int deleteRolePermission(@Param("roleId") Long roleId, @Param("permissionId") Long permissionId);
|
||||
|
||||
Set<SysPermission> findPermissionsByRoleIds(@Param("roleIds") Set<Long> roleIds);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.central.user.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.central.db.mapper.SuperMapper;
|
||||
import com.central.common.model.SysUser;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 用户表 Mapper 接口
|
||||
*
|
||||
* @author zlt
|
||||
* @data 2018-10-29
|
||||
*/
|
||||
public interface SysUserMapper extends SuperMapper<SysUser> {
|
||||
/**
|
||||
* 分页查询用户列表
|
||||
* @param page
|
||||
* @param params
|
||||
* @return
|
||||
*/
|
||||
List<SysUser> findList(Page<SysUser> page, @Param("u") Map<String, Object> params);
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.central.user.mapper;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.ibatis.annotations.Insert;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
|
||||
import com.central.common.model.SysRole;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
* 用户角色关系
|
||||
*/
|
||||
@Mapper
|
||||
public interface SysUserRoleMapper {
|
||||
int deleteUserRole(@Param("userId") Long userId, @Param("roleId") Long roleId);
|
||||
|
||||
@Insert("insert into sys_role_user(user_id, role_id) values(#{userId}, #{roleId})")
|
||||
int saveUserRoles(@Param("userId") Long userId, @Param("roleId") Long roleId);
|
||||
|
||||
/**
|
||||
* 根据用户id获取角色
|
||||
*
|
||||
* @param userId
|
||||
* @return
|
||||
*/
|
||||
@Select("select r.* from sys_role_user ru inner join sys_role r on r.id = ru.role_id where ru.user_id = #{userId}")
|
||||
List<SysRole> findRolesByUserId(Long userId);
|
||||
|
||||
/**
|
||||
* 根据用户ids 获取
|
||||
*
|
||||
* @param userIds
|
||||
* @return
|
||||
*/
|
||||
@Select("<script>select r.*,ru.user_id from sys_role_user ru inner join sys_role r on r.id = ru.role_id where ru.user_id IN " +
|
||||
" <foreach item='item' index='index' collection='list' open='(' separator=',' close=')'> " +
|
||||
" #{item} " +
|
||||
" </foreach>" +
|
||||
"</script>")
|
||||
List<SysRole> findRolesByUserIds(List<Long> userIds);
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.central.user.model;
|
||||
|
||||
import cn.afterturn.easypoi.excel.annotation.Excel;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 用户导出实例
|
||||
* @Author: zlt
|
||||
*/
|
||||
@Data
|
||||
public class SysUserExcel implements Serializable {
|
||||
private static final long serialVersionUID = -5886012896705137070L;
|
||||
|
||||
@Excel(name = "用户姓名", height = 20, width = 30, isImportField = "true_st")
|
||||
private String username;
|
||||
|
||||
@Excel(name = "用户昵称", height = 20, width = 30, isImportField = "true_st")
|
||||
private String nickname;
|
||||
|
||||
@Excel(name = "手机号码", height = 20, width = 30, isImportField = "true_st")
|
||||
private String mobile;
|
||||
|
||||
@Excel(name = "学生性别", replace = { "男_0", "女_1" }, suffix = "生", isImportField = "true_st")
|
||||
private Integer sex;
|
||||
|
||||
@Excel(name = "创建时间", format = "yyyy-MM-dd HH:mm:ss", isImportField = "true_st", width = 20)
|
||||
private Date createTime;
|
||||
|
||||
@Excel(name = "修改时间", format = "yyyy-MM-dd HH:mm:ss", isImportField = "true_st", width = 20)
|
||||
private Date updateTime;
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package com.central.user.service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import com.central.common.model.SysMenu;
|
||||
import com.central.common.service.ISuperService;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
*/
|
||||
public interface ISysMenuService extends ISuperService<SysMenu> {
|
||||
/**
|
||||
* 查询所有菜单
|
||||
*/
|
||||
List<SysMenu> findAll();
|
||||
|
||||
/**
|
||||
* 查询所有一级菜单
|
||||
*/
|
||||
List<SysMenu> findOnes();
|
||||
|
||||
/**
|
||||
* 角色分配菜单
|
||||
* @param roleId
|
||||
* @param menuIds
|
||||
*/
|
||||
void setMenuToRole(Long roleId, Set<Long> menuIds);
|
||||
|
||||
/**
|
||||
* 角色菜单列表
|
||||
* @param roleIds 角色ids
|
||||
* @return
|
||||
*/
|
||||
List<SysMenu> findByRoles(Set<Long> roleIds);
|
||||
|
||||
/**
|
||||
* 角色菜单列表
|
||||
* @param roleIds 角色ids
|
||||
* @param roleIds 是否菜单
|
||||
* @return
|
||||
*/
|
||||
List<SysMenu> findByRoles(Set<Long> roleIds, Integer type);
|
||||
|
||||
/**
|
||||
* 角色菜单列表
|
||||
* @param roleCodes
|
||||
* @return
|
||||
*/
|
||||
List<SysMenu> findByRoleCodes(Set<String> roleCodes, Integer type);
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.central.user.service;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import com.central.common.model.PageResult;
|
||||
import com.central.common.model.Result;
|
||||
import com.central.common.model.SysRole;
|
||||
import com.central.common.service.ISuperService;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
*/
|
||||
public interface ISysRoleService extends ISuperService<SysRole> {
|
||||
void saveRole(SysRole sysRole);
|
||||
|
||||
void deleteRole(Long id);
|
||||
|
||||
/**
|
||||
* 角色列表
|
||||
* @param params
|
||||
* @return
|
||||
*/
|
||||
PageResult<SysRole> findRoles(Map<String, Object> params);
|
||||
|
||||
/**
|
||||
* 新增或更新角色
|
||||
* @param sysRole
|
||||
* @return Result
|
||||
*/
|
||||
Result saveOrUpdateRole(SysRole sysRole);
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
package com.central.user.service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import com.central.common.service.ISuperService;
|
||||
import com.central.user.model.SysUserExcel;
|
||||
import com.central.common.model.PageResult;
|
||||
import com.central.common.model.Result;
|
||||
import com.central.common.model.LoginAppUser;
|
||||
import com.central.common.model.SysRole;
|
||||
import com.central.common.model.SysUser;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
*/
|
||||
public interface ISysUserService extends ISuperService<SysUser> {
|
||||
/**
|
||||
* 获取UserDetails对象
|
||||
* @param username
|
||||
* @return
|
||||
*/
|
||||
LoginAppUser findByUsername(String username);
|
||||
|
||||
LoginAppUser findByOpenId(String username);
|
||||
|
||||
LoginAppUser findByMobile(String username);
|
||||
|
||||
/**
|
||||
* 根据用户名查询用户
|
||||
* @param username
|
||||
* @return
|
||||
*/
|
||||
SysUser selectByUsername(String username);
|
||||
/**
|
||||
* 根据手机号查询用户
|
||||
* @param mobile
|
||||
* @return
|
||||
*/
|
||||
SysUser selectByMobile(String mobile);
|
||||
/**
|
||||
* 根据openId查询用户
|
||||
* @param openId
|
||||
* @return
|
||||
*/
|
||||
SysUser selectByOpenId(String openId);
|
||||
|
||||
/**
|
||||
* 用户分配角色
|
||||
* @param id
|
||||
* @param roleIds
|
||||
*/
|
||||
void setRoleToUser(Long id, Set<Long> roleIds);
|
||||
|
||||
/**
|
||||
* 更新密码
|
||||
* @param id
|
||||
* @param oldPassword
|
||||
* @param newPassword
|
||||
* @return
|
||||
*/
|
||||
Result updatePassword(Long id, String oldPassword, String newPassword);
|
||||
|
||||
/**
|
||||
* 用户列表
|
||||
* @param params
|
||||
* @return
|
||||
*/
|
||||
PageResult<SysUser> findUsers(Map<String, Object> params);
|
||||
|
||||
|
||||
/**
|
||||
* 用户角色列表
|
||||
* @param userId
|
||||
* @return
|
||||
*/
|
||||
List<SysRole> findRolesByUserId(Long userId);
|
||||
|
||||
/**
|
||||
* 状态变更
|
||||
* @param params
|
||||
* @return
|
||||
*/
|
||||
Result updateEnabled(Map<String, Object> params);
|
||||
|
||||
/**
|
||||
* 查询全部用户
|
||||
* @param params
|
||||
* @return
|
||||
*/
|
||||
List<SysUserExcel> findAllUsers(Map<String, Object> params);
|
||||
|
||||
Result saveOrUpdateUser(SysUser sysUser);
|
||||
|
||||
/**
|
||||
* 删除用户
|
||||
*/
|
||||
boolean delUser(Long id);
|
||||
|
||||
/**
|
||||
* 保存用户
|
||||
*
|
||||
* @param users
|
||||
*/
|
||||
void saveUsers(List<SysUser> users);
|
||||
}
|
||||
+89
@@ -0,0 +1,89 @@
|
||||
package com.central.user.service.impl;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.central.common.service.impl.SuperServiceImpl;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import com.central.common.model.SysMenu;
|
||||
import com.central.user.mapper.SysMenuMapper;
|
||||
import com.central.user.mapper.SysRoleMenuMapper;
|
||||
import com.central.user.service.ISysMenuService;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class SysMenuServiceImpl extends SuperServiceImpl<SysMenuMapper, SysMenu> implements ISysMenuService {
|
||||
@Resource
|
||||
private SysRoleMenuMapper sysRoleMenuMapper;
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@Override
|
||||
public void setMenuToRole(Long roleId, Set<Long> menuIds) {
|
||||
sysRoleMenuMapper.delete(roleId, null);
|
||||
|
||||
if (!CollectionUtils.isEmpty(menuIds)) {
|
||||
menuIds.forEach(menuId -> {
|
||||
sysRoleMenuMapper.save(roleId, menuId);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 角色菜单列表
|
||||
* @param roleIds
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public List<SysMenu> findByRoles(Set<Long> roleIds) {
|
||||
return sysRoleMenuMapper.findMenusByRoleIds(roleIds, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 角色菜单列表
|
||||
* @param roleIds 角色ids
|
||||
* @param roleIds 是否菜单
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public List<SysMenu> findByRoles(Set<Long> roleIds, Integer type) {
|
||||
return sysRoleMenuMapper.findMenusByRoleIds(roleIds, type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SysMenu> findByRoleCodes(Set<String> roleCodes, Integer type) {
|
||||
return sysRoleMenuMapper.findMenusByRoleCodes(roleCodes, type);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询所有菜单
|
||||
*/
|
||||
@Override
|
||||
public List<SysMenu> findAll() {
|
||||
return baseMapper.selectList(
|
||||
new QueryWrapper<SysMenu>().orderByAsc("sort")
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询所有一级菜单
|
||||
*/
|
||||
@Override
|
||||
public List<SysMenu> findOnes() {
|
||||
return baseMapper.selectList(
|
||||
new QueryWrapper<SysMenu>()
|
||||
.eq("type", 1)
|
||||
.orderByAsc("sort")
|
||||
);
|
||||
}
|
||||
}
|
||||
+77
@@ -0,0 +1,77 @@
|
||||
package com.central.user.service.impl;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.central.common.constant.CommonConstant;
|
||||
import com.central.common.lock.DistributedLock;
|
||||
import com.central.common.model.*;
|
||||
import com.central.common.service.impl.SuperServiceImpl;
|
||||
import org.apache.commons.collections4.MapUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import com.central.user.mapper.SysRoleMapper;
|
||||
import com.central.user.mapper.SysRoleMenuMapper;
|
||||
import com.central.user.mapper.SysUserRoleMapper;
|
||||
import com.central.user.service.ISysRoleService;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class SysRoleServiceImpl extends SuperServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {
|
||||
private final static String LOCK_KEY_ROLECODE = CommonConstant.LOCK_KEY_PREFIX+"rolecode:";
|
||||
|
||||
@Resource
|
||||
private SysUserRoleMapper userRoleMapper;
|
||||
|
||||
@Resource
|
||||
private SysRoleMenuMapper roleMenuMapper;
|
||||
|
||||
@Autowired
|
||||
private DistributedLock lock;
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@Override
|
||||
public void saveRole(SysRole sysRole) {
|
||||
String roleCode = sysRole.getCode();
|
||||
super.saveIdempotency(sysRole, lock
|
||||
, LOCK_KEY_ROLECODE+roleCode, new QueryWrapper<SysRole>().eq("code", roleCode), "角色code已存在");
|
||||
}
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@Override
|
||||
public void deleteRole(Long id) {
|
||||
baseMapper.deleteById(id);
|
||||
roleMenuMapper.delete(id, null);
|
||||
userRoleMapper.deleteUserRole(null, id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageResult<SysRole> findRoles(Map<String, Object> params) {
|
||||
Integer curPage = MapUtils.getInteger(params, "page");
|
||||
Integer limit = MapUtils.getInteger(params, "limit");
|
||||
Page<SysRole> page = new Page<>(curPage == null ? 0 : curPage, limit == null ? -1 : limit);
|
||||
List<SysRole> list = baseMapper.findList(page, params);
|
||||
return PageResult.<SysRole>builder().data(list).code(0).count(page.getTotal()).build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result saveOrUpdateRole(SysRole sysRole) {
|
||||
if (sysRole.getId() == null) {
|
||||
this.saveRole(sysRole);
|
||||
} else {
|
||||
baseMapper.updateById(sysRole);
|
||||
}
|
||||
return Result.succeed("操作成功");
|
||||
}
|
||||
}
|
||||
+274
@@ -0,0 +1,274 @@
|
||||
package com.central.user.service.impl;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.central.common.constant.CommonConstant;
|
||||
import com.central.common.lock.DistributedLock;
|
||||
import com.central.common.model.*;
|
||||
import com.central.common.service.impl.SuperServiceImpl;
|
||||
import com.central.user.mapper.SysRoleMenuMapper;
|
||||
import com.central.user.model.SysUserExcel;
|
||||
import com.central.user.mapper.SysUserMapper;
|
||||
import org.apache.commons.collections4.MapUtils;
|
||||
import org.apache.commons.lang3.ObjectUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import com.central.user.mapper.SysUserRoleMapper;
|
||||
import com.central.user.service.ISysUserService;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* userService
|
||||
*
|
||||
* @author zlt
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class SysUserServiceImpl extends SuperServiceImpl<SysUserMapper, SysUser> implements ISysUserService {
|
||||
private final static String LOCK_KEY_USERNAME = CommonConstant.LOCK_KEY_PREFIX+"username:";
|
||||
|
||||
@Autowired
|
||||
private PasswordEncoder passwordEncoder;
|
||||
|
||||
@Resource
|
||||
private SysUserRoleMapper userRoleMapper;
|
||||
|
||||
@Resource
|
||||
private SysRoleMenuMapper roleMenuMapper;
|
||||
|
||||
@Autowired
|
||||
private DistributedLock lock;
|
||||
|
||||
@Override
|
||||
public LoginAppUser findByUsername(String username) {
|
||||
SysUser sysUser = this.selectByUsername(username);
|
||||
return getLoginAppUser(sysUser);
|
||||
}
|
||||
|
||||
@Override
|
||||
public LoginAppUser findByOpenId(String username) {
|
||||
SysUser sysUser = this.selectByOpenId(username);
|
||||
return getLoginAppUser(sysUser);
|
||||
}
|
||||
|
||||
@Override
|
||||
public LoginAppUser findByMobile(String username) {
|
||||
SysUser sysUser = this.selectByMobile(username);
|
||||
return getLoginAppUser(sysUser);
|
||||
}
|
||||
|
||||
private LoginAppUser getLoginAppUser(SysUser sysUser) {
|
||||
if (sysUser != null) {
|
||||
LoginAppUser loginAppUser = new LoginAppUser();
|
||||
BeanUtils.copyProperties(sysUser, loginAppUser);
|
||||
|
||||
List<SysRole> sysRoles = userRoleMapper.findRolesByUserId(sysUser.getId());
|
||||
// 设置角色
|
||||
loginAppUser.setRoles(sysRoles);
|
||||
|
||||
if (!CollectionUtils.isEmpty(sysRoles)) {
|
||||
Set<Long> roleIds = sysRoles.parallelStream().map(r -> r.getId()).collect(Collectors.toSet());
|
||||
List<SysMenu> menus = roleMenuMapper.findMenusByRoleIds(roleIds, CommonConstant.PERMISSION);
|
||||
if (!CollectionUtils.isEmpty(menus)) {
|
||||
Set<String> permissions = menus.parallelStream().map(p -> p.getPathMethod()+":"+p.getPath())
|
||||
.collect(Collectors.toSet());
|
||||
// 设置权限集合
|
||||
loginAppUser.setPermissions(permissions);
|
||||
}
|
||||
}
|
||||
return loginAppUser;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据用户名查询用户
|
||||
* @param username
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public SysUser selectByUsername(String username) {
|
||||
List<SysUser> users = baseMapper.selectList(
|
||||
new QueryWrapper<SysUser>().eq("username", username)
|
||||
);
|
||||
return getUser(users);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据手机号查询用户
|
||||
* @param mobile
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public SysUser selectByMobile(String mobile) {
|
||||
List<SysUser> users = baseMapper.selectList(
|
||||
new QueryWrapper<SysUser>().eq("mobile", mobile)
|
||||
);
|
||||
return getUser(users);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据openId查询用户
|
||||
* @param openId
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public SysUser selectByOpenId(String openId) {
|
||||
List<SysUser> users = baseMapper.selectList(
|
||||
new QueryWrapper<SysUser>().eq("openId", openId)
|
||||
);
|
||||
return getUser(users);
|
||||
}
|
||||
|
||||
private SysUser getUser(List<SysUser> users) {
|
||||
SysUser user = null;
|
||||
if (users != null && !users.isEmpty()) {
|
||||
user = users.get(0);
|
||||
}
|
||||
return user;
|
||||
}
|
||||
|
||||
/**
|
||||
* 给用户设置角色
|
||||
*/
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@Override
|
||||
public void setRoleToUser(Long id, Set<Long> roleIds) {
|
||||
SysUser sysUser = baseMapper.selectById(id);
|
||||
if (sysUser == null) {
|
||||
throw new IllegalArgumentException("用户不存在");
|
||||
}
|
||||
|
||||
userRoleMapper.deleteUserRole(id, null);
|
||||
if (!CollectionUtils.isEmpty(roleIds)) {
|
||||
roleIds.forEach(roleId -> {
|
||||
userRoleMapper.saveUserRoles(id, roleId);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Override
|
||||
public Result updatePassword(Long id, String oldPassword, String newPassword) {
|
||||
SysUser sysUser = baseMapper.selectById(id);
|
||||
if (StrUtil.isNotBlank(oldPassword)) {
|
||||
if (!passwordEncoder.matches(oldPassword, sysUser.getPassword())) {
|
||||
return Result.failed("旧密码错误");
|
||||
}
|
||||
}
|
||||
if (StrUtil.isBlank(newPassword)) {
|
||||
newPassword = CommonConstant.DEF_USER_PASSWORD;
|
||||
}
|
||||
SysUser user = new SysUser();
|
||||
user.setId(id);
|
||||
user.setPassword(passwordEncoder.encode(newPassword));
|
||||
baseMapper.updateById(user);
|
||||
return Result.succeed("修改成功");
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageResult<SysUser> findUsers(Map<String, Object> params) {
|
||||
Page<SysUser> page = new Page<>(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"));
|
||||
List<SysUser> list = baseMapper.findList(page, params);
|
||||
long total = page.getTotal();
|
||||
if (total > 0) {
|
||||
List<Long> userIds = list.stream().map(SysUser::getId).collect(Collectors.toList());
|
||||
|
||||
List<SysRole> sysRoles = userRoleMapper.findRolesByUserIds(userIds);
|
||||
list.forEach(u -> {
|
||||
u.setRoles(sysRoles.stream().filter(r -> !ObjectUtils.notEqual(u.getId(), r.getUserId()))
|
||||
.collect(Collectors.toList()));
|
||||
});
|
||||
}
|
||||
return PageResult.<SysUser>builder().data(list).code(0).count(total).build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SysRole> findRolesByUserId(Long userId) {
|
||||
return userRoleMapper.findRolesByUserId(userId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result updateEnabled(Map<String, Object> params) {
|
||||
Long id = MapUtils.getLong(params, "id");
|
||||
Boolean enabled = MapUtils.getBoolean(params, "enabled");
|
||||
|
||||
SysUser appUser = baseMapper.selectById(id);
|
||||
if (appUser == null) {
|
||||
return Result.failed("用户不存在");
|
||||
}
|
||||
appUser.setEnabled(enabled);
|
||||
appUser.setUpdateTime(new Date());
|
||||
|
||||
int i = baseMapper.updateById(appUser);
|
||||
log.info("修改用户:{}", appUser);
|
||||
|
||||
return i > 0 ? Result.succeed(appUser, "更新成功") : Result.failed("更新失败");
|
||||
}
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@Override
|
||||
public Result saveOrUpdateUser(SysUser sysUser) {
|
||||
if (sysUser.getId() == null) {
|
||||
if (StringUtils.isBlank(sysUser.getType())) {
|
||||
sysUser.setType(UserType.BACKEND.name());
|
||||
}
|
||||
sysUser.setPassword(passwordEncoder.encode(CommonConstant.DEF_USER_PASSWORD));
|
||||
sysUser.setEnabled(Boolean.TRUE);
|
||||
}
|
||||
String username = sysUser.getUsername();
|
||||
boolean result = super.saveOrUpdateIdempotency(sysUser, lock
|
||||
, LOCK_KEY_USERNAME+username, new QueryWrapper<SysUser>().eq("username", username)
|
||||
, username+"已存在");
|
||||
//更新角色
|
||||
if (result && StrUtil.isNotEmpty(sysUser.getRoleId())) {
|
||||
userRoleMapper.deleteUserRole(sysUser.getId(), null);
|
||||
List roleIds = Arrays.asList(sysUser.getRoleId().split(","));
|
||||
if (!CollectionUtils.isEmpty(roleIds)) {
|
||||
roleIds.forEach(roleId -> {
|
||||
userRoleMapper.saveUserRoles(sysUser.getId(), Long.parseLong(roleId.toString()));
|
||||
});
|
||||
}
|
||||
}
|
||||
return result ? Result.succeed(sysUser, "操作成功") : Result.failed("操作失败");
|
||||
}
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@Override
|
||||
public boolean delUser(Long id) {
|
||||
userRoleMapper.deleteUserRole(id, null);
|
||||
return baseMapper.deleteById(id) > 0 ? true : false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SysUserExcel> findAllUsers(Map<String, Object> params) {
|
||||
List<SysUserExcel> sysUserExcels = new ArrayList<>();
|
||||
List<SysUser> list = baseMapper.findList(new Page<>(1, -1), params);
|
||||
|
||||
for (SysUser sysUser : list) {
|
||||
SysUserExcel sysUserExcel = new SysUserExcel();
|
||||
BeanUtils.copyProperties(sysUser, sysUserExcel);
|
||||
sysUserExcels.add(sysUserExcel);
|
||||
}
|
||||
return sysUserExcels;
|
||||
}
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@Override
|
||||
public void saveUsers(List<SysUser> users) {
|
||||
users.forEach(u -> baseMapper.insert(u));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
spring:
|
||||
datasource:
|
||||
url: jdbc:mysql://${zlt.datasource.ip}:3306/user-center?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull
|
||||
username: ${zlt.datasource.username}
|
||||
password: ${zlt.datasource.password}
|
||||
driver-class-name: com.mysql.jdbc.Driver
|
||||
|
||||
redis:
|
||||
host: ${zlt.redis.host}
|
||||
port: ${zlt.redis.port}
|
||||
timeout: ${zlt.redis.timeout}
|
||||
|
||||
mybatis-plus:
|
||||
mapper-locations: classpath:/mapper/*Mapper.xml
|
||||
#实体扫描,多个package用逗号或者分号分隔
|
||||
typeAliasesPackage: com.central.common.model
|
||||
global-config:
|
||||
db-config:
|
||||
id-type: auto
|
||||
|
||||
zlt:
|
||||
swagger:
|
||||
enabled: true
|
||||
title: 用户中心
|
||||
description: 用户中心接口文档
|
||||
version: 1.0
|
||||
base-package: com.central.user.controller
|
||||
cache-manager:
|
||||
configs:
|
||||
- key: menu
|
||||
second: 600
|
||||
- key: user
|
||||
second: 1800
|
||||
@@ -0,0 +1,14 @@
|
||||
server:
|
||||
port: 7000
|
||||
|
||||
spring:
|
||||
application:
|
||||
name: user-center
|
||||
cloud:
|
||||
nacos:
|
||||
config:
|
||||
server-addr: ${zlt.nacos.server-addr}
|
||||
file-extension: yml
|
||||
shared-dataids: common.yml
|
||||
discovery:
|
||||
server-addr: ${zlt.nacos.server-addr}
|
||||
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.central.user.mapper.SysMenuMapper">
|
||||
|
||||
</mapper>
|
||||
@@ -0,0 +1,32 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.central.user.mapper.SysPermissionMapper">
|
||||
|
||||
<sql id="where">
|
||||
<where>
|
||||
<if test="permission != null and permission != ''">
|
||||
and t.permission like concat('%', #{permission}, '%')
|
||||
</if>
|
||||
<if test="name != null and name != ''">
|
||||
and t.name like concat('%', #{name}, '%')
|
||||
</if>
|
||||
<if test="searchKey != null and searchKey != ''">
|
||||
and t.name like concat('%', #{searchKey}, '%')
|
||||
</if>
|
||||
</where>
|
||||
</sql>
|
||||
<select id="count" resultType="int">
|
||||
select count(*) from sys_permission t
|
||||
<include refid="where" />
|
||||
</select>
|
||||
|
||||
<select id="findList" resultType="com.central.common.model.SysPermission">
|
||||
select * from sys_permission t
|
||||
<include refid="where" />
|
||||
order by t.id desc
|
||||
<!--<if test="start != null and start >= 0 and length != null and length > 0">-->
|
||||
<!--limit #{start}, #{length}-->
|
||||
<!--</if>-->
|
||||
</select>
|
||||
</mapper>
|
||||
@@ -0,0 +1,22 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.central.user.mapper.SysRoleMapper">
|
||||
<sql id="where">
|
||||
<where>
|
||||
<if test="r.name != null and r.name != ''">
|
||||
and t.name like concat('%', #{r.name}, '%')
|
||||
</if>
|
||||
|
||||
<if test="r.searchKey != null and r.searchKey != '' and r.searchKey=='name'">
|
||||
and t.name like concat('%', #{r.searchValue}, '%')
|
||||
</if>
|
||||
</where>
|
||||
</sql>
|
||||
|
||||
<select id="findList" resultType="com.central.common.model.SysRole">
|
||||
select * from sys_role t
|
||||
<include refid="where" />
|
||||
order by t.id desc
|
||||
</select>
|
||||
</mapper>
|
||||
@@ -0,0 +1,48 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.central.user.mapper.SysRoleMenuMapper">
|
||||
|
||||
<update id="delete">
|
||||
delete from sys_role_menu
|
||||
<where>
|
||||
<if test="roleId != null">
|
||||
and role_id = #{roleId}
|
||||
</if>
|
||||
<if test="menuId != null">
|
||||
and menu_id = #{menuId}
|
||||
</if>
|
||||
</where>
|
||||
</update>
|
||||
|
||||
<select id="findMenusByRoleIds" resultType="com.central.common.model.SysMenu">
|
||||
select distinct t.* from sys_menu t
|
||||
inner join sys_role_menu r on r.menu_id = t.id
|
||||
where
|
||||
r.role_id in
|
||||
<foreach collection="roleIds" index="index" item="roleId"
|
||||
open="(" separator="," close=")">
|
||||
#{roleId}
|
||||
</foreach>
|
||||
<if test="type != null">
|
||||
and t.type = #{type}
|
||||
</if>
|
||||
ORDER BY sort ASC
|
||||
</select>
|
||||
|
||||
<select id="findMenusByRoleCodes" resultType="com.central.common.model.SysMenu">
|
||||
select distinct t.* from sys_menu t
|
||||
inner join sys_role_menu r on r.menu_id = t.id
|
||||
inner join sys_role rl on rl.id = r.role_id
|
||||
where
|
||||
rl.code in
|
||||
<foreach collection="roleCodes" index="index" item="roleCode"
|
||||
open="(" separator="," close=")">
|
||||
#{roleCode}
|
||||
</foreach>
|
||||
<if test="type != null">
|
||||
and t.type = #{type}
|
||||
</if>
|
||||
ORDER BY sort ASC
|
||||
</select>
|
||||
</mapper>
|
||||
@@ -0,0 +1,31 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.central.user.mapper.SysRolePermissionMapper">
|
||||
|
||||
<delete id="deleteRolePermission">
|
||||
delete from sys_role_permission
|
||||
<where>
|
||||
<if test="roleId != null">
|
||||
and roleId = #{roleId}
|
||||
</if>
|
||||
<if test="permissionId != null">
|
||||
and permissionId = #{permissionId}
|
||||
</if>
|
||||
</where>
|
||||
</delete>
|
||||
|
||||
<select id="findPermissionsByRoleIds" resultType="com.central.common.model.SysPermission">
|
||||
select p.* from
|
||||
sys_permission p
|
||||
inner join sys_role_permission rp on
|
||||
p.id = rp.permissionId
|
||||
<where>
|
||||
rp.roleId in
|
||||
<foreach collection="roleIds" item="roleId" open="("
|
||||
separator="," close=")">
|
||||
#{roleId}
|
||||
</foreach>
|
||||
</where>
|
||||
</select>
|
||||
</mapper>
|
||||
@@ -0,0 +1,43 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.central.user.mapper.SysUserMapper">
|
||||
<sql id="where">
|
||||
<where>
|
||||
t.is_del=0
|
||||
<if test="u.id != null and u.id != ''">
|
||||
and t.id like concat('%', #{u.id}, '%')
|
||||
</if>
|
||||
<if test="u.username != null and u.username != ''">
|
||||
and t.username like concat('%', #{u.username}, '%')
|
||||
</if>
|
||||
<if test="u.nickname != null and u.nickname != ''">
|
||||
and t.nickname like concat('%', #{u.nickname}, '%')
|
||||
</if>
|
||||
<if test="u.enabled != null and u.enabled != ''">
|
||||
and t.enabled = #{u.enabled}
|
||||
</if>
|
||||
<if test="u.type != null and u.type != ''">
|
||||
and t.type = #{u.type}
|
||||
</if>
|
||||
<if test="u.searchKey != null and u.searchKey != '' and u.searchKey=='user_id'">
|
||||
and t.id like concat('%', #{u.searchValue}, '%')
|
||||
</if>
|
||||
<if test="u.searchKey != null and u.searchKey != '' and u.searchKey=='username'">
|
||||
and t.username like concat('%', #{u.searchValue}, '%')
|
||||
</if>
|
||||
<if test="u.searchKey != null and u.searchKey != '' and u.searchKey=='nick_name'">
|
||||
and t.nickname like concat('%', #{u.searchValue}, '%')
|
||||
</if>
|
||||
<if test="u.searchKey != null and u.searchKey != '' and u.searchKey=='mobile'">
|
||||
and t.mobile like concat('%', #{u.searchValue}, '%')
|
||||
</if>
|
||||
</where>
|
||||
</sql>
|
||||
|
||||
<select id="findList" resultType="com.central.common.model.SysUser">
|
||||
select * from sys_user t
|
||||
<include refid="where" />
|
||||
order by t.id desc
|
||||
</select>
|
||||
</mapper>
|
||||
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.central.user.mapper.SysUserRoleMapper">
|
||||
<delete id="deleteUserRole">
|
||||
delete from sys_role_user
|
||||
<where>
|
||||
<if test="userId != null">
|
||||
and user_id = #{userId}
|
||||
</if>
|
||||
<if test="roleId != null">
|
||||
and role_id = #{roleId}
|
||||
</if>
|
||||
</where>
|
||||
</delete>
|
||||
</mapper>
|
||||
Reference in New Issue
Block a user