zxl
2025-05-30 ba524bc13846fcbedb231b4bebc9a1a0927c5f70
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
 
package cn.lili.modules.payment.kit.plugin.unionpay;
 
import cn.hutool.core.util.StrUtil;
 
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
 
/**
 * <p>IJPay 让支付触手可及,封装了微信支付、支付宝支付、银联支付等常用的支付方式以及各种常用的接口。</p>
 *
 * <p>不依赖任何第三方 mvc 框架,仅仅作为工具使用简单快速完成支付模块的开发,可轻松嵌入到任何系统里。 </p>
 *
 * <p>IJPay 交流群: 723992875</p>
 *
 * <p>Node.js 版: https://gitee.com/javen205/TNWX</p>
 *
 * <p>云闪付常用配置 Kit</p>
 *
 * @author Javen
 */
public class UnionPayApiConfigKit {
    private static final ThreadLocal<String> TL = new ThreadLocal<String>();
 
    private static final Map<String, UnionPayApiConfig> CFG_MAP = new ConcurrentHashMap<String, UnionPayApiConfig>();
    private static final String DEFAULT_CFG_KEY = "_default_key_";
 
    /**
     * 添加云闪付配置,每个 mchId 只需添加一次,相同 mchId 将被覆盖
     *
     * @param UnionPayApiConfig 云闪付配置
     * @return {@link UnionPayApiConfig} 云闪付配置
     */
    public static UnionPayApiConfig putApiConfig(UnionPayApiConfig UnionPayApiConfig) {
        if (CFG_MAP.size() == 0) {
            CFG_MAP.put(DEFAULT_CFG_KEY, UnionPayApiConfig);
        }
        return CFG_MAP.put(UnionPayApiConfig.getMchId(), UnionPayApiConfig);
    }
 
    public static UnionPayApiConfig setThreadLocalApiConfig(UnionPayApiConfig UnionPayApiConfig) {
        if (StrUtil.isNotEmpty(UnionPayApiConfig.getMchId())) {
            setThreadLocalMchId(UnionPayApiConfig.getMchId());
        }
        return putApiConfig(UnionPayApiConfig);
    }
 
    public static UnionPayApiConfig removeApiConfig(UnionPayApiConfig UnionPayApiConfig) {
        return removeApiConfig(UnionPayApiConfig.getMchId());
    }
 
    public static UnionPayApiConfig removeApiConfig(String mchId) {
        return CFG_MAP.remove(mchId);
    }
 
    public static void setThreadLocalMchId(String mchId) {
        if (StrUtil.isEmpty(mchId)) {
            mchId = CFG_MAP.get(DEFAULT_CFG_KEY).getMchId();
        }
        TL.set(mchId);
    }
 
    public static void removeThreadLocalMchId() {
        TL.remove();
    }
 
    public static String getMchId() {
        String appId = TL.get();
        if (StrUtil.isEmpty(appId)) {
            appId = CFG_MAP.get(DEFAULT_CFG_KEY).getMchId();
        }
        return appId;
    }
 
    public static UnionPayApiConfig getApiConfig() {
        String appId = getMchId();
        return getApiConfig(appId);
    }
 
    public static UnionPayApiConfig getApiConfig(String appId) {
        UnionPayApiConfig cfg = CFG_MAP.get(appId);
        if (cfg == null) {
            throw new IllegalStateException("需事先调用 UnionPayApiConfigKit.putApiConfig(UnionPayApiConfig) 将 mchId 对应的 UnionPayApiConfig 对象存入,才可以使用 UnionPayApiConfigKit.getUnionPayApiConfig() 的系列方法");
        }
        return cfg;
    }
}