java获取微信小程序用户信息

在微信小程序内调用该方法 并传入code 

    // AppID appsecret
    public static final String appid = "xxxxxxxxxxxxxxx";
    public static final String appsecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

 

    /**
     * 获取微信OpenId
     */
    @RequestMapping("/getOpenId")
    public void getOpenId(HttpServletResponse response, String wxCode) {
        writeJsonData(response, new CommonModel(Constants.StatusCode.Success.VALUE,
                Constants.StatusCode.Success.MSG, WxCodeUtils.getSessionKeyOropenid(wxCode)));
    }

    /**
     * 获取微信小程序 session_key 和 openid
     * 用户端
     *
     * @param code 调用微信登陆返回的Code
     * @return
     * @author zhuping
     */
    public static JSONObject getSessionKeyOropenid(String code) {
        //微信端登录code值
        String wxCode = code;
        String requestUrl = "https://api.weixin.qq.com/sns/jscode2session";    //请求地址
        Map<String, String> requestUrlParam = new HashMap<String, String>();
        requestUrlParam.put("appid", appid);    //开发者设置中的appId
        requestUrlParam.put("secret", appsecret);    //开发者设置中的appSecret
        requestUrlParam.put("js_code", wxCode);    //小程序调用wx.login返回的code
        requestUrlParam.put("grant_type", "authorization_code");    //默认参数

        //发送post请求读取调用微信 https://api.weixin.qq.com/sns/jscode2session 接口获取openid用户唯一标识
        JSONObject jsonObject = JSON.parseObject(sendPost(requestUrl, requestUrlParam));

        System.out.println(jsonObject);

        return jsonObject;
    }
   /**
     * 向指定 URL 发送POST方法的请求
     *
     * @param url 发送请求的 URL
     * @param
     * @return 所代表远程资源的响应结果
     */
    public static String sendPost(String url, Map<String, ?> paramMap) {
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";

        String param = "";
        Iterator<String> it = paramMap.keySet().iterator();

        while (it.hasNext()) {
            String key = it.next();
            param += key + "=" + paramMap.get(key) + "&";
        }

        try {
            URL realUrl = new URL(url);
            // 打开和URL之间的连接
            URLConnection conn = realUrl.openConnection();
            // 设置通用的请求属性
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("Accept-Charset", "utf-8");
            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            // 获取URLConnection对象对应的输出流
            out = new PrintWriter(conn.getOutputStream());
            // 发送请求参数
            out.print(param);
            // flush输出流的缓冲
            out.flush();
            // 定义BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            //log.error(e.getMessage(), e);
        }
        //使用finally块来关闭输出流、输入流
        finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        return result;
    }

在小程序内拿到想要的信息传入后台登录或者注册

    /**
     * 登录 or 注册
     *
     * @param request
     * @throws Exception
     */
    @RequestMapping(value = "/deciphering")
    @ResponseBody
    public void deciphering(UserinfoDO userinfoDO, HttpServletRequest request, HttpServletResponse response) {
        //根据openid查询是否有该用户,如果存在则更新,不存在则重新插入
        String openId = userinfoDO.getOpenId();
        UserinfoDO userinfoDO1 = userinfoService.selectDetilByopenId(openId);
        if (userinfoDO1 != null) {
            userinfoService.update(userinfoDO);
        } else {
            userinfoDO.setUserBalance(new BigDecimal(0));
            userinfoDO.setCreateTime(new Date());
            userinfoDO.setUserIdentify(1);
            userinfoService.save(userinfoDO);
        }
        writeJsonData(response, new CommonModel(Constants.StatusCode.Success.VALUE,
                Constants.StatusCode.Success.MSG));
    }

注意小程序内要调起微信授权

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值