123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 |
- //
- // LCCommonHttp.m
- // LiveChat
- //
- // Created by 张灿 on 2018/4/11.
- // Copyright © 2018年 caicai. All rights reserved.
- //
- #import "LCCommonHttp.h"
- #import <AliyunOSSiOS/AliyunOSSiOS.h>
- OSSClient *client;
- @interface LCCommonHttp ()
- @end
- @implementation LCCommonHttp
- + (void)initOSSClientWithModel:(YOUPAILZStsModel *)model{
- id<OSSCredentialProvider> credential = [[OSSFederationCredentialProvider alloc] initWithFederationTokenGetter:^OSSFederationToken * {
- OSSFederationToken * token = [OSSFederationToken new];
- token.tAccessKey = model.youpaipAccessKeyId;
- token.tSecretKey = model.youpaipAccessKeySecret;
- token.tToken = model.youpaipSecurityToken;
- token.expirationTimeInGMTFormat = model.youpaipExpiration;
- return token;
- }];
- NSString *endpoint = [NSString stringWithFormat:@"https://%@",model.youpaipEndpoint];
- client = [[OSSClient alloc] initWithEndpoint:endpoint credentialProvider:credential];
- }
- //关注
- + (void)followUserId:(NSString*)userId{
- [LCCommonHttp followUserId:userId liveId:nil];
- }
- + (void)followUserId:(NSString*)userId liveId:(NSString *)liveId{
-
- [LCHttpHelper requestWithURLString:UserFollow parameters:@{@"follow_uid":userId,@"live_id":liveId.length == 0 ? @"" : liveId} needToken:YES type:(HttpRequestTypePost) success:^(id responseObject) {
-
- NSDictionary* dict = (NSDictionary*)responseObject;
- NSInteger code = [[dict objectForKey:@"code"] integerValue];
- if (code==0) {//成功
- NSInteger is_follow = 0;
- NSString* action = [[dict objectForKey:@"data"]objectForKey:@"action"];
- if ([action isEqualToString:@"add"]) {
- [ZCHUDHelper showTitle:@"已关注"];
- is_follow = 1;
- }else if ([action isEqualToString:@"delete"]){
- [ZCHUDHelper showTitle:@"已取消关注"];
- }
- /// 关注状态改变,发送通知
- [[NSNotificationCenter defaultCenter]postNotificationName:@"ChangeFollowState" object:self userInfo:@{@"follow_uid":userId,@"is_follow":@(is_follow)}];
- }
- } failure:^(NSError *error) {
-
- }];
- }
- //拉黑
- + (void)blackUserId:(NSString*)userId{
- [self blackUserId:userId liveId:nil];
- }
- + (void)blackUserId:(NSString *)userId liveId:(NSString *)liveId{
- [LCHttpHelper requestWithURLString:UserBlack parameters:@{@"black_uid":userId,@"live_id":liveId.length == 0 ? @"" : liveId} needToken:YES type:(HttpRequestTypePost) success:^(id responseObject) {
- NSDictionary* dict = (NSDictionary*)responseObject;
- NSInteger code = [[dict objectForKey:@"code"] integerValue];
- if (code==0) {//成功
- NSString* action = [[dict objectForKey:@"data"]objectForKey:@"action"];
- if ([action isEqualToString:@"add"]) {
- [ZCHUDHelper showTitle:@"已拉黑"];
- }else if ([action isEqualToString:@"delete"]){
- [ZCHUDHelper showTitle:@"已取消拉黑"];
- }
- }
- } failure:^(NSError *error) {
-
- }];
- }
- + (void)dynamicLike:(NSString*)dynamicId type:(NSInteger)type{//0点赞 1取消点赞
- [LCHttpHelper requestWithURLString:DynamicLike parameters:@{@"dynamic_id":dynamicId,@"type":@(type)} needToken:YES type:(HttpRequestTypePost) success:^(id responseObject) {
- NSDictionary* dict = (NSDictionary*)responseObject;
- NSInteger code = [[dict objectForKey:@"code"] integerValue];
- if (code==0) {//成功
- }
- } failure:^(NSError *error) {
-
- }];
- }
- + (void)uploadImages:(NSArray*)images type:(NSString*)type succress:(succressBlock)succress{
-
- NSMutableArray* imgNameArray = [NSMutableArray array];
- for (int i =0; i<images.count; i++) {
- NSString* str = [NSString stringWithFormat:@"file[%d]",i];
- [imgNameArray addObject:str];
- }
-
- // [LCCommonHttp uploadWithImages:images Type:@"anchor" successBlock:^(NSArray<NSString *> *ossImagePaths) {
- // [ZCHUDHelper dismiss];
- //
- // }];
- // + (void)uploadWithImages:(NSArray *)images Type:(NSString *)type successBlock:(void (^)(NSArray <NSString *>*ossImagePaths))succress;
-
- [LCHttpHelper postImageArrayToSeverWithURLString:UploadImages imgArray:images imgNameArray:imgNameArray parmeters:@{@"type":type} needToken:YES success:^(id responseObject) {
-
- NSDictionary* dict = (NSDictionary*)responseObject;
- NSInteger code = [[dict objectForKey:@"code"] integerValue];
- if (code==0) {
- NSArray* imageA = [dict objectForKey:@"data"];
- if (succress) {
- succress(imageA);
- }
- }
- } failure:^(NSError *error) {
- [ZCHUDHelper dismiss];
- }];
-
- }
- /// 获取sts凭证
- /// @param type 类型
- /// album 个人相册
- /// anchor 女神认证
- /// @param succress 成功回调
- + (void)loadStsWithType:(NSString *)type successBlock:(void (^)(YOUPAILZStsModel *model))succress{
- [LCHttpHelper requestWithURLString:STS parameters:@{@"type":type} needToken:YES type:HttpRequestTypePost success:^(id responseObject) {
- NSDictionary* dict = (NSDictionary*)responseObject;
- NSInteger code = [[dict objectForKey:@"code"] integerValue];
- if (code==0) {
- YOUPAILZStsModel *model = [YOUPAILZStsModel mj_objectWithKeyValues:[dict objectForKey:@"data"]];
- if (succress) {
- succress(model);
- }
- }
- } failure:^(NSError *error) {
- if (succress) {
- // succress(nil,error);
- }
- }];
- }
- /// 上传视频文件
- /// @param videoPath 视频文件目录
- /// @param type 类型 (album 个人相册,anchor 女神认证,dynamic 动态)
- /// @param succress 成功回调
- + (void)uploadWithVideoPath:(NSString *)videoPath Type:(NSString *)type successBlock:(void (^)(NSString *ossFilePath))succress{
- [self loadStsWithType:type successBlock:^(YOUPAILZStsModel *model) {
- [self ossUploadFileWithModel:model filePath:videoPath fileData:nil fileType:@".mp4" successBlock:^(NSString *ossFilePath) {
- dispatch_async(dispatch_get_main_queue(), ^{
- if (succress) {
- succress(ossFilePath);
- }
- });
- }];
- }];
- }
- + (void)uploadWithLocalFile:(NSString *)localPath successBlock:(void (^)(NSString *ossFilePath))succress{
- [self loadStsWithType:@"log" successBlock:^(YOUPAILZStsModel *model) {
- [self ossUploadFileWithModel:model filePath:localPath fileData:nil fileType:@".log" successBlock:^(NSString *ossFilePath) {
- dispatch_async(dispatch_get_main_queue(), ^{
- if (succress) {
- succress(ossFilePath);
- }
- });
- }];
- }];
- }
- /// 上传音频文件
- /// @param videoPath 音频文件目录
- /// @param type 类型
- /// album 个人相册
- /// anchor 女神认证
- /// game/img 游戏陪玩上传战绩截图等
- /// game/mp3 游戏陪玩上传15秒录音
- /// dynamic 动态
- /// @param succress 成功回调
- + (void)uploadWithAudioPath:(NSString *)videoPath Type:(NSString *)type successBlock:(void (^)(NSString *ossFilePath))succress{
- [self loadStsWithType:type successBlock:^(YOUPAILZStsModel *model) {
- [self ossUploadFileWithModel:model filePath:videoPath fileData:nil fileType:@".mp3" successBlock:^(NSString *ossFilePath) {
- dispatch_async(dispatch_get_main_queue(), ^{
- if (succress) {
- succress(ossFilePath);
- }
- });
- }];
- }];
- }
- /// 上传AAC格式音频
- + (void)uploadWithAACAudioPath:(NSString *)audioPath successBlock:(void (^)(NSString *ossFilePath))succress{
- [self loadStsWithType:@"aac" successBlock:^(YOUPAILZStsModel *model) {
- [self ossUploadFileWithModel:model filePath:audioPath fileData:nil fileType:@".aac" successBlock:^(NSString *ossFilePath) {
- dispatch_async(dispatch_get_main_queue(), ^{
- if (succress) {
- succress(ossFilePath);
- }
- });
- }];
- }];
- }
- /// 上传图片
- /// @param images 图片数组 只能传递UIImage 或者 NSdata
- /// @param type 类型 (album 个人相册,anchor 女神认证,dynamic 动态)
- /// @param succress 成功回调
- + (void)uploadWithImages:(NSArray *)images Type:(NSString *)type successBlock:(void (^)(NSArray <NSString *>*ossImagePaths))succress{
- if (images.count == 0) {
- return;
- }
- [self loadStsWithType:type successBlock:^(YOUPAILZStsModel *model) {
- __block NSMutableArray *ossImagePaths = [NSMutableArray array];
- dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
- dispatch_async(queue, ^{
- dispatch_group_t groupE = dispatch_group_create();
- for (NSInteger i = 0 ; i < images.count; i ++) {
- NSData *data = images[i];
- if ([data isKindOfClass:[UIImage class]]) {
- data = UIImageJPEGRepresentation((UIImage *)data, 1.0f);
- }
- dispatch_group_enter(groupE);
- [self ossUploadFileWithModel:model filePath:nil fileData:data fileType:@".jpg" successBlock:^(NSString *ossFilePath) {
- dispatch_group_leave(groupE);
- [ossImagePaths addObject:ossFilePath];
- }];
- }
- //所有请求都成功以后刷新页面
- dispatch_group_notify(groupE, dispatch_get_main_queue(), ^{
- if (succress) {
- succress(ossImagePaths);
- }
- });
- });
-
- }];
-
- }
- /// 阿里云文件上传
- /// @param model sts凭证
- /// @param filePath 文件路径
- /// @param fileData 16进制文件
- /// @param fileType 文件类型 例如:.mp4 .jpg
- /// @param succress 成功回调
- + (void)ossUploadFileWithModel:(YOUPAILZStsModel *)model filePath:(NSString *)filePath fileData:(NSData *)fileData fileType:(NSString *)fileType successBlock:(void (^)(NSString *ossFilePath))succress{
-
- // if (!client){
- [self initOSSClientWithModel:model];
- // }
-
- OSSPutObjectRequest * put = [OSSPutObjectRequest new];
- // 配置必填字段,其中bucketName为存储空间名称;objectKey等同于objectName,表示将文件上传到OSS时需要指定包含文件后缀在内的完整路径,例如abc/efg/123.jpg。
- NSString *ossFilePath = [model.youpaipdir stringByAppendingPathComponent:[NSString stringWithFormat:@"%@%@",[LCTools randomFilename],fileType]];
-
- put.bucketName = model.youpaipBucketName;
- put.objectKey = ossFilePath;
-
- if (filePath) {
- put.uploadingFileURL = [NSURL fileURLWithPath:filePath]; // 根据文件目录上传
- }else{
- put.uploadingData = fileData; // 直接上传NSData
- }
- // 配置可选字段。
- put.uploadProgress = ^(int64_t bytesSent, int64_t totalByteSent, int64_t totalBytesExpectedToSend) {
- // 指定当前上传长度、当前已经上传总长度、待上传的总长度。
- // NSLog(@"%lld, %lld, %lld", bytesSent, totalByteSent, totalBytesExpectedToSend);
- };
- // 配置可选字段。
- // put.contentType = @"";
- // put.contentMd5 = @"";
- // put.contentEncoding = @"";
- // put.contentDisposition = @"";
- // 可以在上传文件时设置元信息或者HTTP头部。
- // put.objectMeta = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"value1", @"x-oss-meta-name1", nil];
- OSSTask * putTask = [client putObject:put];
- [putTask continueWithBlock:^id(OSSTask *task) {
- if (!task.error && succress) {
- succress(ossFilePath);
- } else {
- [ZCHUDHelper showTitle:task.error.localizedDescription];
- NSLog(@"upload object failed, error: %@" , task.error);
- }
- return nil;
- }];
- if (fileData) {
- [putTask waitUntilFinished];
- }
-
- }
- @end
|