微信公众号,菜单扫码重定向获取用户信息

使用微信扫码重定向获取用户信息在转向界面

1.构造二维码

2.扫码获取二维码链接和对应的id(为使二维码易于识别,应将链接缩短,所以以下取名为A

3.重定向获取用户信息

4.转到准确链接获取相应信息

(为了方便),使用了重定向3次,即由a->oauthServlet2->B->A,在获取用户时,可将state作为需要的参数对象

带参数传递的oauthServlet写在链接上。


   //扫描方法入口重定向到扫描界面
    public ModelAndView a(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String url = "trkjgzh.com";
        String i = request.getParameter("i");
        System.out.println("i-----------------:" + i);
        String oauthUrl =
                "https://open.weixin.qq.com/connect/oauth2/authorize?appid="
                        + SystemConst.appId + "&redirect_uri=http%3A%2F%2F" + url + "%2FoauthServlet2" +
                        "&response_type=code&scope=snsapi_userinfo&state=" + i + "#wechat_redirect";
        return getRedirect(oauthUrl);
    }
    //扫描方法入口重定向到扫描界面
    public ModelAndView B(HttpServletRequest request, HttpServletResponse response) throws Exception {
         String i= request.getParameter("i");
         String openid = request.getParameter("openId");
        return getRedirect("/trans/tower_A.do?i="+i+"&openId="+openid);
    }

    //扫描从定向和判断系统用户
    public ModelAndView A(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, Object> model = new HashMap<String, Object>();
        String su_id = request.getParameter("su_id");
        if (StringUtil.isEmpty(su_id)) {
            String openid = request.getParameter("openId");
            System.out.println("openid-------------:" + openid);

            User user = new User();
            if (!StringUtil.isEmpty(openid)) {
                user.setKoal_cert_cn(openid);
                user.setMode("koal_cert_cn");
                User u = userService.query(user); //通过微信公众号查询
                if (StringUtil.isEmpty(u)) {
                    model.put("result", "0");
                } else {
                    String tt_id = request.getParameter("i");
                    Tower tower = towerService.query(tt_id);
                    if (!StringUtil.isEmpty(tower)) {
                        model.put("tower", tower);

                        Defect defect = new Defect();
                        defect.setTd_tl_id(tt_id);
                        defect.setMode("td_tl_id2");
                        defect.setOrder("create_time desc");
                        List<Defect> defectList = defectService.list(defect);
                        if (!StringUtil.isEmpty(defectList)) {
                            model.put("defectList",defectList);
                            model.put("dsize", defectList.size());
                        }
                    }
                    model.put("i", tt_id);
                    model.put("su_id", u.getSu_id());
                }
            }

        } else {
            String tt_id = request.getParameter("i");
            Tower tower = towerService.query(tt_id);
            if (!StringUtil.isEmpty(tower)) {
                model.put("tower", tower);
                        Defect defect = new Defect();
                        defect.setTd_tl_id(tt_id);
                        defect.setMode("td_tl_id2");
                        defect.setOrder("create_time desc");
                        List<Defect> defectList = defectService.list(defect);
                        if (!StringUtil.isEmpty(defectList)) {
                            model.put("defectList",defectList);
                            model.put("dsize", defectList.size());
                        }
            }
            model.put("i", tt_id);
            model.put("su_id", su_id);
        }


        return getView(request, model);
    }
重定向链接的servlet:


   
        oauthServlet2
        com.dm.core.OauthServlet2
   

   
        oauthServlet2
        /oauthServlet2
   



package com.dm.core;


import com.dm.core.dao.SqlDao;
import com.dm.core.system.SystemConst;
import com.dm.core.util.StringUtil;
import com.dm.core.util.WebUtil;
import com.dm.core.util.WeiXinUtil;
import com.dm.wechat.domain.Baseinfo;
import com.dm.wechat.domain.MenuManage;
import com.dm.wechat.pojo.SnsUserInfo;
import com.dm.wechat.pojo.WeiXinOauth2Token;


import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**


 * Created by IntelliJ IDEA.
 * User: Zjm
 * Date: 15-3-3
 * Time: 下午4:58
 * To change this template use File | Settings | File Templates.
 */
public class OauthServlet2 extends HttpServlet {
     private ServletConfig config = null;


    public void init(ServletConfig config) throws ServletException {
        System.out.print("初始化servelt成功");
        this.config = config;
    }
    public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
         //用户同意授权,能获得code
        String code = request.getParameter("code");
        String state = request.getParameter("state");
        String openId="";
         System.out.println("state-----------=:"+state);
          //authdeny表示用户不同意授权
        if(!"authdeny".equals(code)){
            //I:
            //获取网页授权access_token  ?
                 WeiXinOauth2Token weiXinOauth2Token = WeiXinUtil.getOauth2AccessToken(SystemConst.appId,SystemConst.appsecret, code);
                System.out.println("------------");
            //网页授权接口访问凭证
            String accessToken = weiXinOauth2Token.getAccessToken();


            //用户标识
          openId = weiXinOauth2Token.getOpenId();


            System.out.println("用户微信号openId:" + openId);
            //用户信息
            //P:
            request.setAttribute("openId",openId);
            request.setAttribute("w_id",openId);
            request.setAttribute("i",state);
        }
        //O: 参数传递使用链接传递
        request.getRequestDispatcher("/trans/tower_B.do?openId="+openId+"&i="+state).forward(request,response);
    }
}

你可能感兴趣的:(微信)