Browse Source

【项目开发】

1.增加代码示例
ChenYL 9 months ago
parent
commit
f51b3f6ba6

+ 1 - 0
.idea/misc.xml

@@ -7,6 +7,7 @@
         <option value="$PROJECT_DIR$/pom.xml" />
         <option value="$PROJECT_DIR$/pom.xml" />
       </list>
       </list>
     </option>
     </option>
+    <option name="workspaceImportForciblyTurnedOn" value="true" />
   </component>
   </component>
   <component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="21" project-jdk-type="JavaSDK">
   <component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="21" project-jdk-type="JavaSDK">
     <output url="file://$PROJECT_DIR$/out" />
     <output url="file://$PROJECT_DIR$/out" />

+ 24 - 0
data-easy/src/main/java/com/dataeasy/server/WeChatMsgPushConfig.java

@@ -0,0 +1,24 @@
+package com.dataeasy.server;
+
+import lombok.Data;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.stereotype.Component; /**
+ * @author myou
+ * @version 1.0.0
+ * @date 2025/3/1 21:35
+ * @description TODO
+ */
+@Data
+@Component
+@ConfigurationProperties(prefix = "wechat")
+public class WeChatMsgPushConfig {
+    /**
+     * 设置要取用的公众号模板
+     */
+    @Value("${wechat.templateId}")
+    private String templateId;
+    //颜色 、url我没配,你要是配了自己写上
+
+
+}

+ 61 - 0
data-easy/src/main/java/com/dataeasy/server/WeChatMsgPushUtil.java

@@ -0,0 +1,61 @@
+package com.dataeasy.server;
+
+import lombok.extern.slf4j.Slf4j;
+import me.chanjar.weixin.common.error.WxErrorException;
+import me.chanjar.weixin.mp.api.WxMpService;
+import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
+import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component; /**
+ * @author myou
+ * @version 1.0.0
+ * @date 2025/3/1 21:41
+ * @description TODO
+ */
+@Slf4j
+@Component
+public class WeChatMsgPushUtil {
+    @Autowired
+    private WxMpService wxMpService;
+    @Autowired
+    private WeChatMsgPushConfig weChatMsgPushConfig; //这个是什么配置的模板类
+
+    /**
+     * 对用户发送消息的接口
+     * @param tradeName 公司名称
+     * @param subscribeTime 预约时间
+     * @param subscribeNum 预约数量
+     * @param Kind 预约种类
+     * @return boolean
+     */
+    public Boolean sendOrderMsg(String tradeName,
+                                String subscribeTime,
+                                String subscribeNum,
+                                String Kind,
+                                String openId,
+                                String urlRel) {
+
+
+        WxMpTemplateMessage templateMessage=WxMpTemplateMessage.builder()
+                .toUser(openId) //这个openid是你要推送的人,如果你不知道怎么拿用户的openid可以来往上面看看
+                .templateId(weChatMsgPushConfig.getTemplateId())//这里是你要发送消息的模板id
+                .url(urlRel)//这里是你的配置的url 例如:www.baidu.com,
+                .build();
+        templateMessage
+                .addData(new WxMpTemplateData(WxMsgSendUserUtil.FIRST,WxMsgSendUserUtil.FIRST_CONTENT,这里你要是刚才配置了文字的颜色导入这个color,下面同理))
+                .addData(new WxMpTemplateData(WxMsgSendUserUtil.KEYWORD_ONE,tradeName))
+                .addData(new WxMpTemplateData(WxMsgSendUserUtil.KEYWORD_TWO,subscribeTime))
+                .addData(new WxMpTemplateData(WxMsgSendUserUtil.KEYWORD_THREE,subscribeNum))
+                .addData(new WxMpTemplateData(WxMsgSendUserUtil.KEYWORD_FOUR,Kind))
+                .addData(new WxMpTemplateData(WxMsgSendUserUtil.REMARK,WxMsgSendUserUtil.REMARK_CONTENT));
+
+        String msgPush=null;
+        try {
+            msgPush = wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
+        } catch (WxErrorException e) {
+            throw new RuntimeException(e);
+        }
+        log.warn("·==++--·推送微信模板信息:{}·--++==·", msgPush != null ? "成功" : "失败");
+        return msgPush!=null;
+    }
+}

+ 108 - 0
data-easy/src/main/java/com/dataeasy/server/WxMpConfiguration.java

@@ -0,0 +1,108 @@
+package com.dataeasy.server;
+
+import lombok.AllArgsConstructor;
+import me.chanjar.weixin.mp.api.WxMpMessageRouter;
+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;
+
+import static me.chanjar.weixin.mp.constant.WxMpEventConstants.CustomerService.KF_CLOSE_SESSION;
+import static me.chanjar.weixin.mp.constant.WxMpEventConstants.CustomerService.KF_CREATE_SESSION;
+import static me.chanjar.weixin.mp.constant.WxMpEventConstants.CustomerService.KF_SWITCH_SESSION; /**
+ * @author myou
+ * @version 1.0.0
+ * @date 2025/3/1 21:36
+ * @description TODO
+ */
+@AllArgsConstructor
+@Configuration
+public class WxMpConfiguration {
+//    @Autowired
+//    private ImgHandler imgHandler;
+//    @Autowired
+//    private TextMsgHandler textMsgHandler;
+//    @Autowired
+//    private KfSessionHandler kfSessionHandler;
+//    @Autowired
+//    private MenuHandler menuHandler;
+//    @Autowired
+//    private ScanHandler scanHandler;
+//    @Autowired
+//    private MsgHandler msgHandler;
+//    @Autowired
+//    private LogHandler logHandler;
+//    @Autowired
+//    private WxMpProperties wxMpProperties;
+//    @Autowired
+//    private SubscribeHandler subscribeHandler;
+//    @Autowired
+//    private UnsubscribeHandler unsubscribeHandler;
+
+//    @Bean
+//    public WxMpService wxMpService() {
+//        WxMpServiceImpl wxMpService = new WxMpServiceImpl();
+//        wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
+//        // 设置多个微信公众号的配置
+//        // wxMpService.setMultiConfigStorages();
+//        return wxMpService;
+//    }
+
+    /**
+     * 这个地方的配置是保存在本地,生产环境需要自己扩展,可以保存在Redis中等等
+     *
+     * @return WxMpConfigStorage
+     */
+//    public WxMpConfigStorage wxMpConfigStorage() {
+//        WxMpDefaultConfigImpl storage = new WxMpDefaultConfigImpl();
+//        storage.setAppId(wxMpProperties.getAppId());
+//        storage.setSecret(wxMpProperties.getSecret());
+//        storage.setAesKey(wxMpProperties.getAesKey());
+//        storage.setToken(wxMpProperties.getToken());
+//        return storage;
+//    }
+
+    @Bean
+    public WxMpMessageRouter messageRouter(WxMpService wxMpService) {
+        final WxMpMessageRouter newRouter = new WxMpMessageRouter(wxMpService);
+
+//        // 记录所有事件的日志 (异步执行)
+//        newRouter.rule().handler(this.logHandler).next();
+//
+//        // 接收客服会话管理事件
+//        newRouter.rule().async(false).msgType(EVENT).event(KF_CREATE_SESSION)
+//                .handler(this.kfSessionHandler).end();
+//        newRouter.rule().async(false).msgType(EVENT).event(KF_CLOSE_SESSION)
+//                .handler(this.kfSessionHandler).end();
+//        newRouter.rule().async(false).msgType(EVENT).event(KF_SWITCH_SESSION)
+//                .handler(this.kfSessionHandler).end();
+//
+//
+//        // 自定义菜单事件
+//        newRouter.rule().async(false).msgType(EVENT).event(EventType.CLICK).handler(this.menuHandler).end();
+//
+//        // 关注事件
+//        newRouter.rule().async(false).msgType(EVENT).event(SUBSCRIBE).handler(this.subscribeHandler).end();
+//
+//        // 取消关注事件
+//        newRouter.rule().async(false).msgType(EVENT).event(UNSUBSCRIBE).handler(this.unsubscribeHandler).end();
+//
+//        // 扫码事件
+//        newRouter.rule().async(false).msgType(EVENT).event(EventType.SCAN).handler(this.scanHandler).end();
+//
+//        // 文本消息处理
+//        newRouter.rule().async(false).msgType(XmlMsgType.TEXT).handler(this.textMsgHandler).end();
+//
+//        // 图片消息处理
+//        newRouter.rule().async(false).msgType(XmlMsgType.IMAGE).handler(this.imgHandler).end();
+//
+//        // 默认
+//        newRouter.rule().async(false).handler(this.msgHandler).end();
+
+        return newRouter;
+    }
+
+}

+ 39 - 8
data-easy/src/main/resources/application-dev.yaml

@@ -13,16 +13,17 @@ biz:
     password: 0123456789
     password: 0123456789
 
 
 logging:
 logging:
-  level: debug
+  level:
+    me.chanjar.weixin.mp: debug
 
 
 # 公众号配置(必填)
 # 公众号配置(必填)
-wx:
-  mp:
-    app-id: wx076677ddbd348bbc
-    secret: 87972dca143f21348ff65c7ac19be1cc
-    token: Csy2001
-    aes-key: 123424
-    use-stable-access-token: false
+#wx:
+#  mp:
+#    app-id: wx076677ddbd348bbc
+#    secret: 87972dca143f21348ff65c7ac19be1cc
+#    token: Csy2001
+#    aes-key: 123424
+#    use-stable-access-token: false
 #  wx.mp.app-id=appId
 #  wx.mp.app-id=appId
 #  wx.mp.secret=@secret
 #  wx.mp.secret=@secret
 #  wx.mp.token=@token
 #  wx.mp.token=@token
@@ -33,3 +34,33 @@ wx:
 #
 #
 #
 #
 #  aesKey: rdDwt7K448074aL7Mt6QwOBPYCwIHqSaN
 #  aesKey: rdDwt7K448074aL7Mt6QwOBPYCwIHqSaN
+# 公众号配置(必填)
+wx:
+  mp:
+    app-id: wx076677ddbd348bbc
+    secret: 87972dca143f21348ff65c7ac19be1cc
+    token: Csy2001
+#    aes-key: "@aesKey"
+#    use-stable-access-token: true
+#    config-storage:
+#      type: edis
+#      key-prefix: wx
+#      redis:
+#        host: 127.0.0.1
+#        port: 6379
+        # sentinel-ips: "127.0.0.1:16379,127.0.0.1:26379"
+        # sentinel-name: mymaster
+#      http-client-type: httpclient
+#      http-proxy-host: ""
+#      http-proxy-port: ""
+#      http-proxy-username: ""
+#      http-proxy-password: ""
+    # hosts:
+    #   api-host: "http://proxy.com/"
+    #   open-host: "http://proxy.com/"
+    #   mp-host: "http://proxy.com/"
+
+wechat:
+  templateId: Ckf2pycVvLM-238QGp-YvG_MTPuvYGM8jVsyH8IF7_c
+#  color: 这个可以不配就是文字的颜色你想要骚一点就找16进制的就可以了默认为黑色
+#  url:  这个可以不配点击模板跳转的url