CNLiveUploadIcon.m
3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//
// CNLiveUploadIcon.m
// CNLiveUserSystemDemo
//
// Created by iOS on 16/9/20.
// Copyright © 2016年 zhulin. All rights reserved.
//
#import "CNLiveUploadIcon.h"
#import "CNLiveUserSystemTools.h"
#import "CNLiveNetworkManager.h"
#import "NSDictionary+Safety.h"
//获取access_token
static NSString *accessTokenURL = @"http://api.cnlive.com/open/api2/token";
//上传头像
static NSString *uploadIconURL = @"http://api.cnlive.com/open/api2/user/updateUserFaceById";
@implementation CNLiveUploadIcon
- (void)uploadIconWithAppId:(NSString *)appId uid:(NSString *)uid icon:(UIImage *)image token:(NSString *)token secret:(NSString *)secret success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
[self getAccessTokenRequestWithAppId:appId uid:uid icon:image token:token secret:secret success:success failure:failure];
}
#pragma mark - request
- (void)getAccessTokenRequestWithAppId:(NSString *)appId uid:(NSString *)uid icon:(UIImage *)image token:(NSString *)token secret:(NSString *)secret success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
NSDictionary *paramters = @{@"appId":[CNLiveUserSystemTools verify:appId],
@"secret":[CNLiveUserSystemTools verify:secret]};
[[CNLiveNetworkManager manager] requestPostURL:accessTokenURL parameters:paramters success:^(NSURLResponse * _Nullable response, id _Nullable responseObject) {
responseObject = [responseObject validatedDictionary];
if ([[responseObject allKeys] containsObject:@"errorCode"]) {
if ([[responseObject objectForKey:@"errorCode"] isEqualToString:@"0"]) {
NSDictionary *dic = responseObject[@"data"];
[self uploadIconRequestWithUid:uid icon:image token:token access_token:dic[@"access_token"] success:success failure:failure];
} else {
NSError *error = [NSError errorWithDomain:NSURLErrorDomain code:-1 userInfo:@{NSLocalizedDescriptionKey :responseObject[@"errorMessage"]}];
failure(error);
}
} else {
NSError *error = [NSError errorWithDomain:NSURLErrorDomain code:-1 userInfo:@{NSLocalizedDescriptionKey :@"获取access_token失败"}];
failure(error);
}
} failure:^(NSURLResponse * _Nullable response, NSError * _Nullable error) {
failure(error);
}];
}
- (void)uploadIconRequestWithUid:(NSString *)uid icon:(UIImage *)image token:(NSString *)token access_token:(NSString *)accessToken success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
NSData *data = UIImageJPEGRepresentation(image, 0.5);
NSDictionary *paramters = @{@"uid":[CNLiveUserSystemTools verify:uid],
@"clientPlat":@"i",
@"token":[CNLiveUserSystemTools verify:token],
@"access_token":[CNLiveUserSystemTools verify:accessToken]};
[[CNLiveNetworkManager manager] uploadImageWithURL:uploadIconURL parameters:paramters imageData:data success:^(NSURLResponse * _Nullable response, id _Nullable responseObject) {
responseObject = [responseObject validatedDictionary];
success(responseObject);
} failure:^(NSURLResponse * _Nullable response, NSError * _Nullable error) {
failure(error);
}];
}
@end