Commit 62b49ac1 authored by renandong's avatar renandong 🇨🇳

1,消息单推

2,推送回调
parent 6dc5c169
package com.weface.code;
import lombok.Data;
import lombok.experimental.Accessors;
/**
* @author : Administrator
* @date : 2022/4/19 14:35
*/
@Data
@Accessors(chain = true)
public class PushResultCode {
private int code;
private String result;
public static PushResultCode ok() {
return new PushResultCode().setCode(0).setResult("ok");
}
public static PushResultCode error() {
return new PushResultCode().setCode(1).setResult("error");
}
}
package com.weface.common.utils;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
/**
* @author : Administrator
* @date : 2022/2/16 14:58
*/
public class SnowIdUtil {
private static final Snowflake SNOWFLAKE;
static {
SNOWFLAKE = IdUtil.getSnowflake(1, 1);
}
/**
* 通过雪花算法获取id,统一封装调用
*
* @return id
*/
public static Long nextId() {
return SNOWFLAKE.nextId();
}
}
package com.weface.component;
import cn.hutool.core.util.IdUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.weface.common.utils.SnowIdUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
......@@ -49,7 +49,7 @@ public class GeTuiPush {
public void toSinglePush(String alia) {
Map<String, Object> param = new HashMap<>();
param.put("request_id", String.valueOf(IdUtil.createSnowflake(1, 1).nextId()));
param.put("request_id", String.valueOf(SnowIdUtil.nextId()));
Map<String, Object> audience = new HashMap<>();
List<String> alias = new ArrayList<>();
alias.add(alia);
......@@ -75,7 +75,7 @@ public class GeTuiPush {
public void toSingleBatchPush(String alia) {
Map<String, Object> param = new HashMap<>();
param.put("request_id", String.valueOf(IdUtil.createSnowflake(1,1).nextId()));
param.put("request_id", String.valueOf(SnowIdUtil.nextId()));
Map<String, Object> audience = new HashMap<>();
List<String> alias = new ArrayList<>();
alias.add(alia);
......
package com.weface.component;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HtmlUtil;
import cn.hutool.http.HttpException;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.weface.code.CommonResult;
......@@ -14,12 +11,15 @@ import com.weface.common.exception.RRException;
import com.weface.common.utils.Base64Util;
import com.weface.common.utils.HttpUtil;
import com.weface.common.utils.RedisUtil;
import com.weface.common.utils.SnowIdUtil;
import com.weface.config.GeTuiApp;
import com.weface.config.GeTuiConfig;
import com.weface.dto.InformForm;
import com.weface.service.PushLogService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
......@@ -38,6 +38,9 @@ import java.util.concurrent.TimeUnit;
@Slf4j
public class GeTuiService {
@Autowired
private PushLogService pushLogService;
@Resource
private GeTuiApp geTuiApp;
public static final String GE_TUI_BASE_URL = "https://restapi.getui.com/v2/";
......@@ -51,7 +54,7 @@ public class GeTuiService {
private final static long TIMEOUT = (12 * 60 * 60);
/**
* 获取token并缓存一份
* 统一获取个推token
*
* @param key 缓存key
* @return 从缓存中拿token
......@@ -192,25 +195,6 @@ public class GeTuiService {
return null;
}
/**
* 获取appId
*
* @param appName app名称
* @return appId
*/
public String getAppId(String appName) {
return getConfig(appName).getAppId();
}
/**
* 获取app配置
*
* @param appName app名称
* @return app配置
*/
public GeTuiConfig getConfig(String appName) {
return geTuiApp.getApps().get(appName);
}
/**
* 获取看看生活个像
......@@ -278,36 +262,185 @@ public class GeTuiService {
}
/**
* 【toSingle】执行别名单推
* 转换推送token
*
* @param messageTemplate 应用id
* @param appId 消息模板
* @param token 接口访问凭据
* @return 消息推送结果
* @param key 应用名称
* @return 应用token
*/
public String singlePushAlias(Integer device, MessageTemplate messageTemplate, String appId, String token) throws UnsupportedEncodingException {
Map<String, Object> map = convertBeanToMap(device, false, 1, messageTemplate);
//请求url
String url = GE_TUI_BASE_URL + appId + "/push/single/alias";
log.info("执行别名单推");
return generalPost(url, token, map);
private String getPushToken(String key) {
String token = null;
switch (key) {
//看看生活
case "kksh":
token = getAuthToken("kk_sh_token_ge_tui");
break;
//看看社保
case "kksb":
token = getAuthToken("kk_sb_token_ge_tui");
break;
case "kkwj":
token = getAuthToken("kk_wj_token_ge_tui");
break;
case "kkmz":
token = getAuthToken("kk_mz_token_ge_tui");
break;
default:
break;
}
return token;
}
/**
* 群推
*
* @param informForm 推送消息
* @return 执行状态
*/
public CommonResult listPush(InformForm informForm) {
try {
String type = informForm.getEquipmentType();
String appId = getAppId(type);
String pushToken = getPushToken(type);
if (StringUtils.isBlank(pushToken)) {
return CommonResult.failed();
}
Integer speed = informForm.getSpeed();
informForm.setSpeed(speed == null ? 0 : speed);
MessageTemplate template = getTemplate(informForm);
template.setGroup_name("toApp");
Map<String, Object> map = convertBeanToMap(informForm.getDevice(), false, 1, template);
//请求url
String url = GE_TUI_BASE_URL + appId + "/push/all";
log.info("执行群推");
String result = generalPost(url, pushToken, map);
if (result == null) {
return CommonResult.failed();
}
JSONObject jsonObject = JSONObject.parseObject(result);
if ((jsonObject.containsKey("code") && jsonObject.getInteger("code") == 0)) {
JSONObject data = jsonObject.getJSONObject("data");
if (data != null) {
//保存单推结果
pushLogService.saveLog(data, null, null);
}
return CommonResult.success(data);
}
} catch (UnsupportedEncodingException e) {
log.error("执行群推失败:{}", e.getMessage());
e.printStackTrace();
}
return CommonResult.failed();
}
/**
* 别名单推
*
* @param informForm 推送消息
* @return 执行结果
*/
public CommonResult pushSingleAlias(InformForm informForm) {
try {
log.error("单推参数:{}",informForm);
String type = informForm.getEquipmentType();
String appId = getAppId(type);
String pushToken = getPushToken(type);
if (StringUtils.isBlank(pushToken)) {
return CommonResult.failed();
}
MessageTemplate template = getTemplate(informForm);
template.setAudience(new MessageTemplate.Audience(informForm.getCid()));
template.setGroup_name("toSingleAlias");
Map<String, Object> map = convertBeanToMap(informForm.getDevice(), false, 1, template);
//请求url
String url = GE_TUI_BASE_URL + appId + "/push/single/alias";
log.info("执行别名单推");
String result = generalPost(url, pushToken, map);
log.error("推送结果:{}",result);
if (StringUtils.isEmpty(result)) {
return CommonResult.failed();
}
JSONObject jsonObject = JSONObject.parseObject(result);
if ((jsonObject.containsKey("code") && jsonObject.getInteger("code") == 0)) {
JSONObject data = jsonObject.getJSONObject("data");
if (data != null) {
//保存单推结果
pushLogService.saveLog(data, informForm.getPhone(), informForm.getMessageTemplate());
}
return CommonResult.success();
}
} catch (UnsupportedEncodingException e) {
log.error("执行别名单推失败:{}", e.getMessage());
e.printStackTrace();
}
return CommonResult.failed();
}
/**
* 【toSingle】执行cid单推
*
* @param device 设备类型
* @param messageTemplate 消息
* @param appId appId
* @param token 鉴权
* @param informForm 推送消息
* @return 执行结果
*/
public String singlePushCid(Integer device, MessageTemplate messageTemplate, String appId, String token) throws UnsupportedEncodingException {
Map<String, Object> map = convertBeanToMap(device, false, 0, messageTemplate);
//请求url
String url = GE_TUI_BASE_URL + appId + "/push/single/cid";
log.info("执行cid单推");
return generalPost(url, token, map);
public CommonResult pushSingleCid(InformForm informForm) {
try {
String type = informForm.getEquipmentType();
String appId = getAppId(type);
String pushToken = getPushToken(type);
if (StringUtils.isBlank(pushToken)) {
return CommonResult.failed();
}
MessageTemplate template = getTemplate(informForm);
template.setAudience(new MessageTemplate.Audience(informForm.getCid()));
template.setGroup_name("toSingleCid");
Map<String, Object> map = convertBeanToMap(informForm.getDevice(), false, 0, template);
//请求url
String url = GE_TUI_BASE_URL + appId + "/push/single/cid";
log.info("执行cid单推");
String result = generalPost(url, pushToken, map);
if (StringUtils.isEmpty(result)) {
return CommonResult.failed();
}
JSONObject jsonObject = JSONObject.parseObject(result);
if ((jsonObject.containsKey("code") && jsonObject.getInteger("code") == 0)) {
JSONObject data = jsonObject.getJSONObject("data");
if (data != null) {
//保存单推结果
pushLogService.saveLog(data, informForm.getPhone(), informForm.getMessageTemplate());
}
return CommonResult.success();
}
} catch (UnsupportedEncodingException e) {
log.error("执行别名单推失败:{}", e.getMessage());
e.printStackTrace();
}
return CommonResult.failed();
}
/**
* 将内容转为推送模板
*
* @param informForm 推送数据
* @return 推送模板
*/
private MessageTemplate getTemplate(InformForm informForm) {
MessageTemplate messageTemplate = new MessageTemplate();
messageTemplate.setSettings(new MessageTemplate.Settings(3600000, informForm.getSpeed()));
MessageTemplate.PushMessage pushMessage = new MessageTemplate.PushMessage();
MessageTemplate.PushMessage.Transmission transmission = new MessageTemplate.PushMessage.Transmission();
transmission.setTitle(informForm.getTitle());
transmission.setContent(informForm.getBody());
transmission.setUrl(informForm.getUrl());
int pushType = informForm.getPushType() == null ? 0 : informForm.getPushType();
transmission.setPushType(pushType);
if (pushType == 1) {
transmission.setClassTitle(informForm.getClassTitle());
transmission.setClassName(informForm.getClassName());
transmission.setNeedLogin(informForm.getNeedLogin());
}
pushMessage.setTransmission(transmission);
messageTemplate.setPush_message(pushMessage);
return messageTemplate;
}
/**
......@@ -378,87 +511,6 @@ public class GeTuiService {
return generalPost(url, token, map);
}
/**
* 群推
*
* @param informForm 消息
* @return 执行状态
*/
public CommonResult listPush(InformForm informForm) {
MessageTemplate messageTemplate = new MessageTemplate();
try {
GeTuiConfig kksh = getConfig("kksh");
GeTuiConfig kksb = getConfig("kksb");
GeTuiConfig kkwj = getConfig("kkwj");
GeTuiConfig kkmz = getConfig("kkmz");
Integer device = informForm.getDevice();
if (device == null) {
device = 4;
}
Integer speed = informForm.getSpeed();
messageTemplate.setSettings(new MessageTemplate.Settings(3600000, speed == null ? 0 : speed));
MessageTemplate.PushMessage pushMessage = new MessageTemplate.PushMessage();
MessageTemplate.PushMessage.Transmission transmission = new MessageTemplate.PushMessage.Transmission();
transmission.setTitle(informForm.getTitle());
transmission.setContent(informForm.getBody());
transmission.setUrl(HtmlUtil.unescape(informForm.getUrl()));
pushMessage.setTransmission(transmission);
messageTemplate.setPush_message(pushMessage);
String result = null;
switch (informForm.getEquipmentType()) {
//看看生活
case "kksh":
String kk_sh_token_ge_tui = getAuthToken("kk_sh_token_ge_tui");
result = listPushToApp(device, messageTemplate, kksh.getAppId(), kk_sh_token_ge_tui);
break;
//看看社保
case "kksb":
String kk_sb_token_ge_tui = getAuthToken("kk_sb_token_ge_tui");
result = listPushToApp(device, messageTemplate, kksb.getAppId(), kk_sb_token_ge_tui);
break;
case "kkwj":
String kk_wj_token_ge_tui = getAuthToken("kk_wj_token_ge_tui");
result = listPushToApp(device, messageTemplate, kkwj.getAppId(), kk_wj_token_ge_tui);
break;
case "kkmz":
String kk_mz_token_ge_tui = getAuthToken("kk_mz_token_ge_tui");
result = listPushToApp(device, messageTemplate, kkmz.getAppId(), kk_mz_token_ge_tui);
break;
default:
break;
}
if (result == null) {
return CommonResult.failed();
}
JSONObject jsonObject = JSONObject.parseObject(result);
if ((jsonObject.containsKey("code") && jsonObject.getInteger("code") == 0)) {
JSONObject data = jsonObject.getJSONObject("data");
return CommonResult.success(data);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return CommonResult.failed();
}
/**
* 【toApp】执行群推
*
* @param messageTemplate 请求参数
* @param token token
* @return 执行结果
*/
public String listPushToApp(Integer device, MessageTemplate messageTemplate, String appId, String token) throws UnsupportedEncodingException {
Map<String, Object> map = convertBeanToMap(device, false, 1, messageTemplate);
//请求url
String url = GE_TUI_BASE_URL + appId + "/push/all";
log.info("执行群推");
return generalPost(url, token, map);
}
/**
* 实体类封装为map
*
......@@ -471,7 +523,7 @@ public class GeTuiService {
}
//设置请求唯一标识号,10-32位之间;如果request_id重复,会导致消息丢失
Map<String, Object> map = new HashMap<>();
map.put("request_id", String.valueOf(IdUtil.getSnowflake(1, 1).nextId()));
map.put("request_id", String.valueOf(SnowIdUtil.nextId()));
//设置任务组名
if (StringUtils.isNotBlank(messageTemplate.getGroup_name())) {
......@@ -509,42 +561,61 @@ public class GeTuiService {
}
}
//获取推送消息转为map 不转为下划线,忽略空值
MessageTemplate.PushMessage push_message = messageTemplate.getPush_message();
Map<String, Object> push_messages = BeanUtil.beanToMap(push_message, false, true);
MessageTemplate.PushMessage pushMessage = messageTemplate.getPush_message();
Map<String, Object> pushMessages = BeanUtil.beanToMap(pushMessage, false, true);
//透传消息内容,与notification、revoke 三选一,都填写时报错,长度 ≤ 3072
MessageTemplate.PushMessage.Transmission transmission = push_message.getTransmission();
String str = "{\"content\": \"%s\", \"push_type\": \"0\", \"title\": \"%s\", \"url\": \"%s\"}";
String intent = String.format(str, transmission.getContent(), transmission.getTitle(), transmission.getUrl());
push_messages.put("transmission", intent);
MessageTemplate.PushMessage.Transmission transmission = pushMessage.getTransmission();
String intent = getIntent(transmission);
pushMessages.put("transmission", intent);
//撤回消息时使用(仅撤回厂商通道消息,支持的厂商有小米、VIVO),与notification、transmission三选一,都填写时报错(消息撤回请勿填写策略参数)
MessageTemplate.PushMessage.Revoke revoke = push_message.getRevoke();
MessageTemplate.PushMessage.Revoke revoke = pushMessage.getRevoke();
if (revoke != null) {
//设置撤回消息
Map<String, Object> revokes = BeanUtil.beanToMap(revoke, false, true);
push_messages.put("revoke", revokes);
pushMessages.put("revoke", revokes);
}
//设置推送消息
map.put("push_message", push_messages);
map.put("push_message", pushMessages);
//设置厂商通道
map.put("push_channel", setChannel(push_message));
map.put("push_channel", setChannel(pushMessage));
return map;
}
/**
* 封装intent
*
* @param transmission 推送数据
* @return intent
*/
private String getIntent(MessageTemplate.PushMessage.Transmission transmission) {
Integer pushType = transmission.getPushType();
String intent;
if (pushType == 0) {
String str = "{\"content\": \"%s\", \"push_type\": \"0\", \"title\": \"%s\", \"url\": \"%s\"}";
intent = String.format(str, transmission.getContent(), transmission.getTitle(), transmission.getUrl());
} else {
String str = "{\"content\":\"%s\",\"push_type\":\"1\",\"title\":\"%s\",\"className\":\"%s\",\"classTitle\":\"%s\",\"needLogin\":\"%s\"}";
intent = String.format(str, transmission.getContent(), transmission.getTitle(), transmission.getClassName(), transmission.getClassTitle(), transmission.getNeedLogin());
}
return intent;
}
/**
* 设置推送渠道
*
* @param push_message 消息
* @return json
*/
public cn.hutool.json.JSONObject setChannel(MessageTemplate.PushMessage push_message) throws UnsupportedEncodingException {
private Map<String, Object> setChannel(MessageTemplate.PushMessage push_message) throws UnsupportedEncodingException {
Map<String, Object> channel = new HashMap<>();
//获取ios配置
channel.put("ios", getIosChannel(push_message));
//获取android配置
channel.put("android", getAndroidChannel(push_message));
return JSONUtil.parseFromMap(channel);
return channel;
}
/**
......@@ -553,7 +624,7 @@ public class GeTuiService {
* @param push_message 消息
* @return ios配置
*/
private Map<String, Object> getIosChannel(MessageTemplate.PushMessage push_message) throws UnsupportedEncodingException {
private Map<String, Object> getIosChannel(MessageTemplate.PushMessage push_message) {
Map<String, Object> ios = new HashMap<>();
ios.put("type", "notify");
// ios.put("auto_badge", "+1");
......@@ -567,8 +638,7 @@ public class GeTuiService {
alert.put("body", transmission.getContent());
aps.put("alert", alert);
ios.put("aps", aps);
String str = "{\"content\": \"%s\", \"push_type\": \"0\", \"title\": \"%s\", \"url\": \"%s\"}";
String intent = String.format(str, transmission.getContent(), transmission.getTitle(), transmission.getUrl());
String intent = getIntent(transmission);
ios.put("payload", intent);
return ios;
}
......@@ -589,14 +659,44 @@ public class GeTuiService {
notification.put("click_type", "intent");
String pack = "com.weface.kksocialsecurity";
String component = pack + "/com.weface.kksocialsecurity.activity.WellcomeActivity";
String str = "intent://com.weface.kksocialsecurity/localopen?push_type=0&url=%s#Intent;scheme=kankan;package=%s;component=%s;S.kkparam={\"content\": \"%s\", \"push_type\": \"0\", \"title\": \"%s\", \"url\": \"%s\"};end";
String intent = String.format(str, URLEncoder.encode(transmission.getUrl(), "UTF-8"), pack, component, transmission.getContent(), transmission.getTitle(), URLEncoder.encode(transmission.getUrl(), "UTF-8"));
String str = "intent://com.weface.kksocialsecurity/localopen?push_type=%d&url=%s#Intent;scheme=kankan;package=%s;component=%s;S.kkparam={\"content\": \"%s\", \"push_type\": \"%d\", \"title\": \"%s\", \"url\": \"%s\"};end";
Integer pushType = transmission.getPushType();
String url;
int push;
if (pushType == 0) {
url = URLEncoder.encode(transmission.getUrl(), "UTF-8");
push = 0;
} else {
url = transmission.getClassName();
push = 2;
}
String intent = String.format(str, push, url, pack, component, transmission.getContent(), push, transmission.getTitle(), url);
notification.put("intent", intent);
ups.put("notification", notification);
android.put("ups", ups);
return android;
}
/**
* 获取appId
*
* @param appName app名称
* @return appId
*/
public String getAppId(String appName) {
return getConfig(appName).getAppId();
}
/**
* 获取app配置
*
* @param appName app名称
* @return app配置
*/
public GeTuiConfig getConfig(String appName) {
return geTuiApp.getApps().get(appName);
}
/**
* post请求通用
*
......
......@@ -3,15 +3,13 @@ package com.weface.component;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.convert.ConvertException;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weface.common.utils.SnowIdUtil;
import com.weface.entity.MenuTagsEntity;
import com.weface.entity.UserTagEntity;
import com.weface.entity.UserMenusEntity;
import com.weface.entity.UserTagEntity;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -38,7 +36,6 @@ public class MenuService {
*/
public List<UserMenusEntity> getTagUser(Map<String, Object> map, List<UserTagEntity> userInfo) {
try {
Snowflake snowflake = IdUtil.getSnowflake(1, 1);
//获取所有用户标签
List<Map<String, Object>> userTag = Convert.convert(new TypeReference<List<Map<String, Object>>>() {
}, map.get("userTag"));
......@@ -62,7 +59,7 @@ public class MenuService {
for (Long aLong : list) {
//填充数据
UserMenusEntity userMenusEntity = new UserMenusEntity();
userMenusEntity.setId(snowflake.nextId());
userMenusEntity.setId(SnowIdUtil.nextId());
userMenusEntity.setUserId(uid);
userMenusEntity.setTagsId(aLong);
userMenusEntity.setIsValid(1);
......
......@@ -107,7 +107,7 @@ public class MessageTemplate implements Serializable {
private String content;
private String url;
//0:h5 1:app原生
private String push_type;
private Integer pushType;
//跳转功能名称
private String className;
//功能标题
......
package com.weface.controller;
import com.weface.code.PushResultCode;
import com.weface.service.PushCallBackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
/**
* @author : Administrator
* @date : 2022/4/19 13:51
*/
@RestController
@RequestMapping("/push")
public class PushCallBackController {
@Autowired
private PushCallBackService pushCallBackService;
@PostMapping("/call/back/info")
public PushResultCode callBackForPush(HttpServletRequest request) throws IOException {
return pushCallBackService.saveCallBackInfo(request);
}
}
\ No newline at end of file
......@@ -42,4 +42,14 @@ public class PushController {
public CommonResult pushAll(@Validated({Default.class}) InformForm informForm) {
return geTuiService.listPush(informForm);
}
@PostMapping("/single/alias")
public CommonResult pushSingleAlias(@Validated({Default.class}) InformForm informForm) {
return geTuiService.pushSingleAlias(informForm);
}
@PostMapping("/single/cid")
public CommonResult pushSingleCid(@Validated({Default.class}) InformForm informForm) {
return geTuiService.pushSingleCid(informForm);
}
}
package com.weface.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.weface.entity.PushCallBackEntity;
import org.apache.ibatis.annotations.Mapper;
/**
*
*
* @author admin
* @date 2022-04-20 09:54:09
*/
@Mapper
public interface PushCallBackDao extends BaseMapper<PushCallBackEntity> {
}
package com.weface.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.weface.entity.PushLogEntity;
import org.apache.ibatis.annotations.Mapper;
/**
*
*
* @author admin
* @date 2022-04-20 09:54:09
*/
@Mapper
public interface PushLogDao extends BaseMapper<PushLogEntity> {
}
......@@ -16,9 +16,10 @@ public class InformForm {
@NotBlank
private String body;
//跳转url
@NotBlank
private String url;
//推送类型 0:h5 1:原生
private String push_type;
private Integer pushType;
//跳转功能名称
private String className;
//功能标题
......@@ -32,5 +33,11 @@ public class InformForm {
private Integer device;
//定速推送,例如100,个推控制下发速度在100条/秒左右,0表示不限速
private Integer speed;
//推送目标
private String[] cid;
//推送手机号
private String phone;
//推送模板
private Integer messageTemplate;
}
package com.weface.entity;
import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
/**
*
*
* @author admin
* @date 2022-04-20 09:54:09
*/
@Data
@TableName("tb_push_call_back")
public class PushCallBackEntity implements Serializable {
private static final long serialVersionUID = 1L;
/**
*
*/
@TableId
private Long id;
/**
* 应用 ID
*/
private String appid;
/**
* 接收人CID
*/
private String cid;
/**
* 任务 ID(即 ContentID)
*/
private String taskid;
/**
* 消息 ID
*/
private String msgid;
/**
* 结果码(200 -- 成功,400 -- 推送苹果接口失败, 401 -- 用户不存在,402 -- 非活跃用户,500 -- 系统内部异常)目前只有 200 和 400,后续版本扩展。
*/
private String code;
/**
* 推送结果描述
*/
private String desc;
/**
* 签名
*/
private String sign;
/**
* 回执 ID
*/
private String actionid;
/**
* 回执上传时间
*/
private Long recvtime;
/**
* 根据配置返回
*/
private String alias;
/**
* 状态
*/
private Integer status;
/**
* 是否删除 1:未删除 0 :删除
*/
@TableLogic(value = "1", delval = "0")
@TableField(fill = FieldFill.INSERT)
private Integer isValid;
/**
* 创建时间
*/
@TableField(fill = FieldFill.INSERT)
private Date createTime;
/**
* 更新时间
*/
private Date updateTime;
}
package com.weface.entity;
import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
/**
*
*
* @author admin
* @date 2022-04-20 09:54:09
*/
@Data
@TableName("tb_push_log")
public class PushLogEntity implements Serializable {
private static final long serialVersionUID = 1L;
/**
*
*/
@TableId
private Long id;
/**
*
*/
private String taskId;
/**
*
*/
private String cid;
/**
*
*/
private String cidStatus;
/**
*
*/
private String phone;
/**
*
*/
private Integer messageTemplate;
/**
*
*/
private Integer isArrive;
/**
*
*/
@TableLogic(value = "1", delval = "0")
@TableField(fill = FieldFill.INSERT)
private Integer isValid;
/**
*
*/
@TableField(fill = FieldFill.INSERT)
private Date createTime;
/**
*
*/
private Date updateTime;
}
package com.weface.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.weface.code.PushResultCode;
import com.weface.entity.PushCallBackEntity;
import javax.servlet.http.HttpServletRequest;
/**
* @author admin
* @email admin@163.com
* @date 2022-04-20 09:54:09
*/
public interface PushCallBackService extends IService<PushCallBackEntity> {
public PushResultCode saveCallBackInfo(HttpServletRequest request);
}
package com.weface.service;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.IService;
import com.weface.entity.PushLogEntity;
/**
* @author admin
* @email admin@163.com
* @date 2022-04-20 09:54:09
*/
public interface PushLogService extends IService<PushLogEntity> {
public void saveLog(JSONObject data, String phone, Integer messageTemplate);
}
package com.weface.serviceimpl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weface.code.CommonResult;
import com.weface.common.utils.BeanUtils;
import com.weface.common.utils.SnowIdUtil;
import com.weface.dao.MenuTagsDao;
import com.weface.dto.MenuTagsForm;
import com.weface.entity.MenuTagsEntity;
......@@ -24,7 +23,7 @@ import java.util.stream.Collectors;
* @author Administrator
*/
@Slf4j
@Transactional
@Transactional(rollbackFor = {Exception.class})
@Service("menuTagsService")
public class MenuTagsServiceImpl extends ServiceImpl<MenuTagsDao, MenuTagsEntity> implements MenuTagsService {
......@@ -69,9 +68,8 @@ public class MenuTagsServiceImpl extends ServiceImpl<MenuTagsDao, MenuTagsEntity
try {
MenuTagsEntity menuTagsEntity = new MenuTagsEntity();
BeanUtil.copyProperties(menuTagsForm, menuTagsEntity, BeanUtils.getNullPropertyNames(menuTagsForm));
Snowflake snowflake = IdUtil.getSnowflake(1, 1);
menuTagsEntity.setId(snowflake.nextId());
menuTagsEntity.setId(SnowIdUtil.nextId());
String tagCode = "";
boolean flag = false;
while (!flag) {
......
package com.weface.serviceimpl;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weface.code.PushResultCode;
import com.weface.common.utils.SnowIdUtil;
import com.weface.config.GeTuiApp;
import com.weface.config.GeTuiConfig;
import com.weface.dao.PushCallBackDao;
import com.weface.entity.PushCallBackEntity;
import com.weface.service.PushCallBackService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import javax.annotation.Resource;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;
/**
* @author : Administrator
* @date : 2022/4/19 14:08
*/
@Service("pushCallBackService")
@Slf4j
@Transactional(rollbackFor = {Exception.class})
public class PushCallBackServiceImpl extends ServiceImpl<PushCallBackDao, PushCallBackEntity> implements PushCallBackService {
@Resource
private GeTuiApp geTuiApp;
/**
* 保存推送回调
*
* @param request 回调请求
* @return 执行结果
*/
@Override
public PushResultCode saveCallBackInfo(HttpServletRequest request) {
JSONObject jsonObject = new JSONObject();
try {
byte[] bytes = getRequestInputStream(request);
String callInfo = new String(bytes);
PushCallBackEntity callBackInfo = JSONObject.parseObject(callInfo, PushCallBackEntity.class);
String appId = callBackInfo.getAppid();
String str = appId + callBackInfo.getCid() + callBackInfo.getTaskid() + callBackInfo.getMsgid() + getMasterSecret(appId);
String sign = md5Encode(str);
if (!callBackInfo.getSign().equals(sign)) {
return PushResultCode.error();
}
String taskid = callBackInfo.getTaskid();
String cid = callBackInfo.getCid();
callBackInfo.setId(SnowIdUtil.nextId());
callBackInfo.setUpdateTime(new Date());
callBackInfo.setStatus(1);
this.save(callBackInfo);
return PushResultCode.ok();
} catch (IOException e) {
log.error("处理回调函数异常:{}", e.getMessage());
e.printStackTrace();
}
return PushResultCode.error();
}
private String getMasterSecret(String appId) {
String masterSecret = null;
Map<String, GeTuiConfig> apps = geTuiApp.getApps();
for (Map.Entry<String, GeTuiConfig> entry : apps.entrySet()) {
if (appId.equals(entry.getValue().getAppId())) {
masterSecret = entry.getValue().getMastersecret();
break;
}
}
Assert.notNull(masterSecret, "密钥不能为空");
return masterSecret;
}
public static String md5Encode(String str) {
MessageDigest md = DigestUtils.getMd5Digest();
return Arrays.toString(md.digest(str.getBytes(StandardCharsets.UTF_8)));
}
public static byte[] getRequestInputStream(HttpServletRequest request) throws IOException {
ServletInputStream input = request.getInputStream();
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int position = 0;
while (true) {
position = input.read(buffer);
if (position == -1) {
break;
}
output.write(buffer, 0, position);
}
return output.toByteArray();
}
}
package com.weface.serviceimpl;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weface.common.utils.SnowIdUtil;
import com.weface.dao.PushLogDao;
import com.weface.entity.PushLogEntity;
import com.weface.service.PushLogService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Transactional(rollbackFor = {Exception.class})
@Service("pushLogService")
public class PushLogServiceImpl extends ServiceImpl<PushLogDao, PushLogEntity> implements PushLogService {
@Override
public void saveLog(JSONObject data, String phone, Integer messageTemplate) {
List<PushLogEntity> logList = new ArrayList<>();
for (Map.Entry<String, Object> stringObjectEntry : data.entrySet()) {
String key = stringObjectEntry.getKey();
Object value = stringObjectEntry.getValue();
if (value != null) {
JSONObject jsonObject1 = JSONObject.parseObject(value.toString());
for (Map.Entry<String, Object> objectEntry : jsonObject1.entrySet()) {
String key1 = objectEntry.getKey();
Object value1 = objectEntry.getValue();
PushLogEntity pushLogEntity = new PushLogEntity();
pushLogEntity.setId(SnowIdUtil.nextId());
pushLogEntity.setTaskId(key);
pushLogEntity.setCid(key1);
pushLogEntity.setCidStatus(value1.toString());
pushLogEntity.setPhone(phone);
pushLogEntity.setMessageTemplate(messageTemplate);
pushLogEntity.setIsArrive(0);
pushLogEntity.setIsValid(1);
Date date = new Date();
pushLogEntity.setCreateTime(date);
pushLogEntity.setUpdateTime(date);
logList.add(pushLogEntity);
}
}
}
if (!CollectionUtils.isEmpty(logList)) {
this.saveBatch(logList);
}
}
}
\ No newline at end of file
......@@ -2,12 +2,11 @@ package com.weface.serviceimpl;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weface.code.CommonResult;
import com.weface.common.utils.SnowIdUtil;
import com.weface.dao.UserMenusDao;
import com.weface.dto.UserMenuFrom;
import com.weface.entity.UserMenusEntity;
......@@ -24,7 +23,7 @@ import java.util.stream.Collectors;
@Slf4j
@Service("userMenusService")
@Transactional
@Transactional(rollbackFor = {Exception.class})
public class UserMenusServiceImpl extends ServiceImpl<UserMenusDao, UserMenusEntity> implements UserMenusService {
/**
......@@ -81,10 +80,9 @@ public class UserMenusServiceImpl extends ServiceImpl<UserMenusDao, UserMenusEnt
return CommonResult.failed("用户标签已存在");
}
List<UserMenusEntity> userMenusEntities = new ArrayList<>(tagsId.length);
Snowflake snowflake = IdUtil.getSnowflake(1, 1);
for (String aLong : saveTags) {
UserMenusEntity userMenusEntity = new UserMenusEntity();
userMenusEntity.setId(snowflake.nextId());
userMenusEntity.setId(SnowIdUtil.nextId());
userMenusEntity.setUserId(userId);
userMenusEntity.setTagsId(Convert.toLong(aLong));
userMenusEntity.setCreateTime(new Date());
......
......@@ -14,6 +14,7 @@ import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;
......@@ -53,7 +54,7 @@ public class UserTagsTask {
/**
* 更新用户标签
*/
// @Scheduled(cron = "0 0 23 * * ?")
@Scheduled(cron = "0 0 23 * * ?")
public void updateUserTags() {
try {
//获取每次处理的id起始值
......
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.weface.dao.PushCallBackDao">
<!-- 可根据自己的需求,是否要使用 -->
<resultMap type="com.weface.entity.PushCallBackEntity" id="pushCallBackMap">
<result property="id" column="id"/>
<result property="appid" column="appid"/>
<result property="cid" column="cid"/>
<result property="taskid" column="taskid"/>
<result property="msgid" column="msgid"/>
<result property="code" column="code"/>
<result property="desc" column="desc"/>
<result property="sign" column="sign"/>
<result property="actionid" column="actionId"/>
<result property="recvtime" column="recvtime"/>
<result property="alias" column="alias"/>
<result property="status" column="status"/>
<result property="isValid" column="is_valid"/>
<result property="createTime" column="create_time"/>
<result property="updateTime" column="update_time"/>
</resultMap>
</mapper>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.weface.dao.PushLogDao">
<!-- 可根据自己的需求,是否要使用 -->
<resultMap type="com.weface.entity.PushLogEntity" id="pushLogMap">
<result property="id" column="id"/>
<result property="taskId" column="task_id"/>
<result property="cid" column="cid"/>
<result property="cidStatus" column="cid_status"/>
<result property="phone" column="phone"/>
<result property="messageTemplate" column="message_template"/>
<result property="isArrive" column="is_arrive"/>
<result property="isValid" column="is_valid"/>
<result property="createTime" column="create_time"/>
<result property="updateTime" column="update_time"/>
</resultMap>
</mapper>
\ No newline at end of file
package com.weface;
import cn.hutool.core.util.IdUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.weface.code.CommonResult;
import com.weface.common.utils.RedisUtil;
import com.weface.common.utils.SnowIdUtil;
import com.weface.component.GeTuiService;
import com.weface.component.MenuService;
import com.weface.component.MessageTemplate;
import com.weface.config.GeTuiApp;
import com.weface.dto.InformForm;
import com.weface.entity.MenuTagsEntity;
import com.weface.entity.UserMenusEntity;
......@@ -31,11 +30,9 @@ import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
......@@ -57,9 +54,6 @@ class PushMessageApplicationTests {
@Autowired
private GeTuiService geTuiService;
@Resource
private GeTuiApp geTuiApp;
@Test
void contextLoads() {
}
......@@ -112,45 +106,11 @@ class PushMessageApplicationTests {
}
}
@Test
void testConventTemplate() throws UnsupportedEncodingException {
InformForm informForm = new InformForm();
// String[] alias = {"kksh_10131492"};
String[] alias = {"kksh_59354"};
String token = geTuiService.getAuthToken("kk_sh_token_ge_tui");
// String token = geTuiService.getAuthToken("kk_sb_token_ge_tui");
informForm.setTitle("河南一名小学生确诊阳性!");
informForm.setBody("1月23日新增一例本土确诊病例!为11岁小学生");
informForm.setUrl("https://kk.weface.com.cn/news/page/?id=c0933d0e2fc85f7333b6202dff0e611c&type=hot&from=101");
MessageTemplate messageTemplate = new MessageTemplate();
messageTemplate.setAudience(new MessageTemplate.Audience(alias));
messageTemplate.setSettings(new MessageTemplate.Settings(3600000, 0));
MessageTemplate.PushMessage pushMessage = new MessageTemplate.PushMessage();
MessageTemplate.PushMessage.Transmission transmission = new MessageTemplate.PushMessage.Transmission();
transmission.setTitle(informForm.getTitle());
transmission.setContent(informForm.getBody());
transmission.setUrl(informForm.getUrl());
pushMessage.setTransmission(transmission);
messageTemplate.setPush_message(pushMessage);
String s = geTuiService.singlePushAlias(4, messageTemplate, geTuiService.getAppId("kksh"), token);
// String s = geTuiService.singlePushAlias(1, messageTemplate, geTuiService.getAppId("kksb"), token);
System.out.println(s);
}
@Test
void testRevoke() {
Map<String, Object> map = new HashMap<>();
map.put("request_id", String.valueOf(IdUtil.getSnowflake(1, 1).nextId()));
map.put("request_id", String.valueOf(SnowIdUtil.nextId()));
map.put("audience", "all");
Map<String, Object> push_message = new HashMap<>();
......@@ -166,7 +126,7 @@ class PushMessageApplicationTests {
}
@Test
void testPush() {
void testUserTags() {
//获取标签列表
List<MenuTagsEntity> tags = menuTagsService.list();
List<String> list = Collections.singletonList("d7e78cfa76e1c61699925ec9bb960d53");
......@@ -276,22 +236,6 @@ class PushMessageApplicationTests {
System.out.println(first_map);
}
@Test
void testRandom() {
String token = "54_qYwZpVbWrJDkflE1ha61B4SPwdhNVoCE6g_YG9zL0PL2ki6pZYsNKK__l2fMfmjRyt3C_EIiTq2Hnnla5MK8bGLckSUXUN14_A2UKMhF9xsVHQswPd6MnmVxV1YkSAyFVLEpF76kuCbBmps7GZOfAFAWPE";
// String token = "54_hksRqu53_0Jmw_lopjrJh4Ofzqzb_289qRkfZXxIVhBhk4vjSFH-20sbf6LUg8X1Go4tkVoISnFTSWiyKKUB1Pw8-KKNM8W6GsSpGsj85_fTtm5Uus7ncmkQCS_LxTAybYsUfsU6VaBm6hnHKJSeAAAUTC";
RedisUtil.StringOps.setEx("kksh_wechat:mini_program_token:10011", token, 5400, TimeUnit.SECONDS);
// String appId = "wx0c0e88fde84cc81c";
// String secret = "45aae2597122419a74297d5987965013";
// String code = "ce863cb4acfa518fc6893686d3e9480a0fd5b4b4ba99dd3d68d5ac4e50775943";
// String userPhone = getUserPhone("10011", code);
// System.out.println(userPhone);
}
private static Map<String, String> getAppId(String programType) {
String appId;
String secret;
......@@ -391,4 +335,36 @@ class PushMessageApplicationTests {
}
return null;
}
@Test
public void testSingleAlias() {
InformForm informForm = new InformForm();
informForm.setTitle("缴电费充值通知");
informForm.setBody("尊敬的用户您好,您的户");
informForm.setUrl("mineEarnGold");
informForm.setPushType(1);
informForm.setClassName("mineEarnGold");
informForm.setClassTitle("我的金币");
informForm.setNeedLogin("1");
informForm.setEquipmentType("kksh");
String[] strings = {"kksh_59354"};
informForm.setCid(strings);
CommonResult commonResult = geTuiService.pushSingleAlias(informForm);
System.out.println(commonResult);
}
@Test
public void tesPushApp() {
InformForm informForm = new InformForm();
informForm.setTitle("缴电费充值通知");
informForm.setBody("尊敬的用户您好,您的户");
informForm.setUrl("mineEarnGold");
informForm.setPushType(1);
informForm.setClassName("mineEarnGold");
informForm.setClassTitle("我的金币");
informForm.setNeedLogin("1");
informForm.setEquipmentType("kksh");
CommonResult commonResult = geTuiService.listPush(informForm);
System.out.println(commonResult);
}
}
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