liyang пре 1 година
родитељ
комит
26604c2c95

+ 5 - 0
pom.xml

@@ -143,6 +143,11 @@
 			<artifactId>gson</artifactId>
 		</dependency>
 
+		<dependency>
+			<groupId>org.apache.httpcomponents</groupId>
+			<artifactId>httpclient</artifactId>
+		</dependency>
+
 	</dependencies>
 
 	<build>

+ 15 - 0
src/main/java/com/ywt/biz/common/enums/RefundStatusEnum.java

@@ -23,4 +23,19 @@ public enum RefundStatusEnum {
     public int getValue() {
         return value;
     }
+
+    public static String getDisplayName(int value) {
+        switch (value) {
+            case 0:
+                return PENDING.displayName;
+            case 1:
+                return SUCCESS.displayName;
+            case 2:
+                return EXCEPTION.displayName;
+            case 3:
+                return CLOSED.displayName;
+            default:
+                return "";
+        }
+    }
 }

+ 18 - 0
src/main/java/com/ywt/biz/common/enums/SexEnum.java

@@ -46,4 +46,22 @@ public enum SexEnum {
 
         return "未知";
     }
+
+    public static SexEnum getByVal(int value) {
+        for(SexEnum sexEnum : SexEnum.values()){
+            if(sexEnum.getValue() == value){
+                return sexEnum;
+            }
+        }
+        return null;
+    }
+
+    public static SexEnum getByName(String displayName) {
+        for(SexEnum sexEnum : SexEnum.values()){
+            if(sexEnum.getDisplayName().equals(displayName)){
+                return sexEnum;
+            }
+        }
+        return null;
+    }
 }

+ 5 - 0
src/main/java/com/ywt/biz/common/util/Checker.java

@@ -257,4 +257,9 @@ public class Checker {
         return value != null && !"".equals(value.trim()) ? value.trim() : defaultValue;
     }
 
+    public static BigDecimal getBigDecimalValue(BigDecimal value) {
+        return value != null ? value : new BigDecimal("0.00");
+    }
+
+
 }

+ 44 - 0
src/main/java/com/ywt/biz/common/util/DateUtil.java

@@ -174,4 +174,48 @@ public class DateUtil {
         calendar.add(Calendar.DATE, -3);
         return calendar.getTime();
     }
+
+    /**
+     * 格式化日期
+     *
+     * @param date
+     * @param pattern
+     * @return
+     */
+    public static String formatDate(Date date, String pattern) {
+        if (StringHelper.isNullOrEmpty(pattern)) {
+            pattern = "yyyy-MM-dd HH:mm:ss";
+        }
+
+        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
+        return sdf.format(date);
+    }
+
+    /**
+     * 得到相对时间
+     *
+     * @param num 天数,大于0表示几天之后的时间,小于0表示几天之前的时间
+     * @return 时间
+     */
+    public static Date getRelativeDate(int num) {
+        return getRelativeDate(new Date(), num);
+    }
+
+    /**
+     * 得到相对时间
+     *
+     * @param date 传入的时间
+     * @param num  前几天,或者后几天
+     * @return 改变之后的时间
+     */
+    public static Date getRelativeDate(Date date, int num) {
+        Calendar calendar = Calendar.getInstance();
+        calendar.setTime(date);
+        calendar.add(Calendar.DATE, num);
+        return calendar.getTime();
+    }
+
+
+
+
 }

+ 13 - 0
src/main/java/com/ywt/biz/common/util/FormatUtil.java

@@ -316,5 +316,18 @@ public class FormatUtil {
         }
         return sb.toString();
     }
+
+    /**
+     * int 缩小100后转double
+     *
+     * @param value
+     * @return
+     */
+    public static double intShrink100ToDobule(Integer value) {
+        if (value == null) {
+            return 0.00;
+        }
+        return value / 100.0;
+    }
 }
 

+ 269 - 0
src/main/java/com/ywt/biz/common/util/HttpUtil.java

