审查视图

src/main/java/com/tianbo/warehouse/controller/UserController.java 7.2 KB
朱兆平 authored
1 2
package com.tianbo.warehouse.controller;
3
import com.alibaba.fastjson.JSON;
朱兆平 authored
4
import com.github.pagehelper.PageInfo;
朱兆平 authored
5
import com.tianbo.warehouse.annotation.LogAnnotation;
朱兆平 authored
6
import com.tianbo.warehouse.annotation.RequestRequire;
7
import com.tianbo.warehouse.annotation.UserPasswordMd5;
8
import com.tianbo.warehouse.annotation.cache.annotation.RedisCacheDelTarget;
9
import com.tianbo.warehouse.controller.response.ResultJson;
朱兆平 authored
10
import com.tianbo.warehouse.model.USERS;
11
import com.tianbo.warehouse.model.UserRole;
12 13
import com.tianbo.warehouse.security.CustomUserDetailService;
import com.tianbo.warehouse.security.filter.JwtTokenUtil;
朱兆平 authored
14
import com.tianbo.warehouse.service.UserService;
15 16
import com.tianbo.warehouse.service.validated.InsertUser;
import com.tianbo.warehouse.service.validated.UpdateUser;
17
import com.tianbo.warehouse.util.RedisUtils;
朱兆平 authored
18 19 20 21
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
朱兆平 authored
22
import lombok.extern.slf4j.Slf4j;
朱兆平 authored
23 24 25
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
26
import org.springframework.validation.BindingResult;
27
import org.springframework.validation.annotation.Validated;
28
import org.springframework.web.bind.annotation.*;
朱兆平 authored
29
30 31
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
32
import javax.validation.Valid;
zhangFan authored
33 34
import java.util.List;
import java.util.Map;
朱兆平 authored
35 36

@RestController
朱兆平 authored
37
@Slf4j
朱兆平 authored
38 39
@RequestMapping("/user")
@Api("swaggerDemoController相关的api")
朱兆平 authored
40 41 42 43 44
public class UserController {

    @Autowired
    UserService userService;
45 46 47 48 49 50
    @Autowired
    CustomUserDetailService userDetailService;

