微信公众号测试号配置 适用于登录注册

news/2024/12/22 2:05:10/

微信小程序注册登录

  • 配置微信公众测试号

配置微信公众测试号

https://mp.weixin.qq.com/cgi-bin/home?t=home/index&lang=zh_CN&token=279116570 进入配置页面

公众页面配置测试账号
2.配置接口配置信息
在这里插入图片描述
token随便找个生成的就行
URL内网穿透的生成的加上你写的签名接口地址进行验证绑定

@RestController
@RequestMapping("/wx/portal")
public class WxPortalController {private final Logger logger = LoggerFactory.getLogger(this.getClass());@Autowiredprivate WxConfig wxConfig;@Autowiredprivate WxMpMessageRouterConfig wxMpMessageRouterConfig;private static final String ENC_TYPE_AES = "aes";/*** 用来进行微信认证,也就是当在:* 公众号官方管理后台->开发->基本配置->修改配置 时,需要进行的校验** @return*/@GetMapping(produces = "text/plain;charset=utf-8")public String authGet(@RequestParam(name = "signature", required = false) String signature,@RequestParam(name = "timestamp", required = false) String timestamp,@RequestParam(name = "nonce", required = false) String nonce,@RequestParam(name = "echostr", required = false) String echostr) {this.logger.info("\n接收到来自微信服务器的认证消息:[{}, {}, {}, {}]", signature,timestamp, nonce, echostr);if (StringUtils.isAnyBlank(signature, timestamp, nonce, echostr)) {// 请求参数非法,请核实throw new IllegalArgumentException("yami.sys.illegalArgument");}if (wxConfig.getWxMpService().checkSignature(timestamp, nonce, signature)) {return echostr;}return echostr;}/*** 接收公众号的消息** @return*/@PostMapping(produces = "application/xml; charset=UTF-8")public String post(@RequestBody String requestBody,@RequestParam("signature") String signature,@RequestParam("timestamp") String timestamp,@RequestParam("nonce") String nonce,@RequestParam("openid") String openid,@RequestParam(name = "encrypt_type", required = false) String encType,@RequestParam(name = "msg_signature", required = false) String msgSignature) {this.logger.info("\n接收微信请求:[openid=[{}], [signature=[{}], encType=[{}], msgSignature=[{}],"+ " timestamp=[{}], nonce=[{}], requestBody=[\n{}\n] ",openid, signature, encType, msgSignature, timestamp, nonce, requestBody);String out = null;if (encType == null) {// 明文传输的消息WxMpXmlMessage inMessage = WxMpXmlMessage.fromXml(requestBody);WxMpXmlOutMessage outMessage = this.route(inMessage);if (outMessage == null) {return "";}out = outMessage.toXml();} else if (ENC_TYPE_AES.equalsIgnoreCase(encType)) {// aes加密的消息WxMpXmlMessage inMessage = WxMpXmlMessage.fromEncryptedXml(requestBody, wxConfig.getWxMpService().getWxMpConfigStorage(),timestamp, nonce, msgSignature);this.logger.debug("\n消息解密后内容为:\n{} ", inMessage.toString());WxMpXmlOutMessage outMessage = this.route(inMessage);if (outMessage == null) {return "";}out = outMessage.toEncryptedXml(wxConfig.getWxMpService().getWxMpConfigStorage());}this.logger.debug("\n组装回复信息:{}", out);return out;}private WxMpXmlOutMessage route(WxMpXmlMessage message) {try {return wxMpMessageRouterConfig.getMessageRouter().route(message);} catch (Exception e) {this.logger.error("路由消息时出现异常!", e);}return null;}/*** 生成二维码** @return url* @throws WxErrorException*/@GetMapping("/create")public String create() {try {WxMpQrcodeService qrcodeService = wxConfig.getWxMpService().getQrcodeService();// 获取二维码ticketWxMpQrCodeTicket wxMpQrCodeTicket = qrcodeService.qrCodeCreateTmpTicket(Constant.SCENEID, 10000);String url = qrcodeService.qrCodePictureUrl(wxMpQrCodeTicket.getTicket(), true);return url;} catch (WxErrorException e) {e.printStackTrace();}return null;}/*** 用于检测扫码和关注状态** @return*/@PostMapping("/checkLogin")public String checkLogin(@RequestBody JSONObject ticketJson) {String ticket = ticketJson.get("ticket").toString();logger.info("ticket---->" + ticket);String openId = null;//如果redis中有ticket凭证则说明用户已扫码说明登陆成功if (RedisUtil.hasKey(ticket)) {openId = RedisUtil.get(ticket).toString();logger.info("openId---->" + openId);//扫码通过则删除RedisUtil.del(ticket);}return openId;}/*** 根据openID获取用户信息** @param openId* @return*/@GetMapping("/getUserInfo")public WxMpUser getUserInfo(String openId) {WxMpUserService userService = wxConfig.getWxMpService().getUserService();try {WxMpUser user = userService.userInfo(openId);return user;} catch (WxErrorException e) {e.printStackTrace();}return null;}
}
/*** 微信路由的配置* @author LGH*/
@Component
@AllArgsConstructor
public class WxMpMessageRouterConfig {private final MenuHandler menuHandler;private final MsgHandler msgHandler;private final SubscribeHandler subscribeHandler;private final ScanHandler scanHandler;private final WxConfig wxConfig;public WxMpMessageRouter getMessageRouter() {WxMpMessageRouter newRouter = new WxMpMessageRouter(wxConfig.getWxMpService());// 自定义菜单事件newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT).event(WxConsts.MenuButtonType.CLICK).handler(this.menuHandler).end();// 关注事件newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT).event(WxConsts.EventType.SUBSCRIBE).handler(this.subscribeHandler).end();// 扫码时已经关注过的事件newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT).event(WxConsts.EventType.SCAN).handler(this.scanHandler).end();// 默认newRouter.rule().async(false).handler(this.msgHandler).end();return newRouter;}}
@Component
public class SubscribeHandler extends AbstractHandler {private final WeixinSubscribeService weixinSubscribeService;@Autowiredpublic SubscribeHandler(WeixinSubscribeService weixinSubscribeService) {this.weixinSubscribeService = weixinSubscribeService;}@Overridepublic WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,Map<String, Object> context, WxMpService weixinService,WxSessionManager sessionManager) throws WxErrorException {this.logger.info("新关注用户 OPENID: " + wxMessage.getFromUser());// 关注时推送的消息String openid = wxMessage.getFromUser();logger.info("WxMpXmlOutMessage.openId-->SubscribeHandler"+openid);// 将ticket 存储进redis 用这个属性来通知前端该用户已经关注了。String ticket = wxMessage.getTicket();RedisUtil.set(ticket,openid,10000);logger.info("WxMpXmlOutMessage.ticket-->SubscribeHandler"+ticket);List<WeixinSubscribe> weixinSubscribes = weixinSubscribeService.listWeixinSubscribeAndMaterial();WxMpKefuService kefuService = weixinService.getKefuService();for (WeixinSubscribe weixinSubscribe : weixinSubscribes) {String msgType = weixinSubscribe.getMsgType();WxMpKefuMessage kefuMessage = new WxMpKefuMessage();kefuMessage.setToUser(openid);kefuMessage.setMsgType(msgType);if (WxConsts.KefuMsgType.TEXT.equals(msgType)) {kefuMessage.setContent(weixinSubscribe.getContent());} else if (WxConsts.KefuMsgType.NEWS.equals(msgType)) {WxMpNewsArticle article = Json.parseObject(weixinSubscribe.getContent(), WxMpNewsArticle.class);WxMpKefuMessage.WxArticle wxArticle = new WxMpKefuMessage.WxArticle();wxArticle.setDescription(article.getDigest());wxArticle.setPicUrl(article.getThumbUrl());wxArticle.setTitle(article.getTitle());wxArticle.setUrl(article.getUrl());kefuMessage.setArticles(Collections.singletonList(wxArticle));} else {kefuMessage.setMediaId(weixinSubscribe.getMediaId());}kefuService.sendKefuMessage(kefuMessage);}return null;}}

3.接口安全域名修改
在这里插入图片描述

4.网页账号配置回调地址
在这里插入图片描述
5.使用natapp或者其他内网穿透工具生成一个随机域名(自行查找)


http://www.ppmy.cn/news/736027.html

相关文章

海外主体企业微信公众号如何开通及认证?

公众平台可以将品牌、企业信息等推送给上亿的微信用户&#xff0c;降低宣传成本&#xff0c;提升知名度。公众号认证认证后&#xff0c;用户将在微信中看到特有的标识&#xff0c;同时公众号将获得更丰富的高级接口&#xff0c;可向用户提供更有价值的个性化服务。 海外主体公…

微信公众号认证法人信息验证失败怎么办?

个体工商户认证公众号时&#xff0c;提示&#xff1a; “企业不存在或企业信息未更新”或“企业法定代表人身份证号码不一致”&#xff0c;法定代表人验证失败&#xff0c;该怎么办呢&#xff1f; 公众号快速认证流程 1.准备资料&#xff1a;个体户营业执照、经营者身份号码、…

没有对公账户怎么开通认证微信公众号?

公众平台主要面向名人、政府、媒体、企业等机构推出的合作推广业务。在这里可以通过微信渠道将品牌推广给上亿的微信用户&#xff0c;减少宣传成本&#xff0c;提高品牌知名度&#xff0c;打造更具影响力的品牌形象。 公众号认证类型 目前可通过认证的类型有政府机关、企业、个…

微信公众帐号测试号申请及配置

测试号的申请不需要注册微信公众号的帐号&#xff0c;只需普通微信号即可。 1. 进入微信公众平台接口测试账号申请 2. 点击登录&#xff0c;然后微信扫码登录 3. 服务端添加验证微信后台访问的逻辑代码 先看下微信的官方文档怎么说&#xff1a; 微信官方文档只给出了PHP的代…

微信公众平台测试帐号申请最新地址

1、打开微信公众平台测试帐号申请地址&#xff1a;https://mp.weixin.qq.com/debug/cgi-bin/sandbox?tsandbox/login 2、通过微信扫一扫授权就能进入到测试号管理页面。 3、测试账号拥有几乎所有的公众号接口&#xff0c;而个人只能申请订阅号&#xff0c;几乎没有接口可用&…

微信公众号开发的测试账号配置

1.首先&#xff0c;你需要一个具有公网IP的域名 例如www.demo.com&#xff0c;它的域名解析的IP是123.123.123.123 2.在微信公众平台登录你的账号 微信公众平台&#xff1a;https://mp.weixin.qq.com 进入 开发->开发者工具 页面 点击 公众平台测试账号 如下图所示&…

微信公众号(服务号)申请及微信认证详细流程图解

微信公众号&#xff08;服务号&#xff09;申请及微信认证详细流程图解 1. 打开电脑浏览器&#xff0c;推荐谷歌浏览器或任何双核浏览器&#xff08;如360、搜狗等&#xff09;极速模式。在地址栏输入网址&#xff1a;https://mp.weixin.qq.com/&#xff0c;打开的页面如图1所…

微信公众平台测试帐号申请网址

我们在开发微信公众账号的时候&#xff0c;微信是提供测试平台给第三方客户使用。 1、申请地址是&#xff1a;https://mp.weixin.qq.com/debug/cgi-bin/sandbox?tsandbox/login&#xff0c;用户需要通过微信扫一扫授权就能进入到测试号管理页面。 2、扫描进入后&#xff0c;可…