From 209411ec4fed99a1802c8af1154cc4010ad142e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=86=E6=98=9F=E9=9C=96?= <729219176@qq.com> Date: Sat, 7 Jan 2023 13:15:35 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BE=AE=E4=BF=A1=E6=B6=88=E6=81=AF=E9=80=9A?= =?UTF-8?q?=E7=9F=A5=E7=9B=B8=E5=85=B3=E4=BB=A3=E7=A0=81=E8=A1=A5=E5=85=85?= =?UTF-8?q?=EF=BC=8C=E5=AD=98=E5=9C=A8=E9=97=AE=E9=A2=98=EF=BC=8C=E6=9C=AA?= =?UTF-8?q?=E8=B0=83=E8=AF=95=E9=80=9A=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/ghy/GhyApplication.java | 7 + ghy-admin/src/main/resources/application.yaml | 8 +- ghy-system/pom.xml | 6 + .../com/ghy/system/config/WxMsgConfig.java | 42 ++++ .../com/ghy/system/service/IWxMsgService.java | 24 +++ .../system/service/impl/WxMsgServiceImpl.java | 179 ++++++++++++++++++ 6 files changed, 263 insertions(+), 3 deletions(-) create mode 100644 ghy-system/src/main/java/com/ghy/system/config/WxMsgConfig.java create mode 100644 ghy-system/src/main/java/com/ghy/system/service/IWxMsgService.java create mode 100644 ghy-system/src/main/java/com/ghy/system/service/impl/WxMsgServiceImpl.java diff --git a/ghy-admin/src/main/java/com/ghy/GhyApplication.java b/ghy-admin/src/main/java/com/ghy/GhyApplication.java index 972c2c44..9fe241ad 100644 --- a/ghy-admin/src/main/java/com/ghy/GhyApplication.java +++ b/ghy-admin/src/main/java/com/ghy/GhyApplication.java @@ -3,6 +3,8 @@ package com.ghy; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; 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" + " ''-' `'-' `-..-' "); } + + @Bean + public RestTemplate restTemplate(){ + return new RestTemplate(); + } } \ No newline at end of file diff --git a/ghy-admin/src/main/resources/application.yaml b/ghy-admin/src/main/resources/application.yaml index 1606c6e6..50deb484 100644 --- a/ghy-admin/src/main/resources/application.yaml +++ b/ghy-admin/src/main/resources/application.yaml @@ -59,7 +59,7 @@ spring: time-zone: GMT+8 date-format: yyyy-MM-dd HH:mm:ss profiles: - active: druid + active: dev # 文件上传 servlet: multipart: @@ -104,8 +104,10 @@ swagger: #小程序配置 wx: - appId: 'wxc39c2af3ea24cd37' - secret: '96d566c3169b8715904950d9cf3cbf60' +# appId: 'wxc39c2af3ea24cd37' +# secret: '96d566c3169b8715904950d9cf3cbf60' + appId: 'wx105ce607b514ff2a' + secret: '51df6f2d33bf639fa05891af7a69a56e' # appId: 'wx404f2439a8c24e15' # secret: '49ade04a817067fe2d65ab2f17afce75' diff --git a/ghy-system/pom.xml b/ghy-system/pom.xml index f6635e84..bd5fc6b2 100644 --- a/ghy-system/pom.xml +++ b/ghy-system/pom.xml @@ -23,6 +23,12 @@ ghy-common + + + com.github.binarywang + weixin-java-mp + 3.3.0 + \ No newline at end of file diff --git a/ghy-system/src/main/java/com/ghy/system/config/WxMsgConfig.java b/ghy-system/src/main/java/com/ghy/system/config/WxMsgConfig.java new file mode 100644 index 00000000..639522e0 --- /dev/null +++ b/ghy-system/src/main/java/com/ghy/system/config/WxMsgConfig.java @@ -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; +} diff --git a/ghy-system/src/main/java/com/ghy/system/service/IWxMsgService.java b/ghy-system/src/main/java/com/ghy/system/service/IWxMsgService.java new file mode 100644 index 00000000..d07c01a0 --- /dev/null +++ b/ghy-system/src/main/java/com/ghy/system/service/IWxMsgService.java @@ -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); +} diff --git a/ghy-system/src/main/java/com/ghy/system/service/impl/WxMsgServiceImpl.java b/ghy-system/src/main/java/com/ghy/system/service/impl/WxMsgServiceImpl.java new file mode 100644 index 00000000..47375405 --- /dev/null +++ b/ghy-system/src/main/java/com/ghy/system/service/impl/WxMsgServiceImpl.java @@ -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 params = new HashMap<>(); + params.put("APPID", appId); + params.put("APPSECRET", secret); + ResponseEntity 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 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"; + } + } +}