Java实现微信公众号扫描二维码未关注时跳转关注界面已关注跳转业务界面

以下只是测试代码写得不太整齐规范,但是主要思路以及要注意的都写了:

前提:需要申请认证的微信公众号;获取对应的APPID和APPSECRET;并且还需要获取到用户信息权限(点击“修改“添加服务器的域名地址),前期工作安装测试账号为例给大家展示下:

1)、公众测试账号获取

  访问上面的连接,选择“接口测试号申请”获得直接打开http://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index通过微信客户端扫码登录即可登录。

  登录完即可获取到一个测试公众账号的信息。主要有appId和appsecret两个参数,这将唯一标示一个公众号,并且需要将他们作为参数获取用户的信息。

2)、关注公众号

   用户只有关注了这个公众号了,才能通过打开有公众号信息的链接去授权第三方登录,并获取用户信息的操作。故我们还需要用我们的微信关注微信号,操作如下:
  还是刚刚那个登录成功后跳转的页面,我们可以看到,该页面有一个二维码,我们可以通过扫描该二维码进行关注,关注成功在右边的“用户列表”会多一个用户的信息。如下图所示:

3)配置回调函数
  我们在微信客户端访问第三方网页(即我们自己的网页)的时候,我们可以通过微信网页授权机制,我们不仅要有前面获取到的appid和appsecret还需要有当用户授权之后,回调的域名设置,即用户授权后,页面会跳转到哪里。具体的配置如下:
  还是在刚刚的页面,有一个“网页授权获取用户基本信息”,点击后面的修改

填写回调的域名:

生产环境上配置回调的域名地方

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

在微信公众号请求用户网页授权之前,开发者需要先到公众平台官网中的“开发 - 接口权限 - 网页服务 - 网页帐号 - 网页授权获取用户基本信息”的配置选项中,修改授权回调域名。请注意,这里填写的是域名(是一个字符串),而不是URL,因此请勿加 http:// 等协议头;

如果你的网址没有被列入过黑名单,就会在顶部出现

然后,域名配置就成功了!可以进行开发了。

一。授权开发的流程(详情的东西请以官网为准,在此就不多说了):具体而言,网页授权流程分为四步:

1、引导用户进入授权页面同意授权,获取code

2、通过code换取网页授权access_token(与基础支持中的access_token不同)

3、如果需要,开发者可以刷新网页授权access_token,避免过期

4、通过网页授权access_token和openid获取用户基本信息(支持UnionID机制)

注意:微信静默授权与非静默授权所拿到OpenId与关注微信公众号后所拿到的OpenID是一致的,但是与微信开放平台的扫码登录中OpenId是不一样的,此时可以通过UnionId来做业务上关联。

       同时,通过BIZ跳转到微信关注页面方法,已经被微信所屏蔽。只能通过微信内部跳转才可以,如:推送消息点击连接跳到关注页,或是通过微信扫-扫扫码方式都可以。

二.按照如上流程我就不多说废话直接粘代码供大家参考,请大家多多指教(代码里的所有APPID,APPSECRET都是使用的官网提供测试账号)

1.设计一个公用网络请求工具类:

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.springframework.util.DigestUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class WXAuthUtil {
    public static final String APPID="wx9b39273dce55dc2f";
    public static final String APPSECRET ="06f175852111045f81ebf62db28a44a0";
    private static final String TOKEN = "immco";
    public static JSONObject doGetJson(String url) throws ClientProtocolException, IOException {
        JSONObject jsonObject =null;
        DefaultHttpClient client = new DefaultHttpClient();
        HttpGet httpGet =new HttpGet(url);
        HttpResponse response =  client.execute(httpGet);
        HttpEntity entity =response.getEntity();
        if(entity!=null)
        {
            //把返回的结果转换为JSON对象
            String result =EntityUtils.toString(entity, "UTF-8");
            jsonObject =JSON.parseObject(result);
        }
        
        return jsonObject;
    }
   
}
 

