Skip to content

Commit 553f762

Browse files
committed
私信 api 接口更新
1 parent 80d9618 commit 553f762

File tree

2 files changed

+49
-31
lines changed

2 files changed

+49
-31
lines changed

Coding_iOS/Util/Manager/CodingNetAPIClient.m

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -237,39 +237,57 @@ - (void)uploadImage:(UIImage *)image path:(NSString *)path name:(NSString *)name
237237
successBlock:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
238238
failureBlock:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
239239
progerssBlock:(void (^)(CGFloat progressValue))progress{
240-
241-
NSData *data = [image dataForCodingUpload];
242240

243-
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
244-
formatter.dateFormat = @"yyyyMMddHHmmss";
245-
NSString *str = [formatter stringFromDate:[NSDate date]];
246-
NSString *fileName = [NSString stringWithFormat:@"%@_%@.jpg", [Login curLoginUser].global_key, str];
241+
NSData *data = [image dataForCodingUpload];
242+
NSString *fileName = [NSString stringWithFormat:@"%@_%@.jpg", [Login curLoginUser].global_key, [[NSDate date] stringWithFormat:@"yyyyMMddHHmmss"]];
247243
DebugLog(@"\nuploadImageSize\n%@ : %.0f", fileName, (float)data.length/1024);
248-
249-
AFHTTPRequestOperation *operation = [self POST:path parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
250-
[formData appendPartWithFileData:data name:name fileName:fileName mimeType:@"image/jpeg"];
251-
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
252-
DebugLog(@"Success: %@ ***** %@", operation.responseString, responseObject);
253-
id error = [self handleResponse:responseObject];
254-
if (error && failure) {
255-
failure(operation, error);
256-
}else{
257-
success(operation, responseObject);
258-
}
259-
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
260-
DebugLog(@"Error: %@ ***** %@", operation.responseString, error);
261-
if (failure) {
262-
failure(operation, error);
263-
}
264-
}];
265244

266-
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
267-
CGFloat progressValue = (float)totalBytesWritten/(float)totalBytesExpectedToWrite;
268-
if (progress) {
269-
progress(progressValue);
270-
}
271-
}];
272-
[operation start];
245+
__weak typeof(self) weakSelf = self;
246+
void (^uploadBlock)(NSDictionary *) = ^(NSDictionary *uploadParams){
247+
AFHTTPRequestOperation *operation = [weakSelf POST:path parameters:uploadParams constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
248+
[formData appendPartWithFileData:data name:name fileName:fileName mimeType:@"image/jpeg"];
249+
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
250+
DebugLog(@"Success: %@ ***** %@", operation.responseString, responseObject);
251+
id error = [self handleResponse:responseObject];
252+
if (error && failure) {
253+
failure(operation, error);
254+
}else{
255+
success(operation, responseObject);
256+
}
257+
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
258+
DebugLog(@"Error: %@ ***** %@", operation.responseString, error);
259+
if (failure) {
260+
failure(operation, error);
261+
}
262+
}];
263+
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
264+
CGFloat progressValue = (float)totalBytesWritten/(float)totalBytesExpectedToWrite;
265+
if (progress) {
266+
progress(progressValue);
267+
}
268+
}];
269+
[operation start];
270+
};
271+
if ([path isEqualToString:@"https://up.qbox.me/"]) {//先拿 token
272+
NSDictionary *params = @{
273+
@"fileName": fileName,
274+
@"fileSize": @(data.length)
275+
};
276+
[[CodingNetAPIClient sharedJsonClient] requestJsonDataWithPath:@"api/upload_token/public/images" withParams:params withMethodType:Get andBlock:^(id data, NSError *error) {
277+
if (data) {
278+
NSDictionary *result = data[@"data"];
279+
NSMutableDictionary *uploadParams = @{}.mutableCopy;
280+
uploadParams[@"token"] = result[@"uptoken"];
281+
uploadParams[@"x:time"] = result[@"time"];
282+
uploadParams[@"x:authToken"] = result[@"authToken"];
283+
uploadParams[@"x:userId"] = result[@"userId"];
284+
uploadParams[@"key"] = fileName;
285+
uploadBlock(uploadParams);
286+
}
287+
}];
288+
}else{
289+
uploadBlock(nil);
290+
}
273291
}
274292

275293
- (void)uploadAssets:(NSArray *)assets

Coding_iOS/Util/Manager/Coding_NetAPIManager.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3055,7 +3055,7 @@ - (void)uploadTweetImage:(UIImage *)image
30553055
done(nil, [NSError errorWithDomain:@"DATA EMPTY" code:0 userInfo:@{NSLocalizedDescriptionKey : @"有张照片没有读取成功"}]);
30563056
return;
30573057
}
3058-
[[CodingNetAPIClient sharedJsonClient] uploadImage:image path:@"api/tweet/insert_image" name:@"tweetImg" successBlock:^(AFHTTPRequestOperation *operation, id responseObject) {
3058+
[[CodingNetAPIClient sharedJsonClient] uploadImage:image path:@"https://up.qbox.me/" name:@"file" successBlock:^(AFHTTPRequestOperation *operation, id responseObject) {
30593059
NSString *reslutString = [responseObject objectForKey:@"data"];
30603060
DebugLog(@"%@", reslutString);
30613061
done(reslutString, nil);

0 commit comments

Comments
 (0)