- 在企业微信后台中添加应用
配置回调域
-
编辑
- 记录下应用的相关信息,在后文要用到
- controller
@Controller
public class QiYeWxLoginController {
private static final String agentId = "AgentId";
private static final String ClienSecret = "Secret";
private static final String ClientID = "企业ID";
private static final Logger log = LoggerFactory.getLogger(QiYeWxLoginController.class);
@GetMapping("/index")
public String index(ModelMap modelMap){
modelMap.put("clientID",ClientID);
modelMap.put("agentId",agentId);
return "index";
}
/**
* 获取扫码登录地址
*/
@GetMapping(value = "login")
@ResponseBody
public Object login( @RequestParam("code") String code) throws IOException {
try {
Map<String,Object> params = new HashMap<>(2);
params.put("corpid",ClientID);
params.put("corpsecret",ClienSecret);
String accessToken = HttpUtil.get("https://qyapi.weixin.qq.com/cgi-bin/gettoken", params,30000);
log.info("accessToken>>>>>>>>>>>>>" +accessToken);
JSONObject jsonObjects = JSONObject.parseObject(accessToken);
Integer errcodes = (Integer) jsonObjects.get("errcode");
if(errcodes == 0){
Map<String,Object> param = new HashMap<>(2);
param.put("access_token",jsonObjects.getString("access_token"));
param.put("code",code);
String result = HttpUtil.get("https://qyapi.weixin.qq.com/cgi-bin/auth/getuserinfo", param,30000);
JSONObject jsonObject = JSON.parseObject(result);
Integer errcode = (Integer) jsonObject.get("errcode");
log.info("userid>>>>>>>>>>>>>" +result);
if (errcode == 0) {
String userId = (String) jsonObject.get("userid");
Map<String,Object> paramUser = new HashMap<>(2);
paramUser.put("access_token",jsonObjects.getString("access_token"));
paramUser.put("userid",userId);
String userinfo = HttpUtil.get("https://qyapi.weixin.qq.com/cgi-bin/user/get",
paramUser,30000);
JSONObject jsonObjectUser = JSONObject.parseObject(userinfo);
Integer errorUsercode = (Integer) jsonObjectUser.get("errcode");
if(errorUsercode == 0){
log.info("获取用户信息>>>>>>>>>>>>>" + jsonObjectUser);
return jsonObjectUser;
}
}
}
// 请求重定向
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
4.index.html
<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>企业微信扫码登录demo</title>
<!-- 生产环境版本,优化了尺寸和速度 -->
<script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
<script src="https://wwcdn.weixin.qq.com/node/wework/wwopen/js/wwLogin-1.2.7.js"></script>
<style>
* {
margin: 0;
padding: 0;
}
body {
width: 100%;
background: #fff;
}
</style>
</head>
<body style="background-color: #e9eaeb;">
<div style="width: 432px;min-height: 366px;margin-top: 28px;margin: 30px auto 0;background-color: white">
<div id="wx_reg" style="padding-top: 42px;text-align: center;overflow: hidden;min-height: 438px;">
</div>
</div>
</body>
<script>
var appid = '[[${clientID}]]'
var agentid = [[${agentId}]]
var wwLogin = new WwLogin({
"id": "wx_reg",
"appid": appid, // 填你的企业微信企业id
"agentid": agentid,// 填你的自建应用id
"redirect_uri": "https://26d978f657.oicp.vip/login", // 填你的可信域名里的跳转链接,一定要有http或者https
"state": "",
"href": "",
"lang": "zh",
});
</script>
</html>