Java——微信公众开发,获取openid

package cn.com.dzqc.controller;


import cn.com.dzqc.entity.QcEmployeeInformation;
import cn.com.dzqc.service.QcEmployeeInformationService;
import cn.com.dzqc.util.HttpGetUtil;
import cn.com.dzqc.util.SystemConstants;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/weixin")
public class Weixin {
    @Autowired
    private QcEmployeeInformationService emplService;
    @RequestMapping("/frist")
    public void bianma(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        System.out.println("进入-----------");
        response.setContentType("text/html");
        response.setCharacterEncoding("UTF-8");
        request.setCharacterEncoding("UTF-8");
        String redirect_uri = URLEncoder.encode("http://m.dzqcedu.com/salary/weixin/openid", "UTF-8");
        StringBuffer url = new StringBuffer("https://open.weixin.qq.com/connect/oauth2/authorize?redirect_uri=" + redirect_uri +
                "&appid=" + "开发者IDxxxxxxxxxxxxxxxx" + "&response_type=code&scope=snsapi_base&state=1#wechat_redirect");
        System.out.println("重定向-----------");
        response.sendRedirect(url.toString());
    }
    @RequestMapping("/openid")
    public ModelAndView huidai(HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        QcEmployeeInformation qcEmployeeInformation=null;
        response.setContentType("text/html");
        try {
            request.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        /*获取openid*/
        String attribute = (String) session.getAttribute("openid");
        if (attribute != null) {
            /*查询用户表是否有这个openID*/
            qcEmployeeInformation = this.emplService.selByOpenid(attribute);
        } else {
            response.setCharacterEncoding("UTF-8");
            String code = request.getParameter("code");
            Map params = new HashMap();
            params.put("secret", "xxxxxxxxxxxxxxxxxxxxxxxxxx");
            params.put("appid", "xxxxxxxxxxxxxxxxx");
            params.put("grant_type", "authorization_code");
            params.put("code", code);
            String result = HttpGetUtil.httpRequestToString(
                    "https://api.weixin.qq.com/sns/oauth2/access_token", params);
            JSONObject jsonObject = JSONObject.fromObject(result);
            String openid = jsonObject.get("openid").toString();
            System.out.println("得到的openid为:" + openid);
              if(openid==null|| openid.equals("")){
                  ModelAndView m = new ModelAndView();
                  m.setViewName("index");
                  return  m;
              }
            qcEmployeeInformation = this.emplService.selByOpenid(openid);
            session.setAttribute("openid", openid);
        }
        ModelAndView m = new ModelAndView();
        if (qcEmployeeInformation != null) {
            String username=qcEmployeeInformation.getName();
            String idCord=qcEmployeeInformation.getIdCard();
            m.addObject("username", username);
            m.addObject("idCard", idCord);
            m.setViewName("zhbg_home");
            return m;
        }
        System.out.println("//"+session.getAttribute("openid"));
        m.setViewName("index");
        return m;
    }

 


}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值