You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					
					
						
							159 lines
						
					
					
						
							4.8 KiB
						
					
					
				
			
		
		
	
	
							159 lines
						
					
					
						
							4.8 KiB
						
					
					
				| package com.ynxbd.common.action.base;
 | |
| 
 | |
| import com.ynxbd.common.helper.common.HttpHelper;
 | |
| import com.ynxbd.wx.wxfactory.ReqParamHelper;
 | |
| import org.apache.commons.lang3.StringUtils;
 | |
| import org.apache.struts2.ServletActionContext;
 | |
| import org.apache.struts2.action.ServletRequestAware;
 | |
| import org.apache.struts2.convention.annotation.ParentPackage;
 | |
| import org.slf4j.MDC;
 | |
| 
 | |
| import javax.servlet.ServletContext;
 | |
| import javax.servlet.http.HttpServletRequest;
 | |
| import javax.servlet.http.HttpServletResponse;
 | |
| import javax.servlet.http.HttpSession;
 | |
| import java.io.IOException;
 | |
| import java.io.PrintWriter;
 | |
| import java.io.Serializable;
 | |
| import java.io.UnsupportedEncodingException;
 | |
| import java.math.BigDecimal;
 | |
| 
 | |
| 
 | |
| /**
 | |
|  * @author 王绍全
 | |
|  * @version v3.0.0
 | |
|  * @project:微信公众号
 | |
|  * @date 2022年02月21日下午12:39:32
 | |
|  * @copyright: 2022云南新八达科技有限公司 All rights reserved.
 | |
|  */
 | |
| @ParentPackage("default")
 | |
| public class BaseAction implements Serializable, ServletRequestAware {
 | |
|     private static final long serialVersionUID = 3878349193368881604L;
 | |
| 
 | |
|     protected HttpServletRequest request;
 | |
| 
 | |
|     protected HttpServletResponse response;
 | |
| 
 | |
| 
 | |
|     @Override
 | |
|     public void withServletRequest(HttpServletRequest request) {
 | |
|         try {
 | |
|             request.setCharacterEncoding("utf-8");
 | |
|             MDC.remove("ip");
 | |
|             MDC.put("ip", HttpHelper.getIpAddress(request));
 | |
|         } catch (UnsupportedEncodingException e) {
 | |
|             e.printStackTrace();
 | |
|         }
 | |
| 
 | |
|         this.request = request;
 | |
| 
 | |
|         HttpServletResponse response = ServletActionContext.getResponse();
 | |
|         response.setContentType("application/json;charset=utf-8");
 | |
|         /* 允许跨域的主机地址 */
 | |
|         response.setHeader("Access-Control-Allow-Origin", "*");
 | |
|         /* 允许跨域的请求方法GET, POST, HEAD 等 */
 | |
|         response.setHeader("Access-Control-Allow-Methods", "*");
 | |
|         /* 重新预检验跨域的缓存时间 (s) */
 | |
|         response.setHeader("Access-Control-Max-Age", "3600");
 | |
|         /* 允许跨域的请求头 */
 | |
|         response.setHeader("Access-Control-Allow-Headers", "*");
 | |
|         /* 是否携带cookie */
 | |
|         response.setHeader("Access-Control-Allow-Credentials", "true");
 | |
|     }
 | |
| 
 | |
|     protected HttpServletRequest getRequest() {
 | |
|         return ServletActionContext.getRequest();
 | |
|     }
 | |
| 
 | |
|     protected HttpServletResponse getResponse() {
 | |
|         return ServletActionContext.getResponse();
 | |
|     }
 | |
| 
 | |
|     protected HttpSession getSession() {
 | |
|         return this.getRequest().getSession();
 | |
|     }
 | |
| 
 | |
|     protected ServletContext getServletContext() {
 | |
|         return ServletActionContext.getServletContext();
 | |
|     }
 | |
| 
 | |
|     protected String getString(String param) {
 | |
|         String parameter = this.request.getParameter(param);
 | |
|         if ("".equals(parameter)) {
 | |
|             return null;
 | |
|         }
 | |
|         return parameter;
 | |
|     }
 | |
| 
 | |
|     protected String getDecodeString(String param) {
 | |
|         return ReqParamHelper.decode(param);
 | |
|     }
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
|     protected Integer getInteger(String param) {
 | |
|         try {
 | |
|             String val = this.request.getParameter(param);
 | |
|             return StringUtils.isEmpty(val) ? null : Integer.valueOf(val);
 | |
|         } catch (Exception e) {
 | |
|             return null;
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     protected Long getLong(String param) {
 | |
|         try {
 | |
|             String val = this.request.getParameter(param);
 | |
|             return StringUtils.isEmpty(val) ? null : Long.valueOf(val);
 | |
|         } catch (Exception e) {
 | |
|             return null;
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     protected Double getDouble(String param) {
 | |
|         try {
 | |
|             String val = this.request.getParameter(param);
 | |
|             return StringUtils.isEmpty(val) ? null : Double.valueOf(val);
 | |
|         } catch (Exception e) {
 | |
|             return null;
 | |
|         }
 | |
|     }
 | |
| 
 | |
| 
 | |
| 
 | |
|     protected BigDecimal getBigDecimal(String param) {
 | |
|         try {
 | |
|             String val = this.request.getParameter(param);
 | |
|             return StringUtils.isEmpty(val) ? null : new BigDecimal(val);
 | |
|         } catch (Exception e) {
 | |
|             return null;
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     protected Boolean getBoolean(String param) {
 | |
|         try {
 | |
|             String val = this.request.getParameter(param);
 | |
|             return StringUtils.isEmpty(val) ? null : Boolean.valueOf(val);
 | |
|         } catch (Exception e) {
 | |
|             return null;
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * 设置响应内容
 | |
|      *
 | |
|      * @param message 响应信息
 | |
|      */
 | |
|     protected String respEnd(String message) {
 | |
|         HttpServletResponse response = this.getResponse();
 | |
|         response.setContentType("application/json;charset=utf-8");
 | |
|         try (PrintWriter pw = response.getWriter()) {
 | |
|             pw.write(message);
 | |
|             pw.flush();
 | |
|         } catch (IOException e) {
 | |
|             e.printStackTrace();
 | |
|         }
 | |
|         return "SUCCESS";
 | |
|     }
 | |
| 
 | |
| }
 | |
| 
 |