维护网站信息/微信代运营
- [ ]具体代码流程如下
这里主要是放的代码实现,具体的思路流程在这篇文章
关于微信开发者工具无法调试的问题解决方案在篇文章
我这里是这样做的,前端拦截登录页面,当用户访问登录页面的时候,前端拦截拦截登录界面。访问我的接口 我返回一个获取code的链接
现在更新一下,有一个新的方法,就是省略前端拦截的那个方法,在企业微信里边直接设置好获取code的那个链接地址,然后让前端拿着code去请求你后台login接口就可以实现授权登录了,这种方法比较快一点,相当于是之拦截了一次页面就跳转了
// 字符串拼接好网址返回给前端@RequestMapping(value = "/qywxauth", method = RequestMethod.GET)public Result<String> qywxauth(HttpServletResponse response) throws Exception {//回调网址是你在企业微信应用配置的回调地址保持一致String reUrl = "http://oa.123.com";//获取方式静默授权,可选String scopetype = "snsapi_privateinfo";//请求code 路径String sUrl = qywxService.GET_CODE_URL;String wxurl = sUrl.replace("CORPID", corpId).replace("REDIRECT_URI", reUrl).replace("SCOPE", scopetype).replace("AGENTID", agentid);System.out.println(wxurl);return new Result<String>("v", "即将前往微信授权",wxurl);}
这里是获取access_token 的代码
public static Map<String, String> getAccessToken(String appid,String appsecret, String type) {Map<String, String> accessToken = null;String requestUrl = ACCESS_TOKEN_URL.replace("ID", appid).replace("SECRET", appsecret);//这里http请求代码区域我一会贴出来 String Obj = HttpClientUtil.httpGet(requestUrl, "UTF-8");JSONObject jsonObject = JSONObject.parseObject(Obj);// 如果请求成功if (null != jsonObject) {try {accessToken = new HashMap<String, String>();accessToken.put("token", jsonObject.getString("access_token"));accessToken.put("expiresin", jsonObject.getString("expires_in"));} catch (Exception e) {accessToken = null;// 获取token失败System.out.println("获取token失败 errcode:{"+ jsonObject.getIntValue("errcode") + "} errmsg:{"+ jsonObject.getString("errmsg") + "}");}}return accessToken;}
这里是获取用户userid 也就是用户信息的代码块
public String getUserInfo(String code) {String accessToken = getAccessToken(corpId, agentSecret, "app").get("token");// 1.获取请求的url,这里的网址我一会也都贴出来String get_userInfo_url = GET_USERINFO_URL.replace("ACCESS_TOKEN",accessToken).replace("CODE", code);// 2.调用接口,发送请求,获取成员信息String Obj = HttpClientUtil.httpGet(get_userInfo_url, "UTF-8");JSONObject jsonObject = JSONObject.parseObject(Obj);System.out.println("jsonObject:" + jsonObject.toString());// 3.错误消息处理if (null != jsonObject) {if (0 != jsonObject.getIntValue("errcode")) {return null;}}//jsonObject不为空这里获取userid,json是fastjson;return jsonObject.getString("UserId");}
到这里基本上就是这三个步骤,接下来我吧utils 的代码块贴出来,还有几个请求网址的代码
//上面获取userinfo的指向代码,官网api上都有
public static final String GET_USERINFO_URL = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=ACCESS_TOKEN&code=CODE";
//获取token的代码
public static final String ACCESS_TOKEN_URL = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ID&corpsecret=SECRET";
//获取code的代码public static final String GET_CODE_URL = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=CORPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&agentid=AGENTID&state=STATE#wechat_redirect";
这里是工具类的代码
//url 是路径,code是编码格式
public static String httpGet(String url,String code) {System.out.println("GetPage:"+url);String content = null;HttpClient httpClient = new HttpClient();//设置headerhttpClient.getParams().setParameter(HttpMethodParams.USER_AGENT,"Mozilla/5.0 (X11; U; Linux i686; zh-CN; rv:1.9.1.2) Gecko/20090803 Fedora/3.5.2-2.fc11 Firefox/3.5.2");GetMethod method = new GetMethod(url);try {int statusCode = httpClient.executeMethod(method);System.out.println("httpClientUtils::statusCode="+statusCode);System.out.println(method.getStatusLine());content = new String(method.getResponseBody(), code);} catch (Exception e) {System.out.println("time out");e.printStackTrace();} finally {if(method!=null)method.releaseConnection();method = null;httpClient = null;}return content;}