Blame view

src/main/java/com/huaheng/api/general/controller/TokenApiController.java 3.24 KB
1
package com.huaheng.api.general.controller;
mahuandong authored
2
3

import com.huaheng.common.utils.StringUtils;
4
import com.huaheng.common.utils.security.ShiroUtils;
mahuandong authored
5
import com.huaheng.framework.shiro.service.PasswordService;
6
import com.huaheng.framework.token.TokenService;
mahuandong authored
7
import com.huaheng.framework.web.controller.BaseController;
游杰 authored
8
import com.huaheng.framework.web.domain.AjaxResult;
mahuandong authored
9
10
11
import com.huaheng.framework.web.domain.Result;
import com.huaheng.pc.system.user.domain.User;
import com.huaheng.pc.system.user.service.IUserService;
游杰 authored
12
13
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
mahuandong authored
14
15
16

import javax.annotation.Resource;
import java.util.Calendar;
游杰 authored
17
import java.util.Map;
mahuandong authored
18
19
20
21
22
23

/**
 * Created by Enzo Cotter on 2020/6/11.
 */
@RestController
@RequestMapping("/api")
游杰 authored
24
public class TokenApiController extends BaseController {
mahuandong authored
25
26
27
28
29
30
31
32
33
34

    @Resource
    private TokenService tokenService;
    @Resource
    private IUserService userService;
    @Resource
    private PasswordService passwordService;

    @PostMapping("/getToken")
    @ResponseBody
35
36
    public Result getToken(String loginName, String password, String warehouseCode) {
        if (StringUtils.isEmpty(loginName) || StringUtils.isEmpty(password)) {
mahuandong authored
37
38
39
40
41
            return Result.error("用户名和密码不能为空");
        }
        if (StringUtils.isEmpty(warehouseCode)) {
            return Result.error("请选择仓库");
        }
42
        User user = userService.selectUserByLoginName(loginName);
mahuandong authored
43
44
        if (!userService.checkWarehouseCodeAndUserName(warehouseCode, loginName)) {
mahuandong authored
45
46
            return Result.error("用户没有该仓库操作权限");
        }
47
        user.setWarehouseCode(warehouseCode);
mahuandong authored
48
        if (user.getPassword().equals(passwordService.encryptPassword(user.getLoginName(), password, user.getSalt()))) {
49
            String token = tokenService.createToken(user, password);
mahuandong authored
50
51
52
            Result ajaxResult = Result.success("成功");
            ajaxResult.put("token", token);
            ajaxResult.put("expireTime", Calendar.getInstance().getTime());
53
54
55
56
57
            //user数据加入session
            AjaxResult ajaxResult1 =  userService.login(loginName, password, warehouseCode, false);
            if(ajaxResult1.getCode() != 200){
                return Result.error("写入session失败!");
            }
mahuandong authored
58
59
60
61
62
63
            return ajaxResult;
        } else {
            return Result.error("密码错误");
        }

    }
游杰 authored
64
65
66
67

    @PostMapping("/getTokenForMobile")
    @ResponseBody
    public AjaxResult getTokenForMobile(@RequestBody @ApiParam(value="code和password的Map集合") Map<String, String> param) {
68
        String loginName = param.get("userName");
游杰 authored
69
        String password = param.get("password");
70
        if (StringUtils.isEmpty(loginName) || StringUtils.isEmpty(password)) {
游杰 authored
71
72
            return AjaxResult.error("用户名和密码不能为空");
        }
73
74
        //需要传入仓库code
        User user = userService.selectUserByLoginName(loginName);
游杰 authored
75
76
77
78
79
80
81
82
        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("密码错误");
        }

    }
mahuandong authored
83
}