申明:下面代码都是取的一小段,没有一些技术基础的不适合看
一:消息队列
//是否需要放到消息队列,分别处理即时消息和定时消息
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