CNLiveUploadIcon.m
4.19 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//
// 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 = @"https://api.cnlive.com/open/api2/token";
//上传头像
static NSString *uploadIconURL = @"https://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 (^)(NSDictionary *))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 (^)(NSDictionary *))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 {
if (failure) {
failure(responseObject);
}
}
} else {
NSDictionary *errorDic = @{@"errorCode": @"-1",
@"errorMessage": @"获取access_token失败"};
if (failure) {
failure(errorDic);
}
}
} failure:^(NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSDictionary *errorDic = @{@"errorCode": [NSString stringWithFormat:@"%ld", error.code], @"errorMessage": error.localizedDescription};
if (failure) {
failure(errorDic);
}
}];
}
- (void)uploadIconRequestWithUid:(NSString *)uid icon:(UIImage *)image token:(NSString *)token access_token:(NSString *)accessToken success:(void (^)(id))success failure:(void (^)(NSDictionary *))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];
if ([[responseObject allKeys] containsObject:@"errorCode"] && [responseObject[@"errorCode"] isEqualToString:@"0"]) {
if (success) {
success(responseObject);
}
} else {
if (failure) {
failure(responseObject);
}
[CNLiveUserSystemTools errorStat:responseObject[@"errorMessage"]];
}
} failure:^(NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSDictionary *errorDic = @{@"errorCode": [NSString stringWithFormat:@"%ld", error.code], @"errorMessage": error.localizedDescription};
if (failure) {
failure(errorDic);
}
[CNLiveUserSystemTools errorStat:error.localizedDescription];
}];
}
@end