com.ape9527.core.controller.BaseController Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ape-core Show documentation
Show all versions of ape-core Show documentation
Ape low code platform core module
The newest version!
package com.ape9527.core.controller;
import com.alibaba.fastjson.JSONArray;
import com.ape9527.auth.service.LoginUserService;
import com.ape9527.utils.ip.AddressUtil;
import com.ape9527.utils.ip.IpUtil;
import com.ape9527.utils.servlet.ServletUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
/**
* 控制层的抽象类,定义了一些通用方法,建议每个controller都集成此类
*
* @author YuanShuai[[email protected]]
*/
@Component
public abstract class BaseController {
@Autowired
private LoginUserService loginUserService;
/**
* 获取request
*
* @return HttpServletRequest
*/
public HttpServletRequest getRequest() {
return ServletUtil.getRequest();
}
/**
* 获取header参数
*
* @param headerName 请求头名称
* @return 请求头值
*/
public String getHeader(String headerName) {
return getRequest().getHeader(headerName);
}
/**
* 获取参数值
*
* @param paramName 参数名
* @return 参数值
*/
public String getParam(String paramName) {
return ServletUtil.getParameter(paramName);
}
/**
* 获取int类型的参数值
*
* @param paramName 参数名
* @return 参数值
*/
public Integer getParamToInt(String paramName) {
return ServletUtil.getParameterToInt(paramName);
}
/**
* 获取token
* @return token
*/
public String getToken(){
return getHeader("Authorization");
}
/**
* 获取当前登录用户ID
*
* @return 用户ID
*/
public String getLoginUserId() {
return loginUserService.getUserId();
}
/**
* 获取当前登录用户部门
*
* @return 部门编号
*/
public String getLoginUserDeptCode() {
return loginUserService.getDeptCode();
}
/**
* 获取当前登录用户角色
*
* @return 角色编号
*/
public JSONArray getLoginUserRoleCode() {
return loginUserService.getRoleCode();
}
/**
* 获取IP地址
* @return
*/
public String getIp(){
return IpUtil.getClientIpAddress(getRequest());
}
/**
* 获取实际位置
* @return
*/
public String getAddress(){
return AddressUtil.getRealAddressByIP(getIp());
}
}