技术标签: # 微信公众号/小程序
https://mp.weixin.qq.com/cgi-bin/loginpage?t=wxm2-login&lang=zh_CN
https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login
链接:https://pan.baidu.com/s/1syGGvdCJqcSPnZdJZpMkTA 提取码:y8y5
开发建议使用测试平台,测试平台拥有所有接口权限
微信公众平台个人只能申请订阅号,而且很多接口没有权限,如菜单,网页录授权
微信接入必须能外网访问,其项目端口必须为80,需要进行路由映射
内网穿透工具 ngrok使用: https://blog.csdn.net/qq_41463655/article/details/92846613
代码写好后启动本地服务,输入内网穿透工具的地址进行接入
我这里使用内网穿透工具是花生壳
微信公众平台在 设置-基本资料 设置服务器配置,启动就ok了,花生壳的域名应该被微信认为是非法连接了,一直提示参数错误,使用 ngrok 工具连接是没有问题的
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://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.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>springboot-weixin</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-weixin</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.dom4j/dom4j -->
<dependency>
<groupId>org.dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>2.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.thoughtworks.xstream/xstream -->
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.11.1</version>
</dependency>
<!-- httpclient -->
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<!-- json-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.30</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package com.example.springbootweixin.utils;
import com.example.springbootweixin.entity.TextMessage;
import com.thoughtworks.xstream.XStream;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class XmlUtils {
/**
* 解析微信发来的请求(XML)
*
* @param request
* @return Map<String, String>
* @throws Exception
*/
@SuppressWarnings("unchecked")
public static Map<String, String> parseXml(HttpServletRequest request) throws Exception {
// 将解析结果存储在HashMap中
Map<String, String> map = new HashMap<String, String>();
// 从request中取得输入流
InputStream inputStream = request.getInputStream();
// 读取输入流
SAXReader reader = new SAXReader();
Document document = reader.read(inputStream);
// 得到xml根元素
Element root = document.getRootElement();
// 得到根元素的所有子节点
List<Element> elementList = root.elements();
// 遍历所有子节点
for (Element e : elementList)
map.put(e.getName(), e.getText());
// 释放资源
inputStream.close();
inputStream = null;
return map;
}
/**
* 文本消息对象转换成xml
*
* @param textMessage
* 文本消息对象
* @return xml
*/
public static String messageToXml(TextMessage textMessage) {
xstream.alias("xml", textMessage.getClass());
return xstream.toXML(textMessage);
}
/**
* 扩展xstream使其支持CDATA
*/
private static XStream xstream = new XStream();
}
package com.example.springbootweixin.utils;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
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.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class HttpClientUtil {
public static String doGet(String url, Map<String, String> param) {
// 创建Httpclient对象
CloseableHttpClient httpclient = HttpClients.createDefault();
String resultString = "";
CloseableHttpResponse response = null;
try {
// 创建uri
URIBuilder builder = new URIBuilder(url);
if (param != null) {
for (String key : param.keySet()) {
builder.addParameter(key, param.get(key));
}
}
URI uri = builder.build();
// 创建http GET请求
HttpGet httpGet = new HttpGet(uri);
// 执行请求
response = httpclient.execute(httpGet);
// 判断返回状态是否为200
if (response.getStatusLine().getStatusCode() == 200) {
resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (response != null) {
response.close();
}
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return resultString;
}
public static String doGet(String url) {
return doGet(url, null);
}
public static String doPost(String url, Map<String, String> param) {
// 创建Httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String resultString = "";
try {
// 创建Http Post请求
HttpPost httpPost = new HttpPost(url);
// 创建参数列表
if (param != null) {
List<NameValuePair> paramList = new ArrayList<>();
for (String key : param.keySet()) {
paramList.add(new BasicNameValuePair(key, param.get(key)));
}
// 模拟表单
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList);
httpPost.setEntity(entity);
}
// 执行http请求
response = httpClient.execute(httpPost);
resultString = EntityUtils.toString(response.getEntity(), "utf-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return resultString;
}
public static String doPost(String url) {
return doPost(url, null);
}
public static String doPostJson(String url, String json) {
// 创建Httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String resultString = "";
try {
// 创建Http Post请求
HttpPost httpPost = new HttpPost(url);
// 创建请求内容
StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
httpPost.setEntity(entity);
// 执行http请求
response = httpClient.execute(httpPost);
resultString = EntityUtils.toString(response.getEntity(), "utf-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return resultString;
}
}
package com.example.springbootweixin.utils;
import java.security.MessageDigest;
import java.util.Arrays;
public class CheckUtil {
public static final String tooken = "123456789"; // 开发者自行定义Tooken
public static boolean checkSignature(String signature, String timestamp, String nonce) {
// 1.定义数组存放tooken,timestamp,nonce
String[] arr = {
tooken, timestamp, nonce };
// 2.对数组进行排序
Arrays.sort(arr);
// 3.生成字符串
StringBuffer sb = new StringBuffer();
for (String s : arr) {
sb.append(s);
}
// 4.sha1加密,网上均有现成代码
String temp = getSha1(sb.toString());
// 5.将加密后的字符串,与微信传来的加密签名比较,返回结果
return temp.equals(signature);
}
public static String getSha1(String str) {
if (str == null || str.length() == 0) {
return null;
}
char hexDigits[] = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
try {
MessageDigest mdTemp = MessageDigest.getInstance("SHA1");
mdTemp.update(str.getBytes("UTF-8"));
byte[] md = mdTemp.digest();
int j = md.length;
char buf[] = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
buf[k++] = hexDigits[byte0 >>> 4 & 0xf];
buf[k++] = hexDigits[byte0 & 0xf];
}
return new String(buf);
} catch (Exception e) {
return null;
}
}
}
package com.example.springbootweixin.entity;
import lombok.Data;
@Data
public class BaseMessage {
/**
* 开发者微信
*/
private String ToUserName;
/**
* 发送方openid
*/
private String FromUserName;
/**
* 创建时间
*/
private long CreateTime;
/**
* 内容类型
*/
private String MsgType;
// /**
// * 消息id
// */
// private long MsgId ;
}
package com.example.springbootweixin.entity;
import lombok.Data;
@Data
public class TextMessage extends BaseMessage {
private String Content;
}
青云客智能聊天机器人API:http://api.qingyunke.com/
package com.example.springbootweixin.controller;
import com.alibaba.fastjson.JSONObject;
import com.example.springbootweixin.entity.TextMessage;
import com.example.springbootweixin.utils.CheckUtil;
import com.example.springbootweixin.utils.HttpClientUtil;
import com.example.springbootweixin.utils.XmlUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.Map;
/**
* 微信事件通知
*/
@RestController
@Slf4j
public class DispatCherServlet {
/**
* 微信验证
*
* @param signature
* @param timestamp
* @param nonce
* @param echostr
* @return
*/
@RequestMapping(value = "/dispatCherServlet", method = RequestMethod.GET)
public String getDispatCherServlet(String signature, String timestamp, String nonce, String echostr) {
boolean checkSignature = CheckUtil.checkSignature(signature, timestamp, nonce);
if (!checkSignature) {
return null;
}
return echostr;
}
/**
* 功能说明:微信事件通知(用户所有消息都会通过改接口接收)
*
* @param request
* @return
* @throws Exception
*/
@RequestMapping(value = "/dispatCherServlet", method = RequestMethod.POST)
public void postdispatCherServlet(HttpServletRequest request, HttpServletResponse response) throws Exception {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
Map<String, String> mapResult = XmlUtils.parseXml(request);
if (mapResult == null) {
return;
}
String msgType = mapResult.get("MsgType");
PrintWriter out = response.getWriter();
if(msgType == "text") {
//获取用户信息
String content = mapResult.get("Content");
String toUserName = mapResult.get("ToUserName");
String fromUserName = mapResult.get("FromUserName");
String textMessage = null;
//判断回复
if (content.equals("微信公众号")) {
textMessage = setTextMessage("我是后台接口自动回复的哦", toUserName, fromUserName);
} else {
// 调用第三方智能接口(青云客智能聊天机器人API)
String resultStr = HttpClientUtil.doGet("http://api.qingyunke.com/api.php?key=free&appid=0&msg=" + content);
JSONObject jsonObject = new JSONObject().parseObject(resultStr);
Integer integer = jsonObject.getInteger("result");
if (integer == null || integer != 0) {
textMessage = setTextMessage("亲,系统出错啦!", toUserName, fromUserName);
} else {
String result = jsonObject.getString("content");
textMessage = setTextMessage(result, toUserName, fromUserName);
}
}
log.info("postdispatCherServlet() info:{}", textMessage);
out.print(textMessage);
}
out.close();
}
// 回复消息封装
public String setTextMessage(String content, String toUserName, String fromUserName) {
TextMessage textMessage = new TextMessage();
textMessage.setCreateTime(System.currentTimeMillis());
textMessage.setFromUserName(toUserName);
textMessage.setToUserName(fromUserName);
textMessage.setContent(content);
textMessage.setMsgType("text");
String messageToXml = XmlUtils.messageToXml(textMessage);
return messageToXml;
}
}
别忘了看第二步微信公众号接入当前本地项目
文章浏览阅读472次。轻量级密码在设计时要考虑其应用目标平台,已提出的轻量级密码有面向硬件实现的设计、面向软件实现的设计和综合考虑软硬件实现的混合设计。或者在现有典型分组密码的基础上,对密码算法的组件进行轻量化的改进;还有于2011年发布的Piccolo、Lblock、KLEIN、LED、EPCBC算法,2012年发布的PRINCE、TWICE算法,2014年发布的LEA算法,2015年发布的SIMECK、SIMON算法,2016年发布的QTL算法,2017年发布的Magpie算法,2018年发布的Surge、SFN算法。_什么是轻量级密码
文章浏览阅读2k次。ippicv_2019_lnx_intel64_general_20180723.tgz 在github地址https://github.com/opencv/opencv_3rdparty/tree/ippicv/master_20180723/ippicv git clone [email protected]:opencv/opencv_3rdparty.git 即可下载 (有其他版本,自..._ubuntu opencv ippicv wechat
文章浏览阅读1.4k次。1.使用方法修改字符串的大小写在msg.title()中,msg后面的句点(.)让python对变量msg执行方法title()指定的操作。每个方法后面都跟着一对括号,这是因为方法通常需要额外的信息来完成其工作。这种信息是在括号内提供的。函数title()不需要额外的信息,因此它后面的括号是空的。title()以首字母大写的方式显示每个单词,即将每个单词的首字母都改为大写。这很有用,因为你经常需要..._python字符串分行
文章浏览阅读175次。React样式策略_jsx中编写classname 为什么要写styles.
文章浏览阅读471次。您好我正在使用android中的计算器。 我有一个EditText在wchich不会显示,具体取决于特定号码的点击,例如当用户按1时,它将显示1.现在当用户点击2时,它将显示12等等。从Android中的EditText获取文本点击按钮我有这样的事情public class Main extends Activity implements OnClickListener {public stati..._androidstudio点击按钮获取文本框内容
文章浏览阅读679次。第3章 信息系统治理_it审计的固有风险
文章浏览阅读3.8k次。本人ThinkPad E460,Win10操作系统,因为工作内容,需要在特定的网络中开发,要连接隐藏网络。公司分配的是台式机,给了个2.4g的网卡,5g没到货时,用着是在憋屈,所以就用自己的笔记本连接隐藏5g网。然后按照正常步骤进行:点击网络图标 - 网络和Internet设置 - 网络和共享中心 - 设置新的网络 - 手动连接到无线网络 ...当点击连接时就给反馈:“无法连接这个..._thinkpad e460安装网卡失败,没法上网
文章浏览阅读1.7k次,点赞5次,收藏31次。之前部署的yolov5-ncnn不支持调用本地摄像头进行在线推理,多少还是感觉遗憾。说实话yolov8-ncnn的部署属实有点割韭菜的嫌疑,这篇博客教你从0部署yolov8到安卓手机。_yolov8部署到手机
文章浏览阅读2.6k次。1、 举例说明计算机图形学的主要应用领域(至少说明5个应用领域)计算机及辅助设计与制造、可视化、图形实时绘制与自然景物仿真、计算机动画、用户接口、计算机艺术2、 分别解释直线生成算法DDA法、中点画线法和Bresenham法的基本原理。 DDA法:设过端点P0(x0 ,y0)、P1(x1 ,y1)的直线段为L(P0 ,P1),则直线段L的斜率L的起点P0的横坐标x0向L的终点P1的横坐标x1步进..._列举有关计算机图形学的应用
文章浏览阅读5.3w次,点赞17次,收藏67次。通过实例化一个对象,使一个类能调用另一个类的方法主题代码主题描述老张开车去东北这件事类人实例变量:名字name实例方法:去go_to车实例方法:run代码class Person: def __inti__(self,name): self.name = name def go_to(self,position,type): ''' :par..._python中类方法如何调用
文章浏览阅读2.2w次,点赞9次,收藏13次。文章目录安装创建连接虽然市面上 Zookeeper 的 WEB 管理工具很丰富,但是却很难找到一款满意的图形化客户端。鉴于这样的情况,经过时间的查找,找到了这款管理 Zookeeper 的图形化工具,取名 PrettyZoo,意为:Pretty nice Zookeeper GUI。PrettyZoo 是一个基于 JavaFX 和 Apache Curator 实现的 Zookeeper 图形化工具,该项目完全开源,可以通过 Github 主页查看。github:https://github.com_prettyzoo mac
文章浏览阅读8.4k次。方便焊接的值value文件在焊接过程中最方便的是在图中直接显示元件的值,因为这样即知道了这个是什么元件,也知道了这个元件的具体指,最适合手焊,如下:在AD软件中如果在comment这个属性就是值得情况(protel软件)只要把原件的标号隐藏掉,并显示conment即可,元件属性如下:这时候导出设置如下:导出结果如下:但是这时候的导出结果过于混乱,调整方式是直接隐..._ad生成gerber文件无边框