Files
mgm-daylight-mcrm-mobile-ios/pages/samlLogin/samlLogin.vue
T
2026-09-22 16:37:00 +08:00

117 lines
3.1 KiB
Vue

<!--sso login-->
<template>
<view class="container">
</view>
</template>
<script>
export default {
data() {
return {
url:'',
intervalId:null,
newUrl:'',
siteData:null,
samlWebview:null
}
},
onLoad(options) {
this.siteData = uni.getStorageSync("siteData");
if(options && options.uid && options.pwd){
let username = options.uid.replaceAll(" ", "+");
let password = options.pwd.replaceAll(" ", "+");
const loginUrl = this.siteData.internalUrl + this.siteData.apiPath + "/app/sso/login_saml_with_mfa?username="+encodeURIComponent(username)+"&password="+encodeURIComponent(password);
this.url = loginUrl;
this.openWebview(loginUrl);
}
if (options && options.url){
this.url = options.url;
this.openWebview(options.url);
}
this.listenUrl()
},
onShow() {
this.siteData = uni.getStorageSync("siteData");
},
methods: {
openWebview(loginUrl) {
// #ifdef APP-PLUS
const wv = plus.webview.create(loginUrl, 'saml-login-webview', {
'uni-app': 'none',
top: '0px',
bottom: '0px',
additionalHttpHeaders: {
'Origin': 'MGM_MCRM_APP'
}
});
this.samlWebview = wv;
const pages = getCurrentPages();
const page = pages[pages.length - 1];
page.$getAppWebview().append(wv);
// #endif
},
listenUrl() {
var that = this;
var count = 0;
this.intervalId = setInterval(() => {
var currentUrl = that.getCurrentUrl();
that.newUrl = currentUrl;
var code = that.getCurrentQueryString(currentUrl, 'SAMLResponse');
var errorCode = that.getCurrentQueryString(currentUrl, 'errorCode');
//uni.$u.toast('url1='+currentUrl);
if(currentUrl.indexOf('/sso_callback/login')!=-1){
//uni.$u.toast('url2='+currentUrl);
if (code){
//uni.$u.toast('get code');
clearInterval(that.intervalId);
uni.redirectTo({
url:'/pages/login/login?code='+encodeURIComponent(code)
})
}
else if (errorCode){
//uni.$u.toast('get errorcode');
clearInterval(that.intervalId);
uni.redirectTo({
url:'/pages/idpLogin/idpLogin?errorCode='+encodeURIComponent(errorCode)
})
}
}
}, 1000);
},
getCurrentQueryString(url, name) {
try{
var queryString = url.substring(url.indexOf('?'));
var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
var r = queryString.substr(1).match(reg);
if (r != null) {
return unescape(r[2]);
}
return null;
}
catch(e){
uni.$u.toast('getCurrentQueryString exception:'+e.errMsg);
return null;
}
},
getCurrentUrl(){
if (this.samlWebview) {
var url = this.samlWebview.getURL();
console.log("=======当前地址======", url);
return url;
}
var pages = getCurrentPages();
var page = pages[pages.length - 1];
var currentWebview = page.$getAppWebview();
var url = currentWebview.children()[0].getURL();
console.log("=======当前地址======", url);
return url;
}
}
}
</script>
<style>
</style>