Files
youle_app_ios/msext/Class/SGGateway/SGGatewayApi.m
2023-12-27 20:38:37 +08:00

262 lines
8.9 KiB
Objective-C
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//
// GatewayApi.m
// PublicFuncClass
//
// Created by an chen on 12-8-30.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
/*
* 释放
*/
#import "SGGatewayApi.h"
#import "NSString+SBJSON.h"
#import "SBJSON.h"
#import "FuncPublic.h"
#import "SGGateway.h"
#import "SGDefineInfo.h"
#import "FuncPublic.h"
@implementation SGGatewayApi
@synthesize _mUrl;
@synthesize _mvReturn;
@synthesize _returnFunction;
@synthesize _act;
@synthesize receivedBufferData;//返回的信息
@synthesize _objDo;
-(id)init
{
if((self=[super init]))
{
receivedBufferData = [[NSMutableData alloc]init];
//(**)
_objDo = [[NSMutableDictionary alloc]init];
_act = [[NSString alloc] initWithString:@""];
DelayerErro = NO;
OnceAlert=YES;
}
return self;
}
-(void)dealloc
{
NSLog(@"释放:%@",_act);
SG_RELEASE(receivedBufferData);
SG_RELEASE(_mUrl);
SG_RELEASE(_act);
SG_RELEASE(_objDo);
[super dealloc];
}
/*
* 延时处理
*/
-(void)delayer
{
[_DelayerTimer invalidate];
[[FuncPublic SharedFuncPublic] StopActivityAnimation];
NSLog(@"通信失败:%@",_act);
if (OnceAlert) {
// OnceAlert=NO;
// [self performSelector:@selector(ComeBack) withObject:nil afterDelay:50.0f];
// UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"网络不可用"
// message:@"无法与服务器通讯请连接到移动数据网络或者WIFI"
// delegate:self
// cancelButtonTitle:@"确定"
// otherButtonTitles:nil, nil];
// [alert show];
// [alert release];
[FuncPublic ShowAlert:@"无法与服务器通讯请连接到移动数据网络或者WIFI" title:@"网络不可用" viewController:nil];
}
//[_mvReturn performSelector:_returnFunction withObject:nil];
DelayerErro = YES;
}
-(void)ComeBack
{
OnceAlert=YES;
}
/*
* 参数:
*
* mv: 回调的目标类
*
* returnFunc: 回调的方法
*
* obj_do: 于后台交互成功后将信息传回mv类
*
* param_post: 于后交互的相关参数
*
* str: url信息
*
*/
-(void)gateway:(id)mv func:(SEL)returnFunc obj_post:(NSMutableDictionary*)obj_post obj_post:(NSMutableDictionary*)param_post STR:(NSString*)str HTTPMethod:(NSString*)_method{
/*
* 延时处理,如果通信超过指定时间,则结束通信
*/
if (![str isEqualToString:@"UpdateAPNSDevice.aspx"]) {
[[FuncPublic SharedFuncPublic] StartActivityAnimation:mv];
}
_DelayerTimer = [NSTimer scheduledTimerWithTimeInterval:GATEWAY_DELAYER target:self selector:@selector(delayer) userInfo:nil repeats:NO];
_mvReturn = mv;
_returnFunction = returnFunc;
_objDo = obj_post;
if( ![[param_post objectForKey:@"t"] isEqual:@""] )//关键联接名
{
if( [param_post objectForKey:@"t"] == nil )
{
_act = str;
}
else
{
_act = [param_post objectForKey:@"t"];
}
}
_mUrl = [[NSString alloc] initWithString:[NSString stringWithFormat:@"%@%@",SERVER,str]];
/*
* 增加一些固传参数 code start
*/
if( [param_post objectForKey:@"code"] != nil )
{
NSArray *array = [param_post allKeys];//[[param_post allKeys]sortedArrayUsingSelector: @selector(compare:)];
NSString* post = @"";
for(int i = 0; i<[array count]; i++){
if( [[param_post objectForKey:[array objectAtIndex:i]] isEqualToString:@"nn"])
continue;
post = [post stringByAppendingString:[param_post objectForKey:[array objectAtIndex:i]]];
}
post = [post stringByAppendingFormat:@"WERTY#$&(HJKfghjWERTYUIFGJFGHadf2222"];
post = [FuncPublic md5:post];
[param_post setObject:post forKey:@"code"];
}
/*
* code end
*/
//生成post的数据
NSData *postData = [self generateFormDataFromPostDictionary:param_post];//将传入的单独obj_post中的key使用和&组合
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; //post的长度
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setHTTPMethod:@"POST"]; //设置使用的方法
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];//设置数据的长度
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];//数据的类型
[request setHTTPBody:postData]; //设置发送的数据
NSString* post_string = [[NSString alloc] initWithData:postData encoding:NSUTF8StringEncoding];//将nsdata转为encoding
if( [_method isEqualToString:@"POST"] )
{
[request setURL:[NSURL URLWithString:_mUrl]];
}else if( [_method isEqualToString:@"POSTtwo"] )
{
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",SERVERTwo,str]]];
}else
{
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",_mUrl,post_string ]]]; //设置调用的url
}
NSLog(@"%@",[NSString stringWithFormat:@"%@?%@",_mUrl,post_string ]);
NSURLConnection *conn = [NSURLConnection connectionWithRequest:request delegate:self]; //开始连接后台
if (conn) {
} else {
}
SG_RELEASE(post_string);
[param_post removeAllObjects];
SG_RELEASE(param_post);
}
- (void)connection:(NSURLConnection *)conn didReceiveResponse:(NSURLResponse *)res {
[receivedBufferData setLength:0];
}
- (void)connection:(NSURLConnection *)conn didReceiveData:(NSData *)receivedData {
[receivedBufferData appendData:receivedData]; //添加记录的资料
}
- (void)connection:(NSURLConnection *)conn didFailWithError:(NSError *)error {
}
-(NSString *)HandelData:(NSString *)str_
{
NSString *string=nil;
//ValidateUserIDAndPassword.aspx UpdateAPNSDevice.aspx InsertBookComment.aspx
if ([_act isEqualToString:@"InsertSuggestInfo.aspx"]||[_act isEqualToString:@"UpdateAPNSDevice.aspx"]||[_act isEqualToString:@"InsertBookComment.aspx"]) {
string=[NSString stringWithFormat:@"{\"data\":[{\"result\":%@}]}",str_];
return string;
}
string=str_;
return string;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)conn {
NSLog(@"apiReturnOk:%@",_act);
if( DelayerErro ) return;
[_DelayerTimer invalidate];
NSString* stra = [[NSString alloc] initWithData:receivedBufferData encoding:NSUTF8StringEncoding]; //转换信息
//去除两边空格
NSString* str = [[NSString alloc]initWithFormat:@"%@",[stra stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
SG_RELEASE(stra);
if ([str isEqualToString: @""] == YES) {
return;
}
if(1){
NSLog(@"[%@]",str);
}
NSString *handleStr=[self HandelData:str];
NSMutableDictionary * obj_return = [handleStr JSONValue];//使用js解压
if( obj_return == nil )
{
[[FuncPublic SharedFuncPublic] StopActivityAnimation];//需要于funcpublic共用
// [FuncPublic ShowAlert:@"Api错误!"];
// [FuncPublic ShowAlert:@"没有数据!"];
[FuncPublic ShowAlert:@"没有数据!" title:nil viewController:nil];
return;
}
NSLog(@"jsonDecode完成:%@",_act);
[_objDo setObject:obj_return forKey:@"apiReturnData"];//将当前返回的信息和之前传入的参数,整合
NSLog(@"开始回调:%@",_act);
[_mvReturn performSelector:_returnFunction withObject:_objDo];//传到想应的方法中 _objDo之前如果有传参进来一并返回
SG_RELEASE(str);
NSLog(@"接口%@完成",_act);
[[FuncPublic SharedFuncPublic] StopActivityAnimation];
}
- (NSData*)generateFormDataFromPostDictionary:(NSDictionary*)dict
{
NSArray* keys = [dict allKeys];
NSMutableData* result = [NSMutableData data];
for (int i = 0; i < [keys count]; i++)
{
id value = [dict valueForKey: [keys objectAtIndex:i]];
if(i > 0){
[result appendData:[@"&" dataUsingEncoding:NSUTF8StringEncoding]];//NSASCIIStringEncoding
}
[result appendData:[[NSString stringWithFormat:@"%@" ,[keys objectAtIndex:i]] dataUsingEncoding:NSUTF8StringEncoding]];
[result appendData:[@"=" dataUsingEncoding:NSUTF8StringEncoding]];
if ([value isKindOfClass:[NSData class]])
{
[result appendData:value];
}
else {
[result appendData:DATA(value)];
}
}
return result;
}
@end