MobileUserController.java
7.81 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package com.huaheng.mobile.general;
import com.huaheng.auth.form.LoginBody;
import com.huaheng.common.core.constant.CacheConstants;
import com.huaheng.common.core.constant.Constants;
import com.huaheng.common.core.constant.UserConstants;
import com.huaheng.common.core.domain.R;
import com.huaheng.common.core.enums.UserStatus;
import com.huaheng.common.core.exception.BaseException;
import com.huaheng.common.core.utils.IdUtils;
import com.huaheng.common.core.utils.StringUtils;
import com.huaheng.common.core.web.domain.AjaxResult;
import com.huaheng.common.redis.service.RedisService;
import com.huaheng.common.security.utils.SecurityUtils;
import com.huaheng.system.api.RemoteCompanyService;
import com.huaheng.system.api.RemoteLogService;
import com.huaheng.system.api.RemoteUserService;
import com.huaheng.system.api.RemoteWarehouseService;
import com.huaheng.system.api.domain.Company;
import com.huaheng.system.api.domain.SysUser;
import com.huaheng.system.api.model.LoginUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.huaheng.common.core.web.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
*
* @author Enzo Cotter
* @date 2019/12/15
*/
@RestController
@Api(tags = {"移动端用户信息"}, value = "移动端用户信息MobileUserController")
public class MobileUserController extends BaseController {
@Resource
private RemoteCompanyService companyService;
@Resource
private RemoteLogService remoteLogService;
@Resource
private RemoteUserService remoteUserService;
@Resource
private RemoteWarehouseService warehouseService;
@Autowired
private RedisService redisService;
private final static long EXPIRE_TIME = Constants.TOKEN_EXPIRE * 60 * 60;
private final static String ACCESS_TOKEN = CacheConstants.LOGIN_TOKEN_KEY;
protected static final long MILLIS_SECOND = 1000;
@ApiOperation(value = "用户登录")
@PostMapping("login")
public R<?> login(@RequestBody LoginBody form) {
// 用户登录get
LoginUser userInfo = login(form.getUsername(), form.getPassword(), form.getWarehouseCode());
// 获取登录token
return R.ok(createToken(userInfo));
}
@PostMapping("/getCompanyInfo")
@ApiOperation("获取公司信息")
public AjaxResult getCompanyInfo(@RequestBody Map<String, String> param){
String userId = param.get("userId");
String warehouseCode = param.get("warehouseCode");
List<Company> companies = companyService.selectCompanyByCurrentUserId(userId, warehouseCode);
List<CompanyInfo> companyInfos = new ArrayList<CompanyInfo>();
for(Company company : companies) {
companyInfos.add(new CompanyInfo(company.getId(), company.getCode(), company.getName()));
}
return AjaxResult.success(companyInfos);
}
/**
* 登录
*/
public LoginUser login(String username, String password, String warehouseCode) {
// 用户名或密码为空 错误
if (StringUtils.isAnyBlank(username, password, warehouseCode)) {
remoteLogService.saveLogininfor(username, Constants.LOGIN_FAIL, "用户/密码必须填写");
throw new BaseException("用户/密码必须填写");
}
// 用户名不在指定范围内 错误
if (username.length() < UserConstants.USERNAME_MIN_LENGTH
|| username.length() > UserConstants.USERNAME_MAX_LENGTH) {
remoteLogService.saveLogininfor(username, Constants.LOGIN_FAIL, "用户名不在指定范围");
throw new BaseException("用户名不在指定范围");
}
// 查询用户信息
R<LoginUser> userResult = remoteUserService.getUserInfo(username, warehouseCode);
if (StringUtils.isNull(userResult) || StringUtils.isNull(userResult.getData())) {
remoteLogService.saveLogininfor(username, Constants.LOGIN_FAIL, "登录用户不存在");
throw new BaseException("登录用户:" + username + " 不存在");
}
LoginUser userInfo = userResult.getData();
SysUser user = userResult.getData().getSysUser();
if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) {
remoteLogService.saveLogininfor(username, Constants.LOGIN_FAIL, "对不起,您的账号已被删除");
throw new BaseException("对不起,您的账号:" + username + " 已被删除");
}
if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
remoteLogService.saveLogininfor(username, Constants.LOGIN_FAIL, "用户已停用,请联系管理员");
throw new BaseException("对不起,您的账号:" + username + " 已停用");
}
if (!SecurityUtils.matchesPassword(password, user.getPassword())) {
remoteLogService.saveLogininfor(username, Constants.LOGIN_FAIL, "用户密码错误");
throw new BaseException("用户不存在/密码错误");
}
remoteLogService.saveLogininfor(username, Constants.LOGIN_SUCCESS, "登录成功");
return userInfo;
}
/**
* 创建令牌
*/
public Map<String, Object> createToken(LoginUser loginUser)
{
// 生成token
String token = IdUtils.fastUUID();
loginUser.setToken(token);
loginUser.setUserid(loginUser.getSysUser().getUserId());
loginUser.setUsername(loginUser.getSysUser().getUserName());
loginUser.setSysUser(null);
refreshToken(loginUser);
// 保存或更新用户token
Map<String, Object> map = new HashMap<String, Object>();
map.put("access_token", token);
map.put("expires_in", EXPIRE_TIME);
map.put("warehouseCode", loginUser.getWarehouseCode());
map.put("companies", loginUser.getCompanies());
map.put("userId", loginUser.getUserid());
redisService.setCacheObject(ACCESS_TOKEN + token, loginUser, EXPIRE_TIME, TimeUnit.SECONDS);
return map;
}
/**
* 刷新令牌有效期
*
* @param loginUser 登录信息
*/
public Long refreshToken(LoginUser loginUser)
{
loginUser.setLoginTime(System.currentTimeMillis());
loginUser.setExpireTime(loginUser.getLoginTime() + EXPIRE_TIME * MILLIS_SECOND);
// 根据uuid将loginUser缓存
String userKey = getTokenKey(loginUser.getToken());
redisService.setCacheObject(userKey, loginUser, EXPIRE_TIME, TimeUnit.SECONDS);
return EXPIRE_TIME;
}
private String getTokenKey(String token)
{
return ACCESS_TOKEN + token;
}
//
// @PostMapping("/reservation")
// public AjaxResult reservation(String localDate, String warehouseCode) {
// DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
// LocalDate date2 = LocalDate.parse(localDate, fmt);
// return AjaxResult.success(reservationService.reservationTime(date2, warehouseCode));
// }
//
// /**
// * 新增保存预约
// */
// @ApiOperation(value = "新增预约 ", notes = "新增预约 ", httpMethod = "POST")
// @PostMapping("/addSave")
// @ResponseBody
// public AjaxResult addSave(@RequestBody @ApiParam(value="WarehouseId和warehouseCode的Map集合") Reservation reservation) {
// DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
// reservation.setBeginTime(LocalDateTime.parse(reservation.getUserDef1()));
// reservation.setEndTime(LocalDateTime.parse(reservation.getUserDef2()));
// reservation.setUserDef1(null);
// reservation.setUserDef2(null);
// return toAjax(reservationService.save(reservation));
// }
}