All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.qingxun.javasdkapi.clients.interfaces.CommonOpenApiClient Maven / Gradle / Ivy

package com.qingxun.javasdkapi.clients.interfaces;

import cn.hutool.crypto.SecureUtil;
import com.qingxun.javasdkapi.utils.RandomUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.Set;
import java.util.TreeMap;

public abstract class CommonOpenApiClient {

    /**
     * 应用的唯一识别标记
     */
    private String appId;

    private String privateKey;

    public CommonOpenApiClient(String appId, String privateKey) {
        this.privateKey = privateKey;
        this.appId = appId;
        if (StringUtils.isEmpty(appId)){
            throw new NullPointerException("传入appId为空");
        }
        if (StringUtils.isEmpty(privateKey)){
            throw new NullPointerException("传入privateKey为空");
        }
    }


    public String getAppId(){
        return  this.appId;
    }

    public String getPrivateKey(){
        return  this.privateKey;
    }


    public  TreeMap generateCommonParameter(TreeMap map) {
        String nonce_str = RandomUtil.getRandomStr(16);
        map.put("nonce_str", nonce_str);
        map.put("appid", this.getAppId());
        map.put("privatekey", this.getPrivateKey());
        StringBuilder result = new StringBuilder();
        Set set = map.keySet();
        for (String key : set) {
            result.append("&").append(key).append("=").append(map.get(key));
        }
        result = new StringBuilder(result.toString().replaceFirst("&", ""));
        String token = SecureUtil.md5(result.toString()).toUpperCase();
        map.put("token", token);
        map.remove("privatekey");
        return map;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy