微信公众号开发

微信官方文档参考地址: https://developers.weixin.qq.com/doc/offiaccount/Getting_Started/Overview.html

1、微信公众号平台基础设置

1-1、白名单配置

在这里插入图片描述

1-2、回调接口配置

服务器地址为后端服务部署接口地址,令牌,消息加密秘钥这个自己生成,对数据进行加密需要

2、后台服务接口

2-1、公众号二维码生成接口

**注意说明:**当前接口主要是生成临时二维码,调用腾讯服务器获取公众号二维码并将企业logo合成到二维码中。

2-1-1、controller代码逻辑
 @GetMapping("qrcode")
    public Response login(HttpServletResponse response, HttpServletRequest request){
        return wxMpManager.qrcode();
    }
2-1-2、service接口
public Response qrcode() {
      Map<String,Object> showQrcodeMap = new HashMap();
        String accessToken = getAccessToken();
        String url = String.format(PUBLICACCOUNT_QRCODE_URL,accessToken);
        Map<String,Object> paramMap = new HashMap();
        paramMap.put("expire_seconds",604800);
        paramMap.put("action_name","QR_SCENE");
        Map<String,Object> sceneMap = new HashMap();
        Map<String,Object> sceneMap2 = new HashMap();
        sceneMap2.put("scene_id",IdGen.uuid());
        sceneMap.put("scene",sceneMap2);
        paramMap.put("action_info",sceneMap);
        HttpEntity httpEntity = new HttpEntity<>(new Gson().toJson(paramMap),new HttpHeaders());
        ResponseEntity<String> response  = restTemplate.postForEntity(url,httpEntity,String.class);
        JSONObject jsonObject =new JSONObject(response.getBody());
        log.info("临时二维码获取ticket,获取json:{}",jsonObject);
        String qrCodeUrl = jsonObject.getString("url");
        if(StringUtils.isEmpty(qrCodeUrl)){
            log.error("临时二维码获取ticket,获取json:{}",jsonObject);
            return ResponseUtils.returnCommonException("二维码获取失败");
        }
        try(ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            InputStream inputStream = getClass().getResourceAsStream(JIANMO_LOG_PATH)){
            BufferedImage bufferedImage =QRCodeUtil.createImage(qrCodeUrl,inputStream , false, QR_CODE_SIZE);
            ImageIO.write(bufferedImage,"jpg",outputStream);
            String content = Base64.getEncoder().encodeToString(outputStream.toByteArray());
            showQrcodeMap.put("showQrcodeUrl", "data:image/jpeg;base64,"+content);
            showQrcodeMap.put("ticket",jsonObject.getString("ticket"));
        } catch (IOException e) {
            log.error(e.getMessage(),e);
        } catch (Exception e) {
           log.error(e.getMessage(),e);
        }
        return ResponseUtils.returnObjectSuccess(showQrcodeMap);
    }

2-2、微信回调接口

2-2-1、微信公众号平台配置回调校验接口

说明: 验证回调接口参数是否合法,如果合法直接返回入参中的echostr,这样公众平台中的回调校验才能正常通过。

@RequestMapping(method ={RequestMethod.GET,RequestMethod.POST},value ="login" )
    public String login(PublicAccountCallDTO publicAccountCallDTO,HttpServletRequest request,HttpServletResponse response) {
        log.info("publicAccountCallDTO={}",publicAccountCallDTO);
        boolean flag= wxMpManager.getSignature(publicAccountCallDTO);
         return flag ? publicAccountCallDTO.getEchostr():"";
    }
    public boolean getSignature(PublicAccountCallDTO publicAccountCallDTO) {
        if(StringUtils.isEmpty(publicAccountCallDTO.getSignature())){
            return false;
        }
        String[] array = new String[]{wxpayConfig.getWxOpenToken() , publicAccountCallDTO.getTimestamp() , publicAccountCallDTO.getNonce()};
        StringBuilder sb = new StringBuilder();
        // 字符串排序
        Arrays.sort(array);
        for (int i = 0; i < 3; i++) {
            sb.append(array[i]);
        }
        String signature = DigestUtils.sha1Hex(sb.toString());
        return signature.equals(publicAccountCallDTO.getSignature());
    }
2-2-2、controller业务逻辑代码接口

