登录 注册

eos应用与企业微信集成

发布时间:Jun 12, 2019 10:21:24 AM 发布人:管理员


归属栏目: EOS开发


外部链接: 


关键字: 


归属栏目: EOS开发


文章来源: 


摘要: 

eos应用与企业微信集成--获取用户

本文介绍EOS应用如何与企业微信进行集成,如何获取企业微信提供的API获取当前用户信息,方便开发人员在开发企业微信小程序的时候参考使用。

1  企业微信jsAPI详细调用说明请访问: https://work.weixin.qq.com/api/doc#10028/OAuth2.0接入流程说明

1.eos中新建运算逻辑主要是利用企业微信的jsAPI来获取当前用户的userid,代码如下:

import java.io.BufferedReader;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.net.HttpURLConnection;

import java.net.URL;

import java.net.URLConnection;

 

import com.eos.system.annotation.Bizlet;

import com.primeton.bfs.engine.json.JSONException;

import com.primeton.bfs.engine.json.JSONObject;

 

/**

 * @author yefeng

 * @date 2018-04-18 09:47:05

 *

 */

@Bizlet("JsAPIUtil")

public class JsAPIUtil {

 

      /**获取企业微信当前登录用户

       * @param CODE

       * @param CORPID

       * @param CORPSECRET

       * @return

       * @throws JSONException

       */

      @Bizlet("")

      public static String getUser(String CODE,String CORPID,String CORPSECRET) throws JSONException {

            String userid="";

            String urlStr = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid="+CORPID+"&corpsecret="+CORPSECRET;  //gettoken

        JSONObject json = processUrl(urlStr);

        if(null!=json&&null!=json.getString("access_token")){

               String userurl = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token="+json.getString("access_token")+"&code="+CODE;

               JSONObject user= processUrl(userurl);

               if(0==user.getInt("errcode")){

                    userid=user.getString("UserId");

               }

        }

            return userid;

      }

        private static JSONObject processUrl(String urlStr) {

              URL url;

              String rs="";

              JSONObject json=null;

              try {

                  url = new URL(urlStr);

                  URLConnection URLconnection = url.openConnection();

                  HttpURLConnection httpConnection = (HttpURLConnection)URLconnection;

                  int responseCode = httpConnection.getResponseCode();

                  if (responseCode == HttpURLConnection.HTTP_OK) {

                      InputStream urlStream = httpConnection.getInputStream();

                      BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlStream));

                      String sCurrentLine = "";

                      String sTotalString = "";

                      while ((sCurrentLine = bufferedReader.readLine()) != null) {

                          sTotalString += sCurrentLine;

                      }

                      rs= sTotalString;

                  }else{

                      System.err.println("失败");

                  }

                  json = new JSONObject(rs);

              } catch (Exception e) {

                  e.printStackTrace();

              }

                  return json;

          }

}

2.在前端jsp中调用

var CODE="<%=request.getParameter("code") %>";//从企业微信集成应用的url中获取

var CORPID="wxaf9ee15633cf3594";

var CORPSECRET="d5WsHUPZK4zT5qffXc-BdJNIFXGYz9QLKfMOTMy7sl4";

function getWXUser(CODE,CORPID,CORPSECRET){

         $.ajax({

              url: "com.jinke.omp.mobile.jsAPI.getUser.biz.ext",

              data:JSON.stringify({code:CODE,corpid:CORPID,corpsecret:CORPSECRET}),

             type : "POST",

                  cache : false,

                  contentType : "text/json",

              success: function(data){

                      setCookie("UserId", data.UserId);

                      loginEOS(data.UserId);

                  },

              error:function(XMLHttpRequest, textStatus, errorThrown){

              }

          });

}

注意:code是在企业微信配置主页url时自动获取并传过来的,其中redirect_uri需要转码,如:

https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxee9e4dcd65a1ce39&redirect_uri=http%3a%2f%2fomp.jinke.com%3a9090%2fdefault%2fmobile%2fmobileindex.jsp&response_type=code&scope=snsapi_base&agentid=1000027&state=STATE#wechat_redirect

 


相关文章

返回