用户管理控制

master
王皓雯 6 years ago
parent c634f44199
commit 566c2710eb

@ -0,0 +1,178 @@
package com.macro.mall.controller;
import com.macro.mall.dto.CommonResult;
import com.macro.mall.dto.UmsAdminLoginParam;
import com.macro.mall.dto.UmsAdminParam;
import com.macro.mall.model.UmsAdmin;
import com.macro.mall.model.UmsPermission;
import com.macro.mall.model.UmsRole;
import com.macro.mall.service.UmsAdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.security.Principal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
*
*/
@Controller
@Api(tags = "UmsAdminController", description = "后台用户管理")
@RequestMapping("/admin")
public class UmsAdminController {
@Autowired
private UmsAdminService adminService;//后台用户服务
@Value("${jwt.tokenHeader}")
private String tokenHeader;
@Value("${jwt.tokenHead}")
private String tokenHead;
@ApiOperation(value = "用户注册")
@RequestMapping(value = "/register", method = RequestMethod.POST)
@ResponseBody
public Object register(@RequestBody UmsAdminParam umsAdminParam, BindingResult result) {
UmsAdmin umsAdmin = adminService.register(umsAdminParam);//用户服务
if (umsAdmin == null) {
new CommonResult().failed();
}
return new CommonResult().success(umsAdmin);
}
@ApiOperation(value = "登录以后返回token")
@RequestMapping(value = "/login", method = RequestMethod.POST)
@ResponseBody
public Object login(@RequestBody UmsAdminLoginParam umsAdminLoginParam, BindingResult result) {//登录
String token = adminService.login(umsAdminLoginParam.getUsername(), umsAdminLoginParam.getPassword());//用户名密码登录返回token
if (token == null) {
return new CommonResult().validateFailed("用户名或密码错误");
}
Map<String, String> tokenMap = new HashMap<>();//建立新的哈希表存储token值
tokenMap.put("token", token);
tokenMap.put("tokenHead", tokenHead);
return new CommonResult().success(tokenMap);
}
@ApiOperation(value = "刷新token")
@RequestMapping(value = "/token/refresh", method = RequestMethod.GET)
@ResponseBody
public Object refreshToken(HttpServletRequest request) {//Http请求
String token = request.getHeader(tokenHeader);//请求获取表头
String refreshToken = adminService.refreshToken(token);
if (refreshToken == null) {
return new CommonResult().failed();
}
Map<String, String> tokenMap = new HashMap<>();
tokenMap.put("token", token);
tokenMap.put("tokenHead", tokenHead);
return new CommonResult().success(tokenMap);
}
@ApiOperation(value = "获取当前登录用户信息")
@RequestMapping(value = "/info", method = RequestMethod.GET)
@ResponseBody
public Object getAdminInfo(Principal principal) {//获取用户信息
/*AttributePrincipal principal = (AttributePrincipal) request.getUserPrincipal();*/
String username = principal.getName();
UmsAdmin umsAdmin = adminService.getAdminByUsername(username);
Map<String, Object> data = new HashMap<>();
data.put("username", umsAdmin.getUsername());
data.put("roles", new String[]{"TEST"});//用户角色
data.put("icon", umsAdmin.getIcon());//用户头像
return new CommonResult().success(data);
}
@ApiOperation(value = "退出功能")
@RequestMapping(value = "/logout", method = RequestMethod.POST)
@ResponseBody
public Object logout() {
return new CommonResult().success(null);
}
@ApiOperation("根据用户名或姓名分页获取用户列表")
@RequestMapping(value = "/list",method = RequestMethod.GET)
@ResponseBody
public Object list(@RequestParam(value = "name",required = false) String name,//用户名
@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum){
List<UmsAdmin> adminList = adminService.list(name,pageSize,pageNum);
return new CommonResult().pageSuccess(adminList);
}
@ApiOperation("获取指定用户信息")
@RequestMapping(value = "/{id}",method = RequestMethod.GET)
@ResponseBody
public Object getItem(@PathVariable Long id){//根据id获取用户信息
UmsAdmin admin = adminService.getItem(id);
return new CommonResult().success(admin);
}
@ApiOperation("获取指定用户信息")
@RequestMapping(value = "/update/{id}",method = RequestMethod.POST)
@ResponseBody
public Object update(@PathVariable Long id,@RequestBody UmsAdmin admin){
int count = adminService.update(id,admin);
if(count>0){
return new CommonResult().success(count);
}
return new CommonResult().failed();
}
@ApiOperation("删除指定用户信息")
@RequestMapping(value = "/delete/{id}",method = RequestMethod.POST)
@ResponseBody
public Object delete(@PathVariable Long id){
int count = adminService.delete(id);
if(count>0){
return new CommonResult().success(count);
}
return new CommonResult().failed();
}
@ApiOperation("给用户分配角色")
@RequestMapping(value = "/role/update",method = RequestMethod.POST)
@ResponseBody
public Object updateRole(@RequestParam("adminId") Long adminId,//更新角色内容
@RequestParam("roleIds") List<Long> roleIds){
int count = adminService.updateRole(adminId,roleIds);
if(count>=0){
return new CommonResult().success(count);
}
return new CommonResult().failed();
}
@ApiOperation("获取指定用户的角色")
@RequestMapping(value = "/role/{adminId}",method = RequestMethod.GET)
@ResponseBody
public Object getRoleList(@PathVariable Long adminId){//获取指定用户角色列表
List<UmsRole> roleList = adminService.getRoleList(adminId);
return new CommonResult().success(roleList);
}
@ApiOperation("给用户分配+ - 权限")
@RequestMapping(value = "/permission/update",method = RequestMethod.POST)
@ResponseBody
public Object updatePermission(@RequestParam Long adminId,
@RequestParam("permissionIds") List<Long> permissionIds){
int count = adminService.updatePermission(adminId,permissionIds);
if(count>0){
return new CommonResult().success(count);
}
return new CommonResult().failed();
}
@ApiOperation("获取用户所有权限(包括+ - 权限)")
@RequestMapping(value = "/permission/{adminId}",method = RequestMethod.GET)
@ResponseBody
public Object getPermissionList(@PathVariable Long adminId){
List<UmsPermission> permissionList = adminService.getPermissionList(adminId);
return new CommonResult().success(permissionList);
}
}
Loading…
Cancel
Save