CNLiveFeedback.m 2.39 KB
//
//  CNLiveFeedback.m
//  CNLiveUserSystemDemo
//
//  Created by iOS on 16/9/13.
//  Copyright © 2016年 zhulin. All rights reserved.
//

#import "CNLiveFeedback.h"
#import "CNLiveNetworkManager.h"
#import "CNLiveUserSystemTools.h"
#import "NSDictionary+Safety.h"

//反馈
static  NSString *feedbackURL = @"https://api.cnlive.com/open/api2/user/userReport";

@implementation CNLiveFeedback

- (void)feedbackWithAppId:(NSString *)appId appKey:(NSString *)appKey contentId:(NSString *)contentId message:(NSString *)message success:(void (^)(id result))success failure:(void (^)(NSError *))failure
{
    
    NSDate *date = [NSDate date];
    NSInteger time = [date timeIntervalSince1970];
    NSString *timeString = [NSString stringWithFormat:@"%ld", (long)time];
    
    NSDictionary *params = @{@"appId":[CNLiveUserSystemTools verify:appId],
                             @"id":[CNLiveUserSystemTools verify:contentId],
                             @"msg":[CNLiveUserSystemTools verify:message],
                             @"clientPlat":@"i",
                             @"platform_id":[NSBundle mainBundle].bundleIdentifier,
                             @"timestamp": timeString};
    
    NSMutableDictionary *parameter = [NSMutableDictionary dictionaryWithDictionary:params];
    NSString *string = [NSString stringWithFormat:@"%@&key=%@", [CNLiveUserSystemTools signvalue:parameter], [CNLiveUserSystemTools verify:appKey]];
    NSString *signString = [[CNLiveUserSystemTools sha1:string] uppercaseString];
    
    [parameter setObject:signString forKey:@"sign"];
    [self requestWithParameter:parameter success:success failure:failure];

}

#pragma mark - request

- (void)requestWithParameter:(NSDictionary *)param success:(void(^)(id result))success failure :(void (^)(NSError *  error))failure
{
    [[CNLiveNetworkManager manager] requestPostURL:feedbackURL parameters:param success:^(NSURLResponse * _Nullable response, id  _Nullable responseObject) {
        
        responseObject = [responseObject validatedDictionary];
        success(responseObject);
        
        if (![responseObject[@"errorCode"] isEqualToString:@"0"]) {
            [CNLiveUserSystemTools errorStat:responseObject[@"errorMessage"]];
        }

    } failure:^(NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        failure(error);
        [CNLiveUserSystemTools errorStat:error.localizedDescription];

    }];
}

@end