微信订阅号点击菜单栏获取用户信息

26 篇文章 0 订阅
20 篇文章 0 订阅

一、业务场景:点击订阅号菜单栏,跳转到一个页面,在页面中需要识别用户身份

二、背景:

有的认证订阅号有网页授权获取用户基本信息

有的认证订阅号没有网页授权获取用户基本信息

认证服务号是有这个接口权限的,所以用认证服务号开发,不用担心这个接口权限问题


三、功能实现后,用户点击菜单栏,在跳转到页面之前会有一个授权登录页面,这个与拥有网页授权接口的实现,在体验度上有区别




1、要解决这个问题需要用到微信开放平台https://open.weixin.qq.com/



这里同样需要认证,认证费300每年

2、注册号微信开放平台账号后,并认证,绑定自己的微信订阅号,创建网页应用,等待网页应用审核



创建网页应用时有一个回调url,填写就是订阅号点击菜单栏需要跳转页面所在的域名



在同一个微信开发者账号内,创建的网页应用,绑定的公众号,用户unionid是相同的,也就是通

unionid来判断用户身份


如果开发者拥有多个移动应用、网站应用和公众帐号,可通过获取用户基本信息中的unionid来区分用户的唯一性,
因为只要是同一个微信开放平台帐号下的移动应用、网站应用和公众帐号,用户的unionid是唯一的。换句话说,
同一用户,对同一个微信开放平台下的不同应用,unionid是相同的。


3、绑定好订阅号后,再次通过openid获取用户信息时就会拿到unionid,在订阅号没有绑定开放平台之前是获取不到这个值的。网页应用审核通过后,获取appid 和 APPSECRET ,记得查看到APPSECRET就保存好,之后看还要扫码,很麻烦



4、准备好前面两项工作后就可以开发了


5、自定义菜单栏(view)类型,url按照以下规则填写

String url ="https://open.weixin.qq.com/connect/oauth2/authorize?appid="+开放平台网页应用APPID
+"&redirect_uri="+开放平台网页应用回调域名下的某个路径+"&response_type=code&scope=snsapi_login
&state=123#wechat_redirect";




6、测试

①网页应用的回调url填的:www.test.com

②菜单栏设置的url是:String url ="https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=http://www.test.com/me/getInfor.htm&response_type=code&scope=snsapi_login&state=123#wechat_redirect";



7、自定义菜单创建完成之后,代码开发如下

@Controller
@RequestMapping("me")
public class MeController {

	@RequestMapping("getInfor")
	public String getInfor(HttpServletRequest req) {
		String CODE =  req.getParameter("code");
		String state =  req.getParameter("state");
		String accessTokenURL = "https://api.weixin.qq.com/sns/oauth2/access_token?"
				+ "appid="+开放平台APPID+"&secret="+开放平台APPSECRET+"&code=CODE&"
						+ "grant_type=authorization_code"; 
	    JSONObject jsonObject=HttpRequestUtil.httpRequest(accessTokenURL.
	    		replaceAll("CODE", code), "GET", null);
        boolean containsValue = jsonObject.containsKey("errcode");
		if(containsValue){
			String errcode = jsonObject.getString("errcode");
			System.err.println("有错误");
		}else{
			String unionid = jsonObject.getString("unionid");
		}
		
		return "pc/user-manage/";
	}

获取到unionid,之后就可以和公众号用户的 unionid进行匹配,从而获取用户订阅号openid,然后获取用户详细信息。所以订阅号的用户信息需要提前做好数据库保存工作。


8、相关方法

public static JSONObject httpRequest(String requestUrl, String requestMethod, String outputStr) {
		
		JSONObject jsonObject = null;
		StringBuffer buffer = new StringBuffer();
		try {
			// 创建SSLContext对象,并使用我们指定的信任管理器初始化
			TrustManager[] tm = { new MyX509TrustManager() };
			SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
			sslContext.init(null, tm, new java.security.SecureRandom());
			// 从上述SSLContext对象中得到SSLSocketFactory对象
			SSLSocketFactory ssf = sslContext.getSocketFactory();

			URL url = new URL(requestUrl);
			HttpsURLConnection httpUrlConn = (HttpsURLConnection) url.openConnection();
			httpUrlConn.setSSLSocketFactory(ssf);

			httpUrlConn.setDoOutput(true);
			httpUrlConn.setDoInput(true);
			httpUrlConn.setUseCaches(false);
			// 设置请求方式(GET/POST)
			httpUrlConn.setRequestMethod(requestMethod);

			if ("GET".equalsIgnoreCase(requestMethod))
				httpUrlConn.connect();

			// 当有数据需要提交时
			if (null != outputStr) {
				OutputStream outputStream = httpUrlConn.getOutputStream();
				// 注意编码格式,防止中文乱码
				outputStream.write(outputStr.getBytes("UTF-8"));
				outputStream.close();
			}

			// 将返回的输入流转换成字符串
			InputStream inputStream = httpUrlConn.getInputStream();
			InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
			BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

			String str = null;
			while ((str = bufferedReader.readLine()) != null) {
				buffer.append(str);
			}
			bufferedReader.close();
			inputStreamReader.close();
			// 释放资源
			inputStream.close();
			inputStream = null;
			httpUrlConn.disconnect();
			jsonObject = JSONObject.fromObject(buffer.toString());
			//System.out.println("jsonObject="+jsonObject);
		} catch (ConnectException ce) {
			ce.printStackTrace();
			System.out.println("网络链接失败!");
		}catch (UnknownHostException uhe) {
			uhe.printStackTrace();
			System.out.println("微信API无法访问....!");
			//httpRequest(requestUrl, requestMethod, outputStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jsonObject;
	}


9、点击菜单栏,进入登录界面,成功截图




  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值