微信消息通知相关代码补充,存在问题,未调试通。
This commit is contained in:
parent
6dd21f31e7
commit
209411ec4f
|
|
@ -3,6 +3,8 @@ package com.ghy;
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 启动程序
|
* 启动程序
|
||||||
|
|
@ -27,4 +29,9 @@ public class GhyApplication
|
||||||
" | | \\ / \\ / \n" +
|
" | | \\ / \\ / \n" +
|
||||||
" ''-' `'-' `-..-' ");
|
" ''-' `'-' `-..-' ");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public RestTemplate restTemplate(){
|
||||||
|
return new RestTemplate();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -59,7 +59,7 @@ spring:
|
||||||
time-zone: GMT+8
|
time-zone: GMT+8
|
||||||
date-format: yyyy-MM-dd HH:mm:ss
|
date-format: yyyy-MM-dd HH:mm:ss
|
||||||
profiles:
|
profiles:
|
||||||
active: druid
|
active: dev
|
||||||
# 文件上传
|
# 文件上传
|
||||||
servlet:
|
servlet:
|
||||||
multipart:
|
multipart:
|
||||||
|
|
@ -104,8 +104,10 @@ swagger:
|
||||||
|
|
||||||
#小程序配置
|
#小程序配置
|
||||||
wx:
|
wx:
|
||||||
appId: 'wxc39c2af3ea24cd37'
|
# appId: 'wxc39c2af3ea24cd37'
|
||||||
secret: '96d566c3169b8715904950d9cf3cbf60'
|
# secret: '96d566c3169b8715904950d9cf3cbf60'
|
||||||
|
appId: 'wx105ce607b514ff2a'
|
||||||
|
secret: '51df6f2d33bf639fa05891af7a69a56e'
|
||||||
# appId: 'wx404f2439a8c24e15'
|
# appId: 'wx404f2439a8c24e15'
|
||||||
# secret: '49ade04a817067fe2d65ab2f17afce75'
|
# secret: '49ade04a817067fe2d65ab2f17afce75'
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,12 @@
|
||||||
<artifactId>ghy-common</artifactId>
|
<artifactId>ghy-common</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!--微信模版消息推送三方sdk-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.github.binarywang</groupId>
|
||||||
|
<artifactId>weixin-java-mp</artifactId>
|
||||||
|
<version>3.3.0</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|
@ -0,0 +1,42 @@
|
||||||
|
package com.ghy.system.config;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author 但星霖
|
||||||
|
* 微信消息msg 实体
|
||||||
|
* @date 2023-01-06 13:38
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class WxMsgConfig {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 接收者(用户)的 openid
|
||||||
|
*/
|
||||||
|
private String touser;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 所需下发的订阅模板id
|
||||||
|
*/
|
||||||
|
private String template_id;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 点击消息后跳转的页面
|
||||||
|
*/
|
||||||
|
private String page = "index";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;默认为正式版
|
||||||
|
*/
|
||||||
|
private String miniprogram_state = "developer";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 进入小程序查看”的语言类型,支持zh_CN(简体中文)、en_US(英文)、zh_HK(繁体中文)、zh_TW(繁体中文),默认为zh_CN返回值
|
||||||
|
*/
|
||||||
|
private String lang = "zh_CN";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 模板数据,这里定义为object是希望所有的模板都能使用这个消息配置
|
||||||
|
*/
|
||||||
|
private Object data;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,24 @@
|
||||||
|
package com.ghy.system.service;
|
||||||
|
|
||||||
|
import com.ghy.common.enums.OrderStatus;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author 但星霖
|
||||||
|
* 微信消息推送业务接口层
|
||||||
|
* @date 2023-01-06 13:34
|
||||||
|
*/
|
||||||
|
public interface IWxMsgService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 微信消息通知发送
|
||||||
|
*
|
||||||
|
* @param openId openId
|
||||||
|
* @param remind 提醒
|
||||||
|
* @param orderId 订单编号
|
||||||
|
* @param name 姓名
|
||||||
|
* @param orderStatus 订单类型
|
||||||
|
* @param time 时间
|
||||||
|
* @return 是否推送成功
|
||||||
|
*/
|
||||||
|
Boolean sendMsg(String openId, String remind, String orderId, String name, OrderStatus orderStatus, Long time);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,179 @@
|
||||||
|
package com.ghy.system.service.impl;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.ghy.common.enums.OrderStatus;
|
||||||
|
import com.ghy.system.config.WxMsgConfig;
|
||||||
|
import com.ghy.system.service.IWxMsgService;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.http.HttpEntity;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author 但星霖
|
||||||
|
* @date 2023-01-06 14:02
|
||||||
|
* 微信消息推送业务接口实现层
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Service
|
||||||
|
public class WxMsgServiceImpl implements IWxMsgService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RestTemplate restTemplate;
|
||||||
|
|
||||||
|
@Value("${wx.appId}")
|
||||||
|
private String appId;
|
||||||
|
@Value("${wx.secret}")
|
||||||
|
private String secret;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Boolean sendMsg(String openId, String remind, String orderId, String name, OrderStatus orderStatus, Long time) {
|
||||||
|
WxMsgConfig wxMsgConfig = getQRCodeMsgConfig(openId, remind, orderId, name, orderStatus, time);
|
||||||
|
// 通知urL
|
||||||
|
String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + getAccessToken();
|
||||||
|
log.info("微信消息通知请求地址:{}", url);
|
||||||
|
log.info("微信消息通知请求参数:{}", wxMsgConfig);
|
||||||
|
JSONObject responseData = postData(url, wxMsgConfig);
|
||||||
|
log.info("微信消息通知请求参数:{}", JSON.toJSONString(responseData));
|
||||||
|
Integer errorCode = responseData.getInteger("errcode");
|
||||||
|
String errorMessage = responseData.getString("errmsg");
|
||||||
|
if (errorCode == 0) {
|
||||||
|
log.info("用户:" + "openId" + "消息推送成功");
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
log.error("微信消息通知请求参数,errcode:{},errorMessage:{}", errorCode, errorMessage);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取acctoken
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public String getAccessToken() {
|
||||||
|
Map<String, String> params = new HashMap<>();
|
||||||
|
params.put("APPID", appId);
|
||||||
|
params.put("APPSECRET", secret);
|
||||||
|
ResponseEntity<String> responseEntity = restTemplate.getForEntity(
|
||||||
|
"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={APPID}&secret={APPSECRET}", String.class, params);
|
||||||
|
String body = responseEntity.getBody();
|
||||||
|
JSONObject object = JSON.parseObject(body);
|
||||||
|
return object.getString("access_token");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 消息体系组装
|
||||||
|
*
|
||||||
|
* @param openId openId
|
||||||
|
* @param remind 提醒信息
|
||||||
|
* @param orderId 订单ID
|
||||||
|
* @param name 用户姓名
|
||||||
|
* @param orderStatus 订单状态
|
||||||
|
* @param time 时间信息
|
||||||
|
* @return WxMsgConfig实体信息
|
||||||
|
*/
|
||||||
|
private WxMsgConfig getQRCodeMsgConfig(String openId, String remind, String orderId, String name, OrderStatus orderStatus, Long time) {
|
||||||
|
// 基础数据添加。
|
||||||
|
JSONObject jsonObject = new JSONObject();
|
||||||
|
// 订单编号
|
||||||
|
jsonObject.put("character_string22", orderId);
|
||||||
|
// 用户姓名
|
||||||
|
jsonObject.put("thing19", name);
|
||||||
|
// 下单时间
|
||||||
|
jsonObject.put("time1", new Date(time));
|
||||||
|
// 提醒信息
|
||||||
|
jsonObject.put("thing5", remind);
|
||||||
|
/**
|
||||||
|
* 消息推送配置参数拼接
|
||||||
|
*/
|
||||||
|
WxMsgConfig wxMsgConfig = new WxMsgConfig();
|
||||||
|
// openId
|
||||||
|
wxMsgConfig.setTouser(openId);
|
||||||
|
// 模板id
|
||||||
|
wxMsgConfig.setTemplate_id("gFoPAYF4J6Y_O5OzzNUbvMxTrsAS7aUWffdNH42xhzM");
|
||||||
|
// 数据data
|
||||||
|
wxMsgConfig.setData(jsonObject);
|
||||||
|
return wxMsgConfig;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送请求
|
||||||
|
*
|
||||||
|
* @param url http 推送url
|
||||||
|
* @param param 消息体系信息
|
||||||
|
* @return JSONObject实体信息
|
||||||
|
*/
|
||||||
|
public JSONObject postData(String url, WxMsgConfig param) {
|
||||||
|
MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
|
||||||
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
headers.setContentType(type);
|
||||||
|
HttpEntity<WxMsgConfig> httpEntity = new HttpEntity<>(param, headers);
|
||||||
|
return restTemplate.postForObject(url, httpEntity, JSONObject.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取对应模板id
|
||||||
|
*
|
||||||
|
* @param orderStatus 订单状态
|
||||||
|
* @return 模板ID
|
||||||
|
*/
|
||||||
|
private String templateIdGet(OrderStatus orderStatus) {
|
||||||
|
/**
|
||||||
|
* GOING_CANCEL(-2, "待上门时申请取消"),
|
||||||
|
* SERVER_CANCEL(-3, "服务中申请取消"),
|
||||||
|
* FINISH_CHECK_CANCEL(-4, "待确认时申请取消"),
|
||||||
|
* RECEIVE(0, "待接单"),
|
||||||
|
* PLAIN(1, "待排期"),
|
||||||
|
* GOING(2, "待上门"),
|
||||||
|
* SERVER(3, "服务中"),
|
||||||
|
* FINISH_CHECK(4, "待确认"),
|
||||||
|
* FINISH(5, "已完成"),
|
||||||
|
* CANCEL(6, "已取消");
|
||||||
|
*/
|
||||||
|
switch (orderStatus) {
|
||||||
|
case GOING_CANCEL:
|
||||||
|
// 待上门时申请取消
|
||||||
|
return "12";
|
||||||
|
case SERVER_CANCEL:
|
||||||
|
// 服务中申请取消
|
||||||
|
return "12";
|
||||||
|
case FINISH_CHECK_CANCEL:
|
||||||
|
// 待确认时申请取消
|
||||||
|
return "12";
|
||||||
|
case RECEIVE:
|
||||||
|
// 待接单
|
||||||
|
return "12";
|
||||||
|
case PLAIN:
|
||||||
|
// 待排期
|
||||||
|
return "12";
|
||||||
|
case GOING:
|
||||||
|
// 待上门
|
||||||
|
return "12";
|
||||||
|
case SERVER:
|
||||||
|
// 服务中
|
||||||
|
return "12";
|
||||||
|
case FINISH_CHECK:
|
||||||
|
// 待确认
|
||||||
|
return "12";
|
||||||
|
case FINISH:
|
||||||
|
// 已完成
|
||||||
|
return "12";
|
||||||
|
default:
|
||||||
|
// 已取消
|
||||||
|
return "2";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue