https://github.com/Wechat-Group/WxJava 使用记录

GITHUB地址

https://github.com/Wechat-Group/WxJava

微信公众号开发

<dependency>
    <groupId>com.github.binarywang</groupId>
    <artifactId>weixin-java-mp</artifactId>
    <version>4.0.0</version>
</dependency>            
  • 配置yml
    wx:
      mp:
        useRedis: false
        redisConfig:
          host: 127.0.0.1
          port: 6379
        configs:
          - appId: "******************"
            secret: "*****************"
            token: "*****************"
            aesKey: "*****************"
  • 将demo相关代码直接搬到项目代码中
    • WxPortalController控制器为接通公众号开发设置的接口
      @RequestMapping("/wx/portal/{appid}")
      public class WxPortalController {
      .............................
      .............................

  • 公众号开发配置,服务器URL就是刚才WxPortalController的接口,验证消息来自微信,接通公众号。也是公众号交互,事件推送的唯一入口。
  • 用户关注和取关
    • 参考使用demo代码的SubscribeHandler 和 UnsubscribeHandler,正常直接搬过来实现业务逻辑就可以。不熟悉的话,各种事件可以通过直接观察日志打印来定位不同的handle文件位置,demo代码在各种事件里都有打印可以识别
      关注时可以获取 openid Unionid等信息
      log.debug("\n新关注用户:{}", userWxInfo);
                      MpUser mpUser = new MpUser();
                      mpUser.setOpenId(userWxInfo.getOpenId());
                      mpUser.setUnionId(userWxInfo.getUnionId());
                      mpUser.setStatus("1");
                      mpUserService.subscribe(mpUser);

  • 根据关注事件获取到的用户openid去发送模板消息
    • 文档https://github.com/Wechat-Group/WxJava/wiki/%E5%85%AC%E4%BC%97%E5%8F%B7%E5%BC%80%E5%8F%91%E6%96%87%E6%A1%A3
      
      依赖注入WxMpService(千万不能通过直接new wxPmService的方式,否则会报空指针异常)
      @Autowired
          private WxMpService wxMpService;
      
      
      WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                          .toUser("ozHsF6u1JE8buR1ANBiRv2-Qjwxc").templateId("aOSPkPH7NLibxo5gUKHZOPT4yMZwcU7Rs9-hIERRf2M").url("http://www.baidu.com").build();
                  templateMessage.addData(new WxMpTemplateData("first", "您好,亲爱的小明家长,请查阅宝宝今日在园情况", "#173177"));
                  templateMessage.addData(new WxMpTemplateData("keyword1", "托管1班", "#173177"));
                  templateMessage.addData(new WxMpTemplateData("keyword2", "2021年5月24日", "#173177"));
                  templateMessage.addData(new WxMpTemplateData("keyword3","今天宝宝在幼儿园表现还不错,但是中午吃饭时有点挑食不喜欢吃西红柿,希望家长在家也关注一下", "#173177"));
                  templateMessage.addData(new WxMpTemplateData("remark", "备注信息", "#173177"));
                  wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);

微信小程序开发

  • 引入小程序与引入公众号方式一样,就不累赘了,这里直接记录微信小程序开发中敏感词汇的审核
package com.xxyw.tgxmapi.common.utils;

import cn.binarywang.wx.miniapp.api.WxMaSecCheckService;
import com.google.gson.JsonObject;
import com.xxyw.tgxmapi.common.config.WxMaConfiguration;
import com.xxyw.tgxmapi.common.config.WxMaProperties;
import com.xxyw.tgxmapi.common.exception.CommonException;
import lombok.Data;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.service.WxService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Data
@Component
public class WxCheckMsgImg {
    @Autowired
    private WxMaProperties wxMaProperties;

    private static WxCheckMsgImg wxCheckMsgImg;

    @PostConstruct
    public void init(){
        wxCheckMsgImg = this;
        wxCheckMsgImg.wxMaProperties = wxMaProperties;
    }

    // fileUrls为逗号隔开的字符串
    public static void checkMsgImg( String msg, String fileUrls) throws WxErrorException {
        String appid = wxCheckMsgImg.wxMaProperties.getConfigs().get(0).getAppid();
        // 检测文字
        if(StringUtils.isNotBlank(msg)){
            WxService wxService = WxMaConfiguration.getMaService(appid);
            try{
                JsonObject jsonObject = new JsonObject();
                jsonObject.addProperty("content", msg);
                wxService.post("https://api.weixin.qq.com/wxa/msg_sec_check", jsonObject.toString());
            }catch (WxErrorException e){
                System.out.println("检测失败");
                System.out.println(e);
                throw new CommonException("您发布的内容含有敏感词汇");
            }
        }

        // 检测图片
//        if(StringUtils.isNotBlank(fileUrls)){
//            String[] fileUrlArr = fileUrls.split(",");
//            WxMaSecCheckService wxMaSecCheckService = WxMaConfiguration.getMaService(appid).getSecCheckService();
//            for (int i =0; i<fileUrlArr.length;i++){
//                try{
//                    wxMaSecCheckService.checkImage(fileUrlArr[i]);
//                }catch (WxErrorException e){
//                    System.out.println("检测失败");
//                    System.out.println(e);
//                    throw new CommonException("您发布的图片含有违规信息");
//                }
//
//            }
//        }


    }
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值