微信公眾號初次開發
- 其他操作
- 專案搭建
- maven
- application.yml
- yml引數配置
- 微信客戶端配置
- 搭建完成 實作業務
- 模板訊息推送
- 準備作業
- 模板訊息填寫要求
- 圖文訊息填寫要求
- 推送模板訊息
- 推送模板訊息結果——效果
- 推送圖文訊息
- 問題和結果
- 獲取關注的用戶openId
- 既然有了openId集合和要發送的訊息怎么群發
其他操作
書接上文:微信測驗號申請步驟和除錯.
書接下文:后臺獲取授權用戶的openId.
專案搭建
maven
<dependencies>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.13</version>
</dependency>
<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</artifactId>
<version>1.6.0</version>
</dependency>
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.8</version>
</dependency>
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>3.6.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.61</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8.1</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-autoconfigure</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>3.1.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.9</version>
</dependency>
</dependencies>
application.yml
################### 專案啟動埠 ###################
server:
port: 80
# 微信公眾號配置
wx:
#微信公眾號APPID
appid: 123
#微信公眾號SECRET
secret: 123
#微信公眾號模板ID
templateId: 123
yml引數配置
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* 微信引數
**/
@Data
@Component
@ConfigurationProperties(prefix = "wx")
public class WxMpProperties {
/**
* 公眾號appId
*/
private String appId;
/**
* 公眾號appSecret
*/
private String secret;
/**
* 公眾號模板ID
*/
private String templateId;
}
微信客戶端配置
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 微信配置
**/
@Configuration
public class WxConfig {
@Autowired
WxMpProperties wxMpProperties;
/**
* 構造注入
*
* @param wxMpProperties
*/
WxConfig(WxMpProperties wxMpProperties) {
this.wxMpProperties = wxMpProperties;
}
/**
* 微信客戶端配置存盤
*
* @return
*/
@Bean
public WxMpConfigStorage wxMpConfigStorage() {
WxMpDefaultConfigImpl configStorage = new WxMpDefaultConfigImpl();
// 公眾號appId
configStorage.setAppId(wxMpProperties.getAppId());
// 公眾號appSecret
configStorage.setSecret(wxMpProperties.getSecret());
// 公眾號Token
configStorage.setToken(wxMpProperties.getToken());
// 公眾號EncodingAESKey
configStorage.setAesKey(wxMpProperties.getAesKey());
return configStorage;
}
/**
* 宣告實體
*
* @return
*/
@Bean
public WxMpService wxMpService() {
WxMpService wxMpService = new WxMpServiceImpl();
wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
return wxMpService;
}
}
搭建完成 實作業務
模板訊息推送
準備作業

鏈接: 申請測驗模板.
模板訊息填寫要求
first.DATA 跟上.DATA說明是資料
人員:{{first.DATA}}
地址:{{keyword1.DATA}}
區域:{{keyword2.DATA}}
告警型別:{{remark1.DATA}}
時間:{{remark2.DATA}}
圖文訊息填寫要求
<xml>
<Articles>
<item>
<Title><![CDATA[title1]]></Title>
<Description><![CDATA[description1]]></Description>
<PicUrl><![CDATA[picurl]]></PicUrl>
<Url><![CDATA[url]]></Url>
</item>
<item>
<Title><![CDATA[title]]></Title>
<Description><![CDATA[description]]></Description>
<PicUrl><![CDATA[picurl]]></PicUrl>
<Url><![CDATA[url]]></Url>
</item>
</Articles>
</xml>
推送模板訊息
這里使用微信提供的serviceApi
鏈接: WxMpService.
import com.shangqu.datapush.buildsite.model.emnus.AlarmType;
import com.shangqu.datapush.buildsite.model.mysqlData.AlarmInfo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpAiOpenService;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.WxMpUserService;
import me.chanjar.weixin.mp.bean.result.WxMpUserList;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
/**
* 微信訊息推送
*
**/
@Slf4j
@Component
@CrossOrigin
public class WxMsgPush {
/**
* 微信公眾號API的Service
*/
private final WxMpService wxMpService;
/**
* 構造注入
*/
WxMsgPush(WxMpService wxMpService) {
this.wxMpService = wxMpService;
}
/**
* 發送微信模板資訊
*
* @param openId 接受者openId
* @return 是否推送成功
*/
public Boolean SendWxMsg(String openId,String templateId) {
// 發送模板訊息介面
WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
// 接收者openid
.toUser(openId)
// 模板id
.templateId(templateId)
// 模板跳轉鏈接
.url("www.baidu.com")
.build();
// 添加模板資料
templateMessage.addData(new WxMpTemplateData("first", "null告警""#000000"))
.addData(new WxMpTemplateData("keyword1", "","#000000"))
.addData(new WxMpTemplateData("keyword2","", "#000000"))
.addData(new WxMpTemplateData("keyword3", "", "#000000"))
.addData(new WxMpTemplateData("keyword4", "", "#000000"));
String msgId = null;
try {
// 發送模板訊息
msgId = wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
} catch (WxErrorException e) {
e.printStackTrace();
}
log.warn("·==++--·推送微信模板資訊:{}·--++==·", msgId != null ? "成功" : "失敗");
return msgId != null;
}
}
推送模板訊息結果——效果

點擊詳情會進入百度
推送圖文訊息
/**
* 獲取token
* @return
*/
public String getAccessToken() throws WxErrorException {
String accessToken = wxMpService.getAccessToken();
return accessToken;
}
@ApiOperation(value = "", notes = "")
@ResponseBody
@RequestMapping(value = "/findwx", method = RequestMethod.POST)
public void SendQYMessageimg(@RequestParam("id") String id) throws WxErrorException {
String Url = "http://www.baidu.com";
String mp = getAccessToken();
String strurl = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + mp;
try {
//圖片地址
String PicUrl = "https://img-blog.csdn.net/20160907155302548";
String responeJsonStr = "{";
//用戶openId
responeJsonStr += "\"touser\": \"" + "用戶openId" + "\",";
responeJsonStr += "\"msgtype\": \"news\",";
responeJsonStr += "\"agentid\": \"wxd4aff8****\",";
responeJsonStr += "\"news\": {";
responeJsonStr += "\"articles\": [{";
responeJsonStr += " \"title\": \"" + "震驚外國一小伙竟然做出... ..." + "\",";
responeJsonStr += " \"description\": \" " + "今日一美國男子在家中,竟然做出... ..." + "\",";
responeJsonStr += " \"url\": \"" + Url + "\",";
responeJsonStr += " \"picurl\": \"" + PicUrl + "\"";
responeJsonStr += "}]";
responeJsonStr += "},";
responeJsonStr += "\"safe\":\"0\"";
responeJsonStr += "}";
xstream.connectWeiXinInterface(strurl, responeJsonStr);
} catch (Exception e) {
e.printStackTrace();
}
}
問題和結果
要用戶先發送一條訊息給公眾號,你才能發送成功

點擊訊息會進入百度
獲取關注的用戶openId
/**
* 獲得關注公眾號所有openid
* @return
* @throws WxErrorException
*/
@Override
public List<String> getUserList() throws WxErrorException {
WxMpUserList wxMpUserList = wxMpService.getUserService().userList(null);
List<String> openids = wxMpUserList.getOpenids();
return openids;
}
既然有了openId集合和要發送的訊息怎么群發
微信并沒有提供群發介面–就在自己代碼中迭代openId集合去回圈發送
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/245649.html
標籤:其他
上一篇:2020年的總結和2021的目標
下一篇:企業架構培訓感悟