2.微信登录的实现类(里面包含引导action和确认登录后的回调函数):

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.text.ParseException;
import java.util.UUID;


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


import org.apache.http.client.ClientProtocolException;
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.alibaba.fastjson.JSONObject;
import com.wxutil.auth.SHA1;
import com.wxutil.auth.WXAuthUtil;



/** 
* 
*
* @parameter  
* @since  
* @return  
*/


@Controller
@RequestMapping("/wx")
public class WXLoginController {
    private static final Logger logger = Logger.getLogger(WXLoginController.class);
/**
 * 公众号微信登录授权
 * @param request
 * @param response
 * @return
 * @throws ParseException
 * @author  lbh 
 * @date 创建时间:2018年1月18日 下午7:33:59  
 * @parameter
 */
    @RequestMapping(value = "/wxLogin", method = RequestMethod.GET)
    public String wxLogin(HttpServletRequest request,
            HttpServletResponse response)
            throws ParseException {
        //这个url的域名必须要进行再公众号中进行注册验证,这个地址是成功后的回调地址
        String backUrl="http://d84e26b6.ngrok.io/WX_auth/wx/callBack";
        // 第一步:用户同意授权,获取code
        String url ="https://open.weixin.qq.com/connect/oauth2/authorize?appid="+WXAuthUtil.APPID
                + "&redirect_uri="+URLEncoder.encode(backUrl)
                + "&response_type=code"
                + "&scope=snsapi_userinfo"
                + "&state=STATE#wechat_redirect";

        logger.info("forward重定向地址{" + url + "}");
        //response.sendRedirect(url);
        return "redirect:"+url;//必须重定向,否则不能成功
    }
/**
 * 公众号微信登录授权回调函数
 * @param modelMap
 * @param req
 * @param resp
 * @return
 * @throws ServletException
 * @throws IOException  
 * @parameter
 */
    @RequestMapping(value = "/callBack", method = RequestMethod.GET)
    public String callBack(ModelMap modelMap,HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
    	/*
    	 * start 获取微信用户基本信息
    	 */
    	String code =req.getParameter("code");
    	//第二步:通过code换取网页授权access_token
    	String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="+WX_APPID
    	+ "&secret="+WX_APP_SECRET
    	+ "&code="+code
    	+ "&grant_type=authorization_code";
    	System.out.println("url:"+url);
    	JSONObject jsonObject = JSONObject.parseObject(HttpUtil.getRequestWx(url));
    	/*
         { "access_token":"ACCESS_TOKEN",
         "expires_in":7200,
         "refresh_token":"REFRESH_TOKEN",
         "openid":"OPENID",
         "scope":"SCOPE" 
        }
         */
        String openid = jsonObject.getString("openid");
        String access_token = jsonObject.getString("access_token");
        String refresh_token = jsonObject.getString("refresh_token");
        //第五步验证access_token是否失效;展示都不需要
        String chickUrl="https://api.weixin.qq.com/sns/auth?access_token="+access_token+"&openid="+openid;

        JSONObject chickuserInfo = JSONObject.parseObject(HttpUtil.getRequestWx(chickUrl));
        System.out.println(chickuserInfo.toString());
        if(!"0".equals(chickuserInfo.getString("errcode"))){
             // 第三步:刷新access_token(如果需要)-----暂时没有使用,参考文档https://mp.weixin.qq.com/wiki,
             String refreshTokenUrl="https://api.weixin.qq.com/sns/oauth2/refresh_token?appid="+openid+"&grant_type=refresh_token&refresh_token="+refresh_token;

             JSONObject refreshInfo = JSONObject.parseObject(HttpUtil.getRequestWx(chickUrl));
             /*
            * { "access_token":"ACCESS_TOKEN",
            "expires_in":7200,
            "refresh_token":"REFRESH_TOKEN",
            "openid":"OPENID",
            "scope":"SCOPE" }
            */
             System.out.println(refreshInfo.toString());
             access_token=refreshInfo.getString("access_token");
        }

        // 第四步:判断OpenID是否已经存在数据库(关注过)
        boolean exitUser = false;
        //response.sendRedirect("https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz="+BIZ+"==&scene=110#wechat_redirect");
//
        String redirectUrl = "https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz="
                +biz
                +"&chksm==&scene=0#wechat_redirect";
        if (StringUtils.isNotBlank(openid)) {
            UserToWechatModel uwm = userToWechatService.selectInfoByOpenId(openid);
            if (uwm != null && StringUtils.isNotBlank(uwm.getSubscribe()) && "1".equals(uwm.getSubscribe())) {
                exitUser = true;
                /*redirectUrl="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxc8dd52122e8e6700&redirect_uri=" +
                        "https%3A%2F%2Fwechat.yimidida.com%2Fgalaxy-wechat-business%2Fwechat%2FopenidToOrder" +
                        "&response_type=code&scope=snsapi_base&state=1&connect_redirect=1#wechat_redirect";*/
                redirectUrl="https://wechat-test.yimidida.com/galaxy-wechat-business/wechat/openidToOrder?openid="+openid;
                return "redirect:https://www.baidu.com" ; //必须重定向,否则不能成功
            }


            //String gzh = request.getParameter("gzh");
            String gzh = "";//公众号名字
            String user_agent = req.getHeader("user-agent");

            redirectUrl = "http://mp.weixin.qq.com/mp/getmasssendmsg?__biz=" + biz
                    + "#wechat_webview_type=1&wechat_redirect#rd";

            if (user_agent.indexOf("MicroMessenger")>=0 &&
                    user_agent.indexOf("iPhone")>=0 ) {
                resp.sendRedirect(redirectUrl);
                return null;
            }


            //此方法已被微信封掉
         /*   if (user_agent.indexOf("MicroMessenger")>=0 &&
                    user_agent.indexOf("Android")>=0 ) {
                resp.sendRedirect("weixin://profile/"+gzh);
               return null;
            }*/

        }
        return "redirect:" + redirectUrl; //必须重定向,否则不能成功

        // 第五步:拉取用户信息(需scope为 snsapi_userinfo)
        //https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN
        //String infoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token="+access_token
       /* String infoUrl = "https://api.weixin.qq.com/cgi-bin/user/info?access_token="+access_token
                + "&openid="+openid
                + "&lang=zh_CN";
        System.out.println("infoUrl:"+infoUrl);
        JSONObject userInfo = JSONObject.parseObject(HttpUtil.getRequestWx(infoUrl));*/
        /*
         {    "openid":" OPENID",
         " nickname": NICKNAME,
         "sex":"1",
         "province":"PROVINCE"
         "city":"CITY",
         "country":"COUNTRY",
         "headimgurl":    "http://wx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/46",
         "privilege":[ "PRIVILEGE1" "PRIVILEGE2"     ],
         "unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL"
         }
         */
        /*System.out.println("JSON-----"+userInfo.toString());
        System.out.println("名字-----"+userInfo.getString("nickname"));
        System.out.println("头像-----"+userInfo.getString("headimgurl"));*/


        /*
         * end 获取微信用户基本信息
         */
        //获取到用户信息后就可以进行重定向,走自己的业务逻辑了。。。。。。
        //接来的逻辑就是你系统逻辑了,请自由发挥

    }
    
}
 

二、如何获取微信公众号的“biz”?

如何获取指定微信公众号的”biz”,至于biz编号能做什么我想大家在搜集这个词的时候就应该了,这里就不墨迹了。具体如下:

  • 1、找到你需要获取的公众号页面,找到历史文章页面 

2、然后直接复制该页的链接,里面就包含了”biz“  

最终得到“人民日报”的“biz”为“MjM5MjAxNDM4MA==”(注意:不要遗漏了后面的‘==’哟)