----对weixin-java-mp使用
1.简介
微信公众号的开发,繁琐接口众多,为了方便快速开发使用weixin-java-mp工具进行快速接入,大大提升了开发速度!
准备工作 内网穿透natapp 二级域名
2.导入maven配置
<dependencies>
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>4.1.0</version>
</dependency>
</dependencies>
3.配置yml文件
wx:
mp:
useRedis: false #使用redis开启true
redisConfig: #若整体架构有使用redis 可继承WxMpDefaultConfigImpl 实现调用
host: 127.0.0.1
port: 6379
configs:
- appId: 1111 # 第一个公众号的appid
secret: 1111 # 公众号的appsecret
token: 111 # 接口配置里的Token值
aesKey: 111 # 接口配置里的EncodingAESKey值
## 可在此处添加个性化配置 也会同步到继承WxMpDefaultConfigImpl 实现中
- appId: 2222 # 第二个公众号的appid,以下同上
secret: 1111
token: 111
aesKey: 111
4.配置各项配置类
@ConfigurationProperties(prefix = "wx.mp")
public class WxMpProperties {
}
public class WxMpMyRedisConfigImpl extends WxMpDefaultConfigImpl {
/**
自定义属性必须通过 构造方法传递进来
**/
private String companyName;
private String clickUrl;
private RedisUtil<String,String> redisUtil;
public WxMpMyRedisConfigImpl(RedisUtil<String, String> redisUtil,String companyName,String clickUrl) {
this.redisUtil = redisUtil;
this.companyName = companyName;
this.clickUrl = clickUrl;
}
/**
重写有关AccessToken的4个方法
**/
@Override
public String getAccessToken() {
String s = redisUtil.get(key, appId);
return s;
}
@Override
public boolean isAccessTokenExpired() {
Long expire = redisUtil.getExpire(key, appId);
return expire == null || expire < 2;
}
@Override
public void expireAccessToken() {
redisUtil.delete(RedisKey.WECHAT_ACCESS_TOKEN, appId);
}
@Override
public void updateAccessToken(String accessToken, int expiresInSeconds) {
redisUtil.set(key, appId, accessToken, (long) (expiresInSeconds - 200));
}
public String getCompanyName() {
return companyName;
}
public String getClickUrl() {
return clickUrl;
}
}
@Configuration
@EnableConfigurationProperties(WxMpProperties.class)
public class WxMpConfiguration {
@Bean
public WxMpService wxMpService() {
WxMpService service = new WxMpServiceImpl();
service.setMultiConfigStorages(configs
.stream().map(a -> {
WxMpDefaultConfigImpl configStorage;
if (this.properties.isUseRedis()) {
configStorage = new WxMpMyRedisConfigImpl(redisUtil, a.getCompanyName(), a.getClickUrl());
} else {
configStorage = new WxMpDefaultConfigImpl();
}
configStorage.setAppId(a.getAppId());
configStorage.setSecret(a.getSecret());
configStorage.setToken(a.getToken());
configStorage.setAesKey(a.getAesKey());
return configStorage;
}).collect(Collectors.toMap(WxMpDefaultConfigImpl::getAppId, a -> a, (o, n) -> o)));
return service;
}
}
5.启用微信公众号管理界面开发者功能
注意点:开发者功能开启后 后台管理界面无法实现 自定义菜单 必须使用代码进行菜单自定义
@RequestMapping("/portal/{appid}")
public interface WxPortalService {
/**
* 微信获取开发者权限
*/
@RequestMapping(produces = "text/plain;charset=utf-8", method = RequestMethod.GET)
public String authGet(@PathVariable("appid") String appid,
@RequestParam(name = "signature", required = false) String signature,
@RequestParam(name = "timestamp", required = false) String timestamp,
@RequestParam(name = "nonce", required = false) String nonce,
@RequestParam(name = "echostr", required = false) String echostr);
/**
用于接收微信端消息
*/
@RequestMapping(produces = "application/xml; charset=UTF-8", method = RequestMethod.POST)
public String post(@PathVariable("appid") String appid,
@RequestBody String requestBody,
@RequestParam("signature") String signature,
@RequestParam("timestamp") String timestamp,
@RequestParam("nonce") String nonce,
@RequestParam("openid") String openid,
@RequestParam(name = "encrypt_type", required = false) String encType,
@RequestParam(name = "msg_signature", required = false) String msgSignature);
ps:点击 ‘启用’按钮后生效
6.自定义菜单
请求一次 微信返回 未设置白名单ip 加入管理后台ip白名单
@RequestMapping("/menu/{appid}")
public interface WxMenuService {
@RequestMapping(value = "/createByJson",method = RequestMethod.POST)
public String menuCreate(@PathVariable("appid") String appid, @RequestBody String json) throws WxErrorException;
}
{
"button": [
{
"type": "view",
"name": "xxx",
"key": "SELECT_SHIPMENT",
"url": ""
},
{
"type": "view",
"name": "xxx",
"key": "MY_SHIPMENT",
"url": ""
},
{
"name": "xxx",
"sub_button": [
{
"type": "view",
"name": "xxx",
"key": "BINDING",
"url": ""
}
]
}
]
}
ps: media_id类型和view_limited类型必须使用调用新增永久素材接口返回的合法media_id,永久素材id必须是在“素材管理/新增永久素材”接口上传后获得的合法id
7.网页授权域名
直接将文件内容拷贝出来放入返回值直接返回,如下
public interface WxAuthService {
@RequestMapping({"MP_verify_VwU7Ksmy5fizIsS8.txt"})
public String returnConfigFile();
redirect_uri/?code=CODE&state=STATE
回调使用code获取网页授权的access_token 使用该token 获取用户信息
小技巧:code 是微信回传自动产生code state 可以是多公众号appid
8.素材设置
注意:开发者模式 全部都要调用接口实现
图文消息的具体内容中,微信后台将过滤外部的图片链接,图片url需通过”上传图文消息内的图片获取URL”接口上传图片获取
9.最重要的调用
接口入口先调用再多公众号下,确定当前操作公众号
if (!this.wxService.switchover(state)) {
throw new IllegalArgumentException(String.format("未找到对应appid=[%s]的配置,请核实!", state));
}