/*GET請求/
···
-
(void)GET:(NSString *)url parameters:(NSDictionary *)params success:(CodeSuccessBlock)success failure:(FailureBlock)failure{
NSMutableDictionary *totalParams = [NSMutableDictionary dictionary]; [totalParams addEntriesFromDictionary:[self getCommonParameters]]; [totalParams addEntriesFromDictionary:params]; NSLog(@"URL:%@ params:%@",url,totalParams);
//去除掉首尾的空白字符和換行字符
NSString * removeUrlStr= [url stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
//url字符串UTF-8編碼
NSString *endUrlStr = [removeUrlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];[self.manager GET:endUrlStr parameters:totalParams headers:self.headParams progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
dispatch_async(dispatch_get_main_queue(), ^{
//統(tǒng)一處理Code狀態(tài)
NSInteger code = [self judgeCodeHandleWithJsonDic:responseObject];
NSString * message = responseObject[@"message"];
if (success) {
success(code,responseObject,message);//設(shè)置響應(yīng)頭數(shù)據(jù) NSHTTPURLResponse * response = (NSHTTPURLResponse *)task.response; NSDictionary* headFilesDic = response.allHeaderFields; NSLog(@"GET+++響應(yīng)頭:%@ ",headFilesDic); //響應(yīng)頭中獲取token參數(shù)其掂,含有refresh-token的話,和本地token對比更新token [self refreshTokenWithHeadfilesDic:headFilesDic]; } });
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[MBProgressHUD wj_showPlainText:error.description view:nil];
if (failure) {
failure(error);
}
}];
}
#pragma mark - 響應(yīng)頭中獲取token對比更新
-(void)refreshTokenWithHeadfilesDic:(NSDictionary *)headFilesDic{
NSString * refreshToken = headFilesDic[@"refresh-token"];
if (refreshToken.length > 0) {
//對比更新
NSString * localToken = GetToken;
if (![localToken isEqualToString:refreshToken]) {
//更新token
SaveToken(refreshToken);
}
}
}
···