Skip to content

Commit 8eec2bf

Browse files
author
guorutao
committed
支持俩平台
1 parent f80a803 commit 8eec2bf

7 files changed

Lines changed: 149 additions & 15 deletions

File tree

src/main/java/com/unfbx/zdm_push/api/ServerPushApi.java renamed to src/main/java/com/unfbx/zdm_push/api/ServerJPushApi.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.unfbx.zdm_push.api;
22

33
import com.github.lianjiatech.retrofit.spring.boot.annotation.RetrofitClient;
4-
import com.unfbx.zdm_push.constant.ServerPushResponse;
4+
import com.unfbx.zdm_push.constant.ServerJPushResponse;
55
import retrofit2.http.GET;
66
import retrofit2.http.Path;
77
import retrofit2.http.Query;
@@ -12,7 +12,7 @@
1212
* @Date 2020-12-11
1313
*/
1414
@RetrofitClient(baseUrl = "https://sc.ftqq.com/")
15-
public interface ServerPushApi {
15+
public interface ServerJPushApi {
1616
@GET("{key}.send")
17-
ServerPushResponse sendToServerJiang(@Path("key") String key, @Query("text")String text, @Query("desp")String desp);
17+
ServerJPushResponse sendToServerJiang(@Path("key") String key, @Query("text")String text, @Query("desp")String desp);
1818
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.unfbx.zdm_push.api;
2+
3+
import com.github.lianjiatech.retrofit.spring.boot.annotation.RetrofitClient;
4+
import com.unfbx.zdm_push.constant.ServerPushPlusResponse;
5+
import retrofit2.http.GET;
6+
import retrofit2.http.Query;
7+
import retrofit2.http.QueryMap;
8+
9+
import java.util.Map;
10+
11+
/**
12+
* @Description
13+
* @Author Grt
14+
* @Date 2020-12-11
15+
*/
16+
@RetrofitClient(baseUrl = "http://pushplus.hxtrip.com/")
17+
public interface ServerPushPlusApi {
18+
@GET("send")
19+
ServerPushPlusResponse sendToServerPushPlus(@QueryMap Map<String,String> param);
20+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.unfbx.zdm_push.constant;
2+
3+
/**
4+
* @Description
5+
* @Author Grt
6+
* @Date 2020-12-25
7+
*/
8+
public enum KeyType {
9+
10+
SERVER_J(0,"serverJ"),
11+
PUSH_PLUS(1,"pushPlus"),
12+
;
13+
14+
private int code;
15+
private String value;
16+
17+
KeyType(int code,String value){
18+
this.code = code;
19+
this.value = value;
20+
}
21+
22+
public int getCode() {
23+
return code;
24+
}
25+
26+
public String getValue() {
27+
return value;
28+
}
29+
}

src/main/java/com/unfbx/zdm_push/constant/ServerPushResponse.java renamed to src/main/java/com/unfbx/zdm_push/constant/ServerJPushResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* @Date 2020-12-11
99
*/
1010
@Data
11-
public class ServerPushResponse {
11+
public class ServerJPushResponse {
1212

1313
//{"errno":0,"errmsg":"success","dataset":"done"}
1414

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.unfbx.zdm_push.constant;
2+
3+
import lombok.Data;
4+
5+
/**
6+
* @Description
7+
* {
8+
* code: 200,
9+
* msg: "请求成功",
10+
* data: "发送消息成功",
11+
* count: null
12+
* }
13+
* @Author Grt
14+
* @Date 2020-12-25
15+
*/
16+
@Data
17+
public class ServerPushPlusResponse {
18+
private int code;
19+
private String msg;
20+
private String data;
21+
private Integer count = 0;
22+
23+
/**
24+
* 是否成功
25+
* @param code
26+
* @return
27+
*/
28+
public boolean isSuccess(int code){
29+
if(code == 200){
30+
return true;
31+
}
32+
return false;
33+
}
34+
}

src/main/java/com/unfbx/zdm_push/pipeline/ZdmPipeline.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package com.unfbx.zdm_push.pipeline;
22

3+
import com.unfbx.zdm_push.constant.KeyType;
4+
import com.unfbx.zdm_push.constant.ServerResponse;
35
import com.unfbx.zdm_push.pojo.ZdmInfo;
46
import com.unfbx.zdm_push.service.ServerPush;
57
import lombok.extern.java.Log;
8+
import org.apache.commons.lang3.StringUtils;
69
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.beans.factory.annotation.Value;
711
import org.springframework.stereotype.Service;
812
import us.codecraft.webmagic.ResultItems;
913
import us.codecraft.webmagic.Task;
@@ -20,14 +24,28 @@ public class ZdmPipeline implements Pipeline {
2024

2125
@Autowired
2226
private ServerPush serverPush = new ServerPush();
27+
/**
28+
* 微信推送类型
29+
*/
30+
@Value("${key.type}")
31+
private String keyType;
2332

2433
@Override
2534
public void process(ResultItems resultItems, Task task) {
2635
ZdmInfo zdmInfo = new ZdmInfo();
2736
zdmInfo.setName(resultItems.get("name"));
2837
zdmInfo.setUrl(resultItems.get("url"));
2938
if (resultItems.get("flag")){
30-
serverPush.pushMsgToWechat(zdmInfo);
39+
if(StringUtils.isBlank(keyType)){
40+
log.info("~~~~~~~~~~~~~~~~为配置微信推送类型,到application.yml配置~~~~~~~~~~~~~~~~");
41+
}
42+
if(KeyType.SERVER_J.getValue().equals(keyType)){
43+
ServerResponse serverResponse = serverPush.pushJMsgToWechat(zdmInfo);
44+
}
45+
if(KeyType.PUSH_PLUS.getValue().equals(keyType)){
46+
ServerResponse serverResponse = serverPush.pushPushMsgToWechat(zdmInfo);
47+
}
48+
log.info("~~~~~~~~~~~~~~~~微信推送类型配置错误,到application.yml修改配置~~~~~~~~~~~~~~~~");
3149
}else {
3250
log.info("~~~~~~~~~~~~~~~~暂无更新数据~~~~~~~~~~~~~~~~");
3351
}
Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
package com.unfbx.zdm_push.service;
22

3-
import com.unfbx.zdm_push.api.ServerPushApi;
4-
import com.unfbx.zdm_push.constant.ServerPushResponse;
3+
import com.unfbx.zdm_push.api.ServerJPushApi;
4+
import com.unfbx.zdm_push.api.ServerPushPlusApi;
5+
import com.unfbx.zdm_push.constant.ServerJPushResponse;
6+
import com.unfbx.zdm_push.constant.ServerPushPlusResponse;
57
import com.unfbx.zdm_push.constant.ServerResponse;
68
import com.unfbx.zdm_push.pojo.ZdmInfo;
79
import lombok.extern.java.Log;
8-
import lombok.extern.log4j.Log4j2;
9-
import lombok.extern.slf4j.Slf4j;
10+
11+
import org.apache.commons.lang3.StringUtils;
1012
import org.springframework.beans.factory.annotation.Autowired;
1113
import org.springframework.beans.factory.annotation.Value;
1214
import org.springframework.stereotype.Service;
1315

16+
import java.util.HashMap;
17+
import java.util.Map;
18+
1419
/**
1520
* @Description
1621
* @Author Grt
@@ -22,18 +27,24 @@ public class ServerPush {
2227

2328

2429
@Autowired
25-
private ServerPushApi serverPushApi;
30+
private ServerJPushApi serverJPushApi;
31+
@Autowired
32+
private ServerPushPlusApi serverPushPlusApi;
2633
/**
2734
* 私人密钥
2835
*/
29-
@Value("${key}")
30-
private String key;
36+
@Value("${key.type}")
37+
private String keyValue;
38+
3139

32-
public ServerResponse pushMsgToWechat(ZdmInfo zdmInfo){
40+
public ServerResponse pushJMsgToWechat(ZdmInfo zdmInfo){
41+
if(StringUtils.isBlank(keyValue)){
42+
return ServerResponse.createByError("为配置微信推送密钥,到application.yml配置");
43+
}
44+
ServerJPushResponse serverPushResponse = serverJPushApi.sendToServerJiang(keyValue,zdmInfo.getName(), zdmInfo.getUrl());
3345

34-
ServerPushResponse serverPushResponse = serverPushApi.sendToServerJiang(key,zdmInfo.getName(), zdmInfo.getUrl());
3546
if (serverPushResponse == null){
36-
log.info("推送失败:"+serverPushResponse.getErrmsg());
47+
log.info("推送失败:系统异常");
3748
return ServerResponse.createByError("推送失败");
3849
}
3950
if(serverPushResponse.isSuccess(serverPushResponse.getErrmsg())){
@@ -42,4 +53,26 @@ public ServerResponse pushMsgToWechat(ZdmInfo zdmInfo){
4253
log.info("推送失败:"+serverPushResponse.getErrmsg());
4354
return ServerResponse.createByError("推送失败");
4455
}
56+
57+
public ServerResponse pushPushMsgToWechat(ZdmInfo zdmInfo){
58+
if(StringUtils.isBlank(keyValue)){
59+
return ServerResponse.createByError("为配置微信推送密钥,到application.yml配置");
60+
}
61+
Map<String,String> param = new HashMap<>();
62+
param.put("token",keyValue);
63+
param.put("title",zdmInfo.getName());
64+
param.put("content",zdmInfo.getUrl());
65+
param.put("template","html");
66+
ServerPushPlusResponse serverPushResponse = serverPushPlusApi.sendToServerPushPlus(param);
67+
68+
if (serverPushResponse == null){
69+
log.info("推送失败:系统异常");
70+
return ServerResponse.createByError("推送失败");
71+
}
72+
if(serverPushResponse.isSuccess(serverPushResponse.getCode())){
73+
return ServerResponse.createBySuccess("推送成功");
74+
}
75+
log.info("推送失败:"+serverPushResponse.getMsg());
76+
return ServerResponse.createByError("推送失败");
77+
}
4578
}

0 commit comments

Comments
 (0)