There are a lot of content, please be patient!
Recently, the company had this business demand, and I happened to complete it:
First, you want to connect, first you need a public number, and then you develop the document. https://developers.weixin.qq.com/doc/offiaccount/Getting_Started/Overview.html
But please pay attention to this
ok, let's continue to complete the basic configuration of the public address.
Server address (URL): it must start with http: / / or https: / /, which supports port 80 and port 443 respectively. This URL is very important and needs to respond to the token verification sent by wechat
Token: it must be in English or number and 3-32 characters in length. It's been proven
Message encryption and decryption key: can be generated directly and randomly
Message encryption and decryption methods: plaintext, compatibility, security: business needs choice: I think plaintext is easy (personal opinion)
Detailed explanation of wechat development documents: https://developers.weixin.qq.com/doc/offiaccount/Getting_Started/Overview.html
If I always call it with wechat interface, it will be a bit troublesome, so here I refer to WxJava
gitub: https://github.com/Wechat-Group/WxJava
gitee: https://gitee.com/binary/weixin-java-tools
First, let's take a look at the official document's description of push template message parameters:
When I first read this development document, I was still confused
How can I get openId? I don't know! Well, let's go to duniang and google, and the doubts will not be solved!
ok, next step template Id: template Id; for this, you can apply for or select the existing
I will save unnecessary trouble. It's a Demo, so I choose the existing one:
Select one to add the template:
ok, we have the template id, now
Please take a look at the WxJava documentation in detail
---Create the SpringBoot project first---
Import the pom corresponding to the wxjava public number.
<!-- WxJava official account --> <dependency> <groupId>com.github.binarywang</groupId> <artifactId>weixin-java-mp</artifactId> <version>3.6.0</version>
</dependency>
Then we need to configure the information about the public number. I personally prefer to configure it in yml.
# WeChat public address configuration
wx:
appid: 11111
secret: 22222
token: 33333
aeskey: 44444
If you configure this, you need the Component (entity class) corresponding to this configuration
/**
* @ClassName WXProperties
* @Author chenghao
* @Date 2020/1/10 15:44
**/
@Data
@Component
@ConfigurationProperties(prefix = "wx")
public class WxMpProperties {
/**
* Public address appId
*/
private String appId;
/**
* Public address appSecret
*/
private String secret;
/**
* Public address token
*/
private String token;
/**
* Public address aesKey
*/
private String aesKey;
}
I won't go into details about annotations. I don't understand. I'll go to the official documents
SpringBoot:
Lombok:
https://projectlombok.org/features/all
-----------------------------OK, everyone, let's start now-----------------------------
Let me give you a step-by-step analysis
The template we just selected, these key s are all one parameter, the document says very clearly, assignment replacement!!! It's ok to understand that.
OK, let's go back to WxJava
Well, according to the above example code, I wrote a Demo
/**
* Wechat message push
*
* @ClassName WxMsgPush
* @Author chenghao
* @Date 2020/1/10 16:20
**/
@Slf4j
@Component
public class WxMsgPush {
/**
* WeChat public number API Service
*/
private final WxMpService wxMpService;
/**
* Structural injection
*/
WxMsgPush(WxMpService wxMpService) {
this.wxMpService = wxMpService;
}
/**
* Send wechat template information
*
* @param openId Recipient openId
* @return Push succeeded or not
*/
public Boolean SendWxMsg(String openId) {
// Send template message interface
WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
// Recipient openid
.toUser(openId)
// Template id
.templateId("xxxxxxxxxxxxxxxxxxxxxxxxxxx")
// Template jump link
.url("http://www.baidu.com")
.build();
// Add template data
templateMessage.addData(new WxMpTemplateData("first", "Hello", "#FF00FF"))
.addData(new WxMpTemplateData("keyword1", "This is a test", "#A9A9A9"))
.addData(new WxMpTemplateData("keyword2", "This is another test", "#FF00FF"))
.addData(new WxMpTemplateData("remark", "It's still a test", "#000000"));
String msgId = null;
try {
// Send template message
msgId = wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
} catch (WxErrorException e) {
e.printStackTrace();
}
log.warn("·==++--·Push wechat template information:{}·--++==·", msgId != null ? "Success" : "fail");
return msgId != null;
}
}
wdnmd! I found the wrong report when I came up!!!
Could not autowire. No beans of 'WxMpService' type found. --------- cannot auto wire. Cannot find bean of type 'WxMpService'
I look at the WxMpService interfaceThese implementation classes, Gan!
After careful study, I need to write a Configuration myself
/**
* @ClassName WxConfig
* @Author chenghao
* @Date 2020/1/11 09:23
**/
@Configuration
public class WxConfig {
/**
* Declaration instance
*
* @return
*/
@Bean
public WxMpService wxMpService() {
WxMpService wxMpService = new WxMpServiceImpl();
return wxMpService;
}
ok, start analyzing the code
First of all, this
There are four attributes when you click in to have a look:
/**
* Recipient openid
*/
private String toUser;
/**
* Template ID.
*/
private String templateId;
/**
* Template jump link
* <pre>
* url Both miniprogram and miniprogram are not required fields. If neither field is passed, the template will not jump. If both fields are passed, the template will jump to the applet first.
* Developers can choose one of the jump methods according to actual needs. When the user's version of wechat client does not support skipping applet, it will jump to url.
* </pre>
*/
private String url;
/**
* Data needed for skipping applets, no need for skipping applets to transfer the data
*
* @see #url
*/
private MiniProgram miniProgram;
ok, let's move on
Pay attention to this
You need to match the key of the template you applied for!!!
This is the content of assignment
Color of text
Last step: push
Muddled and forced·········
Click in.Another interface
See the corresponding method
Well, knowing the function of the corresponding method, we can finally push it. But now, I have only one thing that I can read by myself. It's obvious that we're short of information.
It's really a good meal for me
He uses Lambda expression + Stream. I don't need it here. I don't want to be too coquettish
Change the config you wrote, please pay attention to this!!!
/**
* @ClassName WxConfig
* @Author chenghao
* @Date 2020/1/11 09:23
**/
@Configuration
public class WxConfig {
private final WxMpProperties wxMpProperties;
/**
* Structural injection
*
* @param wxMpProperties
*/
WxConfig(WxMpProperties wxMpProperties) {
this.wxMpProperties = wxMpProperties;
}
/**
* Wechat client configuration storage
*
* @return
*/
@Bean
public WxMpConfigStorage wxMpConfigStorage() {
WxMpDefaultConfigImpl configStorage = new WxMpDefaultConfigImpl();
// Public address appId
configStorage.setAppId(wxMpProperties.getAppId());
// Public address appSecret
configStorage.setSecret(wxMpProperties.getSecret());
// Public address Token
configStorage.setToken(wxMpProperties.getToken());
// Public address EncodingAESKey
configStorage.setAesKey(wxMpProperties.getAesKey());
return configStorage;
}
/**
* Declaration instance
*
* @return
*/
@Bean
public WxMpService wxMpService() {
WxMpService wxMpService = new WxMpServiceImpl();
wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
return wxMpService;
}
ok, the main code part is finished. Let's start the test. Please build a Controller by yourself
/**
* Wechat message push
*/
private final WxMsgPush wxMsgPush;
/**
* Structural injection
*/
protected PushMsgApi(WxMsgPush wxMsgPush) {
this.wxMsgPush = wxMsgPush;
}
/** * Send wechat template message */ @ApiOperation("Send wechat template message") @ApiImplicitParams({ @ApiImplicitParam(name = "openId", value = "recipient openId", dataType = "String", paramType = "query") }) @PostMapping("/sendWxInfo") public void sendWxInfo(String openId) { // Execute send Boolean aBoolean = wxMsgPush.SendWxMsg(openId); System.out.println(aBoolean); }
ok! Push complete!! Please write by yourself!!!
(next post a detailed explanation of wechat login)