jinpengyong
2021-03-12 268a86dc9146b450721e4c0e4dbbe0daa03ee9c6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package com.moral.api.controller;
 
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 java.util.Map;
 
import javax.servlet.http.HttpServletRequest;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
 
import com.moral.api.entity.User;
import com.moral.api.service.UserService;
import com.moral.constant.ResultMessage;
import com.moral.util.WebUtils;
 
@Slf4j
@Api(tags = {"用户"})
@RestController
@RequestMapping("/user")
public class UserController {
 
    @Autowired
    private UserService userService;
 
    @ApiOperation(value = "添加账户", notes = "添加账户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "账户,长度6-10", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "password", value = "密码,长度6-20", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "userName", value = "账户名称", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "email", value = "邮箱,格式123456@qq.com", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "mobile", value = "手机号,1开头11为数字", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "wechat", value = "微信", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header", dataType = "String")
    })
    @RequestMapping(value = "addUser", method = RequestMethod.POST)
    public ResultMessage addUser(User user, HttpServletRequest request) {
        Map<String, Object> parameters = WebUtils.getParametersStartingWith(request, null);
        if (!(parameters.containsKey("account") && parameters.containsKey("password"))) {
            return ResultMessage.fail("账户及密码不允许为空!");
        }
        String token = request.getHeader("token");
        Map<String, Object> map = userService.addUser(user, token);
        if (map.containsKey("msg")) {
            return ResultMessage.fail(map.get("msg").toString());
        }
        return ResultMessage.ok();
    }
 
    @ApiOperation(value = "删除账户", notes = "删除账户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header", dataType = "String")
    })
    @RequestMapping(value = "deleteUser", method = RequestMethod.POST)
    public ResultMessage deleteUser(String userId, HttpServletRequest request) {
        if (userId == null) {
            return ResultMessage.fail("请求参数错误");
        }
        String token = request.getHeader("token");
        Map<String, Object> map = userService.deleteUser(Integer.parseInt(userId), token);
        if (map.containsKey("msg")) {
            return ResultMessage.fail(map.get("msg").toString());
        }
        return ResultMessage.ok();
    }
 
    @ApiOperation(value = "修改账户信息", notes = "修改账户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "账户,长度6-10", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "password", value = "密码,长度6-20", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "userName", value = "账户名称", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "email", value = "邮箱,格式123456@qq.com", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "mobile", value = "手机号,1开头11位数字", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "wechat", value = "微信", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header", dataType = "String")
    })
    @RequestMapping(value = "updateUser", method = RequestMethod.POST)
    public ResultMessage updateUser(User user, HttpServletRequest request) {
        String token = request.getHeader("token");
        Map<String, Object> map = userService.updateUser(user, token);
        if (map.containsKey("msg")) {
            return ResultMessage.fail(map.get("msg").toString());
        }
        return ResultMessage.ok();
    }
 
    @ApiOperation(value = "查询账户信息", notes = "查询账户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header", dataType = "String")
    })
    @RequestMapping(value = "getUserInfo", method = RequestMethod.POST)
    public ResultMessage getUserInfo(HttpServletRequest request) {
        Map<String, Object> parameters = WebUtils.getParametersStartingWith(request, null);
        parameters.put("token", request.getHeader("token"));
        Map<String, Object> users = userService.getUsers(parameters);
        if (users.containsKey("msg")) {
            return ResultMessage.fail(users.get("msg").toString());
        }
        return ResultMessage.ok(users.get("users"));
    }
 
}