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

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

  1. 注册一个自己的微信公众号
  2. 登陆微信公众平台,微信公众平台 - 开发 - 基本配置,开启公众号开发者功能,获取AppID和AppSecret,配置服务器。官网提供的是python的方式配置服务器。
    开启公众开发者功能
//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上面。
    让服务器接收消息
<!--发送到服务器的消息格式为text/xml-->
<xml>
 	<ToUserName><![CDATA[toUser]]></ToUserName>
  	<FromUserName><![CDATA[fromUser]]></FromUserName>
  	<CreateTime>1348831860</CreateTime>
  	<MsgType><![CDATA[text]]></MsgType>
  	<Content><![CDATA[this is a test]]></Content>
  	<MsgId>1234567890123456</MsgId>
  	<MsgDataId>xxxx</MsgDataId>
  	<Idx>xxxx</Idx>
</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><![CDATA[toUser]]></ToUserName>
  	<FromUserName><![CDATA[fromUser]]></FromUserName>
  	<CreateTime>12345678</CreateTime>
  	<MsgType><![CDATA[text]]></MsgType>
  	<Content><![CDATA[你好]]></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 "<xml>" +
                "<ToUserName>" + ToUserName + "</ToUserName>" +
                "<FromUserName>" + FromUserName + "</FromUserName>" +
                "<CreateTime>" + CreateTime + "</CreateTime>" +
                "<MsgType>" + MsgType + "</MsgType>" +
                "<Content>" + Content + "</Content>" +
                "</xml>";
    }
}

  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";
}

在这里插入图片描述

  • 7
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值