实现微信公众号自动回复验证码功能

官网开发文档:https://developers.weixin.qq.com/doc/offiaccount/Getting_Started/Getting_Started_Guide.html
不过上面开始的简单服务器搭建都是python做的。

  1. 注册一个自己的微信公众号
  2. 登陆微信公众平台,微信公众平台 - 开发 - 基本配置,开启公众号开发者功能,获取AppID和AppSecret,配置服务器。官网提供的是python的方式配置服务器。
    实现微信公众号自动回复验证码功能_第1张图片
//java开发的后端直接return接收的echostr字符串
@GetMapping("/weixin")
public String checkSignature(String echostr,String signature,Long timestamp,Long nonce){
	System.out.println("echostr: " + echostr);
	System.out.println("signature: " + signature);
	System.out.println("timestamp: " + timestamp);
	System.out.println("nonce: " + nonce);
	return echostr;
}

  1. 开启服务接收消息的功能。开启功能之后,给公众发送的消息会议下面服务器地址post请求的方式将xml消息数据包填写在url上面。
    实现微信公众号自动回复验证码功能_第2张图片

<xml>
 	<ToUserName>ToUserName>
  	<FromUserName>FromUserName>
  	<CreateTime>1348831860CreateTime>
  	<MsgType>MsgType>
  	<Content>Content>
  	<MsgId>1234567890123456MsgId>
  	<MsgDataId>xxxxMsgDataId>
  	<Idx>xxxxIdx>
xml>

参数 官方文档有介绍 描述
ToUserName 开发者微信号
FromUserName 发送方帐号(一个OpenID)
CreateTime 消息创建时间 (整型)
MsgType 消息类型,文本为text
Content 文本消息内容
MsgId 消息id,64位整型
MsgDataId 消息的数据ID(消息如果来自文章时才有)
Idx 多图文时第几篇文章,从1开始(消息如果来自文章时才有)
//实体类接收消息
package com.test.verification_code.pojo;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.Serializable;
@Data
@XmlRootElement(name = "xml")
public class ReceiveMessage implements Serializable {

    private static final long serialVersionUID = 1L;

    @XmlElement(name = "ToUserName")
    private String ToUserName;

    @XmlElement(name = "FromUserName")
    private String FromUserName;

    @XmlElement(name = "CreateTime")
    private Long CreateTime;

    @XmlElement(name = "MsgType")
    private String MsgType;

    @XmlElement(name = "Content")
    private String Content;

    @XmlElement(name = "MsgId")
    private Long MsgId;

    @XmlElement(name = "MsgDataId")
    private Long MsgDataId;

    @XmlElement(name = "Idx")
    private Long Idx;
}

  1. 被动回复消息实体类准备

<xml>
  	<ToUserName>ToUserName>
  	<FromUserName>FromUserName>
  	<CreateTime>12345678CreateTime>
  	<MsgType>MsgType>
  	<Content>Content>
xml>

参数 是否必须 描述
ToUserName 接收方帐号(收到的OpenID)
FromUserName 开发者微信号
CreateTime 消息创建时间 (整型)
MsgType 消息类型,文本为text
Content 回复的消息内容(换行:在 content 中能够换行,微信客户端就支持换行显示)
//回复消息的实体类,可以修改以下toString方法格式为xml的格式即可
@XmlRootElement(name = "xml")
public class SendMessage implements Serializable {

    private static final long serialVersionUID = 1L;

    @XmlElement(name = "ToUserName")
    private String ToUserName;

    @XmlElement(name = "FromUserName")
    private String FromUserName;

    @XmlElement(name = "CreateTime")
    private Long CreateTime;

    @XmlElement(name = "MsgType")
    private String MsgType;

    @XmlElement(name = "Content")
    private String Content;

    @Override
    public String toString() {
        return "" +
                "" + ToUserName + "" +
                "" + FromUserName + "" +
                "" + CreateTime + "" +
                "" + MsgType + "" +
                "" + Content + "" +
                "";
    }
}

  1. service准备,用redis缓存验证码,设置过期时间5分钟
@Service
public class CodeServiceImpl implements CodeService {

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    public String getCode(String key){

        Object result = redisTemplate.opsForValue().get(key);

        if (result == null){
            return null;
        }
        return result.toString();
    }

    public void createCode(String key,Object code){
        redisTemplate.opsForValue().set(key,code,5, TimeUnit.MINUTES);
    }
}

  1. controller处理接收的消息,被动回复消息
//注意接收的数据类型为text/xml,不是application/xml,如果写成了application/xml会出现下图错误。
@PostMapping(value = "/weixin",consumes = "text/xml",produces = "text/xml")
public String getMessage(@RequestBody ReceiveMessage receiveMessage){
	System.out.println(receiveMessage);
	if (receiveMessage.getContent().equals("验证码")){

		String code = codeService.getCode(receiveMessage.getToUserName());
		if (code == null){
			code = CodeUtils.getCode(4);
			codeService.createCode(receiveMessage.getToUserName(),code);
		}
		System.out.println("验证码:" + code);
		SendMessage sendMessage = new SendMessage();
		sendMessage.setContent("验证码是: " + code + ",有效期5分钟");
		sendMessage.setCreateTime(receiveMessage.getCreateTime());
		sendMessage.setFromUserName(receiveMessage.getToUserName());
		sendMessage.setToUserName(receiveMessage.getFromUserName());
		sendMessage.setMsgType("text");
		return sendMessage.toString();
	}
	return "success";
}

在这里插入图片描述

你可能感兴趣的:(微信)