//jq寫法完善版
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>信息平台</title>
<script src="http://rescdn.qqmail.com/node/ww/wwopenmng/js/sso/wwLogin-1.0.0.js" type="text/javascript" charset="utf-8"></script>
<script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<style>
#code{
/*wrp_code_rl_btn*/
width: 100px;
height: 100px;
}
iframe img{
width: 100px;
height: 100px;
}
</style>
</head>
<body >
<input type="text" id="state"/>
<div id="code" onclick="getSessionWX8()"></div>
<script>
function getSessionWX(){
let url = 'https://xxxxxxxxxxxxx.com/getState';
let vm=this;
$.ajax({
url: url,
method: 'get',
dataType: 'json',
success: function(data){
$('#state').val(data.payload.results.state);
getSession();
},
error:function(){
// alert("服務器或網絡問題");
}
});
};
getSessionWX();
function getSession(){
var clientId= $('#state').val();
console.log(clientId);
window.WwLogin({
"id" : "code", //顯示二維碼的容器id
"appid" : "wx86dd16937ec6403f",
"agentid" : "1000014", //企業微信的cropID,在 企業微信管理端->我的企業 中查看
"redirect_uri" :"https://xxxxxxxxx.com", //重定向地址,需要進行UrlEncode
"state" : clientId, //用於保持請求和回調的狀態,授權請求后原樣帶回給企業。該參數可用於防止csrf攻擊(跨站請求偽造攻擊),建議企業帶上該參數
"href" : "", //自定義樣式鏈接,企業可根據實際需求覆蓋默認樣式。詳見文檔底部FAQ
});
}
setInterval(function(){
var clientId= $('#state').val();
let url = 'https://xxxxx.com/getSessionId?state=' + clientId;
$.ajax({
url: url,
method: 'get',
dataType: 'json',
success: function(data){
console.log(data);
// window.location.href='/index.html';
},
error:function(){
//alert("服務器或網絡問題");
}
});
},3000);
</script>
</body>
</html>
//通用大眾,不完整版,存在用戶同時掃碼,用戶串登錄
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>1111</title>
<script src="http://rescdn.qqmail.com/node/ww/wwopenmng/js/sso/wwLogin-1.0.0.js" type="text/javascript" charset="utf-8"></script>
</head>
<body >
<div id="code" ></div>
<script>
window.WwLogin({
"id" : "code", //顯示二維碼的容器id
"appid" : "xxxxxx",
"agentid" : "xxxxxx", //企業微信的cropID,在 企業微信管理端->我的企業 中查看
"redirect_uri" :"xxxxxxxxxxxx", //重定向地址,需要進行UrlEncode
"state" : "3828293919281", //用於保持請求和回調的狀態,授權請求后原樣帶回給企業。該參數可用於防止csrf攻擊(跨站請求偽造攻擊),建議企業帶上該參數
"href" : "", //自定義樣式鏈接,企業可根據實際需求覆蓋默認樣式。詳見文檔底部FAQ
});
</script>
</body>
</html>
B.vue完整版
<div class="itemLogin WXLogin" v-show="isWXCode == true">
<div class="pcOrWX">
<span class="webTitle webTitleWX">企業微信掃碼登錄</span>
<span class="iconfont icon-diannao pcCode" @click="pcWXCode(false)" title="手動輸入登錄"></span>
</div>
<div id="code" @click="getSessionWX1(1)">
<iframe id="iframe1" :src="WXCodeUrl" frameborder="0" scrolling="no" width="320px" height="313px"></iframe>
</div>
</div>
getSessionWX(){
let vm=this;
let url = 'https://xxxxx.com/getState';
$.get(url,function(data){
var data = JSON.parse(data);
//https://open.work.weixin.qq.com/wwopen/sso/qrConnect?appid=wx86dd16937ec6403f&agentid=1000014&redirect_uri=https://xxxxxx.com/loginPCByWx&state=5245fc29-ff12-459d-b880-9f16047ba8cd&login_type=jssdk
vm.stateWX = data.payload.results.state;
var cssUrl = "https://xxxxx.com/static/css/wxerweima.css";
vm.WXCodeUrl = "https://open.work.weixin.qq.com/wwopen/sso/qrConnect?appid=wx86dd16937ec6403f&agentid=1000014&redirect_uri=https://xxxxxx.com/loginPCByWx&state=" + vm.stateWX + "&href=" + cssUrl + "&login_type=jssdk";
},"text");
},
getSessionWXLogin(){
let vm=this;
let url = 'https://xxxxxx.com/getSessionId?state=' + vm.stateWX;
$.get(url,function(data){},"text");
},
pcWXCode(val){
let vm = this;
this.isWXCode = val;
if(vm.isWXCode == true){
setInterval(function(){
vm.getSessionWXLogin();//輪詢后台數據登錄
},3000);
}
},
待完善碼過期后傳入的參數沒有變化問題,后續優化...