在平台上添加企业微信二维码登录

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;
}

 

 3.打开过滤去

<value>loginController.do?pageLogin</value><!-- 二维码登录 -->
<value>loginController.do?getConfigurationData</value><!-- 二维码登录ajax -->

猜你喜欢

转载自www.cnblogs.com/zmmfeng/p/10661739.html