用户点击一个微信菜单的时候,可以发送一个消息,也可以打开一个页面链接,下面说页面链接的情况
如果微信菜单打开这个url的话:http://www.netsharp.org/nav/weixin/index?originalId=gh_cd4d099926d5
微信要求url必须配置成这样的:https://open.weixin.qq.com/connect/oauth2/authorize?from=weixin&appid=wx19a3e7936ff4ec13&redirect_uri=http%3A%2F%2Fwww.netsharp.org%2Fnav%2Faddins%2Fweixin%2Findex%3ForiginalId%3Dgh_cd4d099926d5&response_type=code&scope=snsapi_base&state=#wechat_redirect
其中:redirect_uri部分是开发人员真正想让客户看到的链接,而且需要编码
微信打开菜单以后,页面先跳转到微信的oauth认证页,然后再跳转到业务页面,首先netsharp会自动把"originalId"参数传递过去,在业务页面如果想获取openId则通过oauth认证页给业务页面追加的一个参数“code”获取,netsharp把根据code获取粉丝信息给封装了,参见业务页面的代码:
<%@page language="java" contentType="text/html; charset=utf-8" pageEncoding="UTF-8"%> <%@page import="org.netsharp.wx.pa.base.IFansService" %> <%@page import="org.netsharp.communication.ServiceFactory" %> <%@page import="org.netsharp.wx.pa.entity.Fans" %> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width"> <meta name="format-detection" content="telephone=no" /> <title>oauth测试页面</title> </head> <body > <% String originalId = (String)request.getParameter("originalId"); response.getWriter().write("originalId:"+originalId+"<br/>"); String code = (String)request.getParameter("code"); response.getWriter().write("code:"+code+"<br/>"); //oauth中必须根据code才能得到粉丝信息 IFansService fansService = ServiceFactory.create(IFansService.class); Fans fans = fansService.attachByOpenId(code, originalId); response.getWriter().write("openId:"+fans.getOpenId()+"<br/>"); response.getWriter().write("nickname:"+fans.getNickname()+"<br/>"); response.getWriter().write("img:<img src=‘"+fans.getHeadImgUrl()+"‘ style=‘width:60px;height:60px‘ /><br/>"); %> </body> </html>
测试页的效果如下:
时间: 2024-10-11 16:43:43