//tokenvalidate.js file
//$(document).ready(function() {
var tokenvalue = getAllUrlParams().token;
//alert(tokenvalue);
var reload = false;
if(tokenvalue!==undefined)
{
localStorage.setItem("token", tokenvalue);
reload = true;
}
else {
// Retrieve value in local storage
tokenvalue = localStorage.getItem("token");
}
if(window.location.hostname == "abc.net"){
var url="../users/tokenvalidate";
}
else {
var url="../users/tokenvalidate";
}
var key= tokenvalue;
fetch(url,{
method: 'get',
headers:{
token:key
}
})
.then(function(response) { return response.json(); })
.then(function(data) {
console.log(data.success);
if(!data.success) {
// $('#openModal').modal("show");
document.getElementById('openModal').click();
// window.location = location.protocol + '//' + location.host + '/login.html';
}
if(reload == true){
window.location = location.protocol + '//' + location.host + location.pathname;
}
})
.catch(function(error) {
if(window.location.hostname == "abc.net"){
window.location="http://abc/Help";
} else {
window.location="https://abc/Help";
}
// If there is any error you will catch them here
console.log(error);
})
// });
function login(eMail,passWord){
if(window.location.hostname == "abc.net"){
var url="../api/login";
}
//var key= tokenvalue;
fetch(url,{
method: 'post',
headers:{
'Content-Type':'application/json'
},
body:JSON.stringify({'email':eMail,
'password':passWord
})
})
.then(function(response) { return response.json(); })
.then(function(data) {
console.log(data.success);
if(!data.success) {
document.getElementById('errormsg').style.display = 'block';
}else{
document.getElementById('errormsg').style.display = 'none';
$('#modal_login').modal('hide');
localStorage.setItem("token", data.success);
}
})
.catch(function(error) {
// If there is any error you will catch them here
console.log(error);
});
}
function getAllUrlParams(url) {
// get query string from url (optional) or window
var queryString = url ? url.split('?')[1] : window.location.search.slice(1);
// we'll store the parameters here
var obj = {};
// if query string exists
if (queryString) {
// stuff after # is not part of query string, so get rid of it
queryString = queryString.split('#')[0];
// split our query string into its component parts
var arr = queryString.split('&');
for (var i=0; i<arr.length; i++) {
// separate the keys and the values
var a = arr[i].split('=');
// in case params look like: list[]=thing1&list[]=thing2
var paramNum = undefined;
var paramName = a[0].replace(/\[\d*\]/, function(v) {
paramNum = v.slice(1,-1);
return '';
});
// set parameter value (use 'true' if empty)
var paramValue = typeof(a[1])==='undefined' ? true : a[1];
// if parameter name already exists
if (obj[paramName]) {
// convert value to array (if still string)
if (typeof obj[paramName] === 'string') {
obj[paramName] = [obj[paramName]];
}
// if no array index number specified...
if (typeof paramNum === 'undefined') {
// put the value on the end of the array
obj[paramName].push(paramValue);
}
// if array index number specified...
else {
// put the value at that index number
obj[paramName][paramNum] = paramValue;
}
}
// if param name doesn't exist yet, set it
else {
obj[paramName] = paramValue;
}
}
}
return obj;
}
我有一个用于登录验证的模式。验证必须在所有页面上进行。我想保持该模式通用并在所有页面中加载它。
<div class="modal fade" id="modal_login" tabindex="-1" data-backdrop="static" data-keyboard="false" role="dialog">
<div class="modal-dialog" role="document">
<form method="POST" id="modal_form_login" novalidate="novalidate">
<div class="modal-content">
<div class="modal-header" style="text-align: center;padding-left: 150px;">
<img src="assets/img/logo.svg">
</div>
<div class="modal-body">
<div class="form-group">
<p style="text-align: center"><i>Enter your Apollo Construct email ID and password to log in to Help.</i></p>
<div id="errormsg" style="display: none; color:red;text-align: center; padding-bottom: 1.2rem;"> Invalid token or login credentials, please log in again.</div>
<div class="input-group">
<span class="input-group-addon">
<i class="fa fa-envelope"></i>
</span>
<input type="email" id="modal_login_email" name="modal_login_email" class="form-control" placeholder="Enter email" />
</div>
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon">
<i class="fa fa-unlock-alt"></i>
</span>
<input type="password" id="modal_login_password" name="modal_login_password" class="form-control" placeholder="Password" />
</div>
</div>
<label class="custom-control custom-checkbox">
<input type="checkbox" id="modal_login_remember" class="custom-control-input" />
<span class="custom-control-indicator"></span>
<span class="custom-control-description">Remember me</span>
</label>
<div class="modal-footer">
<input type="button" onclick="login(document.getElementById('modal_login_email').value,document.getElementById('modal_login_password').value);" class="btn btn-primary" value="Login" style="background-color: #19659d; border-color: #19659d;" />
</div>
</div>
</div>
</form>
</div>
</div>
<div href="#_" id="openModal" class="btn btn-primary" data-toggle="modal" data-target="#modal_login" style="visibility: hidden;"></div>
<div class="modal fade" id="modal_login" tabindex="-1" data-backdrop="static" data-keyboard="false" role="dialog">
<div class="modal-dialog" role="document">
<form method="POST" id="modal_form_login" novalidate="novalidate">
<div class="modal-content">
<div class="modal-header" style="text-align: center;padding-left: 150px;">
<img src="assets/img/logo.svg">
</div>
<div class="modal-body">
<div class="form-group">
<p style="text-align: center"><i>Enter your Apollo Construct email ID and password to log in to Help.</i></p>
<div id="errormsg" style="display: none; color:red;text-align: center; padding-bottom: 1.2rem;"> Invalid token or login credentials, please log in again.</div>
<div class="input-group">
<span class="input-group-addon">
<i class="fa fa-envelope"></i>
</span>
<input type="email" id="modal_login_email" name="modal_login_email" class="form-control" placeholder="Enter email" />
</div>
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon">
<i class="fa fa-unlock-alt"></i>
</span>
<input type="password" id="modal_login_password" name="modal_login_password" class="form-control" placeholder="Password" />
</div>
</div>
<label class="custom-control custom-checkbox">
<input type="checkbox" id="modal_login_remember" class="custom-control-input" />
<span class="custom-control-indicator"></span>
<span class="custom-control-description">Remember me</span>
</label>
<div class="modal-footer">
<input type="button" onclick="login(document.getElementById('modal_login_email').value,
document.getElementById('modal_login_password').value);" class="btn btn-primary" value="Login" style="background-color: #19659d; border-color: #19659d;" />
</div>
</div>
</div>
</form>
</div>
</div>
我试图通过保持使用加载功能 在所有页面中
$('#modal')。load('common / modal.html'); 但不起作用 如何解决问题