好运哥电子面单签名工具类

2024-04-23 12:30:04 浏览数 (1)

由于好运哥官方并不直接提供对接sdk,签名文档写的也非常一般,开发人员对接比较麻烦,我根据文档自己编写了签名工具,有需要的可以直接使用,让开发更快一步。当然工具类中有些是不需要的可以自己删除下,我比较懒。下面是具体的类:

代码语言:javascript复制
package com.x3.plug.waybill.utils;

import com.x3.base.core.exception.BusinessException;
import com.x3.base.core.util.JsonUtil;
import com.x3.plug.waybill.entity.PostAddr;
import com.x3.plug.waybill.request.WayBillCreateRequest;
import org.springframework.util.StringUtils;

import java.security.MessageDigest;
import java.util.*;

/**
 * 好运哥电子面单工具类
 */
public class LuckyGoUtil {

    private final static String[] hexDigits = {"0", "1", "2", "3", "4", "5", "6", "7",
            "8", "9", "a", "b", "c", "d", "e", "f"};

    /**
     * 获取签名信息
     * @param obj
     * @param key
     * @return
     */
    public static String getSign(Object obj, String key)  {
        try {
            String json = JsonUtil.toJSONString(obj);
            Map<String,Object> map = JsonUtil.parseObject(json, Map.class);
            ArrayList<String> list = new ArrayList<>();
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                if (!StringUtils.isEmpty(entry.getValue())) {
                    list.add(entry.getKey()   "="   entry.getValue()   "&");
                }
            }
            int size = list.size();
            String[] arrayToSort = list.toArray(new String[size]);
            Arrays.sort(arrayToSort, String.CASE_INSENSITIVE_ORDER);
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < size; i  ) {
                sb.append(arrayToSort[i]);
            }
            String result = sb.toString();
            result  = "key="   key;
            result = MD5Encode(result).toUpperCase();
            return result;
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return "";
    }

    /**
     * MD5编码
     *
     * @param origin 原始字符串
     * @return 经过MD5加密之后的结果
     */
    public static String MD5Encode(String origin) {
        String resultString = null;
        try {
            resultString = origin;
            MessageDigest md = MessageDigest.getInstance("MD5");
            resultString = byteArrayToHexString(md.digest(resultString.getBytes()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultString;
    }

    /**
     * 转换字节数组为16进制字串
     *
     * @param b 字节数组
     * @return 16进制字串
     */
    public static String byteArrayToHexString(byte[] b) {
        StringBuilder resultSb = new StringBuilder();
        for (byte aB : b) {
            resultSb.append(byteToHexString(aB));
        }
        return resultSb.toString();
    }

    /**
     * 转换byte到16进制
     *
     * @param b 要转换的byte
     * @return 16进制格式
     */
    private static String byteToHexString(byte b) {
        int n = b;
        if (n < 0) {
            n = 256   n;
        }
        int d1 = n / 16;
        int d2 = n % 16;
        return hexDigits[d1]   hexDigits[d2];
    }

    /**
     * 根据规则生成oaId
     * (适用用抖音、拼多多、快手、小红书、视频号等其它平台解密)
     *
     * @param request
     * @return
     */
    public static String generateOaIdAccordingToRules(WayBillCreateRequest request) {
        /*
         * 规则说明
         * 将接收方密文信息进行拼接(固定格式),顺序为:姓名(receiver_contact),手机号(receiver_mobile),电话(receiver_tel),详细地址(receiver_address) 中间使用逗号风格,如果电话为空,需要用空格代替
         */

        //获取拼接变量(并且将为空的信息替换为空格)
        PostAddr receiver = request.getReceiver();
        String receiverContact = generateOaIdFieldDetail(receiver.getName()); // 姓名
        String receiverMobile = generateOaIdFieldDetail(receiver.getMobile()); // 手机号
        String receiverTel = generateOaIdFieldDetail(receiver.getMobile());  // 电话
        String receiverAddress = generateOaIdFieldDetail(receiver.getAddress()); // 详细地址

        //拼接获得oaId
        return receiverContact   ","   receiverMobile   ","   receiverTel   ","   receiverAddress;
    }

    /**
     * generateOaIdFieldDetail
     *
     * @param text
     * @return
     */
    private static String generateOaIdFieldDetail(String text) {
        if (text == null || text.isEmpty()) {
            return " ";
        }
        return text;
    }

    /**
     * 通过客户编号获取拣货单号
     *
     * @param orderNumber
     * @return
     */
    public static String getErpOrderIdByOrderNumber(String orderNumber) {
        int index = orderNumber.indexOf("T");
        if (index == -1) {
            throw new BusinessException("客户订单号格式不正确,请检查");
        }
        return orderNumber.substring(0, index);
    }
}

0 人点赞