mirror of
https://github.com/wu736139669/Ash_AWord.git
synced 2026-08-07 06:37:08 +00:00
base
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
//
|
||||
// BaseViewModel.h
|
||||
// TheLifeCircle
|
||||
//
|
||||
// Created by xmfish on 14/12/30.
|
||||
// Copyright (c) 2014年 小鱼网. All rights reserved.
|
||||
//
|
||||
|
||||
#import "MTLModel.h"
|
||||
#import "MTLJSONAdapter.h"
|
||||
|
||||
@interface BaseViewModel : MTLModel<MTLJSONSerializing>
|
||||
|
||||
@property (nonatomic, readonly) NSNumber *errCode;
|
||||
@property (nonatomic, readonly) NSString *errMessage;
|
||||
|
||||
- (BOOL)success;
|
||||
|
||||
- (BOOL)needLogin;
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,38 @@
|
||||
//
|
||||
// BaseViewModel.m
|
||||
// TheLifeCircle
|
||||
//
|
||||
// Created by xmfish on 14/12/30.
|
||||
// Copyright (c) 2014年 小鱼网. All rights reserved.
|
||||
//
|
||||
|
||||
#import "BaseViewModel.h"
|
||||
|
||||
@implementation BaseViewModel
|
||||
+ (NSDictionary *)JSONKeyPathsByPropertyKey {
|
||||
return @{
|
||||
@"errCode": @"errCode",
|
||||
@"errMessage": @"errMessage",
|
||||
};
|
||||
}
|
||||
|
||||
- (instancetype)initWithDictionary:(NSDictionary *)dictionaryValue error:(NSError **)error
|
||||
{
|
||||
self = [super initWithDictionary:dictionaryValue error:error];
|
||||
if (self != nil)
|
||||
{
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (BOOL)success
|
||||
{
|
||||
return ([self.errCode integerValue] == 0);
|
||||
}
|
||||
|
||||
- (BOOL)needLogin
|
||||
{
|
||||
return ([self.errCode integerValue] == 1001);
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,22 @@
|
||||
//
|
||||
// NSString+HXAddtions.h
|
||||
// HXWeb
|
||||
//
|
||||
// Created by hufeng on 12-2-13.
|
||||
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
@interface NSString (HXAddtions)
|
||||
|
||||
+(NSString *) jsonStringWithDictionary:(NSDictionary *)dictionary;
|
||||
|
||||
+(NSString *) jsonStringWithArray:(NSArray *)array;
|
||||
|
||||
+(NSString *) jsonStringWithString:(NSString *) string;
|
||||
|
||||
+(NSString *) jsonStringWithObject:(id) object;
|
||||
|
||||
+(NSString *) jsonStringWithRTArray:(NSArray *)array;
|
||||
@end
|
||||
@@ -0,0 +1,87 @@
|
||||
//
|
||||
// NSString+HXAddtions.m
|
||||
// HXWeb
|
||||
//
|
||||
// Created by hufeng on 12-2-13.
|
||||
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
|
||||
//
|
||||
|
||||
#import "NSString+HXAddtions.h"
|
||||
|
||||
@implementation NSString (HXAddtions)
|
||||
|
||||
+(NSString *) jsonStringWithString:(NSString *) string{
|
||||
return [NSString stringWithFormat:@"\"%@\"",
|
||||
[[string stringByReplacingOccurrencesOfString:@"\n" withString:@"\\n"] stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""]
|
||||
];
|
||||
}
|
||||
+(NSString *) jsonStringWithArray:(NSArray *)array{
|
||||
NSMutableString *reString = [NSMutableString string];
|
||||
[reString appendString:@"["];
|
||||
NSMutableArray *values = [NSMutableArray array];
|
||||
for (id valueObj in array) {
|
||||
NSString *value = [NSString jsonStringWithObject:valueObj];
|
||||
if (value) {
|
||||
[values addObject:[NSString stringWithFormat:@"%@",value]];
|
||||
}
|
||||
}
|
||||
[reString appendFormat:@"%@",[values componentsJoinedByString:@","]];
|
||||
[reString appendString:@"]"];
|
||||
return reString;
|
||||
}
|
||||
+(NSString *) jsonStringWithDictionary:(NSDictionary *)dictionary{
|
||||
NSArray *keys = [dictionary allKeys];
|
||||
NSMutableString *reString = [NSMutableString string];
|
||||
[reString appendString:@"{"];
|
||||
NSMutableArray *keyValues = [NSMutableArray array];
|
||||
for (int i=0; i<[keys count]; i++) {
|
||||
NSString *name = [keys objectAtIndex:i];
|
||||
id valueObj = [dictionary objectForKey:name];
|
||||
NSString *value = [NSString jsonStringWithObject:valueObj];
|
||||
if (value) {
|
||||
[keyValues addObject:[NSString stringWithFormat:@"\"%@\":%@",name,value]];
|
||||
}
|
||||
}
|
||||
[reString appendFormat:@"%@",[keyValues componentsJoinedByString:@","]];
|
||||
[reString appendString:@"}"];
|
||||
return reString;
|
||||
}
|
||||
+(NSString *) jsonStringWithObject:(id) object{
|
||||
NSString *value = nil;
|
||||
if (!object) {
|
||||
return value;
|
||||
}
|
||||
|
||||
if ([object isKindOfClass:[NSString class]]) {
|
||||
value = [NSString jsonStringWithString:object];
|
||||
}else if([object isKindOfClass:[NSDictionary class]]){
|
||||
value = [NSString jsonStringWithDictionary:object];
|
||||
}else if([object isKindOfClass:[NSArray class]]){
|
||||
value = [NSString jsonStringWithArray:object];
|
||||
}else if([object isKindOfClass:[NSNumber class]]){
|
||||
value = [NSString jsonStringWithString:[object stringValue]];
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
+(NSString *) jsonStringWithRTArray:(NSArray *)array{
|
||||
NSMutableString *reString = [[NSMutableString alloc] init];
|
||||
[reString appendString:@"{"];
|
||||
NSMutableArray *values = [[NSMutableArray alloc] initWithArray:array];
|
||||
for (int i=0; i<[values count]; i++) {
|
||||
NSString *tempString = [values objectAtIndex:i];
|
||||
NSString *value = [NSString jsonStringWithString:tempString];
|
||||
if (i%2 == 0) {
|
||||
[reString appendFormat:@"%@:",value];
|
||||
}else{
|
||||
if (i == [values count]-1) {
|
||||
[reString appendFormat:@"%@",value];
|
||||
}else{
|
||||
[reString appendFormat:@"%@,",value];
|
||||
}
|
||||
}
|
||||
}
|
||||
[reString appendFormat:@"}"];
|
||||
return reString;
|
||||
}
|
||||
@end
|
||||
@@ -0,0 +1,49 @@
|
||||
//
|
||||
// PropertyEntity.h
|
||||
// iOSFramework
|
||||
//
|
||||
// Created by xmfish on 14-9-22.
|
||||
// Copyright (c) 2014年 aDu. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
typedef NS_ENUM(NSUInteger, HTTPRequestType) {
|
||||
HTTPRequestTypeWithGET = 0,
|
||||
HTTPRequestTypeWithPOST,
|
||||
HTTPRequestTypeWithPOSTDATA,
|
||||
};
|
||||
|
||||
@class responesOBJ,DCParserConfiguration;
|
||||
|
||||
@interface proFile : NSObject
|
||||
|
||||
@property (nonatomic, strong) NSData *data;
|
||||
@property (nonatomic, strong) NSString *name; //参数名称
|
||||
@property (nonatomic, strong) NSString *filePath;
|
||||
//@"image/jpeg"
|
||||
@property (nonatomic, strong) NSString *mimeType;
|
||||
|
||||
@property (nonatomic, strong) NSArray *img;
|
||||
|
||||
@end
|
||||
|
||||
@interface PropertyEntity : NSObject
|
||||
|
||||
@property (nonatomic, assign) HTTPRequestType requireType;
|
||||
@property (nonatomic, copy) NSString *reqURL;
|
||||
@property (nonatomic, copy) NSString *url;
|
||||
@property (nonatomic, strong) id pro;
|
||||
@property (nonatomic, assign) Class responesOBJ;
|
||||
@property (nonatomic, strong)DCParserConfiguration* config;
|
||||
|
||||
@property (nonatomic, strong) proFile *pFile;
|
||||
//@property (nonatomic, strong) NSArray *img;
|
||||
|
||||
@property (nonatomic, assign) BOOL isCache;
|
||||
|
||||
@property (nonatomic, copy) NSString *localJsonFileName;
|
||||
|
||||
- (NSDictionary *)encodePro;
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,36 @@
|
||||
//
|
||||
// PropertyEntity.m
|
||||
// iOSFramework
|
||||
//
|
||||
// Created by xmfish on 14-9-22.
|
||||
// Copyright (c) 2014年 aDu. All rights reserved.
|
||||
//
|
||||
|
||||
#import "PropertyEntity.h"
|
||||
#import "XMFishBaseCode.h"
|
||||
|
||||
@implementation proFile
|
||||
|
||||
@end
|
||||
|
||||
@implementation PropertyEntity
|
||||
|
||||
- (NSDictionary *)encodePro
|
||||
{
|
||||
// XMFishBaseCode *bc = [[XMFishBaseCode alloc] init];
|
||||
//
|
||||
// [bc setToken:nil];
|
||||
// [bc setCallback:_url];
|
||||
// if (_pro)
|
||||
// {
|
||||
// [bc setParams:[NSMutableDictionary dictionaryWithDictionary:_pro]];
|
||||
// }
|
||||
//
|
||||
// NSMutableString *key = [[NSMutableString alloc]initWithString:[bc getBaseCode]];
|
||||
// [key stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
|
||||
// NSDictionary *dic = @{@"mkey":key};
|
||||
|
||||
return _pro;
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,22 @@
|
||||
//
|
||||
// RequireEngine.h
|
||||
// iOSFramework
|
||||
//
|
||||
// Created by xmfish on 14-9-22.
|
||||
// Copyright (c) 2014年 aDu. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "XiaoYuAPIClient.h"
|
||||
|
||||
@class PropertyEntity;
|
||||
|
||||
typedef void (^CompletionBlock) (id viewModel);
|
||||
typedef void (^FailedBlock) (NSError *error);
|
||||
|
||||
|
||||
@interface RequireEngine : NSObject
|
||||
|
||||
+ (AFHTTPRequestOperation *)requireWithProperty:(PropertyEntity *)proper completionBlock:(CompletionBlock)completionBlock failedBlock:(FailedBlock)failedBlock;
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,167 @@
|
||||
//
|
||||
// RequireEngine.m
|
||||
// iOSFramework
|
||||
//
|
||||
// Created by xmfish on 14-9-22.
|
||||
// Copyright (c) 2014年 aDu. All rights reserved.
|
||||
//
|
||||
|
||||
#import "RequireEngine.h"
|
||||
#import "PropertyEntity.h"
|
||||
|
||||
#import "MTLJSONAdapter.h"
|
||||
#import "NSData+Godzippa.h"
|
||||
|
||||
#import "EGOCache.h"
|
||||
|
||||
@implementation RequireEngine
|
||||
|
||||
+ (void)saveCacheWithAPIKey:(NSString *)apiKey property:(PropertyEntity *)proper data:(id)tweet{
|
||||
if (proper.isCache) {
|
||||
SEL selector = NSSelectorFromString(@"errCode");
|
||||
IMP imp = [tweet methodForSelector:selector];
|
||||
NSString* (*func)(id, SEL) = (void *)imp;
|
||||
NSString *tmpErrCode = func(tweet, selector);
|
||||
DLog(@"%@", tmpErrCode);
|
||||
|
||||
if ([tmpErrCode isKindOfClass:[NSString class]] && [tmpErrCode isEqualToString:@"0"]) {
|
||||
[[EGOCache globalCache] setObject:tweet forKey:apiKey];
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+ (void)responseFromCacheWithAPIKey:(NSString *)apiKey property:(PropertyEntity *)proper completionBlock:(CompletionBlock)completionBlock{
|
||||
if (proper.isCache && [[EGOCache globalCache] hasCacheForKey:apiKey]) {
|
||||
completionBlock([[EGOCache globalCache] objectForKey:apiKey]);
|
||||
}
|
||||
}
|
||||
|
||||
+ (AFHTTPRequestOperation *)requireWithProperty:(PropertyEntity *)proper completionBlock:(CompletionBlock)completionBlock failedBlock:(FailedBlock)failedBlock{
|
||||
|
||||
NSString *apiKey = proper.url;
|
||||
if (proper.pro && proper.pro[@"page"])
|
||||
{
|
||||
apiKey = [apiKey stringByAppendingFormat:@"%@",proper.pro[@"page"]];
|
||||
}
|
||||
|
||||
if (proper.requireType == HTTPRequestTypeWithGET) {
|
||||
return [[XiaoYuAPIClient sharedClient] GET:[proper reqURL]
|
||||
parameters:[proper url] ? [proper encodePro] : [proper pro]
|
||||
success:^(AFHTTPRequestOperation * __unused task, id json) {
|
||||
DLog(@"%@", json);
|
||||
|
||||
//______________________________________
|
||||
#warning just 4 testing .... delete when release
|
||||
NSURL *_localResourceFileURL = [[NSBundle mainBundle] URLForResource:proper.localJsonFileName ? proper.localJsonFileName : @"a" withExtension:@"json"];
|
||||
NSData *data = [NSData dataWithContentsOfURL:_localResourceFileURL];
|
||||
|
||||
if (data) {
|
||||
json = [NSJSONSerialization JSONObjectWithData:data
|
||||
options:kNilOptions
|
||||
error:NULL];
|
||||
}
|
||||
|
||||
DLog(@"%@", json);
|
||||
//______________________________________
|
||||
|
||||
if(json)
|
||||
{
|
||||
NSError *error = nil;
|
||||
id tweet = [MTLJSONAdapter modelOfClass:[proper.responesOBJ class] fromJSONDictionary:json error:&error];
|
||||
|
||||
if (completionBlock)
|
||||
{
|
||||
completionBlock(tweet);
|
||||
}
|
||||
|
||||
[self saveCacheWithAPIKey:apiKey property:proper data:tweet];
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
if (completionBlock)
|
||||
{
|
||||
completionBlock(nil);
|
||||
}
|
||||
}
|
||||
}
|
||||
failure:^(AFHTTPRequestOperation *__unused task, NSError *error) {
|
||||
DLog(@"%@", error.description);
|
||||
|
||||
if (failedBlock)
|
||||
{
|
||||
failedBlock(error);
|
||||
}else
|
||||
{
|
||||
[MBProgressHUD errorHudWithView:nil label:kNetworkErrorTips hidesAfter:1.0];
|
||||
}
|
||||
|
||||
[self responseFromCacheWithAPIKey:apiKey property:proper completionBlock:completionBlock];
|
||||
}];
|
||||
} else if(proper.requireType == HTTPRequestTypeWithPOST){
|
||||
return [[XiaoYuAPIClient sharedClient] POST:[proper reqURL]
|
||||
parameters:[proper url] ? [proper encodePro] : [proper pro]
|
||||
success:^(AFHTTPRequestOperation * __unused task, id JSON) {
|
||||
|
||||
DLog(@"%@", JSON);
|
||||
|
||||
NSError *error = nil;
|
||||
id tweet = [MTLJSONAdapter modelOfClass:[proper.responesOBJ class] fromJSONDictionary:JSON error:&error];
|
||||
|
||||
if (completionBlock) {
|
||||
completionBlock(tweet);
|
||||
}
|
||||
|
||||
[self saveCacheWithAPIKey:apiKey property:proper data:tweet];
|
||||
}
|
||||
failure:^(AFHTTPRequestOperation *__unused task, NSError *error) {
|
||||
NSLog(@"%@", error.description);
|
||||
|
||||
if (failedBlock) {
|
||||
failedBlock(error);
|
||||
}
|
||||
|
||||
[self responseFromCacheWithAPIKey:apiKey property:proper completionBlock:completionBlock];
|
||||
}];
|
||||
} else if(proper.requireType == HTTPRequestTypeWithPOSTDATA){
|
||||
return [[XiaoYuAPIClient sharedClient] POST:[proper reqURL]
|
||||
parameters:[proper url] ? [proper encodePro] : [proper pro]
|
||||
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
|
||||
for (int i = 0; i < proper.pFile.img.count; i++) {
|
||||
UIImage *uploadImage = proper.pFile.img[i];
|
||||
NSString *proName = [proper.pFile.name stringByAppendingFormat:@"%d", i];
|
||||
// NSData* data = UIImageJPEGRepresentation(uploadImage, 0.5);
|
||||
// DLog(@"%ld kB",data.length/(8*1024));
|
||||
[formData appendPartWithFileData:UIImageJPEGRepresentation(uploadImage, 0.5) name:proper.pFile.img.count == 1 ? proper.pFile.name : proName fileName:[proName stringByAppendingString:@".jpg" ] mimeType:@"image/jpg"];
|
||||
}
|
||||
}
|
||||
success:^(AFHTTPRequestOperation * __unused task, id JSON) {
|
||||
|
||||
DLog(@"%@", JSON);
|
||||
|
||||
NSError *error = nil;
|
||||
id tweet = [MTLJSONAdapter modelOfClass:[proper.responesOBJ class] fromJSONDictionary:JSON error:&error];
|
||||
|
||||
if (completionBlock) {
|
||||
completionBlock(tweet);
|
||||
}
|
||||
|
||||
[self saveCacheWithAPIKey:apiKey property:proper data:tweet];
|
||||
}
|
||||
failure:^(AFHTTPRequestOperation *__unused task, NSError *error) {
|
||||
NSLog(@"%@", error.description);
|
||||
|
||||
if (failedBlock) {
|
||||
failedBlock(error);
|
||||
}
|
||||
|
||||
[self responseFromCacheWithAPIKey:apiKey property:proper completionBlock:completionBlock];
|
||||
}];
|
||||
} else {
|
||||
NSAssert(proper.requireType == HTTPRequestTypeWithGET || proper.requireType == HTTPRequestTypeWithPOST, @"you must set requireType to GET or POST, or write something");
|
||||
return nil;
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,23 @@
|
||||
//
|
||||
// AFgzipResponseSerializer.h
|
||||
// iOSFramework
|
||||
//
|
||||
// Created by aDu on 14-10-21.
|
||||
// Copyright (c) 2014年 aDu. All rights reserved.
|
||||
//
|
||||
|
||||
#import "AFURLResponseSerialization.h"
|
||||
|
||||
@interface AFgzipResponseSerializer : AFHTTPResponseSerializer
|
||||
|
||||
/**
|
||||
The serializer used to generate requests to be compressed.
|
||||
*/
|
||||
@property (readonly, nonatomic, strong) id <AFURLResponseSerialization> serializer;
|
||||
|
||||
/**
|
||||
Creates and returns an instance of `AFgzipRequestSerializer`, using the specified serializer to generate requests to be compressed.
|
||||
*/
|
||||
+ (instancetype)serializerWithSerializer:(id <AFURLResponseSerialization>)serializer;
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,122 @@
|
||||
//
|
||||
// AFgzipResponseSerializer.m
|
||||
// iOSFramework
|
||||
//
|
||||
// Created by aDu on 14-10-21.
|
||||
// Copyright (c) 2014年 aDu. All rights reserved.
|
||||
//
|
||||
|
||||
#import "AFgzipResponseSerializer.h"
|
||||
|
||||
#import "NSData+Godzippa.h"
|
||||
|
||||
@interface AFgzipResponseSerializer ()
|
||||
@property (readwrite, nonatomic, strong) id <AFURLResponseSerialization> serializer;
|
||||
@end
|
||||
|
||||
@implementation AFgzipResponseSerializer
|
||||
|
||||
+ (instancetype)serializerWithSerializer:(id<AFURLResponseSerialization>)serializer {
|
||||
AFgzipResponseSerializer *gzipSerializer = [self serializer];
|
||||
gzipSerializer.serializer = serializer;
|
||||
|
||||
return gzipSerializer;
|
||||
}
|
||||
|
||||
- (id)responseObjectForResponse:(NSURLResponse *)response
|
||||
data:(NSData *)data
|
||||
error:(NSError *__autoreleasing *)error{
|
||||
|
||||
NSHTTPURLResponse *httpURLResponse = (NSHTTPURLResponse *)response;
|
||||
NSDictionary *headers = httpURLResponse.allHeaderFields;
|
||||
NSString *gzipStr = headers[@"Content-Encoding"];
|
||||
// NSString *identity = headers[@"Transfer-Encoding"];
|
||||
|
||||
NSError *serializationError = nil;
|
||||
NSDictionary *json = nil;
|
||||
|
||||
if ([gzipStr isEqualToString:@"gzip"]) {
|
||||
#ifdef DEBUG
|
||||
NSData *decompressedData = [data dataByGZipDecompressingDataWithError:nil];
|
||||
if (decompressedData) {
|
||||
NSLog(@"%@", [NSString stringWithUTF8String:[decompressedData bytes]]);
|
||||
|
||||
}
|
||||
#endif
|
||||
|
||||
if (!serializationError && data) {
|
||||
NSError *decompressionError;
|
||||
|
||||
NSData *decompressedData = [data dataByGZipDecompressingDataWithError:&decompressionError];
|
||||
|
||||
if (!decompressionError && decompressedData) {
|
||||
NSError *serializationJSONError;
|
||||
json = [NSJSONSerialization JSONObjectWithData:decompressedData
|
||||
options:kNilOptions
|
||||
error:&serializationJSONError];
|
||||
|
||||
if (!serializationJSONError && json) {
|
||||
return json;
|
||||
} else {
|
||||
if (error) {
|
||||
*error = serializationJSONError;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (error) {
|
||||
*error = decompressionError;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (error) {
|
||||
*error = serializationError;
|
||||
}
|
||||
}
|
||||
|
||||
return json;
|
||||
} else {
|
||||
|
||||
NSError *serializationJSONError;
|
||||
json = [NSJSONSerialization JSONObjectWithData:data
|
||||
options:kNilOptions
|
||||
error:&serializationJSONError];
|
||||
|
||||
if (!serializationJSONError && json) {
|
||||
return json;
|
||||
} else {
|
||||
if (error) {
|
||||
*error = serializationJSONError;
|
||||
}
|
||||
}
|
||||
|
||||
return json;
|
||||
}
|
||||
}
|
||||
|
||||
#pragma mark - NSCoder
|
||||
|
||||
- (id)initWithCoder:(NSCoder *)decoder {
|
||||
self = [super init];
|
||||
if (!self) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
self.serializer = [decoder decodeObjectForKey:NSStringFromSelector(@selector(serializer))];
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)encodeWithCoder:(NSCoder *)coder {
|
||||
[coder encodeObject:self.serializer forKey:NSStringFromSelector(@selector(serializer))];
|
||||
}
|
||||
|
||||
#pragma mark - NSCopying
|
||||
|
||||
- (id)copyWithZone:(NSZone *)zone {
|
||||
AFgzipResponseSerializer *serializer = [[[self class] allocWithZone:zone] init];
|
||||
serializer.serializer = self.serializer;
|
||||
|
||||
return serializer;
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,27 @@
|
||||
//
|
||||
// XMFishBaseCode.h
|
||||
// FanLiKa
|
||||
//
|
||||
// Created by aDu on 14-10-23.
|
||||
// Copyright (c) 2014年 xmfish. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
@interface XMFishBaseCode : NSObject
|
||||
|
||||
@property (nonatomic, strong, readwrite) NSString *token;
|
||||
@property (nonatomic, strong, readwrite) NSString *callback;
|
||||
@property (nonatomic, strong, readwrite) NSMutableDictionary *params;
|
||||
|
||||
@property (nonatomic, strong, readwrite) NSString *uid;
|
||||
@property (nonatomic, strong, readwrite) NSString *username;
|
||||
@property (nonatomic, strong, readwrite) NSString *cityId;
|
||||
|
||||
@property (nonatomic, strong, readwrite) NSString *mobile;
|
||||
|
||||
- (NSString *)getBaseCode;
|
||||
|
||||
+ (NSString *)getWkey;
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,156 @@
|
||||
//
|
||||
// XMFishBaseCode.m
|
||||
// FanLiKa
|
||||
//
|
||||
// Created by aDu on 14-10-23.
|
||||
// Copyright (c) 2014年 xmfish. All rights reserved.
|
||||
//
|
||||
|
||||
#import "XMFishBaseCode.h"
|
||||
#import "NSString+HXAddtions.h"
|
||||
#import "SecureUDID.h"
|
||||
#import <CoreLocation/CoreLocation.h>
|
||||
@implementation XMFishBaseCode
|
||||
|
||||
static char encodingTable[] = "<Aa0Bb1Cc2Dd3Ee4Ff5Gg6Hh7Ii8Jj9KkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz>";
|
||||
|
||||
- (id)init
|
||||
{
|
||||
self = [super init];
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)setParams:(NSMutableDictionary *)Params
|
||||
{
|
||||
if (_params != Params)
|
||||
{
|
||||
_params = nil;
|
||||
_params = Params;
|
||||
}
|
||||
}
|
||||
|
||||
- (NSString *)getBaseCode
|
||||
{
|
||||
|
||||
|
||||
NSMutableDictionary *data = [[NSMutableDictionary alloc] init];
|
||||
|
||||
NSTimeInterval timeStamp = [[NSDate date] timeIntervalSince1970];
|
||||
data[@"timestamp"] = [NSString stringWithFormat:@"%f", timeStamp];
|
||||
|
||||
|
||||
//app版本号
|
||||
NSString *appVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
|
||||
if (appVersion)
|
||||
{
|
||||
[data setObject:appVersion forKey:@"appversion"];
|
||||
}
|
||||
|
||||
//网络类型
|
||||
NSString *netWorkType = [NSString stringWithFormat:@"%d", [CommonUtil networkType]];
|
||||
[data setObject:netWorkType forKey:@"networktype"];
|
||||
|
||||
//设备类型
|
||||
NSString *deviceName = [CommonUtil getUtsName];
|
||||
if (deviceName)
|
||||
{
|
||||
[data setObject:deviceName forKey:@"devicename"];
|
||||
}
|
||||
|
||||
//唯一的设备号,用于服务端识别游客
|
||||
NSString *domain = @"com.shengshi.ufun";
|
||||
NSString *key = @"xmfishufunkey";
|
||||
NSString *udid = [SecureUDID UDIDForDomain:domain usingKey:key];
|
||||
if (udid)
|
||||
{
|
||||
[data setObject:udid forKey:@"udid"];
|
||||
}
|
||||
|
||||
DLog(@"Http param : %@", data);
|
||||
return [NSString jsonStringWithDictionary:data];
|
||||
}
|
||||
|
||||
- (NSData *)encode:(const uint8_t*) input length:(NSInteger) length
|
||||
{
|
||||
NSInteger _dlen =((length + 2)/3 * 4)+1;
|
||||
NSMutableData* tmpdata = [NSMutableData dataWithLength:_dlen];
|
||||
uint8_t* tmpchar = (uint8_t*)tmpdata.mutableBytes;
|
||||
srand((unsigned)time(NULL));
|
||||
|
||||
for (NSInteger i = 0; i < length; i += 3)
|
||||
{
|
||||
NSInteger value = 0;
|
||||
for (NSInteger j = i; j < (i + 3); j++)
|
||||
{
|
||||
value <<= 8;
|
||||
if (j < length)
|
||||
{
|
||||
value |= (0xFF & input[j]);
|
||||
}
|
||||
}
|
||||
NSInteger index = (i / 3) * 4;
|
||||
tmpchar[index + 0] = encodingTable[(value >> 18) & 0x3F];
|
||||
tmpchar[index + 1] = encodingTable[(value >> 12) & 0x3F];
|
||||
if ((i + 1) < length)
|
||||
{
|
||||
tmpchar[index + 2] = encodingTable[(value >> 6) & 0x3F];
|
||||
}
|
||||
else
|
||||
{
|
||||
tmpchar[index + 2] = 0;
|
||||
_dlen--;
|
||||
}
|
||||
if ((i + 2) < length)
|
||||
{
|
||||
tmpchar[index + 3] = encodingTable[(value >> 0) & 0x3F];
|
||||
}
|
||||
else
|
||||
{
|
||||
tmpchar[index + 3] = 0;
|
||||
_dlen--;
|
||||
}
|
||||
}
|
||||
int pos=1+ abs((rand()*1234))%60; //字符偏移量
|
||||
int add=encodingTable[pos];
|
||||
int step=((add%_dlen)%5)+4;
|
||||
NSMutableData *_output = [NSMutableData dataWithLength:_dlen];
|
||||
uint8_t* output = (uint8_t*)_output.mutableBytes;
|
||||
tmpchar[_dlen-1]=add;
|
||||
for (int k = 0; k <= _dlen; k+=step)
|
||||
{
|
||||
int m=0;
|
||||
if (k+step+1 > _dlen)
|
||||
{
|
||||
while (m < step && tmpchar[k+m] > 0)
|
||||
{
|
||||
output[k+m]=tmpchar[k+m];
|
||||
m++;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
while (m < step && tmpchar[k+step-m-1] > 0)
|
||||
{
|
||||
output[k+m]=tmpchar[k+step-m-1];
|
||||
m++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return _output;
|
||||
}
|
||||
|
||||
- (NSString *)encode:(NSString *)string
|
||||
{
|
||||
NSData *rayBety=[string dataUsingEncoding:NSUTF8StringEncoding];
|
||||
NSData *newBety=[self encode:rayBety.bytes length:rayBety.length];
|
||||
NSString *newString=[[NSString alloc]initWithData:newBety encoding:NSUTF8StringEncoding];
|
||||
return newString;
|
||||
}
|
||||
|
||||
+ (NSString *)getWkey
|
||||
{
|
||||
return [[[XMFishBaseCode alloc] init] getBaseCode];
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,15 @@
|
||||
//
|
||||
// XiaoYuAPIClient.h
|
||||
// iOSFramework
|
||||
//
|
||||
// Created by xmfish on 14-9-22.
|
||||
// Copyright (c) 2014年 aDu. All rights reserved.
|
||||
//
|
||||
|
||||
#import "AFHTTPRequestOperationManager.h"
|
||||
|
||||
@interface XiaoYuAPIClient : AFHTTPRequestOperationManager
|
||||
|
||||
+ (instancetype)sharedClient;
|
||||
+ (NSString *)getApiBaseString;
|
||||
@end
|
||||
@@ -0,0 +1,50 @@
|
||||
//
|
||||
// XiaoYuAPIClient.m
|
||||
// iOSFramework
|
||||
//
|
||||
// Created by xmfish on 14-9-22.
|
||||
// Copyright (c) 2014年 aDu. All rights reserved.
|
||||
//
|
||||
|
||||
#import "XiaoYuAPIClient.h"
|
||||
|
||||
#import "AFgzipResponseSerializer.h"
|
||||
|
||||
#import "AFURLRequestSerialization.h"
|
||||
|
||||
@implementation XiaoYuAPIClient
|
||||
|
||||
|
||||
|
||||
+ (instancetype)sharedClient
|
||||
{
|
||||
static XiaoYuAPIClient *_sharedClient = nil;
|
||||
static dispatch_once_t onceToken;
|
||||
dispatch_once(&onceToken, ^{
|
||||
_sharedClient = [[XiaoYuAPIClient alloc] initWithBaseURL:[NSURL URLWithString:[self getApiBaseString]]];
|
||||
_sharedClient.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
|
||||
|
||||
[_sharedClient.requestSerializer setValue:@"text/html" forHTTPHeaderField:@"Content-Type"];
|
||||
_sharedClient.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
|
||||
|
||||
// [_sharedClient.requestSerializer setValue:@"gzip" forHTTPHeaderField:@"Content-Encoding"];
|
||||
|
||||
// [_sharedClient.requestSerializer setValue:@"identity" forHTTPHeaderField:@"Accept-Encoding"];
|
||||
|
||||
// _sharedClient.responseSerializer = [AFgzipResponseSerializer serializerWithSerializer:[AFHTTPResponseSerializer serializer]];
|
||||
//
|
||||
// _sharedClient.requestSerializer = [AFJSONRequestSerializer serializer];
|
||||
// [_sharedClient.responseSerializer setValue:@"gizp" forKey:@"Content-Encoding"];
|
||||
// _sharedClient.responseSerializer.acceptableContentTypes = [_sharedClient.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
|
||||
|
||||
});
|
||||
|
||||
return _sharedClient;
|
||||
}
|
||||
|
||||
+ (NSString *)getApiBaseString
|
||||
{
|
||||
return Ash_AWord_API_URL;
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user