NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
MBProgressHUD * hud = [C_CFactory c_CCreateLoadingHUDWithView:self.navigationController.view];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager.requestSerializer setValue:@application/x-www-form-urlencoded forHTTPHeaderField:@Content-Type];
[manager.requestSerializer willChangeValueForKey:@timeoutInterval];
manager.requestSerializer.timeoutInterval = 30;
[manager.requestSerializer didChangeValueForKey:@timeoutInterval];
/*
/apps/v1/user/login
登录
POST
报文编码格式: UTF-8
更新时间:2021-04-27 13:38:27
Body 请求参数
Form-data
生成预览数据
参数名 类型
必填
说明 全部展开
user_name
[string] 可选项,用户名 展开
email
[string] 是 必填项,邮箱 展开
from
[int] 是 必填项,来源,4-安卓 5-苹果 展开
*/
NSMutableDictionary * parameter = [[NSMutableDictionary alloc] init];
parameter[@user_name] = [C_CDataBaseTool shareUserManager].userDict[@nickname];
parameter[@email] = [defaults valueForKey:USERID];
parameter[@form] = @5;
/*
$param = [
email=gt; whx@hofan.cn,
from=gt; 4,
user_name=gt;'whx',
]
数组转为查询字符串【键值对以等号”=”连接,键值对间以符合”amp;”连接】,例如:
$str = 'email=whx@hofan.cnamp;from=4amp;user_name=whx'
*/
NSString * sha256 = [self hmacSHA256WithSecret:access_secret content:[NSString stringWithFormat:@email=%@amp;from=5amp;user_name=%@,[defaults valueForKey:USERID],[C_CDataBaseTool shareUserManager].userDict[@nickname]]];
/*
一、请求头部
Access-Key:
组成形式:密钥(key) + 分号(;) + 签名(sign)
示例:
NFM2NUQxNHM2NWFkZ*1U0RzZFNBRHNhZA;56a925276c4e669953808ce17c0ab5b7f23dd0d58def893e5521362cc0120060
*/
[manager.requestSerializer setValue:[NSString stringWithFormat:@%@;%@,access_key,sha256] forHTTPHeaderField:@Access-Key];
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@POST URLString:access_url parameters:nil constructingBodyWithBlock:^(idlt;AFMultipartFormDatagt; formData) {
for (NSString *key in parameter.allKeys) {
// 循环拿到所有参数进行拼接
[formData appendPartWithFormData:[parameter[key] dataUsingEncoding:NSUTF8StringEncoding] name:key];
}
} error:nil];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@application/json,@text/html,@text/json, @text/javascript, @text/plain, nil];
NSURLSessionUploadTask *uploadTask;
uploadTask = [manager uploadTaskWithStreamedRequest:request progress:^(NSProgress * _Nonnull uploadProgress) {
} completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if (error) {
[hud hide:YES afterDelay:0.1];
NSLog(@Error: %@, error);
[C_CPromptView c_CCreateWarningWithView:self.navigationController.view andTitle:[NSString stringWithFormat:@%@,error] isHidden:YES];
} else {
NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONWritingPrettyPrinted error:nil];
NSLog(@请求客服token === %@, responseObject);
NSLog(@请求客服token:%@,dict);
[hud hide:YES afterDelay:0.1];
C_COnlineServiceViewController * vc = [[C_COnlineServiceViewController alloc] initWithNibName:@C_COnlineServiceViewController bundle:nil];
vc.hidesBottomBarWhenPushed = YES;
vc.serviceUrl = [NSString stringWithFormat:@https://test-pre.afterservice.vip/mobile/chat?app=CENGCENamp;token=%@,dict[@data][@token]];
[self.navigationController pushViewController:vc animated:YES];
}
}];
[uploadTask resume];
}
/**
* 加密方式,MAC算法: HmacSHA256
*
* @param secret 秘钥
* @param content 要加密的文本
*
* @return 加密后的字符串
*/
- (NSString *)hmacSHA256WithSecret:(NSString *)secret content:(NSString *)content {
const char *cKey = [secret cStringUsingEncoding:NSASCIIStringEncoding];
// 加密的内容有可能有中文 所以用NSUTF8StringEncoding
const char *cData = [content cStringUsingEncoding:NSUTF8StringEncoding];
unsigned char cHMAC[CC_SHA256_DIGEST_LENGTH];
//SHA256加密
CCHmac(kCCHmacAlgSHA256, cKey, strlen(cKey), cData, strlen(cData), cHMAC);
NSMutableString *hexString = [NSMutableString string];
//转换成字符串
for (int i=0; ilt;sizeof(cHMAC); i++)
{
[hexString appendFormat:@%02x, cHMAC[i]];
}
return hexString;
}