1. 程式人生 > >在平臺上添加企業微信二維碼登錄

在平臺上添加企業微信二維碼登錄

等於 fad uniq bject UNC out 回調 保持 fields

1.在登錄頁面先寫一個模態框;

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="width:90%;height:80%;">
   <div class="modal-dialog">
         <div class="modal-content" align="center">
              <div class="modal-header"
> <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button> <h4 class="modal-title" id="myModalLabel" > 企業微信二維碼登錄</h4> </div> <div class="modal-body"> <
div id="code"></div> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">關閉</button> </div> </div><!-- /.modal-content --> </
div><!-- /.modal --> </div>

2.在javascript中寫

2.1 先在常量配置中取出企業微信的配置數據

function setWxworkDate(){
        var url="login.do?getConfigurationData";
        $.ajax({
            type:"POST",
            url:url,
            data:{
            },
            async:false,
            success:function(data){
                appid = jQuery.parseJSON(data).appid;
                agentid = jQuery.parseJSON(data).agentid;
                redirect_uri = jQuery.parseJSON(data).redirect_uri;
            }
        });
        $("#appid").val(appid);
        $("#agentid").val(agentid);
        $("#redirect_uri").val(redirect_uri);        
     }
/**
     * 獲取小號角辦公平臺常量配置表中企業微信二維碼登錄的配置信息;
     * 
     * @param ids
     * @return
     * @throws Exception
     */
    @RequestMapping(params = "getConfigurationData")
    @ResponseBody
    public Map<String, String> getConfigurationData(HttpServletRequest request) throws Exception {
        
        SysConstantDataEntity SysConstantData = new SysConstantDataEntity();
        SysConstantData = systemService.findUniqueByProperty(SysConstantDataEntity.class, "xcode", "wxwork");
        List<XhjSysConstantDataEntity> xhjSysConstantDatas = new ArrayList<SysConstantDataEntity>();
        String appid = "";
        String agentid = "";
        String redirect_uri = "";
        String corpsecret = "";
        if (SysConstantData != null) {
            SysConstantDatas = systemService.findByProperty(SysConstantDataEntity.class, "pid", SysConstantData.getId());
            if (SysConstantDatas.size() > 0) {
                for (SysConstantDataEntity sysConstantData : SysConstantDatas) {
                    //判斷取出來的值是否等於配置信息中對應值關系
                    if ("wxworkappid".equals(sysConstantData.getXcode())) {
                        appid = sysConstantData.getName();
                    }else if ("agentid".equals(sysConstantData.getXcode())) {
                        agentid = sysConstantData.getName();
                    }else if ("redirect_uri".equals(sysConstantData.getXcode())) {
                        redirect_uri = sysConstantData.getName();
                    }else if ("corpsecret".equals(sysConstantData.getXcode())) {
                        corpsecret = sysConstantData.getName();
                    }
                }
            }
        }
        //把取出來的值放在map集合
        Map<String, String> maps = new HashMap<String,String>();
        maps.put("appid", appid);
        maps.put("agentid", agentid);
        maps.put("redirect_uri", redirect_uri);
        maps.put("corpsecret", corpsecret);
        return maps;
    }

2.2根據企業微信的配置信息拿到企業微信的二維碼

setWxworkDate();
        var appid = $("#appid").val();
        var agentid = $("#agentid").val();
        var redirect_uri = $("#redirect_uri").val();
        window.WwLogin({
            "id" : "code",  //顯示二維碼的容器id
            "appid" : appid,
            "agentid" : agentid,  //企業微信的cropID,在 企業微信管理端->我的企業 中查看
            "redirect_uri" : redirect_uri+"/zmm/login.do?pageLog",//重定向地址,需要進行UrlEncode
            "state" : "123",   //用於保持請求和回調的狀態,授權請求後原樣帶回給企業。該參數可用於防止csrf攻擊(跨站請求偽造攻擊),建議企業帶上該參數
            "href" : "",    //自定義樣式鏈接,企業可根據實際需求覆蓋默認樣式。詳見文檔底部FAQ

        });

2.3根據二維碼拿到員工的工號

/**
     * 瀏覽器內登錄
     * @param model
     * @param code
     * @param request
     * @return
     */
    @RequestMapping(params = "pageLog")
    public String pageLogin(Model model, String code, HttpServletRequest request, HttpServletResponse response){
        try {
            Map<String, String> maps = getConfigurationData(request);
            String corpid  = maps.get("appid");
            String corpsecret  = maps.get("corpsecret");
            String url1 = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid="+corpid+"&corpsecret="+corpsecret;
            String result = sendGet(url1);
            JSONObject object = JSONObject.fromObject(result);
            String access_token = (String) object.get("access_token");
            //JSONObject userMsg = WeixinUtil.getUserMsg(access_token, code);     //根據access_token和code獲取用戶ticket
            String url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token="+access_token+"&code="+code;
            String str = sendGet(url);
            JSONObject object1 = JSONObject.fromObject(str);
            //獲取員工工號
            String UserId = (String) object1.get("UserId");
            //判斷員工數據庫是否有這個員工工號
            List<TSUser> userName = systemService.findByProperty(TSUser.class, "userName", UserId);
            if (userName.size() > 0) {
                //request.setAttribute("UserId", UserId);
                //request.setAttribute("QRCode", "wxwork");
                HttpSession session = ContextHolderUtils.getSession();
                session.setAttribute("UserId", UserId);
                session.setAttribute("QRCode", "wxwork");
            }
            return "login/login";
        } catch (Exception e) {
            e.printStackTrace();
            return "login/login";
        }
    }

/**
* 向指定URL發送GET方法的請求
*
* @param url
* 發送請求的URL
* @param param
* 請求參數,請求參數應該是 name1=value1&name2=value2 的形式。
* @return URL 所代表遠程資源的響應結果
*/
public static String sendGet(String url) {
  String result = "";
  BufferedReader in = null;
  try {
    String urlNameString = url;
    URL realUrl = new URL(urlNameString);
    // 打開和URL之間的連接
    URLConnection connection = realUrl.openConnection();
    // 設置通用的請求屬性
    connection.setRequestProperty("accept", "*/*");
    connection.setRequestProperty("connection", "Keep-Alive");
    connection.setRequestProperty("user-agent",
    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
    // 建立實際的連接
    connection.connect();
    // 獲取所有響應頭字段
    Map<String, List<String>> map = connection.getHeaderFields();
    // 遍歷所有的響應頭字段
    for (String key : map.keySet()) {
    //System.out.println(key + "--->" + map.get(key));
  }
  // 定義 BufferedReader輸入流來讀取URL的響應
  in = new BufferedReader(new InputStreamReader(
  connection.getInputStream()));
  String line;
  while ((line = in.readLine()) != null) {
    result += line;
  }
  } catch (Exception e) {
    e.printStackTrace();
  }
  // 使用finally塊來關閉輸入流
  finally {
  try {
    if (in != null) {
      in.close();
    }
  } catch (Exception e2) {
    e2.printStackTrace();
  }
  }
  return result;
}

 

在平臺上添加企業微信二維碼登錄