微信访问网页时判断微信用户是否关注公众号

一、开放平台与公众平台

微信公众平台:
https://mp.weixin.qq.com/cgi-bin/loginpage
微信开放平台:
https://open.weixin.qq.com/
微信官方文档:https://developers.weixin.qq.com/doc/offiaccount/Getting_Started/Overview.html
两者的区别:
开放平台是网站或app使用的接口平台,利用开放平台可在自己的网站或app上开发微信帐户登录、微信分享等功能!
公众平台是微信号的一种,也具有开发功能,是在公众号中开发出更多功能,例如微网站等!
简单来讲,公众平台的开发功能是在微信平台的基础上的,而开放平台是在你自己的平台上开发的与微信相关的一些功能。

备注:本文开发内容是微信公众平台的内容,涉及官方文档如下:
获取全局token
https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html
网页授权
https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html
获取用户基本信息(UnionID机制)
https://developers.weixin.qq.com/doc/offiaccount/User_Management/Get_users_basic_information_UnionID.html#UinonId

二、 微信公众平台配置

个人学习时可申请测试帐号:
https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Requesting_an_API_Test_Account.html
注意:公众平台接口调用仅支持80端口,且项目支持外网访问,个人学习时可在本地配置内网穿透,参考https://blog.csdn.net/qq_37718403/article/details/106093162

1. 获取appid, appsecret,添加白名单

正式帐号

前提:正式帐号接入微信公众平台的配置参考
https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Access_Overview.html
官方描述:
在这里插入图片描述

AppID和AppSecret可在“微信公众平台-开发-基本配置”页中获得(需要已经成为开发者,且帐号没有异常状态)(appSecret只展示一次,需保存下来,否则需要重置获取)。
获取access_token时需要添加IP白名单。
在这里插入图片描述
点击查看
在这里插入图片描述

点击修改在这里插入图片描述

测试帐号

登录测试帐号
在这里插入图片描述

2. 添加网页

正式帐号

官方描述:
在这里插入图片描述
进入公众号设置=》功能设置=》网页授权域名
clipboard.png

点击设置,input框中输入授权回调页的域名参考第1点(只能填写一个),下载第3点中的txt文档,上传至服务器的根目录。
clipboard.png

测试帐号

点击【网页授权获取用户基本信息】的修改,将域名添加进去
在这里插入图片描述
在这里插入图片描述

三、java后端实现

微信开放接口全局返回码说明参考:https://mp.weixin.qq.com/wiki…

源码

WxController

package com.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.ParseException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.util.WeiXinUtil;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/wx")
public class WxController {
   
	public static final String APPID="";
    public static final String APPSECRET ="";
    
    private static final Logger logger = Logger.getLogger(WxController.class);
	
	@RequestMapping(value = "/wxLogin", method = RequestMethod.GET)
	public String wxLogin(HttpServletRequest request, HttpServletResponse response)throws ParseException, UnsupportedEncodingException {
   
		//这个地址是成功后的回调地址,域名必须和公众号中配置的域名一致
		String backUrl="http://xiayehuimou.free.idcfengye.com/ssm_demo/wx/callBack";
		// 第一步:用户同意授权,获取code
		String url ="https://open.weixin.qq.com/connect/oauth2/authorize?appid="+ APPID
					+ "&redirect_uri="
					+URLEncoder.encode(backUrl, "utf-8")
					+ "&response_type=code"
					+ "&scope=snsapi_userinfo"
					+ "&state=STATE#wechat_redirect";
		logger.info("forward重定向地址{" + url + "}");
		return "redirect:" + url ;
	}
	
	@RequestMapping(value = "/callBack", method = RequestMethod.GET)
	public String callBack(ModelMap modelMap,HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
   
		String code =req.getParameter("code");
		//第二步:通过code换取网页授权access_token
		String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="+APPID
				+ "&secret="+APPSECRET
				+ "&code="+code
				+ "&grant_type=authorization_code";
		System.out.println("url:"+url);
		JSONObject jsonObject = WeiXinUtil.httpsRequest(url, "GET", null);
		String openid = jsonObject.getString("openid");
		String access_token = jsonObject.getString("access_token");
		String refresh_token = jsonObject.getString("refresh_token");
		//第五步验证access_token是否失效;展示都不需要
		Stri
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值