微信开发基础
# 微信公众号开发基础
# 1、准备开发环境
微信公众平台 (qq.com)测试账号系统 (opens new window)
- 可以申请一个测试账号,个人只能申请订阅号,几乎没有接口可用,并且消息推送还会受到次数限制。如果只是做开发测试的话,那么测试帐号比较好用
appID: wxc9ede44f5ebdf1a8
appsecret: a99d98034df27fb88a6fb20da1e3df70
2
- 与微信端的交互只能用域名的80端口,可以用nginx、natapp或者网关来转发请求。
新建nginx配置文件 wx.zxp.com.conf nginx: download (opens new window)
upstream wx-zxp{
ip_hash;
server localhost:18085;
}
server {
listen 80;
server_name wx.zxp.com;
charset utf-8;
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_buffering off;
proxy_pass http://wx-zxp;
client_max_body_size 3m;
}
location ~* \.(jpg|jpeg|png|gif|swf)$ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_buffering off;
proxy_pass http://wx-zxp;
expires 3d;
}
location ~* \.(js|css)$ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_buffering off;
proxy_pass http://wx-zxp;
expires 7d;
}
location ~* \.flv$ {
proxy_pass http://wx-zxp;
flv;
}
location /nginx-status{
stub_status on;
access_log off;
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
nginx -t
nginx -s reload
2
# 2、内网穿透
# A、注册花生壳
花生壳-免费内网穿透软件|端口映射工具|DNS免费动态域名解析_花生壳,内网也能用-贝锐花生壳官网 (oray.com) (opens new window)
设置好内网主机和端口后,即可访问: http://421l02640d.hsk.top http://4210ri2640.zicp.vip/
参考文章:微信公众号还能这么玩,你居然不知道! - 贝锐客服中心 (oray.com) (opens new window)
# B、ngrok.cc
Sunny-Ngrok内网转发 - 国内内网映射服务器 (opens new window)
隧道id: d8fa5274b0d243fd | port18085 | http | 127.0.0.1:18085 | Ngrok(客户端下载 (opens new window)) | 免费不过期 | http://zxp.free.idcfengye.com |
---|
# C、frp
# 服务器端配置frps.ini
[common]
bind_port = 7000
vhost_http_port = 9988 #由于80端口已暂用这里我们使用Nginx做端口映射到80端口来做微信开发的调试,如何映射后文会介绍
#连接池
max_pool_count = 5
#token验证
privilege_token = javen
#自定义二级域名
subdomain_host = frp.bygmzx.com
#控制面板
dashboard_port = 9999
dashboard_user = javen
dashboard_pwd = javen
#日志
log_file = ./frps.log
log_level = info
log_max_days = 3
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
客户端配置frpc.ini
[common]
server_addr = 121.35.99.12 # 服务器IP
server_port = 7000 # 服务器bind_port
privilege_token = javen
[wx]
type = http
local_port = 8080 # 映射到本地的8080端口
subdomain = wx
# 如果不使用SSH可以将其注释掉
[ssh]
type = tcp
local_ip = 127.0.0.1
local_port = 22
remote_port = 6000
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Releases · fatedier/frp (github.com) (opens new window)
参考:FRP | IJPay (gitee.io) (opens new window)
# 一、wxJava与springboot整合
# 1、引starter
<binarywang-weixin.version>4.1.4.B</binarywang-weixin.version>
<!-- 微信公众号 -->
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>wx-java-mp-spring-boot-starter</artifactId>
<version>${binarywang-weixin.version}</version>
</dependency>
<!-- 微信小程序 -->
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>wx-java-miniapp-spring-boot-starter</artifactId>
<version>${binarywang-weixin.version}</version>
</dependency>
<!-- 企点 -->
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>wx-java-qidian-spring-boot-starter</artifactId>
<version>${binarywang-weixin.version}</version>
</dependency>
<!-- 微信支付 -->
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>wx-java-pay-spring-boot-starter</artifactId>
<version>${binarywang-weixin.version}</version>
</dependency>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
spring.factories引入了 WxMpAutoConfiguration, 注入了
WxMpService
: 主要调用微信的api,默认采用HttpClient方式调用,将信息保存到内存当中(Memory、jedis、redisTemplate)
@Configuration
@EnableConfigurationProperties(WxMpProperties.class)
@Import({ WxMpStorageAutoConfiguration.class, WxMpServiceAutoConfiguration.class })
public class WxMpAutoConfiguration {}
2
3
4
主要的配置文件WxMpProperties
# 2、写属性配置
# 微信公众号配置,见WxMpProperties源码
wx:
mp:
app-id: wxc9ede44f5ebdf1a8
secret: a99d98034df27fb88a6fb20da1e3df70
aes-key:
token: shollin
2
3
4
5
6
7
# 3、写类配置
这里主要配置路由规则,见源码:WxMpMessageRouter,微信消息路由器,通过代码化的配置,把来自微信的消息交给handler处理
WxMpMessageRouter配置说明:
配置路由规则时要按照从细到粗的原则,否则可能消息可能会被提前处理
默认情况下消息只会被处理一次,除非使用 WxMpMessageRouterRule.next()
规则的结束必须用WxMpMessageRouterRule.end()或者WxMpMessageRouterRule.next(),否则不会生效
@Bean
public WxMpMessageRouter messageRouter(WxMpService wxMpService) {
final WxMpMessageRouter newRouter = new WxMpMessageRouter(wxMpService);
// 记录所有事件的日志 (异步执行)
newRouter.rule().handler(this.logHandler).next();
// 接收客服会话管理事件
newRouter.rule().async(false).msgType(EVENT).event(KF_CREATE_SESSION)
.handler(this.kfSessionHandler).end();
newRouter.rule().async(false).msgType(EVENT).event(KF_CLOSE_SESSION)
.handler(this.kfSessionHandler).end();
newRouter.rule().async(false).msgType(EVENT).event(KF_SWITCH_SESSION)
.handler(this.kfSessionHandler).end();
// 门店审核事件
newRouter.rule().async(false).msgType(EVENT).event(POI_CHECK_NOTIFY).handler(this.storeCheckNotifyHandler).end();
// 自定义菜单事件
newRouter.rule().async(false).msgType(EVENT).event(EventType.CLICK).handler(this.menuHandler).end();
// 点击菜单连接事件
newRouter.rule().async(false).msgType(EVENT).event(EventType.VIEW).handler(this.nullHandler).end();
// 关注事件
newRouter.rule().async(false).msgType(EVENT).event(SUBSCRIBE).handler(this.subscribeHandler).end();
// 取消关注事件
newRouter.rule().async(false).msgType(EVENT).event(UNSUBSCRIBE).handler(this.unsubscribeHandler).end();
// 上报地理位置事件
newRouter.rule().async(false).msgType(EVENT).event(EventType.LOCATION).handler(this.locationHandler).end();
// 接收地理位置消息
newRouter.rule().async(false).msgType(XmlMsgType.LOCATION).handler(this.locationHandler).end();
// 扫码事件
newRouter.rule().async(false).msgType(EVENT).event(EventType.SCAN).handler(this.scanHandler).end();
// 默认
newRouter.rule().async(false).handler(this.msgHandler).end();
return newRouter;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 4、无starter的配置
starter主要帮助我们生成WxMpService, 而这个需要微信公众号的appid等信息,而公众号很多时候是保存在数据库当中的,因此需要在配置的时候,需要通过读取数据库数据,再生成WxMpService。
# 二、wxJava多公众号与springboot整合
# 基础开发工具
# 1、解析请求的数据为map
dom4j
/**
* 解析微信公众号回调xml
* @param request 请求
* @return
*/
public static Map<String,String> parseRequest(HttpServletRequest request){
Map<String,String> map=new HashMap<String,String>();
SAXReader reader=new SAXReader();
Document document= null;
try {
//读取输入流获取文档对象
document = reader.read(request.getInputStream());
//根据文档对象获取根节点
Element rootElement=document.getRootElement();
//获取根所有子节点
List<Element> elements = rootElement.elements();
for (Element e:elements) {
map.put(e.getName(),e.getStringValue());
}
} catch (DocumentException | IOException e) {
e.printStackTrace();
}
return map;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2、
参考项目:
WxJava: WxJava - 微信开发 Java SDK,支持微信支付、开放平台、公众号、企业号/企业微信、小程序等的后端开发 (opens new window)