TokenApi.java
3.24 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
package com.huaheng.api.general.controller;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.shiro.service.PasswordService;
import com.huaheng.framework.token.TokenService;
import com.huaheng.framework.web.controller.BaseController;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.framework.web.domain.Result;
import com.huaheng.pc.system.user.domain.User;
import com.huaheng.pc.system.user.service.IUserService;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Map;
/**
* Created by Enzo Cotter on 2020/6/11.
*/
@RestController
@RequestMapping("/api")
public class TokenApi extends BaseController {
@Resource
private TokenService tokenService;
@Resource
private IUserService userService;
@Resource
private PasswordService passwordService;
@PostMapping("/getToken")
@ResponseBody
public Result getToken(String loginName, String password, String warehouseCode) {
if (StringUtils.isEmpty(loginName) || StringUtils.isEmpty(password)) {
return Result.error("用户名和密码不能为空");
}
if (StringUtils.isEmpty(warehouseCode)) {
return Result.error("请选择仓库");
}
User user = userService.selectUserByLoginName(loginName);
if (!userService.checkWarehouseCodeAndUserName(warehouseCode, loginName)) {
return Result.error("用户没有该仓库操作权限");
}
user.setWarehouseCode(warehouseCode);
if (user.getPassword().equals(passwordService.encryptPassword(user.getLoginName(), password, user.getSalt()))) {
String token = tokenService.createToken(user, password);
Result ajaxResult = Result.success("成功");
ajaxResult.put("token", token);
ajaxResult.put("expireTime", Calendar.getInstance().getTime());
//user数据加入session
AjaxResult ajaxResult1 = userService.login(loginName, password, warehouseCode, false);
if(ajaxResult1.getCode() != 200){
return Result.error("写入session失败!");
}
return ajaxResult;
} else {
return Result.error("密码错误");
}
}
@PostMapping("/getTokenForMobile")
@ResponseBody
public AjaxResult getTokenForMobile(@RequestBody @ApiParam(value="code和password的Map集合") Map<String, String> param) {
String loginName = param.get("userName");
String password = param.get("password");
if (StringUtils.isEmpty(loginName) || StringUtils.isEmpty(password)) {
return AjaxResult.error("用户名和密码不能为空");
}
//需要传入仓库code
User user = userService.selectUserByLoginName(loginName);
if (user.getPassword().equals(passwordService.encryptPassword(user.getLoginName(), password, user.getSalt()))) {
String token = tokenService.createTokenForMobile(user);
return AjaxResult.success("获取token成功").setData(token);
} else {
return AjaxResult.error("密码错误");
}
}
}