TokenController.java
2.03 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
package com.huaheng.framework.token;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.framework.shiro.service.PasswordService;
import com.huaheng.framework.web.controller.BaseController;
import com.huaheng.framework.web.domain.Result;
import com.huaheng.pc.system.user.domain.User;
import com.huaheng.pc.system.user.service.IUserService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.Calendar;
/**
* Created by Enzo Cotter on 2020/6/11.
*/
@RestController
@RequestMapping("/api")
public class TokenController extends BaseController {
@Resource
private TokenService tokenService;
@Resource
private IUserService userService;
@Resource
private PasswordService passwordService;
@PostMapping("/getToken")
@ResponseBody
public Result getToken(String username, String password, String warehouseCode) {
if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
return Result.error("用户名和密码不能为空");
}
if (StringUtils.isEmpty(warehouseCode)) {
return Result.error("请选择仓库");
}
User user = userService.selectUserByLoginName(username);
if (!userService.checkWarehouseCodeAndUserName(warehouseCode, username)) {
return Result.error("用户没有该仓库操作权限");
}
if (user.getPassword().equals(passwordService.encryptPassword(user.getLoginName(), password, user.getSalt()))) {
String token = tokenService.createToken(user);
Result ajaxResult = Result.success("成功");
ajaxResult.put("token", token);
ajaxResult.put("expireTime", Calendar.getInstance().getTime());
return ajaxResult;
} else {
return Result.error("密码错误");
}
}
}