    @Autowired
    RedisUtils redisUtils;
朱兆平 authored
51 52 53
    @ApiOperation(value = "查询用户列表及信息", notes = "查询用户列表及单个用户信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "pageNum", value = "分页-当前页", required = false, dataType = "int",defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "分页-每页显示多少条", required = false, dataType = "int",defaultValue = "5")})
朱兆平 authored
54
    @RequestRequire
朱兆平 authored
55
    @GetMapping("/list")
56
    public ResultJson<PageInfo> list(@RequestParam(value = "pageNum",required = false,defaultValue = "1")
朱兆平 authored
57
                                        int pageNum,
58
                                @RequestParam(value = "pageSize",required = false,defaultValue = "5")
朱兆平 authored
59
                                        int pageSize,
shenhailong authored
60 61
                                @RequestParam(value = "userName",required = false) String username,
                                @RequestParam(value = "realName",required = false) String realname)
朱兆平 authored
62
    {
shenhailong authored
63 64

        PageInfo<USERS> usersPageInfo = userService.selectAllUser(pageNum,pageSize, username,  realname);
65
        return new ResultJson("200","success",usersPageInfo);
朱兆平 authored
66 67
    }
朱兆平 authored
68
    public String getCurrentUser(){
朱兆平 authored
69 70 71

        //通过session获取当前登录的用户信息
        UserDetails userDetails =(UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
朱兆平 authored
72
        return userDetails.getUsername();
朱兆平 authored
73
    }
74
朱兆平 authored
75
    @LogAnnotation(moduleName = "用户管理",operate = "用户编辑")
朱兆平 authored
76
    @PutMapping("/edit")
77
    public ResultJson updateUserById(@Validated(UpdateUser.class) @RequestBody USERS user){
78
        user.setPassword(null);
79 80
       int i = userService.updateByPrimaryKeySelective(user);
        ResultJson resultJson = new ResultJson();
81 82
        return i==1 ? new ResultJson("200","success") :new ResultJson("500","update faild");
83 84
    }
85 86 87 88 89 90 91 92
    @LogAnnotation(moduleName = "用户管理",operate = "用户密码修改")
    @UserPasswordMd5
    @PutMapping("/password")
    public ResultJson updateUserPassById(@RequestBody USERS user){
        int i = userService.updateByPrimaryKeySelective(user);
        return i==1 ? new ResultJson("200","success") :new ResultJson("500","update faild");
    }
93
    @UserPasswordMd5
朱兆平 authored
94
    @LogAnnotation(moduleName = "用户管理",operate = "用户添加")
朱兆平 authored
95
    @PostMapping("/add")
96
    public ResultJson addUser(@RequestBody @Validated(InsertUser.class) USERS user, HttpServletRequest request, HttpServletResponse response, BindingResult bindingResult){
97 98 99 100 101

        if (bindingResult.hasErrors()){
           String s =  bindingResult.toString();
        }
102 103
        int i = userService.insertSelective(user);
        ResultJson resultJson = new ResultJson();
104 105
        return i==1 ? new ResultJson("200","新建账户成功") :new ResultJson("500","insert faild");
106 107
    }
朱兆平 authored
108
    @LogAnnotation(moduleName = "用户管理",operate = "用户删除")
朱兆平 authored
109
    @DeleteMapping("/del")
110 111 112 113
    public ResultJson delUser(@RequestBody USERS user,HttpServletRequest request,HttpServletResponse response){
        //String username = getusername();
        int i = userService.deleteByPrimaryKey(user.getUserId());
        ResultJson resultJson = new ResultJson();
114 115 116
        return i==1 ? new ResultJson("200","删除账户成功") :new ResultJson("500","delete faild");
    }
朱兆平 authored
117
    @PutMapping("/roleset")
118
    public ResultJson roleSet(@RequestBody Map<String,Object> map,HttpServletRequest request,HttpServletResponse respons){
119 120
        Integer id = (Integer) map.get("userId");
        List<Integer> roles = (List<Integer>) map.get("roleIds");
zhangFan authored
121 122 123
        UserRole userRole = new UserRole();
        userRole.setUserId(id);
        userRole.setRoleIds(roles);
124 125
        int i =  userService.setUserRole(userRole);
        return i==1 ? new ResultJson("200","设置角色成功") :new ResultJson("500","设置角色失败");
126 127
    }
128 129 130 131
    /**
     * 刷新redis权限缓存
     */
    @ApiOperation(value = "更新用户权限缓存", notes = "重新生成用户的信息到redis")
132
    @RedisCacheDelTarget(cacheKey = "com.tianbo.warehouse.service.imp.PermissionServiceImp")
133 134 135 136 137 138 139 140
    @PutMapping("/resetToken")
    public ResultJson resetToken(HttpServletRequest request,HttpServletResponse respons) {
        /**
         * 更新目标用户的权限缓存
         */
        String authHeader = request.getHeader("Authorization");
        if (authHeader != null && authHeader.startsWith("Bearer ")) {
            final String authToken = authHeader.substring("Bearer ".length());
朱兆平 authored
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
            try {
                String userJson = redisUtils.get(authToken);
                if (userJson != null) {
                    USERS u = JSON.parseObject(userJson, USERS.class);
                    String username = u.getUsername();

//                  String username = JwtTokenUtil.parseToken(authToken);
                    if (username != null) {
                        UserDetails userDetails = userDetailService.loadUserByUsername(username);
                        if (userDetails != null) {
                            String json = JSON.toJSONString(userDetails);
                            redisUtils.set(authToken, json, 3600 * 24 * 7);
                            return new ResultJson("200", "缓存更新成功");
                        }
                    }
156
                }
朱兆平 authored
157 158 159
            }catch (Exception e){
                log.error(e.toString());
                return new ResultJson("500","缓存更新失败");
160 161 162 163
            }
        }
        return new ResultJson("500","缓存更新失败");
    }
朱兆平 authored
164
}