企业微信扫码功能

主要先在企业微信创建应用,然后获取该应用的相关参数,具体可以参考企业微信文档,以下相关参数配置及代码

callBack.address=http://www.*****.com/ebox/api/v1/user/callBack
qqAddress=https://qyapi.weixin.qq.com
qqCodeAddress=https://open.weixin.qq.com
corpId=ww1**281b66436****
corpSecret=GJPmBMIpKeL_-0ica48848mK2OMmILikxT*******
agentId=10000**
scope=snsapi_privateinfo
successHtml=http://****/scan_success.html
failHtml=http://****/scan_fail.html

//微信扫码返回个人信息
public String callback(String code, String state, HttpServletResponse response, HttpServletRequest request) {
    String corpid = serverConfig.get("corpId");
    String corpsecret = serverConfig.get("corpSecret");
    String success = serverConfig.get("successHtml");
    String fail = serverConfig.get("failHtml");
    String qqAddress = serverConfig.get("qqAddress");
    logger.info("code  " + code);
    try {
        String token = qqAddress + "/cgi-bin/gettoken?corpid=" + corpid + "&corpsecret=" + corpsecret;
        //获取access_token
        String access_token = getResult(token, "access_token");
        String userId = qqAddress + "/cgi-bin/user/getuserinfo?access_token="
                + access_token + "&code=" + code;
        //获取用户UserId
        String userId_token = getResult(userId, "UserId");
        String userInfoUrl = qqAddress + "/cgi-bin/user/get?access_token=" + access_token + "&userid=" + userId_token;
        //net.sf.json.JSONObject myJsonObject3 = path(userInfoUrl);
        //logger.info("userInfoUrl" + myJsonObject3);
        String name = getResult(userInfoUrl, "name");
        String userid = userId_token;
        String flagId = state;
        WxUser wxUser = new WxUser();
        wxUser.setName(name);
        wxUser.setUserid(userid);
        wxUser.setFlagID(flagId);
        wxUser.setCreateTime(System.currentTimeMillis());
        //把个人信息存入表中
        wxUserService.createWxUser(wxUser);
        //返回成功页面
        return "redirect:" + success;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "redirect:" + fail;
}

public String getResult(String url, String param) throws IOException {
    String resp = toAuth(url);//拼接字符串得到url
    Map<String, Object> map = gson.fromJson(resp,
            new TypeToken<Map<String, Object>>() {
            }.getType());
    logger.info(map);
    return map.get(param).toString();
}

public String toAuth(String Get_Token_Url) throws IOException {
    httpClient = HttpClients.createDefault();
    httpGet = new HttpGet(Get_Token_Url);
    CloseableHttpResponse response = httpClient.execute(httpGet);
    System.out.println(response.toString());
    String resp;
    try {
        HttpEntity entity = response.getEntity();
        System.out.println(response.getAllHeaders());
        resp = EntityUtils.toString(entity, "utf-8");
        EntityUtils.consume(entity);
    } finally {
        response.close();
    }
    return resp;
}


//生成微信二维码

public WxUser wxLogin(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
    String corpid = serverConfig.get("corpId");
    String callBack = serverConfig.get("callBackAddress");
    String agentId = serverConfig.get("agentId");
    String scope = serverConfig.get("scope");
    String redirect_uri = URLEncoder.encode(callBack, "utf-8");
    String qqCodeAddress = serverConfig.get("qqCodeAddress");
    String url = qqCodeAddress + "/connect/oauth2/authorize?appid=APPID&redirect_uri=R_URL&response_type=code&scope=SCOPE&agentid=AGENTID&state=STATE#wechat_redirect";
    //定义二维码的参数
    //String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";
    String state = UUIDUtils.generateUUID();
    url = url.replace("APPID", corpid).replace("R_URL", redirect_uri).replace("SCOPE", scope).replace("AGENTID", agentId).replace("STATE", state);
    logger.info("url" + url);
    WxUser wxuser = new WxUser();
    wxuser.setFlagID(state);
    wxuser.setQrCode(url);
    return wxuser;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值