Commit 4bb20e93 authored by renandong's avatar renandong 🇨🇳

1.加入mob推送

parent 94f7388b
......@@ -73,4 +73,44 @@ public class Constant {
return value;
}
}
/**
* mob推送目标
*/
public enum PushTarget {
RADIO(1, "广播群推"),
ALIAS(2, "别名推送"),
TAGS(3, "标签推送"),
RIDS(4, "rid推送"),
CITY(5, "地理位置推送"),
BLOCK(6, "用户分群推送"),
AREA(9, "复杂地理位置推送"),
FILE(14, "fileId推送"),
BATCH_ALIAS(22, "别名批量推送"),
BATCH_RIDS(33, "rid批量推送");
private int key;
private String value;
PushTarget(int key, String value) {
this.key = key;
this.value = value;
}
public int getKey() {
return key;
}
public void setKey(int key) {
this.key = key;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
}
package com.weface.component;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HtmlUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.weface.code.CommonResult;
import com.weface.common.utils.Constant;
import com.weface.common.utils.Model;
import com.weface.dto.InformForm;
import com.weface.service.PushLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author : Administrator
* @date : 2022/5/30 17:49
* mob 推送
*/
@Component
@Slf4j
public class MobPushService {
/**
* mob推送key
*/
private static final String APP_KEY = "3511c2579cad6";
/**
* mob推送密钥
*/
private static final String APP_SECRET = "f446a56b98aae09beb5721ebdbe0d2e8";
@Autowired
private PushLogService pushLogService;
/**
* mob 推送
*
* @param informForm 推送信息
* @param target 推送目标类型
* @return 执行结果
*/
public CommonResult mobPush(InformForm informForm, Constant.PushTarget target) {
String pushUrl = "http://api.push.mob.com/v3/push/createPush";
Map<String, Object> request = new HashMap<>();
//枚举值 webapi
request.put("source", "webapi");
//自定义任务id(用户自自定义生成且唯一、不能重复)
request.put("workno", IdUtil.fastUUID());
//appkey
request.put("appkey", APP_KEY);
informForm.setUrl(HtmlUtil.unescape(informForm.getUrl()));
String[] split = new String[0];
if (target.getKey() != 1) {
//推送目标
String cid = informForm.getCid();
if (cid.contains(",")) {
split = cid.split(",");
} else {
split = new String[]{cid};
}
}
Map<String, Object> pushTarget = getPushTarget(target.getKey(), split);
request.put("pushTarget", pushTarget);
//推送展示细节配置
Map<String, Object> pushNotify = getPushNotify(informForm);
request.put("pushNotify", pushNotify);
Map<String, Object> pushForward = getPushForward();
request.put("pushForward", pushForward);
//groupId: AB分组测试ID
request.put("groupId", target.getValue());
String body = generalPost(request, pushUrl);
if (body != null) {
pushLogService.saveMobLog(JSONObject.parseObject(body), informForm);
return CommonResult.success();
}
return null;
}
/**
* mob批量推送
*
* @param informForm 推送内容
* @param target 推送目标
* @return 执行结果
*/
public Model batchMobPush(InformForm informForm, Constant.PushTarget target) {
String pushUrl = "http://api.push.mob.com/v3/push/createMulti";
Map<String, Object> request = new HashMap<>(2);
Map<String, Object> pushWork = new HashMap<>();
//枚举值 webapi
pushWork.put("source", "webapi");
//Mob-appkey
pushWork.put("appkey", APP_KEY);
//推送展示细节配置
Map<String, Object> pushNotify = getBatchPushNotify(informForm);
pushWork.put("pushNotify", pushNotify);
//link 相关打开配置
Map<String, Object> pushForward = getPushForward();
pushWork.put("pushForward", pushForward);
//是否重复推送
//pushWork.put("repate", repate);
//repate 重复记录原始ID
//pushWork.put("parentId", parentId);
//groupId: AB分组测试ID
pushWork.put("groupId", target.getValue());
request.put("pushWork", pushWork);
//推送目标
String[] split;
String cid = informForm.getCid();
if (cid.contains(",")) {
split = cid.split(",");
} else {
split = new String[]{cid};
}
Map<String, Object> items = getItems(split, informForm.getTitle(), informForm.getBody(), target.getKey());
request.put("items", new Object[]{items});
String body = generalPost(request, pushUrl);
log.info("批量推送结果:{}", body);
if (body != null) {
return Model.ok();
}
return Model.error();
}
private Map<String, Object> getItems(String[] target, String title, String content, Integer pushType) {
Map<String, Object> items = new HashMap<>();
items.put("itemId", IdUtil.fastUUID());
//items.put("workNo", workNo);
//items.put("appPackages", new String[]{});
if (pushType.equals(Constant.PushTarget.ALIAS.getKey())) {
items.put("alias", target);
}
if (pushType.equals(Constant.PushTarget.RIDS.getKey())) {
items.put("rids", target);
}
items.put("title", title);
items.put("content", content);
return items;
}
/**
* 批量推送
*
* @param informForm 推送信息
* @return 执行结果
*/
private Map<String, Object> getBatchPushNotify(InformForm informForm) {
//推送展示细节配置
Map<String, Object> pushNotify = new HashMap<>(8);
//1 android;2 ios
pushNotify.put("plats", new Integer[]{1, 2});
//推送类型:1通知;2自定义
pushNotify.put("type", 1);
//android通知消息, type=1, android
Map<String, Object> androidNotify = getAndroidNotify();
if (!androidNotify.isEmpty()) {
pushNotify.put("androidNotify", androidNotify);
}
//ios通知消息, type=1, ios
Map<String, Object> iosNotify = getIosNotify();
if (!iosNotify.isEmpty()) {
pushNotify.put("iosNotify", iosNotify);
}
if (informForm.getSpeed() != null) {
pushNotify.put("speed", informForm.getSpeed());
}
//推送时,不走厂商通道。1: 开启,不走厂商通道;其它,关闭 ,默认关闭。
pushNotify.put("skipFactory", 0);
//推送策略: * 1:先走tcp,再走厂商 * 2:先走厂商,再走tcp * 3:只走厂商 * 4:只走tcp (厂商透传policy只支持策略3或4)
pushNotify.put("policy", 2);
//拓展参数
pushNotify.put("extrasMapList", getExtrasMapList(informForm));
return pushNotify;
}
/**
* 获取推送目标
*
* @param type 推送类型
* @param target 推送目标
* @return 推送目标配置
*/
private Map<String, Object> getPushTarget(Integer type, Object target) {
//推送目标
Map<String, Object> pushTarget = new HashMap<>(2);
//目标类型:1广播;2别名;3标签;4regid;5地理位置;6用户分群;9复杂地理位置推送;14fileid推送
pushTarget.put("target", type);
//集合元素限制1000个以内
if (target instanceof String[]) {
String[] str = (String[]) target;
if (str.length > 1000) {
return null;
}
}
//推送目标
switch (type) {
case 1:
//target:1 => 1广播
break;
case 2:
//arget:2 => 设置推送别名集合["alias1","alias2"]集合元素限制1000个以内
pushTarget.put("alias", target);
break;
case 3:
//target:3 => 设置推送标签集合["tag1","tag2"]集合元素限制1000个以内
pushTarget.put("tags", target);
//target:3 => 标签组合方式:1并集;2交集;3补集(3暂不考虑)
pushTarget.put("tagsType", 1);
break;
case 4:
//target:4 => 设置推送Registration Id集合["id1","id2"]集合元素限制1000个以内
pushTarget.put("rids", target);
break;
case 5:
//target:5 => 推送地理位置 城市,地理位置推送时(示例:上海市注意要带“市”),city, province, country 必须有一个不为空
/*if (str.length > 0) {
pushTarget.put("province", str[0]);
}
if (str.length > 1) {
pushTarget.put("city", str[1]);
}
if (str.length > 2) {
pushTarget.put("country", str[2]);
}*/
if (target instanceof Map) {
Map<String, String> address = Convert.convert(new TypeReference<Map<String, String>>() {
@Override
public Type getType() {
return super.getType();
}
}, target);
pushTarget.putAll(address);
}
break;
case 6:
pushTarget.put("block", target);
break;
case 9:
pushTarget.put("pushAreas", target);
break;
case 14:
pushTarget.put("fileId", target);
break;
default:
break;
}
return pushTarget;
}
/**
* 推送展示细节配置
*
* @param informForm 推送内容细节
* @return 推送展示配置
*/
private Map<String, Object> getPushNotify(InformForm informForm) {
Map<String, Object> pushNotify = getBatchPushNotify(informForm);
//推送内容, 如果内容长度超过厂商的限制, 则内容会被截断. vivo不允许纯表情
pushNotify.put("content", informForm.getBody());
//如果不设置,则默认的通知标题为应用的名称。如果标题长度超过厂商的限制, 则标题会被截断. vivo不允许纯表情
pushNotify.put("title", informForm.getTitle());
return pushNotify;
}
/**
* link 相关打开配置 目前默认打开首页
*
* @return 消息跳转地址配置
*/
private Map<String, Object> getPushForward() {
Map<String, Object> pushForward = new HashMap<>(1);
pushForward.put("nextType", 0);
return pushForward;
}
/**
* 链接跳转地址配置
*
* @param informForm 推送信息
* @return 跳转地址配置
*/
private List<Map<String, Object>> getExtrasMapList(InformForm informForm) {
// String pack = "com.weface.kksocialsecurity";
// String component = pack + "/com.weface.kksocialsecurity.activity.WellcomeActivity";
Integer pushType = informForm.getPushType();
int push;
if (pushType == null || pushType == 0) {
push = 0;
} else {
push = 2;
}
List<Map<String, Object>> list = new ArrayList<>(2);
//设置包名
// list.add(getFiled("package", pack));
//设置路径
// list.add(getFiled("component", component));
//推送内容
// list.add(getFiled("content", informForm.getBody()));
//推送类型 0 h5 2 原生
list.add(getFiled("push_type", push));
//推送地址
list.add(getFiled("url", informForm.getUrl()));
//推送标题
// list.add(getFiled("title", informForm.getTitle()));
return list;
}
/**
* 获取字段
*
* @param key key
* @param value value
* @return 字段
*/
private Map<String, Object> getFiled(String key, Object value) {
Map<String, Object> map = new HashMap<>(2);
map.put("key", key);
map.put("value", value);
return map;
}
/**
* 安卓通知细节配置
*
* @return 安卓细节
*/
private Map<String, Object> getAndroidNotify() {
Map<String, Object> androidNotify = new HashMap<>(1);
androidNotify.put("warn", "123");
//androidNotify.put("content", new String[]{"测试这是一条数据"});
//androidNotify.put("style", "3");
return androidNotify;
}
/**
* ios通知细节配置
*
* @return ios细节
*/
private Map<String, Object> getIosNotify() {
Map<String, Object> iosNotify = new HashMap<>(0);
return iosNotify;
}
/**
* 推送回调配置
*
* @param url 回调地址
* @param params 回调参数
* @return 回调配置
*/
private Map<String, Object> getPushCallback(String url, Object params) {
Map<String, Object> pushCallback = new HashMap<>(2);
pushCallback.put("url", url);
pushCallback.put("params", params);
return pushCallback;
}
/**
* 自定义参数配置
*
* @param title 自定义标题
* @param content 自定义内容
* @return 自定义参数配置
*/
private Map<String, Object> getCustomNotify(String title, String content) {
Map<String, Object> customNotify = new HashMap<>(2);
customNotify.put("customTitle", title);
customNotify.put("customType", content);
return customNotify;
}
/**
* mob 通用post请求
*
* @param map 请求参数
* @param url 请求地址
* @return 执行结果
*/
private String generalPost(Map<String, Object> map, String url) {
String request;
if (map == null) {
request = "";
} else {
request = JSONObject.toJSONString(map);
}
log.info("推送内容:{}", request);
HttpRequest post = HttpUtil.createPost(url);
post.header("Content-Type", "application/json");
post.header("key", APP_KEY);
post.header("sign", getSign(request));
String body = post.body(request).execute().body();
log.info("推送结果:{}", body);
if (JSONUtil.isJson(body)) {
JSONObject json = JSONObject.parseObject(body);
if (json.containsKey("status") && json.getInteger("status").equals(200))
return body;
}
return null;
}
/**
* mob 通用get请求
*
* @param url 请求路径
* @return 执行结果
*/
private String generalGet(String url) {
HttpRequest get = HttpUtil.createGet(url);
get.header("key", APP_KEY);
get.header("sign", getSign(""));
String body = get.execute().body();
log.info("推送结果:{}", body);
if (JSONUtil.isJson(body)) {
JSONObject json = JSONObject.parseObject(body);
if (json.containsKey("status") && json.getInteger("status").equals(200))
return body;
}
return null;
}
/**
* 获取签名 请求体+AppSecret来进行MD5加密
*
* @param body 请求体
* @return sign
*/
private String getSign(String body) {
return SecureUtil.md5(body + APP_SECRET);
}
}
package com.weface.controller;
import com.weface.code.CommonResult;
import com.weface.common.utils.Constant;
import com.weface.common.validator.ValidatorParam;
import com.weface.common.validator.ValidatorUtils;
import com.weface.component.GeTuiService;
import com.weface.component.MobPushService;
import com.weface.dto.InformForm;
import com.weface.dto.MsgDTO;
import com.weface.dto.PushDTO;
......@@ -25,7 +27,8 @@ public class PushController {
private PushService pushService;
@Autowired
private GeTuiService geTuiService;
@Autowired
private MobPushService mobPushService;
@PostMapping("list")
public CommonResult pushList(@RequestBody PushDTO param) throws Exception {
......@@ -40,6 +43,7 @@ public class PushController {
@PostMapping("/all")
public CommonResult pushAll(InformForm informForm) {
ValidatorUtils.validateEntity(informForm, InformForm.PushValid.class);
mobPushService.mobPush(informForm, Constant.PushTarget.RADIO);
return geTuiService.listPush(informForm);
}
......@@ -50,6 +54,10 @@ public class PushController {
if (commonResult != null) {
return commonResult;
}
CommonResult result = mobPushService.mobPush(informForm, Constant.PushTarget.ALIAS);
if (result != null) {
return CommonResult.success();
}
return geTuiService.pushSingleAlias(informForm);
}
......@@ -60,6 +68,10 @@ public class PushController {
if (commonResult != null) {
return commonResult;
}
CommonResult result = mobPushService.mobPush(informForm, Constant.PushTarget.RIDS);
if (result != null) {
return CommonResult.success();
}
return geTuiService.pushSingleCid(informForm);
}
}
......@@ -24,6 +24,10 @@ public class PushLogEntity implements Serializable {
* 应用名称
*/
private String appName;
/**
* 推送类型 1:个推 2:袤博
*/
private Integer pushType;
/**
* 任务id
*/
......
......@@ -14,7 +14,7 @@ import java.util.List;
public interface PushLogService extends IService<PushLogEntity> {
/**
* 保存日志
* 保存日志 个推
*
* @param data 返回数据
* @param informForm 推送数据
......@@ -22,6 +22,14 @@ public interface PushLogService extends IService<PushLogEntity> {
*/
public void saveLog(JSONObject data, InformForm informForm, String appId);
/**
* 保存推送日志 袤博
*
* @param data 返回数据
* @param informForm 推送数据
*/
public void saveMobLog(JSONObject data, InformForm informForm);
/**
* 查询日志
*
......
......@@ -33,31 +33,9 @@ public class PushLogServiceImpl extends ServiceImpl<PushLogDao, PushLogEntity> i
try {
List<PushLogEntity> logList = new ArrayList<>();
for (String key : data.keySet()) {
PushLogEntity pushLogEntity = new PushLogEntity();
Long id = SnowIdUtil.nextId();
pushLogEntity.setId(id);
pushLogEntity.setAppName(informForm.getEquipmentType());
pushLogEntity.setTaskId(key);
String str = data.toString();
if (str.length() > 200) {
pushLogEntity.setResult(str.substring(0, 200));
} else {
pushLogEntity.setResult(str);
}
String phone = informForm.getPhone();
if (StringUtils.isNotEmpty(phone)) {
DES des = DES.getInstanceDes();
pushLogEntity.setPhone(des.encrypt(phone));
}
Integer messageTemplate = informForm.getMessageTemplate();
if (messageTemplate != null) {
pushLogEntity.setMessageTemplate(messageTemplate);
}
pushLogEntity.setPushTarget(informForm.getCid() == null ? "all" : informForm.getCid());
pushLogEntity.setPushContent(informForm.getBody());
pushLogEntity.setArriveStatus(1001);
pushLogEntity.setUpdateTime(new Date());
logList.add(pushLogEntity);
PushLogEntity pushLog = getPushLog(id, informForm, key, data.toString(), 1);
logList.add(pushLog);
RedisUtil.HashOps.hPutEx(Constant.PUSH_TASK_INFO, key, String.valueOf(id), 2L, TimeUnit.HOURS);
}
if (!CollectionUtils.isEmpty(logList)) {
......@@ -69,6 +47,56 @@ public class PushLogServiceImpl extends ServiceImpl<PushLogDao, PushLogEntity> i
}
}
@Override
public void saveMobLog(JSONObject data, InformForm informForm) {
try {
Long id = SnowIdUtil.nextId();
String batchId = data.getJSONObject("res").getString("batchId");
PushLogEntity pushLog = getPushLog(id, informForm, batchId, data.toString(), 2);
this.save(pushLog);
} catch (Exception e) {
log.error("保存mob推送日志错误:{}", e.getMessage());
e.printStackTrace();
}
}
/**
* 推送日志存储
*
* @param id id
* @param informForm 推送内容
* @param taskId 任务编号
* @param result 推送结果
* @param pushType 推送类型 1:个推 2:袤博
* @return 执行结果
* @throws Exception 异常
*/
private PushLogEntity getPushLog(Long id, InformForm informForm, String taskId, String result, Integer pushType) throws Exception {
PushLogEntity pushLogEntity = new PushLogEntity();
pushLogEntity.setId(id);
pushLogEntity.setAppName(informForm.getEquipmentType());
pushLogEntity.setPushType(pushType);
pushLogEntity.setTaskId(taskId);
if (result.length() > 200) {
result = result.substring(0, 200);
}
pushLogEntity.setResult(result);
String phone = informForm.getPhone();
if (StringUtils.isNotEmpty(phone)) {
DES des = DES.getInstanceDes();
pushLogEntity.setPhone(des.encrypt(phone));
}
Integer messageTemplate = informForm.getMessageTemplate();
if (messageTemplate != null) {
pushLogEntity.setMessageTemplate(messageTemplate);
}
pushLogEntity.setPushTarget(informForm.getCid() == null ? "all" : informForm.getCid());
pushLogEntity.setPushContent(informForm.getBody());
pushLogEntity.setArriveStatus(1001);
pushLogEntity.setUpdateTime(new Date());
return pushLogEntity;
}
/**
* 查询日志
*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment