Created
July 4, 2018 06:42
-
-
Save xjinza/b725d3e2638efa2736e519b8e8b609f6 to your computer and use it in GitHub Desktop.
angularjs 拦截器 添加token #angularjs #登录 #安全
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function httpInterceptor($httpProvider, $provide) { | |
$provide.factory('myHttpInterceptor', function($q, $location,$injector, $rootScope) { | |
//获取url参数 | |
var urlParms = (function() { | |
var searchStr = window.location.search; | |
var urlParms = searchStr ? window.jollyApp.getStringToJSON(searchStr) : $location.search(); //解析Url参数 | |
var env = window.jollyApp.ENV; | |
if (!urlParms['frm']) { | |
urlParms['frm'] = env.is_android ? 1 : env.is_ios ? 2 : 10; | |
} | |
console.log(urlParms,855) | |
return urlParms; | |
})(); | |
var finish=false; | |
var loading=false; //是否正在从客户端获取用户信息 | |
var getUserInfoPromise; | |
var tokenNew; | |
var tokenError=false; | |
return { | |
'request': function(config) { | |
function addTokenNew(tokenNew) { | |
$rootScope.tokenNew = tokenNew; | |
var params = config.params || {}; | |
var data = params.data; | |
if (data !== undefined) { //是否经过api.php转发 | |
if (typeof data === 'string') { | |
data = JSON.parse(data || '{}'); | |
data.tokenNew = tokenNew; | |
config.params.data = JSON.stringify(data); | |
} | |
if (typeof data === 'object') { | |
config.params.data.tokenNew = tokenNew; | |
} | |
} else { | |
config.params.tokenNew = tokenNew; | |
} | |
} | |
var getUserInfo = function() { | |
var deffer = $q.defer(); | |
setWebViewData(urlParms).init('userInfo=1&callback=userInfoTokenNew', function(res) { | |
var data = JSON.parse(res); | |
data.tokenNew = tokenNew = '11'; | |
console.log(config, 467); | |
addTokenNew(data.tokenNew); | |
setUserInfo(data); | |
// setTimeout(function() { | |
console.log(config, 567); | |
deffer.resolve(config); | |
finish = true; | |
// }, 2000); | |
}); | |
// $timeout(function() { | |
// addTokenNew(44); | |
// deffer.resolve(config); | |
// finish = true; | |
// }, 2000); | |
return deffer.promise; | |
}; | |
if (config.method === 'JSONP') { | |
if (urlParms['appTypeId'] === undefined) { //不是app内不做处理 | |
return config; | |
} | |
if (!finish) { | |
if (!loading) { | |
loading = true; | |
console.log(config.url, config.params.url, 11); | |
return getUserInfoPromise = getUserInfo(); | |
} else { | |
var deffer = $q.defer(); | |
getUserInfoPromise.then(function() { //等待getUserInfoPromise完成 | |
addTokenNew(tokenNew); | |
deffer.resolve(config); | |
}); | |
console.log(config.url, config.params.url, 22); | |
return deffer.promise; | |
} | |
}else{ | |
console.log(config.url,config.params.url,33) | |
addTokenNew(tokenNew); | |
return config; | |
} | |
} else { //只对jsonp接口判断,json文件下载等不做处理 | |
return config; | |
} | |
}, | |
'requestError': function(rejection) { | |
return $q.reject(rejection); | |
}, | |
'response': function(response) { | |
var data = response.data; | |
if (data) { | |
if (data.messageCode === '0114'&&!tokenError) { //多次tokenError只处理一次 | |
tokenError=true; | |
setWebViewData(urlParms).init('userLogin=1&tokenError=1&callback=userLogin', function(res) { | |
$injector.get('$state').reload(); | |
// setTimeout(function() { | |
// location.reload(); | |
// },3000) | |
}); | |
} | |
} | |
return response; | |
}, | |
'responseError': function(rejection) { | |
return $q.reject(rejection); | |
} | |
}; | |
}); | |
$httpProvider.interceptors.push('myHttpInterceptor'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment