四、搭建企业级微信公众号

目录

1、公众号路径

2、搭建步骤

2.1 参考路径

2.2 外网映射工具

2.3 项目配置


1、公众号路径

微信公众号平台:

https://mp.weixin.qq.com/cgi-bin/home?t=home/index&lang=zh_CN&token=795093844w

微信公众号测试平台:

https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login

账号密码:新微信号

2、搭建步骤

2.1 参考路径

https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421135319

2.2 外网映射工具

https://natapp.cn/

windows环境运行

运行   natapp -authtoken=a021a8fe913ea048 

http://mtmayikt.natapp1.cc/wx/portal/ wx6f8ce0ccadf3afff

微信公众号框架  https://github.com/Wechat-Group/WxJava

2.3 项目配置

pom.xml

<dependency>
			<groupId>com.github.binarywang</groupId>
			<artifactId>weixin-java-mp</artifactId>
			<version>3.3.0</version>
			<exclusions>
				<exclusion>
					<artifactId>xstream</artifactId>
					<groupId>com.thoughtworks.xstream</groupId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>
				spring-cloud-starter-netflix-eureka-client
			</artifactId>
			<exclusions>
				<exclusion>
					<artifactId>xstream</artifactId>
					<groupId>com.thoughtworks.xstream</groupId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<artifactId>xstream</artifactId>
			<groupId>com.thoughtworks.xstream</groupId>
			<version>1.4.10</version>
		</dependency>

application.yml

logging:
  level:
    org.springframework.web: INFO
    com.github.binarywang.demo.wx.mp: DEBUG
    me.chanjar.weixin: DEBUG
wx:
  mp:
    configs:
      - appId: wx6f8ce0ccadf3afff #(一个公众号的appid)
        secret: cf2b0b4908a84d92b38b939a6fbabe09#(公众号的appsecret)
        token: mayikt #(接口配置里的Token值)

mayikt: 
  weixin: 
    registration:
       code: 
       ###微信注册码消息
        message:  您的注册码为:registrationCodeMessage,请关注<a href="https://ke.qq.com/course/273548">腾讯课堂免费公开课</a>,欢迎观看97后架构师余老师的精品课程讲解。官方QQ群:<a href='https://jq.qq.com/?_wv=1027&k=5TVfAMF'>193086273</a>,期待你的加入,感谢!
    ###默认提示消息
    default: 
      registration:
        code: 
          message: 您的消息,我们已经收到,会及时回复给您的!

微信公众号项目demo

@Component
public class MsgHandler extends AbstractHandler {
	// 用户发送手机验证码提示
	@Value("${mayikt.weixin.registration.code.message}")
	private String registrationCodeMessage;
	// 默认用户发送验证码提示
	@Value("${mayikt.weixin.default.registration.code.message}")
	private String defaultRegistrationCodeMessage;

	@Override
	public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService weixinService,
			WxSessionManager sessionManager) {

		if (!wxMessage.getMsgType().equals(XmlMsgType.EVENT)) {
			// TODO 可以选择将消息保存到本地
		}

		// 当用户输入关键词如“你好”,“客服”等,并且有客服在线时,把消息转发给在线客服
		try {
			if (StringUtils.startsWithAny(wxMessage.getContent(), "你好", "客服")
					&& weixinService.getKefuService().kfOnlineList().getKfOnlineList().size() > 0) {
				return WxMpXmlOutMessage.TRANSFER_CUSTOMER_SERVICE().fromUser(wxMessage.getToUser())
						.toUser(wxMessage.getFromUser()).build();
			}
		} catch (WxErrorException e) {
			e.printStackTrace();
		}

		// TODO 组装回复消息
		// 1.验证关键字是否为手机号码类型
		String fromMsg = wxMessage.getContent();

		if (RegexUtils.checkMobile(fromMsg)) {
			// 如果发送消息为手机号码类型,则发送短信验证码
			int registCode = registCode();
			String retContext = registrationCodeMessage.replaceAll("registrationCodeMessage", registCode + "");
			return new TextBuilder().build(retContext, wxMessage, weixinService);

		}
		return new TextBuilder().build(defaultRegistrationCodeMessage, wxMessage, weixinService);

	}

	// 获取注册码
	private int registCode() {
		int registCode = (int) (Math.random() * 9000 + 1000);
		return registCode;
	}

}

关注公众号,在公众号里输入手机,返回验证码,之后验证码放入redis

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值