注意说明: 当前接口响应公众平台回调,所以返回格式为xml文件格式,为了避免返回数据乱码,此处需要二外配置produces = “application/xml; charset=UTF-8”

 @RequestMapping(method ={RequestMethod.POST},value ="login",produces =  "application/xml; charset=UTF-8")
    public String login( @RequestBody String requestBody,PublicAccountCallDTO publicAccountCallDTO,HttpServletRequest request,
                         HttpServletResponse response) {
        log.info("body={},publicAccountCallDTO={},paramMap={}",requestBody,publicAccountCallDTO,new Gson().toJson(request.getParameterMap()));

        boolean flag= wxMpManager.getSignature(publicAccountCallDTO);
        if(!flag){
            log.error("非法回调,body={},publicAccountCallDTO={}",requestBody,publicAccountCallDTO);
            return "";
        }
        wxMpManager.updateUser(requestBody,publicAccountCallDTO.getOpenid(), request);

        WxMpXmlMessage inMessage = WxMpXmlMessage.fromXml(requestBody);
        WxMpXmlOutMessage outMessage = wxMpMessageRouter.route(inMessage);
        if(outMessage!=null) {
            log.info("输出xml数据,{}",outMessage.toXml());
            return outMessage.toXml();
        }
        return "";
    }

2-3、微信公众号扫码关注客户端消息推送

2-3-1、公众号关注消息处理
@Component
@Slf4j
public class WxMpSubscribeHandler implements WxMpMessageHandler {
    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService wxMpService,
                                    WxSessionManager sessionManager) throws WxErrorException {
        log.info("wxMessage={}",wxMessage);
        return WxMpXmlOutMessage.TEXT().
                fromUser(wxMessage.getToUser()).
                toUser(wxMessage.getFromUser()).
                content("欢迎关注,用户登录成功").
                build();
    }
}
2-3-2、公众号取消关注消息处理
@Component
@Slf4j
public class WxMpUnSubscribeHandler implements WxMpMessageHandler {
    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService wxMpService,
                                    WxSessionManager sessionManager) throws WxErrorException {
        log.info("wxMessage={}",wxMessage);
        return WxMpXmlOutMessage.TEXT().
                fromUser(wxMessage.getToUser()).
                toUser(wxMessage.getFromUser()).
                content("请别离开我").
                build();
    }
}
2-3-3、公众号扫码场景消息处理
@Component
@Slf4j
public class WxMpScanHandler implements WxMpMessageHandler {
    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService wxMpService,
                                    WxSessionManager sessionManager) throws WxErrorException {
        log.info("绑定系统用户成功,wxMessage={}",wxMessage);
        return WxMpXmlOutMessage.TEXT().
                content("用户登录成功!").
                fromUser(wxMessage.getToUser()).
                toUser(wxMessage.getFromUser()).
                build();
    }
}

2-4、消息路由


@Configuration
public class WeChartConfig {

    @Resource
    private WxMpSubscribeHandler wxMpSubscribeHandler;

    @Resource
    private WxMpUnSubscribeHandler wxUnSubscribeHandler;

    @Resource
    private WxMpScanHandler wxMpScanHandler;
    @Resource
    private WeChartWxMpService weChartWxMpService;

    @Bean
    public WxMpMessageRouter wxMpMessageRouter(){
        WxMpMessageRouter router = new WxMpMessageRouter(weChartWxMpService);
        router.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT).event(WxConsts.EventType.SUBSCRIBE)
                .handler(wxMpSubscribeHandler).end();
        router.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT).event(WxConsts.EventType.UNSUBSCRIBE)
                .handler(wxUnSubscribeHandler).end();
        router.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT).event(WxConsts.EventType.SCAN)
                .handler(wxMpScanHandler).end();
        return router;
    }
}

公众号平台参考文档:
1、https://developers.weixin.qq.com/doc/offiaccount/Account_Management/Generating_a_Parametric_QR_Code.html
2、https://developers.weixin.qq.com/doc/offiaccount/User_Management/Get_users_basic_information_UnionID.html#UinonId

公众号接口参考信息:
//获取accessToken
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=&secret=
//临时二维码获取ticket
https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=%s
//公众号获取用户信息接口
https://api.weixin.qq.com/cgi-bin/user/info?access_token=%s&openid=%s&lang=zh_CN

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

山不在高_有仙则灵

你的奖励是我的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值