TokenApi.java 3.23 KB
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("密码错误");
        }

    }
}