公众号小程序初始化
This commit is contained in:
parent
3775d26aa4
commit
81c25fcbfd
@ -43,7 +43,15 @@ public class MobileTestController {
|
|||||||
* 测试微信公众号模板消息发送
|
* 测试微信公众号模板消息发送
|
||||||
*/
|
*/
|
||||||
@GetMapping("/sendOfficialAccountTemplate")
|
@GetMapping("/sendOfficialAccountTemplate")
|
||||||
public void sendOfficialAccountTemplateMessage(PatientVO patientVO) {
|
public void sendOfficialAccountTemplateMessage() {
|
||||||
weChatOfficialAccountUtils.sendOfficialAccountTemplateMessage(patientVO);
|
weChatOfficialAccountUtils.sendOfficialAccountTemplateMessage();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 测试微信小程序模板消息发送
|
||||||
|
*/
|
||||||
|
@GetMapping("/sendAppletTemplate")
|
||||||
|
public void sendAppletTemplate(PatientVO patientVO) {
|
||||||
|
weChatOfficialAccountUtils.sendAppletTemplateMessage(patientVO);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -51,18 +51,14 @@ public class WeChatOfficialAccountUtils {
|
|||||||
*/
|
*/
|
||||||
public String getWeChatOfficialAccountAccessToken() {
|
public String getWeChatOfficialAccountAccessToken() {
|
||||||
String accessToken;
|
String accessToken;
|
||||||
//小程序
|
|
||||||
String accessTokenRedisKey = Constants.NURSE_STATION_APPLET_ACCESS_TOKEN + "accessToken";
|
|
||||||
//公众号
|
//公众号
|
||||||
//String accessTokenRedisKey = Constants.WE_CHAT_OFFICIAL_ACCOUNT_ACCESS_TOKEN + "accessToken";
|
String accessTokenRedisKey = Constants.WE_CHAT_OFFICIAL_ACCOUNT_ACCESS_TOKEN + "accessToken";
|
||||||
//从Redis中取出accessToken
|
//从Redis中取出accessToken
|
||||||
Object object = redisTemplate.opsForValue().get(accessTokenRedisKey);
|
Object object = redisTemplate.opsForValue().get(accessTokenRedisKey);
|
||||||
if (Objects.isNull(object)) {
|
if (Objects.isNull(object)) {
|
||||||
//没有,获取accessToken
|
//没有,获取accessToken
|
||||||
//小程序
|
|
||||||
String accessTokenUrl = Constants.WE_CHAT_ACCESS_TOKEN_URL + "&appid=" + weChatAppletChatConfig.getAppletId() + "&secret=" + weChatAppletChatConfig.getSecret();
|
|
||||||
//公众号
|
//公众号
|
||||||
//String accessTokenUrl = Constants.WE_CHAT_ACCESS_TOKEN_URL + "&appid=" + weChatOfficialAccountConfig.getOfficialAccountAppId() + "&secret=" + weChatOfficialAccountConfig.getOfficialAccountAppSecret();
|
String accessTokenUrl = Constants.WE_CHAT_ACCESS_TOKEN_URL + "&appid=" + weChatOfficialAccountConfig.getOfficialAccountAppId() + "&secret=" + weChatOfficialAccountConfig.getOfficialAccountAppSecret();
|
||||||
//发送请求
|
//发送请求
|
||||||
String result = HttpUtils.sendGet(accessTokenUrl);
|
String result = HttpUtils.sendGet(accessTokenUrl);
|
||||||
if (StringUtils.isBlank(result)) {
|
if (StringUtils.isBlank(result)) {
|
||||||
@ -87,29 +83,118 @@ public class WeChatOfficialAccountUtils {
|
|||||||
return accessToken;
|
return accessToken;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取院后小程序accessToken
|
||||||
|
*
|
||||||
|
* @return 微信小程序的accessToken
|
||||||
|
*/
|
||||||
|
public String getWeChatAppletAccessToken() {
|
||||||
|
String accessToken;
|
||||||
|
//小程序
|
||||||
|
String accessTokenRedisKey = Constants.WE_CHAT_APPLET_ACCESS_TOKEN + "accessToken";
|
||||||
|
//从Redis中取出accessToken
|
||||||
|
Object object = redisTemplate.opsForValue().get(accessTokenRedisKey);
|
||||||
|
if (Objects.isNull(object)) {
|
||||||
|
//没有,获取accessToken
|
||||||
|
//小程序
|
||||||
|
String accessTokenUrl = Constants.WE_CHAT_ACCESS_TOKEN_URL + "&appid=" + weChatAppletChatConfig.getAppletId() + "&secret=" + weChatAppletChatConfig.getSecret();
|
||||||
|
//发送请求
|
||||||
|
String result = HttpUtils.sendGet(accessTokenUrl);
|
||||||
|
if (StringUtils.isBlank(result)) {
|
||||||
|
throw new ServiceException("获取微信小程序accessToken信息失败", 201);
|
||||||
|
}
|
||||||
|
AccessToken officialAccountAccessToken = JSON.parseObject(result, AccessToken.class);
|
||||||
|
if (Objects.isNull(officialAccountAccessToken)) {
|
||||||
|
throw new ServiceException("获取微信小程序accessToken信息失败");
|
||||||
|
}
|
||||||
|
if (Objects.nonNull(officialAccountAccessToken.getErrcode()) && officialAccountAccessToken.getErrcode() != SUCCESS_CODE) {
|
||||||
|
throw new ServiceException("获取微信小程序accessToken信息失败,失败信息为:" + officialAccountAccessToken.getErrmsg(), 201);
|
||||||
|
}
|
||||||
|
if (StringUtils.isBlank(officialAccountAccessToken.getAccessToken())) {
|
||||||
|
throw new ServiceException("微信小程序accessToken信息为空");
|
||||||
|
}
|
||||||
|
accessToken = officialAccountAccessToken.getAccessToken();
|
||||||
|
//存入Redis中
|
||||||
|
redisTemplate.opsForValue().set(accessTokenRedisKey, accessToken, 3600, TimeUnit.SECONDS);
|
||||||
|
} else {
|
||||||
|
accessToken = (String) object;
|
||||||
|
}
|
||||||
|
return accessToken;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 微信公众号模板消息发送
|
* 微信公众号模板消息发送
|
||||||
*/
|
*/
|
||||||
public void sendOfficialAccountTemplateMessage(PatientVO patientVO) {
|
public void sendOfficialAccountTemplateMessage() {
|
||||||
//获取微信公众号的accessToken
|
//获取微信公众号的accessToken
|
||||||
String accessToken = this.getWeChatOfficialAccountAccessToken();
|
String accessToken = this.getWeChatOfficialAccountAccessToken();
|
||||||
//定义模板内容,公众模板内容
|
//定义模板内容,公众模板内容
|
||||||
Map<String, Object> paramsMap = new LinkedHashMap<>();
|
Map<String, Object> paramsMap = new LinkedHashMap<>();
|
||||||
|
paramsMap.put("touser", "oSwvX5qknp3DrAXfBgFjoMvG6WCI");
|
||||||
|
paramsMap.put("template_id", "WUCYtSbH-QFRV_fMcfmn86QLsz1zo881QW7fQNTWOjc");
|
||||||
|
//微信小程序跳转内容
|
||||||
|
Map<String, Object> miniprogramMap = new LinkedHashMap<>();
|
||||||
|
miniprogramMap.put("appid", "wxdc32268eca6b78f9");
|
||||||
|
miniprogramMap.put("pagepath", "pages/startup/startup");
|
||||||
|
//微信小程序模板data内容
|
||||||
|
Map<String, Object> dataMap = new LinkedHashMap<>();
|
||||||
|
dataMap.put("phrase7", new MessageValueEntity("泉医陪护"));
|
||||||
|
dataMap.put("character_string3", new MessageValueEntity("000026315412331612100"));
|
||||||
|
dataMap.put("time6", new MessageValueEntity("2024-03-25 16:21:32"));
|
||||||
|
dataMap.put("time10", new MessageValueEntity("2024-03-27 10:00:00"));
|
||||||
|
dataMap.put("thing11", new MessageValueEntity("济南市槐荫区首诺城市之光东座22楼E10"));
|
||||||
|
paramsMap.put("miniprogram", miniprogramMap);
|
||||||
|
paramsMap.put("data", dataMap);
|
||||||
|
//拼接请求地址并发送
|
||||||
|
String messageUrl = Constants.OFFICIAL_ACCOUNT_TEMPLATE_SEND_URL + accessToken;
|
||||||
|
String param = JSON.toJSONString(paramsMap);
|
||||||
|
String result = HttpUtils.sendPostJson(messageUrl, param);
|
||||||
|
//返回参数映射
|
||||||
|
AccessToken errCode = JSON.parseObject(result, AccessToken.class);
|
||||||
|
if (Objects.nonNull(errCode) && Objects.nonNull(errCode.getErrcode())) {
|
||||||
|
switch (errCode.getErrcode()) {
|
||||||
|
case Constants.SUCCESS_ERROR_CODE:
|
||||||
|
log.info("发送消息成功!");
|
||||||
|
break;
|
||||||
|
case Constants.INVALID_CREDENTIAL_ACCESS_TOKEN_ISINVALID_OR_NOT_LATEST:
|
||||||
|
log.error("取 access_token 时 AppSecret 错误,或者 access_token 无效!");
|
||||||
|
break;
|
||||||
|
case Constants.INVALID_OPENID:
|
||||||
|
log.error("不合法的 OpenId!");
|
||||||
|
break;
|
||||||
|
case Constants.INVALID_ACCESS_TOKEN:
|
||||||
|
log.error("合法的 access_token!");
|
||||||
|
break;
|
||||||
|
case Constants.INVALID_TEMPLATE_ID:
|
||||||
|
log.error("不合法的 template_id!");
|
||||||
|
break;
|
||||||
|
case Constants.ARGUMENT_INVALID:
|
||||||
|
log.error("参数无效!");
|
||||||
|
break;
|
||||||
|
case Constants.API_UNAUTHORIZED:
|
||||||
|
log.error("API 功能未授权!");
|
||||||
|
break;
|
||||||
|
case Constants.DENY_SUBSCRIPTION:
|
||||||
|
log.error("用户拒接订阅!");
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 微信小程序模板消息发送
|
||||||
|
*/
|
||||||
|
public void sendAppletTemplateMessage(PatientVO patientVO) {
|
||||||
|
//获取微信小程序的accessToken
|
||||||
|
String accessToken = this.getWeChatAppletAccessToken();
|
||||||
|
//定义模板内容
|
||||||
|
Map<String, Object> paramsMap = new LinkedHashMap<>();
|
||||||
paramsMap.put("touser", patientVO.getOpenId());
|
paramsMap.put("touser", patientVO.getOpenId());
|
||||||
paramsMap.put("template_id", "S_c9bR4znSWpXg-6ACIMn7AkaR11dzo113XM8w4CKz0");
|
paramsMap.put("template_id", "S_c9bR4znSWpXg-6ACIMn7AkaR11dzo113XM8w4CKz0");
|
||||||
paramsMap.put("page", "/postDischarge/homePage/subscriptionMessage?id =" + patientVO.getSignPatientManageRouteNodeId());
|
paramsMap.put("page", "/postDischarge/homePage/subscriptionMessage?id =" + patientVO.getSignPatientManageRouteNodeId());
|
||||||
//微信小程序跳转内容
|
|
||||||
// Map<String, Object> miniprogramMap = new LinkedHashMap<>();
|
|
||||||
//miniprogramMap.put("appid", "wxdc32268eca6b78f9");
|
|
||||||
// miniprogramMap.put("pagepath", "/postDischarge/homePage/subscriptionMessage?id =" + patientVO.getSignPatientManageRouteNodeId());
|
|
||||||
//微信小程序模板data内容
|
|
||||||
Map<String, Object> dataMap = new LinkedHashMap<>();
|
Map<String, Object> dataMap = new LinkedHashMap<>();
|
||||||
// dataMap.put("phrase7", new MessageValueEntity("泉医陪护"));
|
|
||||||
// dataMap.put("character_string3", new MessageValueEntity("000026315412331612100"));
|
|
||||||
// dataMap.put("time6", new MessageValueEntity("2024-03-25 16:21:32"));
|
|
||||||
// dataMap.put("time10", new MessageValueEntity("2024-03-27 10:00:00"));
|
|
||||||
// dataMap.put("thing11", new MessageValueEntity("济南市槐荫区首诺城市之光东座22楼E10"));
|
|
||||||
//paramsMap.put("miniprogram", miniprogramMap);
|
|
||||||
dataMap.put("thing1", new MessageValueEntity("每日签到"));
|
dataMap.put("thing1", new MessageValueEntity("每日签到"));
|
||||||
dataMap.put("thing2", new MessageValueEntity(LocalDate.now().getYear() + "年" + LocalDate.now().getMonthValue() + "月" + LocalDate.now().getDayOfMonth() + "日签到成功"));
|
dataMap.put("thing2", new MessageValueEntity(LocalDate.now().getYear() + "年" + LocalDate.now().getMonthValue() + "月" + LocalDate.now().getDayOfMonth() + "日签到成功"));
|
||||||
dataMap.put("thing3", new MessageValueEntity(patientVO.getRouteNodeName() + "第" + patientVO.getRouteNodeDay()));
|
dataMap.put("thing3", new MessageValueEntity(patientVO.getRouteNodeName() + "第" + patientVO.getRouteNodeDay()));
|
||||||
|
|||||||
@ -77,7 +77,7 @@ public class subscribeTaskServiceImpl implements SubscribeTaskService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (PatientVO patientVO : patientVOS) {
|
for (PatientVO patientVO : patientVOS) {
|
||||||
weChatOfficialAccountUtils.sendOfficialAccountTemplateMessage(patientVO);
|
weChatOfficialAccountUtils.sendAppletTemplateMessage(patientVO);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user