@@ -0,0 +1,269 @@
+package com.ywt.biz.common.util;
+
+import org.apache.http.Header;
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.entity.UrlEncodedFormEntity;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.message.BasicNameValuePair;
+import org.apache.http.util.EntityUtils;
+
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * HTTP请求工具类
+ * Created by huangguoping.
+ */
+public class HttpUtil {
+
+    /**
+     * 返回 byte[], 支持 GET 方式下载文件
+     * Added by Walker on 2019年9月5日
+     * @param url
+     * @param params
+     * @param headers
+     * @return
+     * @throws IOException
+     */
+    @SuppressWarnings("javadoc")
+    public static ByteArrayResp httpGetByteArray(String url, Map<String,String> params, Map<String, String> headers) throws IOException {
+
+        HttpClient client = HttpClients.createDefault();
+
+        StringBuilder stringBuilder = new StringBuilder();
+        if (params != null && params.size() > 0){
+            for (Map.Entry<String, String> entry : params.entrySet()){
+                if (entry.getValue() == null || entry.getValue().equals("")){
+                    stringBuilder.append(entry.getKey()).append("=");
+                    stringBuilder.append("&");
+                    continue;
+                }
+                try {
+                    stringBuilder.append(entry.getKey()).append("=").append(URLEncoder.encode(entry.getValue(), "utf-8"));
+                } catch (UnsupportedEncodingException e) {
+                }
+                stringBuilder.append("&");
+            }
+        }
+        if (stringBuilder.length() > 0) {
+            stringBuilder.replace(stringBuilder.length() - 1, stringBuilder.length() - 1, "&");
+        }
+        StringBuilder stringBuilder_url = new StringBuilder();
+        if (url.contains("?")){
+            if (url.endsWith("?")) stringBuilder_url.append(url).append(stringBuilder);
+            else if (url.endsWith("&")) stringBuilder_url.append(url).append(stringBuilder);
+            else stringBuilder_url.append(url).append("&").append(stringBuilder);
+        }else{
+            stringBuilder_url.append(url).append("?").append(stringBuilder);
+        }
+
+        HttpGet hget = new HttpGet(stringBuilder_url.toString());
+        if (headers != null && headers.size() > 0){
+            for (Map.Entry<String, String> entry : headers.entrySet()){
+                if (entry.getValue() == null || entry.getValue().equals("")) continue;
+                hget.addHeader(entry.getKey(), entry.getValue());
+            }
+        }
+
+        HttpResponse response = client.execute(hget);
+        ByteArrayResp resp = new ByteArrayResp();
+        resp.code = response.getStatusLine().getStatusCode();
+        try{
+            HttpEntity entity = response.getEntity();
+            Header contentEncode = entity.getContentEncoding();
+            resp.content = EntityUtils.toByteArray(entity);
+        } catch (Exception e){
+            // do nothing...
+        }
+        return resp;
+    }
+
+    public static Resp httpGet(String url, Map<String,String> params, Map<String, String> headers, String encode) throws IOException {
+
+        HttpClient client = HttpClients.createDefault();
+
+        StringBuilder stringBuilder = new StringBuilder();
+        if (params != null && params.size() > 0){
+            for (Map.Entry<String, String> entry : params.entrySet()){
+                if (entry.getValue() == null || entry.getValue().equals("")){
+                    stringBuilder.append(entry.getKey()).append("=");
+                    stringBuilder.append("&");
+                    continue;
+                }
+                try {
+                    stringBuilder.append(entry.getKey()).append("=").append(URLEncoder.encode(entry.getValue(), "utf-8"));
+                } catch (UnsupportedEncodingException e) {
+                }
+                stringBuilder.append("&");
+            }
+        }
+        if (stringBuilder.length() > 0) {
+            stringBuilder.replace(stringBuilder.length() - 1, stringBuilder.length() - 1, "&");
+        }
+        StringBuilder stringBuilder_url = new StringBuilder();
+        if (url.contains("?")){
+            if (url.endsWith("?")) stringBuilder_url.append(url).append(stringBuilder);
+            else if (url.endsWith("&")) stringBuilder_url.append(url).append(stringBuilder);
+            else stringBuilder_url.append(url).append("&").append(stringBuilder);
+        }else{
+            stringBuilder_url.append(url).append("?").append(stringBuilder);
+        }
+
+        HttpGet hget = new HttpGet(stringBuilder_url.toString());
+        if (headers != null && headers.size() > 0){
+            for (Map.Entry<String, String> entry : headers.entrySet()){
+                if (entry.getValue() == null || entry.getValue().equals("")) continue;
+                hget.addHeader(entry.getKey(), entry.getValue());
+            }
+        }
+
+        HttpResponse response = client.execute(hget);
+        Resp resp = new Resp();
+        resp.code = response.getStatusLine().getStatusCode();
+        try{
+            HttpEntity entity = response.getEntity();
+            Header contentEncode = entity.getContentEncoding();
+            if (contentEncode != null) encode = contentEncode.getValue();
+            resp.content = EntityUtils.toString(entity, encode);
+        } catch (Exception e){
+        }
+        return resp;
+    }
+
+    public static Resp httpGet(String url, Map<String,String> params) throws IOException {
+        return httpGet(url, params, null, "utf-8");
+    }
+
+    /**
+     * 表单提交POST请求
+     * @param url
+     * @param params
+     * @param headers
+     * @param encode
+     * @return
+     * @throws IOException
+     */
+    public static Resp httpPost(String url, Map<String,String> params, Map<String, String> headers, String encode) throws IOException {
+        HttpClient client = HttpClients.createDefault();
+
+        HttpPost hPost = new HttpPost(url);
+        if (headers != null && headers.size() > 0){
+            for (Map.Entry<String, String> entry : headers.entrySet()){
+                if (entry.getValue() == null || entry.getValue().equals("")) continue;
+                hPost.addHeader(entry.getKey(), entry.getValue());
+            }
+        }
+
+        if (params != null && params.size() > 0){
+            List<BasicNameValuePair> parameters = new ArrayList<>();
+            for (Map.Entry<String, String> entry : params.entrySet()){
+                if (entry.getValue() == null || entry.getValue().equals("")) continue;
+                parameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
+            }
+            hPost.setEntity(new UrlEncodedFormEntity(parameters, "utf-8"));
+        }
+
+        HttpResponse response = client.execute(hPost);
+        Resp resp = new Resp();
+        resp.code = response.getStatusLine().getStatusCode();
+        if (resp.code == 200){
+            HttpEntity entity = response.getEntity();
+            Header contentEncode = entity.getContentEncoding();
+            if (contentEncode != null) encode = contentEncode.getValue();
+            resp.content = EntityUtils.toString(entity, encode);
+        }
+        return resp;
+    }
+
+    /**
+     * 开放式数据提交POST请求
+     * @param url
+     * @param data
+     * @param headers
+     * @param encode
+     * @return
+     * @throws IOException
+     */
+    public static Resp httpPost(String url, String data, Map<String, String> headers, String encode) throws IOException {
+        HttpClient client = HttpClients.createDefault();
+
+        HttpPost hPost = new HttpPost(url);
+        if (headers != null && headers.size() > 0){
+            for (Map.Entry<String, String> entry : headers.entrySet()){
+                if (entry.getValue() == null || entry.getValue().equals("")) continue;
+                hPost.addHeader(entry.getKey(), entry.getValue());
+            }
+        }
+
+        if (data != null && !data.equals("")){
+            hPost.setEntity(new StringEntity(data, "utf-8"));
+        }
+
+        HttpResponse response = client.execute(hPost);
+        Resp resp = new Resp();
+        resp.code = response.getStatusLine().getStatusCode();
+        try{
+            HttpEntity entity = response.getEntity();
+            Header contentEncode = entity.getContentEncoding();
+            if (contentEncode != null) encode = contentEncode.getValue();
+            resp.content = EntityUtils.toString(entity, encode);
+        } catch (Exception e){
+        }
+        return resp;
+    }
+
+    public static Resp httpPost(String url, Map<String,String> params) throws IOException {
+        return httpPost(url, params, null, "utf-8");
+    }
+
+    public static class Resp {
+        public int code;
+        public String content;
+
+        public int getCode() {
+            return code;
+        }
+
+        public void setCode(int code) {
+            this.code = code;
+        }
+
+        public String getContent() {
+            return content;
+        }
+
+        public void setContent(String content) {
+            this.content = content;
+        }
+    }
+
+    public static class ByteArrayResp {
+        public int code;
+        public byte[] content;
+
+        public int getCode() {
+            return code;
+        }
+
+        public void setCode(int code) {
+            this.code = code;
+        }
+
+        public byte[] getContent() {
+            return content;
+        }
+
+        public void setContent(byte[] content) {
+            this.content = content;
+        }
+    }
+}