xuekang
2024-05-11 bac0878349a1db23e7b420ea164e22fb9db73a99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package com.nuvole.util;
// @formatter:off
 
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;
import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSON;
import com.nuvole.constants.SystemConstants;
import jakarta.servlet.ServletInputStream;import jakarta.servlet.http.HttpServletRequest;import jakarta.servlet.http.HttpServletResponse;import org.apache.commons.text.StringEscapeUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
 
/**
 * .-~~~~~~~~~-._       _.-~~~~~~~~~-.
 * __.'  @Author     ~.   .~    代码无Bug   `.__
 * .'//     liu.q        \./       (秘籍)      \\`.
 * .'// [916000612@qq.com]  |   欲练神功   引刀自宫  \\`.
 * .'// .-~"""""""~~~~-._     |    _,-~~~~"""""""~-.  \\`.
 * .'//.-"  2019-04-09     `-.  |  .-'     10:48       "-.\\`.
 * .'//______.============-..   \ | /   ..-============.______\\`.
 * .'______________________________\|/______________________________`.
 *
 * @Description :
 */
// @formatter:on
public class CommonUtil {
 
    /**
     * @Author : liuq
     * @Date : Create in 2018/7/1 上午1:20
     * @Description : 驼峰转下划线
     */
    public static String camel2Underline(String line) {
        return StrUtil.toUnderlineCase(line);
    }
 
    /*
     * @Author : liu.q [916000612@qq.com]
     *
     * @Date : 2019-04-29 16:45
     *
     * @Description : 下划线转驼峰
     */
    public static String Underline2camel(String para) {
        return StrUtil.toCamelCase(para);
    }
 
    /**
     * @Author : liu.q
     * @Date : 2019-03-01 09:49
     * @Description : 获取request
     */
    public static HttpServletRequest getRequest() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        return request;
    }
 
    /**
     * @Author : liu.q
     * @Date : 2019-03-01 09:49
     * @Description : response
     */
    public static HttpServletResponse getResponse() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes();
        HttpServletResponse response = requestAttributes.getResponse();
        return response;
    }
 
    /**
     * @Author : liu.q
     * @Date : 2019-03-01 09:51
     * @Description : 获取访问地址
     */
    public static String getHost() {
        HttpServletRequest request = getRequest();
        String scheme = request.getHeader("X-Forwarded-Scheme");
        if (scheme == null) {
            scheme = request.getScheme();
        }
        String port = request.getHeader("X-Forwarded-port");
        if (port == null) {
            port = request.getServerPort() + "";
        }
        if ("443".equals(port) || "80".equals(port)) {
            return scheme + "://" + request.getServerName();
        }
        return scheme + "://" + request.getServerName() + ":" + port;// +request.getRequestURI();
    }
 
    /**
     * 获取请求参数
     *
     * @return
     */
    public static Map getParameterMap() {
        // 参数Map
        Map properties = CommonUtil.getRequest().getParameterMap();
        // 返回值Map
        Map returnMap = new HashMap();
        Iterator entries = properties.entrySet().iterator();
        Map.Entry entry;
        String name = "";
        String value = "";
        while (entries.hasNext()) {
            entry = (Map.Entry) entries.next();
            name = (String) entry.getKey();
            Object valueObj = entry.getValue();
            if (null == valueObj) {
                continue;
            } else if (valueObj instanceof String[]) {
                String[] values = (String[]) valueObj;
                for (int i = 0; i < values.length; i++) {
                    value = values[i] + ",";
                }
                value = value.substring(0, value.length() - 1);
            } else {
                value = valueObj.toString();
                // 当value为string empty为有效值 by zxc on 2020/5/11 15:00
                if ("".equals(value)) {
                    continue;
                }
            }
            returnMap.put(name, value);
        }
        return returnMap;
    }
 
    public static String getExportSign(String url, String timestamp) {
        System.out.println("is url:" + URLUtil.getPath(url));
        return Convert
                .toStr(SecureUtil.md5(SecureUtil.md5(URLUtil.getPath(url) + timestamp) + SystemConstants.EXPORT_SIGN_K).hashCode());
    }
 
    public static String listToString(List<String> items) {
        String result = "";
        if (items != null && items.size() > 0) {
            for (int i = 0; i < items.size(); i++) {
                if (i == 0) {
                    result += items.get(i);
                } else {
                    result += "," + items.get(i);
                }
            }
        }
        return result;
    }
 
    public static <T> T getObjFromReq(Class<T> clazz) {
        String s = JSON.toJSONString(getParameterMap());
        return JSON.parseObject(StringEscapeUtils.unescapeHtml4(s), clazz);
    }
 
    public static <T> T getObjFromReqBody(Class<T> clazz) {
        String jsonStr = getRequestBody();
        return JSON.parseObject(StringEscapeUtils.unescapeHtml4(jsonStr), clazz);
    }
 
    /**
     * 获取request body值
     *
     * @param
     * @return
     */
    private static String getRequestBody() {
        HttpServletRequest request = getRequest();
        try {
            return getStringFromInputStream(request.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
 
    public static String getStringFromInputStream(ServletInputStream stream) throws IOException {
        StringBuilder sb = new StringBuilder();
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
            char[] ch = new char[1024];
            int bytesRead;
            while ((bytesRead = reader.read(ch)) != -1) {
                sb.append(ch, 0, bytesRead);
            }
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return sb.toString();
    }
 
    /**
     * 获取用户标识
     *
     * @return
     */
    public static String getUserAgent() {
        HttpServletRequest request = getRequest();
        return request.getHeader("User-Agent");
    }
 
    /**
     * 获取客户端标识
     *
     * @return 1.支付宝-小程序, 2.支付宝-生活号, 3.微信-小程序, 4.微信-公众号
     */
    public static String getClientType() {
        HttpServletRequest request = getRequest();
        return request.getHeader("CLIENT-TYPE");
    }
 
    /**
     * 是否为微信小程序
     *
     * @return
     */
    public static boolean isWxMin() {
        String userAgent = getUserAgent();
        return userAgent.contains("MiniProgram");
    }
 
    /**
     * 是否为微信内置浏览器
     *
     * @return
     */
    public static boolean isWxBro() {
        String userAgent = getUserAgent();
        return userAgent.contains("MicroMessenger");
    }
 
    /**
     * 是否为支付宝内置浏览器
     *
     * @return
     */
    public static boolean isAliBro() {
        String userAgent = getUserAgent();
        return userAgent.contains("Alipay");
    }
 
    /**
     * 是否为支付宝小程序客户端
     *
     * @return
     */
    public static boolean isAliMin() {
        String userAgent = getUserAgent();
        String clientType = getClientType();
        return userAgent.contains("Alipay") && SystemConstants.APP_TYPE_ALI_MINI.equals(clientType);
    }
 
    /**
     * 是否为翼支付
     *
     * @return
     */
    public static boolean isBestPayBro() {
        String userAgent = getUserAgent();
        return userAgent.contains("Bestpay");
    }
 
    /**
     * 是否为云闪付
     *
     * @return
     */
    public static boolean isCloudPayBro() {
        String userAgent = getUserAgent();
        return userAgent.contains("CloudPay");
    }
 
    public static boolean isUnionPayBro() {
        String userAgent = getUserAgent();
        return userAgent.contains("UnionPay");
    }
 
}