From 30d8e227e8d823b6c38c3b9c90ac2df03b63befe Mon Sep 17 00:00:00 2001 From: admin <weikou2014> Date: 星期二, 25 二月 2025 16:41:22 +0800 Subject: [PATCH] 淘宝转链接口更新 --- fanli/src/main/java/com/yeshi/fanli/util/push/VIVOPushUtil.java | 367 +++++++++++++++++++++++++++++++++++++++------------ 1 files changed, 277 insertions(+), 90 deletions(-) diff --git a/fanli/src/main/java/com/yeshi/fanli/util/push/VIVOPushUtil.java b/fanli/src/main/java/com/yeshi/fanli/util/push/VIVOPushUtil.java index ed4abcd..045becc 100644 --- a/fanli/src/main/java/com/yeshi/fanli/util/push/VIVOPushUtil.java +++ b/fanli/src/main/java/com/yeshi/fanli/util/push/VIVOPushUtil.java @@ -1,90 +1,277 @@ -package com.yeshi.fanli.util.push; - -import java.io.IOException; -import java.io.UnsupportedEncodingException; -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; - -import org.apache.commons.httpclient.HttpClient; -import org.apache.commons.httpclient.HttpException; -import org.apache.commons.httpclient.methods.PostMethod; -import org.apache.commons.httpclient.methods.StringRequestEntity; -import org.yeshi.utils.HttpUtil; - -import com.yeshi.fanli.exception.push.VIVOPushException; -import com.yeshi.fanli.util.StringUtil; - -import net.sf.json.JSONObject; - -/** - * VIVO鐨勬帹閫佸府鍔� - * - * @author Administrator - * - */ -public class VIVOPushUtil { - - /** - * POST璇锋眰 - * - * @param url - * @param body - * @param headers - * @return - */ - private static String post(String url, String body, Map<String, String> headers) { - HttpClient httpClient = new HttpClient(); - PostMethod pm = new PostMethod(url); - try { - pm.setRequestEntity(new StringRequestEntity(body, "application/json", "UTF-8")); - } catch (UnsupportedEncodingException e) { - e.printStackTrace(); - } - - if (headers != null) { - Iterator<String> its = headers.keySet().iterator(); - while (its.hasNext()) { - String key = its.next(); - pm.setRequestHeader(key, headers.get(key)); - } - } - - try { - httpClient.executeMethod(pm); - return pm.getResponseBodyAsString(); - } catch (HttpException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - return null; - } - - /** - * 鑾峰彇鎺堟潈鐮� - * - * @param appId - * @param appKey - * @param appSecret - * @return - * @throws VIVOPushException - */ - public static String getAuthToken(String appId, String appKey, String appSecret) throws VIVOPushException { - JSONObject params = new JSONObject(); - long timestamp = System.currentTimeMillis(); - params.put("appId", appId); - params.put("appKey", appKey); - params.put("timestamp", timestamp); - params.put("sign", StringUtil.Md5(appId + appKey + timestamp + appSecret)); - String result = post("https://host:port/message/auth", params.toString(), null); - JSONObject resultJson = JSONObject.fromObject(result); - if (resultJson.optInt("result") != 0) - throw new VIVOPushException(resultJson.optInt("result"), resultJson.optString("desc")); - return resultJson.optString("authToken"); - } - - - - -} +package com.yeshi.fanli.util.push; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.UUID; + +import com.vivo.push.sdk.notofication.Message; +import com.vivo.push.sdk.notofication.Result; +import com.vivo.push.sdk.notofication.TargetMessage; +import com.vivo.push.sdk.server.Sender; +import com.yeshi.fanli.dto.push.PushTypeEnum; +import com.yeshi.fanli.exception.push.PushException; +import com.yeshi.fanli.util.Constant; +import com.yeshi.fanli.util.StringUtil; + +/** + * VIVO鐨勬帹閫佸府鍔� + * + * @author Administrator + * + */ +public class VIVOPushUtil { + + private static final int APP_ID = 15206; + private static final String APP_KEY = "996c07d6-ec2b-41ef-b7c9-abb51ac63abf"; + private static final String APP_SECRET = "882548de-8183-4d29-9769-48b2bb4169c8"; + static String authToken = null; + static long authTokenTime = 0L; + + /** + * 鑾峰彇鎺堟潈鐮� + * + * @param appId + * @param appKey + * @param appSecret + * @return + * @throws Exception + */ + private static String getAuthToken(int appId, String appKey, String appSecret) throws Exception { + Sender sender = new Sender(appSecret);// 瀹炰緥鍖朣ender + sender.initPool(20, 10);// 璁剧疆杩炴帴姹犲弬鏁帮紝鍙�夐」 + Result result = sender.getToken(appId, appKey);// 鍙戦�侀壌鏉冭姹� + if (result.getResult() == 0) { + return result.getAuthToken(); + } + throw new Exception("token鑾峰彇澶辫触"); + } + + private static String getAuthToken() { + if (StringUtil.isNullOrEmpty(authToken) || (System.currentTimeMillis() - authTokenTime) > 5000 * 60 * 60L) { + try { + String token = getAuthToken(APP_ID, APP_KEY, APP_SECRET); + authToken = token; + authTokenTime = System.currentTimeMillis(); + } catch (Exception e) { + e.printStackTrace(); + } + } + return authToken; + } + + private static Message.Builder getBaseMessageBuidler(String title, String content, + Map<String, String> customParams) { + Message.Builder buidler = new Message.Builder(); + + buidler = buidler.title(title).content(content).skipType(4).skipContent("todo").notifyType(3) + .requestId(UUID.randomUUID().toString()); + buidler = buidler.clientCustomMap(customParams); + return buidler; + } + + private static Message.Builder getMessageBuidler(PushTypeEnum type, String title, String content, String activity, + String url, String webUrl, String id) { + Map<String, String> data = new HashMap<>(); + if (type != null) + data.put("type", type.name()); + if (activity != null) + data.put("activity", activity); + if (url != null) + data.put("url", url); + if (webUrl != null) + data.put("webUrl", webUrl); + if (id != null) + data.put("id", id + ""); + return getBaseMessageBuidler(title, content, data); + } + + // 鎺ㄩ�佹墍鏈夎澶� + private static void pushAll(Message.Builder builder) { + String authToken = getAuthToken(); + if (StringUtil.isNullOrEmpty(authToken)) + return; + try { + Sender sender = new Sender(APP_SECRET); + sender.initPool(20, 10);// 璁剧疆杩炴帴姹犲弬鏁帮紝鍙�夐」 + sender.setAuthToken(authToken);// 璁剧疆鎺ㄩ�佺殑蹇呰鍙傛暟authToken(璋冪敤閴存潈鏂规硶鑾峰緱) + Message allSendMessage = builder.build();// 鏋勫缓瑕佸叏閲忔帹閫佺殑娑堟伅浣� + Result result = sender.sendToAll(allSendMessage); + if (result.getResult() == 0)// 鎴愬姛 + { + + } + } catch (Exception e) { + + } + } + + // 鎺ㄩ�侀儴鍒嗚澶� + private static boolean push(List<String> regIdList, Message.Builder builder) { + String authToken = getAuthToken(); + if (StringUtil.isNullOrEmpty(authToken)) + return false; + try { + Sender sender = new Sender(APP_SECRET); + sender.initPool(20, 10);// 璁剧疆杩炴帴姹犲弬鏁帮紝鍙�夐」 + sender.setAuthToken(authToken);// 璁剧疆鎺ㄩ�佺殑蹇呰鍙傛暟authToken(璋冪敤閴存潈鏂规硶鑾峰緱) + Message saveList = builder.timeToLive(3600).build();// 鏋勫缓瑕佷繚瀛樼殑鎵归噺鎺ㄩ�佹秷鎭綋 + Result result = null; + if (regIdList.size() > 1) { + result = sender.saveListPayLoad(saveList);// 鍙戦�佷繚瀛樼兢鎺ㄦ秷鎭姹� + String taskId = result.getTaskId(); + Set<String> regIds = new HashSet<>();// 鏋勫缓鎵归噺鎺ㄩ�佺敤鎴风兢 + for (String regId : regIdList) + regIds.add(regId); + TargetMessage targetMessage = new TargetMessage.Builder().requestId(UUID.randomUUID().toString()) + .regIds(regIds).taskId(taskId).build();// 鏋勫缓鎵归噺鎺ㄩ�佺殑娑堟伅浣� + result = sender.sendToList(targetMessage);// 鎵归噺鎺ㄩ�佺粰鐢ㄦ埛 + } else + { + result = sender.sendSingle(builder.regId(regIdList.get(0)).timeToLive(3600).build()); + } + if (result.getResult() == 0)// 鎴愬姛 + { + return true; + } + } catch (Exception e) { + e.printStackTrace(); + } + return false; + } + + /** + * 鎺ㄩ�佸晢鍝� + * + * @param registerIds + * -鏈�澶у��1000 + * @param title + * @param content + * @param goodsType + * @param goodsId + * @throws PushException + */ + public static void pushGoods(List<String> registerIds, String title, String content, int goodsType, String goodsId,String androidBaseActivity) + throws PushException { + if (registerIds != null && registerIds.size() > 500) + throw new PushException(1, "璁惧鏁颁笉鑳借秴杩�500涓�"); + + String activity = androidBaseActivity + + ".ui.recommend.GoodsBrowserActivity"; + Message.Builder builder = getMessageBuidler(PushTypeEnum.goodsdetail, title, content, activity, null, null, + goodsId); + try { + boolean success = push(registerIds, builder); + if (!success) + throw new PushException(2, "鎺ㄩ�佸け璐�"); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * 缃戦〉鎺ㄩ�� + * + * @param registerIds + * 鏈�澶у��1000 + * @param title + * @param content + * @param url + * -缃戦〉閾炬帴 + * @throws PushException + */ + public static void pushUrl(List<String> registerIds, String title, String content, String url,String androidBaseActivity) + throws PushException { + if (registerIds != null && registerIds.size() > 500) + throw new PushException(1, "璁惧鏁颁笉鑳借秴杩�500涓�"); + String activity = androidBaseActivity + ".ui.invite.ShareBrowserActivity"; + Message.Builder builder = getMessageBuidler(PushTypeEnum.url, title, content, activity, null, url, null); + try { + boolean success = push(registerIds, builder); + if (!success) + throw new PushException(2, "鎺ㄩ�佸け璐�"); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * 绔欏唴淇℃帹閫� + * + * @param registerIds + * 鏈�澶у��1000 + * @param title + * @param content + * @param url + * -缃戦〉閾炬帴 + * @throws PushException + */ + public static void pushZNX(List<String> registerIds, String title, String content) throws PushException { + if (registerIds != null && registerIds.size() > 500) + throw new PushException(1, "璁惧鏁颁笉鑳借秴杩�500涓�"); + Message.Builder builder = getMessageBuidler(PushTypeEnum.ZNX, title, content, null, null, null, null); + try { + boolean success = push(registerIds, builder); + if (!success) + throw new PushException(2, "鎺ㄩ�佸け璐�"); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public static void pushWEEX(List<String> registerIds, String title, String content, String weexUrl,String androidBaseActivity) + throws PushException { + String activity = androidBaseActivity + + ".ui.mine.weex.WeexApplicationActivity"; + Message.Builder builder = getMessageBuidler(PushTypeEnum.weex, title, content, activity, weexUrl, weexUrl, + null); + try { + boolean success = push(registerIds, builder); + if (!success) + throw new PushException(2, "鎺ㄩ�佸け璐�"); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public static void pushBaiChuanUrl(List<String> registerIds, String title, String content, String url) + throws PushException { + Message.Builder builder = getMessageBuidler(PushTypeEnum.baichuan, title, content, null, url, null, null); + + try { + boolean success = push(registerIds, builder); + if (!success) + throw new PushException(2, "鎺ㄩ�佸け璐�"); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public static void pushWelfareCenter(List<String> registerIds, String title, String content,String androidBaseActivity) throws PushException { + String activity = androidBaseActivity + ".ui.mine.WelfareCenterActivity"; + Message.Builder builder = getMessageBuidler(PushTypeEnum.welfare, title, content, activity, null, null, null); + try { + boolean success = push(registerIds, builder); + if (!success) + throw new PushException(2, "鎺ㄩ�佸け璐�"); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public static void pushUserSignInNotification(List<String> registerIds, String title, String content,String androidBaseActivity) + throws PushException { + String activity = androidBaseActivity + ".ui.goldtask.GoldTaskActivity"; + Message.Builder builder = getMessageBuidler(PushTypeEnum.signin, title, content, activity, null, null, null); + try { + boolean success = push(registerIds, builder); + if (!success) + throw new PushException(2, "鎺ㄩ�佸け璐�"); + } catch (Exception e) { + e.printStackTrace(); + } + } + +} -- Gitblit v1.8.0