一些实用代码 消息队列 和 反解openIds

申明:下面代码都是取的一小段,没有一些技术基础的不适合看

一:消息队列

//是否需要放到消息队列,分别处理即时消息和定时消息
        if(status == Constants.STATUS_MSG_SEND){
        if( !isTiming ){
        redisHelper.putToQueene(Constants.KEY_LIST_MSG+pubAccount.getLevel(), msgId);
        }else{//存放定时队列
        redisHelper.putToTimingQueene(Constants.KEY_LIST_TIMING_MSG,
        msgId+"||"+pubAccount.getLevel(), msg.getSendTime().getTime());
        }
        }



@Autowired(required = false)
    @Qualifier("jedisCache")
    private RedisCache redisCache;


/**
* 设置缓存
* @param queeneName
* @param value
*/
public void putToQueene(String queeneName, Object value){
RedisList queene = redisCache.getRedisList( redisCache.getTranscoder(), queeneName );
queene.rightPush(value);
}

 /**
* 设置定时消息队列
* @param queeneName
* @param value
*/
public void putToTimingQueene(String queeneName, Object value, double score){
RedisSortedSet sortSet = redisCache.getRedisSortedSet( redisCache.getTranscoder(), queeneName );
sortSet.add(queeneName, score, value );
}


二:反解openIds

List<String> openIds = Arrays.asList( openIdsStr.split( "," ) );
//根据openIds反解出deviceIds
Set<String> deviceIds = new HashSet<>( openIds.size());
for (String oid : openIds) {
String[] decodedId = UmHelper.decodeOpenId(oid);
if (decodedId != null && decodedId.length == 2 && StringUtil.isNumber(decodedId[0])
       && decodedId[0].equals(Long.toString(pubId).intern())) {
     deviceIds.add(decodedId[1]);
 }
}
if(deviceIds!=null){
msgObj.setDeviceIds(deviceIds);
}

    /**
     * 通过openId反解pubId和deviceId
     *
     * @param openId
     * @return 数组中[0]是pubId,[1]是deviceId
     */
    public static String[] decodeOpenId(String openId) {
        String[] pubDevArray = null;
        try {
            String encryptDec = null;


            String[] openIdArray = openId.split("_");
            if (openIdArray.length == 1) {
                encryptDec = convertToBigInt(openId).toString(16);
                pubDevArray = XXTea.decrypt(encryptDec, xxteaKey).split(":");
            } else if (openIdArray.length == 2) {
                encryptDec = openIdArray[0] + convertToBigInt(openIdArray[1]).toString(16);
                pubDevArray = XXTea.decrypt(encryptDec, xxteaKey).split(":");
            }


        } catch (Exception e) {
            log.error("decodeOpenId error! openId:" + openId);
            e.printStackTrace();
        }
        if (pubDevArray == null || pubDevArray.length != 2) {
            return null;
        }
        pubDevArray[0] = convertToBigInt(pubDevArray[0]).toString(10);
        return pubDevArray;
    }



  /**
     * 将62进制转化为10进制
     *
     * @param cryptStr
     * @return
     */
    private static BigInteger convertToBigInt(String cryptStr) {
        BigInteger multiple = BigInteger.valueOf(1L);
        BigInteger result = BigInteger.valueOf(0L);


        char[] chars = cryptStr.toCharArray();
        for (char c : chars) {
            BigInteger position = BigInteger.valueOf(-1L);
            for (int j = 0; j < CHARS.length; j++) {
                if (c == CHARS[j]) {
                    position = BigInteger.valueOf(j);
                    break;
                }
            }
            result = result.multiply(charsLength).add(position);
        }
        
        /*for (int i = 0; i < cryptStr.length(); i++) {
            c = cryptStr.charAt(cryptStr.length() - i - 1);


            BigInteger position = BigInteger.valueOf(-1L);
            for (int j = 0; j < CHARS.length; j++) {
                if (c == CHARS[j]) {
                    position = BigInteger.valueOf(j);
                }
            }


            result = result.add(position.multiply(multiple));
            multiple = multiple.multiply(charsLength);
        }*/
        return result;
    }


以上代码还不能运行,还少了些许方法(方法里面的算法还是挺难懂的)

附上需要的方法,加入这些方法,方能把openid解出deviceid,不知道还有没有考虑不周全的。

输入:AmWWAMDCU3hMqmQv8eXP459qAazB240x

     输出:ubKcumjUa



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值