|  | @ -0,0 +1,229 @@
 | 
	
		
			
				|  |  | package com.yihu.jw.push.service;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import com.alibaba.fastjson.JSONObject;
 | 
	
		
			
				|  |  | import com.getui.push.v2.sdk.ApiHelper;
 | 
	
		
			
				|  |  | import com.getui.push.v2.sdk.GtApiConfiguration;
 | 
	
		
			
				|  |  | import com.getui.push.v2.sdk.api.PushApi;
 | 
	
		
			
				|  |  | import com.getui.push.v2.sdk.common.ApiResult;
 | 
	
		
			
				|  |  | import com.getui.push.v2.sdk.dto.req.Audience;
 | 
	
		
			
				|  |  | import com.getui.push.v2.sdk.dto.req.Settings;
 | 
	
		
			
				|  |  | import com.getui.push.v2.sdk.dto.req.Strategy;
 | 
	
		
			
				|  |  | import com.getui.push.v2.sdk.dto.req.message.PushChannel;
 | 
	
		
			
				|  |  | import com.getui.push.v2.sdk.dto.req.message.PushDTO;
 | 
	
		
			
				|  |  | import com.getui.push.v2.sdk.dto.req.message.PushMessage;
 | 
	
		
			
				|  |  | import com.getui.push.v2.sdk.dto.req.message.android.AndroidDTO;
 | 
	
		
			
				|  |  | import com.getui.push.v2.sdk.dto.req.message.android.GTNotification;
 | 
	
		
			
				|  |  | import com.getui.push.v2.sdk.dto.req.message.android.ThirdNotification;
 | 
	
		
			
				|  |  | import com.getui.push.v2.sdk.dto.req.message.android.Ups;
 | 
	
		
			
				|  |  | import com.getui.push.v2.sdk.dto.req.message.ios.Alert;
 | 
	
		
			
				|  |  | import com.getui.push.v2.sdk.dto.req.message.ios.Aps;
 | 
	
		
			
				|  |  | import com.getui.push.v2.sdk.dto.req.message.ios.IosDTO;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.hospital.consult.WlyyHospitalSysDictDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.push.dao.PushHospitalSysDictDao;
 | 
	
		
			
				|  |  | import org.slf4j.Logger;
 | 
	
		
			
				|  |  | import org.slf4j.LoggerFactory;
 | 
	
		
			
				|  |  | import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  | import org.springframework.data.redis.core.StringRedisTemplate;
 | 
	
		
			
				|  |  | import org.springframework.stereotype.Service;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import java.util.List;
 | 
	
		
			
				|  |  | import java.util.Map;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | /**
 | 
	
		
			
				|  |  |  * Created with IntelliJ IDEA.
 | 
	
		
			
				|  |  |  * 个推推送工具类
 | 
	
		
			
				|  |  |  * @Author: yeshijie
 | 
	
		
			
				|  |  |  * @Date: 2021/7/8
 | 
	
		
			
				|  |  |  * @Description:
 | 
	
		
			
				|  |  |  */
 | 
	
		
			
				|  |  | @Service
 | 
	
		
			
				|  |  | public class GetuiService {
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private StringRedisTemplate redisTemplate;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private PushHospitalSysDictDao sysDictDao;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     private static final Logger logger = LoggerFactory.getLogger(GetuiService.class);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     private static PushApi pushApi;
 | 
	
		
			
				|  |  |     private String AppID;
 | 
	
		
			
				|  |  |     private String AppKey;
 | 
	
		
			
				|  |  |     private String AppSecret;
 | 
	
		
			
				|  |  |     private String MasterSecret;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | //    public static String AppID = "I6JAs97T818HD0hGzG1EH4";
 | 
	
		
			
				|  |  | //    public static String AppKey = "JC2LAW9IK27pansBB4jN87";
 | 
	
		
			
				|  |  | //    public static String AppSecret = "Sn9PLz1hA6NNgyb7WHx45";
 | 
	
		
			
				|  |  | //    public static String MasterSecret = "Rd86CP8bOy7RHyTf4ZE3R4";
 | 
	
		
			
				|  |  |     public static String cloudCareDoctor = "com.zjxl.medicalcare.doctor";
 | 
	
		
			
				|  |  |     public static String cloudCarePatientPad = "com.zjxl.medicalcare.patient.pad";
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     private String key = "hz_yxyzh_push_getui";
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 初始化接口参数
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     private void init(){
 | 
	
		
			
				|  |  |         if(redisTemplate.hasKey(key+ ":AppID")){
 | 
	
		
			
				|  |  |             AppID = redisTemplate.opsForValue().get(key + ":AppID");
 | 
	
		
			
				|  |  |             AppKey = redisTemplate.opsForValue().get(key + ":AppKey");
 | 
	
		
			
				|  |  |             AppSecret = redisTemplate.opsForValue().get(key + ":AppSecret");
 | 
	
		
			
				|  |  |             MasterSecret = redisTemplate.opsForValue().get(key + ":MasterSecret");
 | 
	
		
			
				|  |  |             return;
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         forceInit();
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public void forceInit(){
 | 
	
		
			
				|  |  |         List<WlyyHospitalSysDictDO> dictDOList = sysDictDao.findByDictName(key);
 | 
	
		
			
				|  |  |         for (WlyyHospitalSysDictDO wlyyHospitalSysDictDO:dictDOList){
 | 
	
		
			
				|  |  |             if (wlyyHospitalSysDictDO.getDictCode().equalsIgnoreCase("AppID")){
 | 
	
		
			
				|  |  |                 AppID=wlyyHospitalSysDictDO.getDictValue();
 | 
	
		
			
				|  |  |                 redisTemplate.opsForValue().set(key + ":AppID",AppID);
 | 
	
		
			
				|  |  |             }else if (wlyyHospitalSysDictDO.getDictCode().equalsIgnoreCase("AppKey")){
 | 
	
		
			
				|  |  |                 AppKey=wlyyHospitalSysDictDO.getDictValue();
 | 
	
		
			
				|  |  |                 redisTemplate.opsForValue().set(key + ":AppKey",AppKey);
 | 
	
		
			
				|  |  |             }else if (wlyyHospitalSysDictDO.getDictCode().equalsIgnoreCase("AppSecret")){
 | 
	
		
			
				|  |  |                 AppSecret=wlyyHospitalSysDictDO.getDictValue();
 | 
	
		
			
				|  |  |                 redisTemplate.opsForValue().set(key + ":AppSecret",AppSecret);
 | 
	
		
			
				|  |  |             }else if (wlyyHospitalSysDictDO.getDictCode().equalsIgnoreCase("MasterSecret")){
 | 
	
		
			
				|  |  |                 MasterSecret=wlyyHospitalSysDictDO.getDictValue();
 | 
	
		
			
				|  |  |                 redisTemplate.opsForValue().set(key + ":MasterSecret",MasterSecret);
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         pushApi = getPushApi();
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     private PushApi getPushApi(){
 | 
	
		
			
				|  |  |         GtApiConfiguration apiConfiguration = new GtApiConfiguration();
 | 
	
		
			
				|  |  |         //填写应用配置
 | 
	
		
			
				|  |  |         apiConfiguration.setAppId(AppID);
 | 
	
		
			
				|  |  |         apiConfiguration.setAppKey(AppKey);
 | 
	
		
			
				|  |  |         apiConfiguration.setMasterSecret(MasterSecret);
 | 
	
		
			
				|  |  |         // 接口调用前缀,请查看文档: 接口调用规范 -> 接口前缀, 可不填写appId
 | 
	
		
			
				|  |  |         apiConfiguration.setDomain("https://restapi.getui.com/v2/");
 | 
	
		
			
				|  |  |         // 实例化ApiHelper对象,用于创建接口对象
 | 
	
		
			
				|  |  |         ApiHelper apiHelper = ApiHelper.build(apiConfiguration);
 | 
	
		
			
				|  |  |         // 创建对象,建议复用。目前有PushApi、StatisticApi、UserApi
 | 
	
		
			
				|  |  |         PushApi pushApi = apiHelper.creatApi(PushApi.class);
 | 
	
		
			
				|  |  |         return pushApi;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * Rest-V2厂商推送
 | 
	
		
			
				|  |  |      * type 1 安卓 2 ios
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public void push2(String cid,int type,JSONObject json){
 | 
	
		
			
				|  |  |         init();
 | 
	
		
			
				|  |  |         //根据cid进行单推
 | 
	
		
			
				|  |  |         PushDTO<Audience> pushDTO = new PushDTO<Audience>();
 | 
	
		
			
				|  |  |         // 设置推送参数
 | 
	
		
			
				|  |  |         pushDTO.setRequestId(System.currentTimeMillis() + "");//requestid需要每次变化唯一
 | 
	
		
			
				|  |  |         //配置推送条件
 | 
	
		
			
				|  |  |         // 1: 表示该消息在用户在线时推送个推通道,用户离线时推送厂商通道;
 | 
	
		
			
				|  |  |         // 2: 表示该消息只通过厂商通道策略下发,不考虑用户是否在线;
 | 
	
		
			
				|  |  |         // 3: 表示该消息只通过个推通道下发,不考虑用户是否在线;
 | 
	
		
			
				|  |  |         // 4: 表示该消息优先从厂商通道下发,若消息内容在厂商通道代发失败后会从个推通道下发。
 | 
	
		
			
				|  |  |         Strategy strategy=new Strategy();
 | 
	
		
			
				|  |  |         strategy.setDef(1);
 | 
	
		
			
				|  |  |         Settings settings=new Settings();
 | 
	
		
			
				|  |  |         settings.setStrategy(strategy);
 | 
	
		
			
				|  |  |         pushDTO.setSettings(settings);
 | 
	
		
			
				|  |  |         settings.setTtl(3600000);//消息有效期,走厂商消息需要设置该值
 | 
	
		
			
				|  |  |         PushChannel pushChannel = new PushChannel();
 | 
	
		
			
				|  |  |         if(type == 1){
 | 
	
		
			
				|  |  |             //安卓离线厂商通道推送消息体
 | 
	
		
			
				|  |  |             AndroidDTO androidDTO = new AndroidDTO();
 | 
	
		
			
				|  |  |             Ups ups = new Ups();
 | 
	
		
			
				|  |  |             ThirdNotification notification1 = new ThirdNotification();
 | 
	
		
			
				|  |  |             notification1.setTitle(json.getString("title"));
 | 
	
		
			
				|  |  |             notification1.setBody(json.getString("body"));
 | 
	
		
			
				|  |  |             notification1.setClickType("intent");
 | 
	
		
			
				|  |  |             //component 包名 调起app
 | 
	
		
			
				|  |  |             String intent = "intent:#Intent;launchFlags=0x04000000;action=android.intent.action.oppopush;" +
 | 
	
		
			
				|  |  |                     "component="+json.getString("platform")+"/io.dcloud.PandoraEntry;S.UP-OL-SU=true;" +
 | 
	
		
			
				|  |  |                     "S.title="+json.getString("title")+";" +
 | 
	
		
			
				|  |  |                     "S.content="+json.getString("body")+";" +
 | 
	
		
			
				|  |  |                     "S.payload="+json.getString("payload")+";end";
 | 
	
		
			
				|  |  |             notification1.setIntent(intent);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             ups.setNotification(notification1);
 | 
	
		
			
				|  |  |             //各厂商自有功能单项设置
 | 
	
		
			
				|  |  |             //ups.addOption("HW", "/message/android/notification/badge/class", "io.dcloud.PandoraEntry ");
 | 
	
		
			
				|  |  |             //ups.addOption("HW", "/message/android/notification/badge/add_num", 1);
 | 
	
		
			
				|  |  |             //ups.addOption("HW", "/message/android/notification/importance", "HIGH");
 | 
	
		
			
				|  |  |             //ups.addOption("VV","classification",1);
 | 
	
		
			
				|  |  |             androidDTO.setUps(ups);
 | 
	
		
			
				|  |  |             pushChannel.setAndroid(androidDTO);
 | 
	
		
			
				|  |  |         } else{
 | 
	
		
			
				|  |  |             //推送苹果离线通知标题内容
 | 
	
		
			
				|  |  |             Alert alert=new Alert();
 | 
	
		
			
				|  |  |             alert.setTitle(json.getString("title"));
 | 
	
		
			
				|  |  |             alert.setBody(json.getString("body"));
 | 
	
		
			
				|  |  |             Aps aps = new Aps();
 | 
	
		
			
				|  |  |             //1表示静默推送(无通知栏消息),静默推送时不需要填写其他参数。
 | 
	
		
			
				|  |  |             //苹果建议1小时最多推送3条静默消息
 | 
	
		
			
				|  |  |             aps.setContentAvailable(0);
 | 
	
		
			
				|  |  |             aps.setSound("default");
 | 
	
		
			
				|  |  |             aps.setAlert(alert);
 | 
	
		
			
				|  |  |             IosDTO iosDTO = new IosDTO();
 | 
	
		
			
				|  |  |             iosDTO.setAps(aps);
 | 
	
		
			
				|  |  |             iosDTO.setType("notify");
 | 
	
		
			
				|  |  |             pushChannel.setIos(iosDTO);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         pushDTO.setPushChannel(pushChannel);
 | 
	
		
			
				|  |  |         // PushMessage在线走个推通道才会起作用的消息体
 | 
	
		
			
				|  |  |         PushMessage pushMessage = new PushMessage();
 | 
	
		
			
				|  |  | //        pushMessage.setTransmission(" {title:\""+json.getString("title")+"\",content:\""+json.getString("body")+"\",payload:\""+json.getString("payload")+"\"}");
 | 
	
		
			
				|  |  |         pushMessage.setTransmission(" {\"title\":\""+json.getString("title")+"\",\"content\":\""+json.getString("body")+"\",\"url\":\""+json.getString("url")+"\"}");
 | 
	
		
			
				|  |  |         pushDTO.setPushMessage(pushMessage);
 | 
	
		
			
				|  |  |         // 设置接收人信息
 | 
	
		
			
				|  |  |         Audience audience = new Audience();
 | 
	
		
			
				|  |  |         pushDTO.setAudience(audience);
 | 
	
		
			
				|  |  |         audience.addCid(cid);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         // 进行cid单推
 | 
	
		
			
				|  |  |         ApiResult<Map<String, Map<String, String>>> apiResult = pushApi.pushToSingleByCid(pushDTO);
 | 
	
		
			
				|  |  |         if (apiResult.isSuccess()) {
 | 
	
		
			
				|  |  |             // success
 | 
	
		
			
				|  |  |             logger.info("发送成功:"+apiResult.getData());
 | 
	
		
			
				|  |  |         } else {
 | 
	
		
			
				|  |  |             // failed
 | 
	
		
			
				|  |  |             logger.info("code:" + apiResult.getCode() + ", msg: " + apiResult.getMsg());
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 单推
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public ApiResult<Map<String, Map<String, String>>> pushToSingleByCid(String cid, JSONObject json) {
 | 
	
		
			
				|  |  |         init();
 | 
	
		
			
				|  |  |         //根据cid进行单推
 | 
	
		
			
				|  |  |         PushDTO<Audience> pushDTO = new PushDTO<>();
 | 
	
		
			
				|  |  |         // 设置推送参数
 | 
	
		
			
				|  |  |         pushDTO.setRequestId(System.currentTimeMillis() + "");
 | 
	
		
			
				|  |  |         PushMessage pushMessage = new PushMessage();
 | 
	
		
			
				|  |  |         pushDTO.setPushMessage(pushMessage);
 | 
	
		
			
				|  |  |         GTNotification notification = new GTNotification();
 | 
	
		
			
				|  |  |         pushMessage.setNotification(notification);
 | 
	
		
			
				|  |  |         notification.setTitle(json.getString("title"));
 | 
	
		
			
				|  |  |         notification.setBody(json.getString("body"));
 | 
	
		
			
				|  |  |         notification.setClickType(json.getString("clickType"));
 | 
	
		
			
				|  |  |         notification.setUrl(json.getString("url"));
 | 
	
		
			
				|  |  |         // 设置接收人信息
 | 
	
		
			
				|  |  |         Audience audience = new Audience();
 | 
	
		
			
				|  |  |         pushDTO.setAudience(audience);
 | 
	
		
			
				|  |  |         audience.addCid(cid);
 | 
	
		
			
				|  |  |         //消息离线时间设置
 | 
	
		
			
				|  |  |         Settings settings = new Settings();
 | 
	
		
			
				|  |  |         //	消息离线时间设置,单位毫秒,-1表示不设离线,-1 ~ 3 * 24 * 3600 * 1000(3天)之间
 | 
	
		
			
				|  |  |         settings.setTtl(3 * 24 * 3600 * 1000);
 | 
	
		
			
				|  |  |         pushDTO.setSettings(settings);
 | 
	
		
			
				|  |  |         // 进行cid单推
 | 
	
		
			
				|  |  |         return pushApi.pushToSingleByCid(pushDTO);
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | }
 |