97 lines
2.9 KiB
Java
97 lines
2.9 KiB
Java
package com.gxwebsoft.common.core.utils;
|
|
|
|
import cn.hutool.crypto.SecureUtil;
|
|
import cn.hutool.http.HttpRequest;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.gxwebsoft.common.system.service.UserService;
|
|
import com.gxwebsoft.common.system.vo.PushMessageVO;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import javax.annotation.Resource;
|
|
import java.util.HashMap;
|
|
import java.util.HashSet;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
/**
|
|
* 个推推送消息工具类
|
|
*
|
|
* @author WebSoft
|
|
* @since 2017-06-10 10:10:39
|
|
*/
|
|
@Component
|
|
public class PushUtil {
|
|
@Resource
|
|
RedisUtil redisUtil;
|
|
|
|
@Resource
|
|
private UserService userService;
|
|
private static final String url = "https://fc-mp-ba98f1e0-713d-457b-a0a4-27a7939371e6.next.bspapp.com/unipush";
|
|
|
|
/**
|
|
* 获取鉴权token
|
|
* @return
|
|
*/
|
|
public String getToken() {
|
|
String key = "token:oOVaDtYDYQ8q3lNjhLh401";
|
|
|
|
if(redisUtil.get(key) != null){
|
|
return redisUtil.get(key);
|
|
}
|
|
|
|
HashMap<String, Object> map = new HashMap<>();
|
|
long timeMillis = System.currentTimeMillis();
|
|
String sign = SecureUtil.sha256("AC6IghgsUx7Mwjb7G5eqv" + timeMillis + "JVRkOCXXzA6EyE2Fi5sPr9");
|
|
map.put("sign",sign);
|
|
map.put("timestamp",timeMillis);
|
|
map.put("appkey","AC6IghgsUx7Mwjb7G5eqv");
|
|
final String body = HttpRequest.post(url.concat("/auth")).body(JSONUtil.toJSONString(map)).execute().body();
|
|
final JSONObject jsonObject = JSONObject.parseObject(body);
|
|
final String data = jsonObject.getString("data");
|
|
final JSONObject jsonData = JSONObject.parseObject(data);
|
|
|
|
final String token = jsonData.getString("token");
|
|
final Long expireTime = Long.valueOf(jsonData.getString("expire_time"));
|
|
// 保存token
|
|
|
|
redisUtil.set(key,token, expireTime-System.currentTimeMillis(), TimeUnit.MILLISECONDS);
|
|
return token;
|
|
}
|
|
|
|
/**
|
|
* 执行cid单推
|
|
* cid数组,只能填一个cid
|
|
*
|
|
*/
|
|
public static boolean toSingle(PushMessageVO pushMessageVO){
|
|
final String body = HttpRequest.post(url).body(JSONUtil.toJSONString(pushMessageVO)).execute().body();
|
|
JSONObject jsonObject = JSONObject.parseObject(body);
|
|
if("success".equals(jsonObject.get("errMsg"))){
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
|
|
public boolean toSingle(Integer userId,String title, String content,String type, Object obj){
|
|
PushMessageVO.Payload payload = new PushMessageVO.Payload();
|
|
payload.setType(type);
|
|
payload.setData(obj);
|
|
|
|
String clientId = userService.getById(userId).getClientId();
|
|
HashSet<String> clientIds = new HashSet<>();
|
|
clientIds.add(clientId);
|
|
|
|
PushMessageVO messageVO = PushMessageVO.builder().title(title).content(content).payload(payload).push_clientid(clientIds).build();
|
|
|
|
final String body = HttpRequest.post(url).body(JSONUtil.toJSONString(messageVO)).execute().body();
|
|
|
|
JSONObject jsonObject = JSONObject.parseObject(body);
|
|
if("success".equals(jsonObject.get("errMsg"))){
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
|
|
}
|