🎉 新增微信消息推送Mod

This commit is contained in:
fuhouyin 2022-11-21 11:05:46 +08:00
parent 18ce9397db
commit 34e04a28c4
5 changed files with 256 additions and 3 deletions

View File

@ -1,11 +1,19 @@
### Mod 自己练习的demo
### 个人学习的demo
### 可视化大屏组件
visualization
<img src=https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/aaf0bc4ca40f44f4a2054f4e8d5260b7~tplv-k3u1fbpfcp-watermark.image width=400 height=300 />
### 时间工具类
DateTimeUtils
daysBetween 计算两个日期之间相差的天数
overdueAdvent 计算临期/超期天数
overdueAdvent 计算临期/超期天数
### wx消息推送Mod
wxMod
utils.WxUtils
wxMod.wxRequestController
wxMod.wxSendMsgController
前提:需要提前准备一个微信公众号,个人订阅号无效,可申请注册微信测试号

55
pom.xml
View File

@ -4,6 +4,13 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath/>
</parent>
<groupId>org.example</groupId>
<artifactId>mod</artifactId>
<version>1.0-SNAPSHOT</version>
@ -28,6 +35,11 @@
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc -->
<dependency>
<groupId>org.xerial</groupId>
@ -35,6 +47,49 @@
<version>3.39.2.1</version>
</dependency>
<!-- Apache Http Begin -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.5</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>fluent-hc</artifactId>
<version>4.5.5</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.5</version>
</dependency>
<!-- Apache Http End -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
<!-- 国密SM4 -->
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.70</version>
</dependency>
<!-- 国密SM2 -->
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcmail-jdk15on</artifactId>
<version>1.70</version>
</dependency>
<dependency>
<groupId>org.apache.directory.studio</groupId>
<artifactId>org.apache.commons.codec</artifactId>
<version>1.8</version>
</dependency>
</dependencies>
<build>

View File

@ -0,0 +1,66 @@
package utils;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
public class WxUtils {
private static final String APPID = "";
private static final String APPSECRET="";
private static final String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
/**获取access_token*/
public static String getAccessToken(){
String url = ACCESS_TOKEN_URL.replace("APPID", APPID).replace("APPSECRET", APPSECRET);
JSONObject json = doGetstr(url);
String token = null;
if(json!=null){
token = json.getString("access_token");
}
return token;
}
/**处理doget请求*/
public static JSONObject doGetstr(String url){
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url);
JSONObject jsonObject = null;
try {
CloseableHttpResponse response = httpclient.execute(httpGet);
HttpEntity entity = response.getEntity();
if(entity!=null){
String result = EntityUtils.toString(entity);
jsonObject = JSONObject.parseObject(result);
}
} catch (IOException e) {
e.printStackTrace();
}
return jsonObject;
}
/**处理post请求*/
public static JSONObject doPoststr(String url,String outStr){
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
JSONObject jsonObject = null;
try {
httpPost.setEntity(new StringEntity(outStr, "utf-8"));
CloseableHttpResponse response = httpclient.execute(httpPost);
String result = EntityUtils.toString(response.getEntity(),"utf-8");
jsonObject =JSONObject.parseObject(result);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return jsonObject;
}
}

View File

@ -0,0 +1,83 @@
package wxMod;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.security.MessageDigest;
import java.util.Arrays;
@RestController
@RequestMapping("/wxRequest")
public class wxRequestController {
/**微信请求本服务接口 用于验证*/
@GetMapping("/check")
public void check(HttpServletRequest request, HttpServletResponse response){
System.out.println("success");
String signature = request.getParameter("signature");
String timestamp = request.getParameter("timestamp");
String nonce = request.getParameter("nonce");
String echostr = request.getParameter("echostr");
PrintWriter out = null;
try {
out = response.getWriter();
if(checkSignature(signature, timestamp, nonce)){
out.write(echostr);
}
} catch (IOException e) {
e.printStackTrace();
}finally{
out.close();
}
}
private static boolean checkSignature(String signature,String timestamp,String nonce){
String token = "fuhouyin";
String[] str = new String[]{token,timestamp,nonce};
//排序
Arrays.sort(str);
//拼接字符串
StringBuffer buffer = new StringBuffer();
for(int i =0 ;i<str.length;i++){
buffer.append(str[i]);
}
//进行sha1加密
String temp = encode(buffer.toString());
//与微信提供的signature进行匹对
return signature.equals(temp);
}
private static String encode(String str) {
if (str == null) {
return null;
}
try {
MessageDigest messageDigest = MessageDigest.getInstance("SHA1");
messageDigest.update(str.getBytes());
return getFormattedText(messageDigest.digest());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private static String getFormattedText(byte[] bytes) {
char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5',
'6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
int len = bytes.length;
StringBuilder buf = new StringBuilder(len * 2);
// 把密文转换成十六进制的字符串形式
for (int j = 0; j < len; j++) {
buf.append(HEX_DIGITS[(bytes[j] >> 4) & 0x0f]);
buf.append(HEX_DIGITS[bytes[j] & 0x0f]);
}
return buf.toString();
}
}

View File

@ -0,0 +1,41 @@
package wxMod;
import com.alibaba.fastjson.JSONObject;
import utils.WxUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("/wxSendMsg")
public class wxSendMsgController {
private static String TEMP_URL = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=";
private static String MSGID = ""; //消息模板
@PostMapping("/sendMsgMod")
public void sendMsgMod(String keyword1,String keyword2){
String openid = ""; //openid
String access_token = WxUtils.getAccessToken();
String url = TEMP_URL+access_token;
Map<String,Object> param1 = new HashMap<>();
param1.put("value",keyword1);
Map<String,Object> param2 = new HashMap<>();
param2.put("value",keyword2);
Map<String,Object> data = new HashMap<>();
data.put("keyword1",keyword1);
data.put("keyword2",keyword2);
Map<String,Object> params = new HashMap<>();
params.put("touser",openid);
params.put("template_id",MSGID);
params.put("data",data);
JSONObject jsonObject = new JSONObject(params);
JSONObject json = WxUtils.doPoststr(url,jsonObject.toJSONString());
System.out.println(json.toString());
}
}