Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nuclearsandwich/2bca142c738eac127dc2 to your computer and use it in GitHub Desktop.
Save nuclearsandwich/2bca142c738eac127dc2 to your computer and use it in GitHub Desktop.

####API 接口文档

字段 类型 是否必须 说明
userName string true 用户名
password string true 密码
  • 返回数据
字段 类型 是否必须 说明
userId int true 用户id
status int true 成功登陆返回1,否则返回0
error string true status为0的时候返回错误信息,为1的时候为空字符串""
  • 请求实例:
{
    "userName":"testUser",
    "password":"password",
}
  • 返回数据实例
{
    "userId":1,
    "status":"1",
    "error":""
}
#import "User_Model.h"
-(void)login {
User_API *user_API = [User_API shareInstance];
LOGIN *login = [[LOGIN alloc] init];
login.userName = userNameTextField.text;
login.password = passwordTextField.text;
[user_API userLogin:login];
user_API.login = ^(NSDictionary *dic){
if ([[dic objectForKey:@"status"] intValue] == 1) {
NSLog(@"成功登录 dic = %@",dic);
} else {
NSLog(@"登录失败 error = %@",[dic objectForKey:@"error"]);
}
};
}
#import <Foundation/Foundation.h>
#pragma mark - 登陆
@interface LOGIN : NSObject
@property (strong, nonatomic) NSString *userName;
@property (strong, nonatomic) NSString *password;
@end
#pragma mark - 请求API
// block object
typedef void(^Login) (NSDictionary *dic);
@interface User_API : NSObject
@property (copy, nonatomic) Login login;
// singleton
+(User_API *)shareInstance;
// 用户登录
-(void)userLogin:(LOGIN *) loginModel;
@end
#import "User_Model.h"
#import <AFHTTPRequestOperationManager.h>
#pragma mark - 登陆
@implementation LOGIN
@synthesize userName = _userName;
@synthesize password = _password;
@end
#pragma mark - 请求API
static User_API *userShareInstance;
@implementation User_API
@synthesize login = _login;
+(User_API *)shareInstance{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
userShareInstance = [[User_API alloc] init];
});
return userShareInstance;
}
// 用户登录
-(void)userLogin:(LOGIN *) loginModel{
NSDictionary *parameter = @{@"userName": loginModel.userName,
@"password":loginModel.password,
};
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
// manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
[manager POST:[NSString stringWithFormat:@"http://www.xxx.com/login"] parameters:parameter success:^
(AFHTTPRequestOperation *operation, id responseObject){
NSDictionary *dic = [self getDicFromOperation:operation];
self.login(dic);
}failure:^(AFHTTPRequestOperation *operation, NSError *error) {
self.failrequset([error localizedDescription]);
NSLog(@"userLogin error = %@",[error localizedDescription]);
}];
}
- (NSDictionary *)getDicFromOperation:(AFHTTPRequestOperation *)operation
{
NSString *str = [NSString stringWithString:operation.responseString];
NSData *data = [[NSData alloc] initWithData:[str dataUsingEncoding:NSUTF8StringEncoding]];
//系统自带JSON解析
NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
return resultDic;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment