//
// HallData.m
// TrueWord
//
// Created by king454 on 13-12-11.
// Copyright (c) 2013年 king454. All rights reserved.
//
#import "HallData.h"
#import "DefinClass.h"
#import "ChatCell.h"
#import "HallViewController.h"
static inline NSString *cachesDir(){
return [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
}
@implementation HallData
-(id)init
{
self=[super init];
if (self) {
self.user=[[UserModel alloc]init];
if (![self queue]) {
self.queue=[[[ASINetworkQueue alloc]init]autorelease];
[self queue].maxConcurrentOperationCount = 4;
[ _queue setShouldCancelAllRequestsOnFailure:NO ];
}
}
return self;
}
#pragma mark 加入房间
-(void)joinRoom:(NSString*)userjid andRoomJid:(NSString*)roomjid
{
//先拼接JSON发送给PHP
ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:[NSURL URLWithString:JOINROOMPHP]];
request.userInfo=[NSDictionary dictionaryWithObject:@"join" forKey:@"message"];
[request setPostValue:userjid forKey:@"userjid"];
[request setPostValue:roomjid forKey:@"roomjid"];
[request setDelegate:self];
[self.queue addOperation:request];
[_queue go];
[_delegate showActivity];
}
#pragma mark 退出房间
-(void)exitRoom:(NSString*)userjid andRoomJid:(NSString*)roomjid
{
ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:[NSURL URLWithString:EXITROOMPHP]];
request.userInfo=[NSDictionary dictionaryWithObject:@"exit" forKey:@"message"];
[request setPostValue:userjid forKey:@"userjid"];
[request setPostValue:roomjid forKey:@"roomjid"];
[request setDelegate:self];
[self.queue addOperation:request];
[_queue go];
}
#pragma mark 存入图片
-(void)postImage:(NSString*)fileName
{
ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:[NSURL URLWithString:UPAUDIOORIMAGE]];
request.userInfo=[NSDictionary dictionaryWithObject:@"image" forKey:@"message"];
NSString* filePath=[cachesDir() stringByAppendingPathComponent:fileName];
NSData* fileData=[NSData dataWithContentsOfFile:filePath];
[request setData:fileData withFileName:fileName andContentType:nil forKey:@"image"];
[request setDelegate:self];
[self.queue addOperation:request];
[_queue go];
}
#pragma mark 存入声音
-(void)postAudio:(NSString*)fileName
{
ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:[NSURL URLWithString:UPAUDIOORIMAGE]];
request.userInfo=[NSDictionary dictionaryWithObject:@"audio" forKey:@"message"];
NSString* filePath=[cachesDir() stringByAppendingPathComponent:fileName];
NSData* fileData=[NSData dataWithContentsOfFile:filePath];
[request setData:fileData withFileName:fileName andContentType:nil forKey:@"audio"];
[request setDelegate:self];
[self.queue addOperation:request];
[_queue go];
}
#pragma mark 通过传入token返回一个值
-(void)getUser:(NSString*)token
{
ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:[NSURL URLWithString:SELECTUSERPHP]];
request.userInfo=[NSDictionary dictionaryWithObject:@"token" forKey:@"message"];
[request setPostValue:token forKey:@"token"];
[request setDelegate:self];
[self.queue addOperation:request];
[_queue go];
}
#pragma mark 根据roomjid查找
-(void)getUsers:(NSString*)roomjid
{
ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:[NSURL URLWithString:SELECTUSERWITHROOM]];
request.userInfo=[NSDictionary dictionaryWithObject:@"roomjid" forKey:@"message"];
[request setPostValue:roomjid forKey:@"roomjid"];
[request setDelegate:self];
[self.queue addOperation:request];
[_queue go];
}
-(void)getRoomInformationByPHP
{
ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:[NSURL URLWithString:GETROOMINFORMATION]];
request.userInfo=[NSDictionary dictionaryWithObject:@"room" forKey:@"message"];
[request setDelegate:self];
[self.queue addOperation:request];
[_queue go];
}
#pragma mark 异步代理
- (void)requestFinished:(ASIHTTPRequest *)request
{
if ([_delegate isMemberOfClass:[HallViewController class]]) {
[_delegate removeActivity];
}
if (!self) {
return;
}
NSData *responseData = [request responseData];
NSArray* arr=[NSJSONSerialization JSONObjectWithData:responseData options:1 error:nil];
NSMutableDictionary *dic = arr[0];
if (_delegate) {
if ([[request.userInfo objectForKey:@"message"] isEqualToString:@"image"]) {
if ([_delegate respondsToSelector:@selector(requestFinishedByImage:)]) {
[_delegate requestFinishedByImage:dic];
}
}
if ([[request.userInfo objectForKey:@"message"] isEqualToString:@"audio"]) {
if ([_delegate respondsToSelector:@selector(requestFinishedByAudio:)]) {
[_delegate requestFinishedByAudio:dic];
}
}
if ([[request.userInfo objectForKey:@"message"] isEqualToString:@"token"]) {
if ([_delegate respondsToSelector:@selector(requestFinishedByToken:)]) {
[_delegate requestFinishedByToken:dic];
}
}
if ([[request.userInfo objectForKey:@"message"] isEqualToString:@"roomjid"]) {
if ([_delegate respondsToSelector:@selector(requestFinishedByRoomjid:)]) {
[_delegate requestFinishedByRoomjid:dic];
}
}
if ([[request.userInfo objectForKey:@"message"] isEqualToString:@"room"]) {
if ([_delegate respondsToSelector:@selector(requestFinishedByRoomInformation:)]) {
[_delegate requestFinishedByRoomInformation:dic];
}
}
if ([[request.userInfo objectForKey:@"message"] isEqualToString:@"join"]) {
}
}
//取出返回结果,是否为1
// NSLog(@"string==%@",[dic objectForKey:@"result"]);
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
if ([_delegate isMemberOfClass:[HallViewController class]]) {
[_delegate removeActivity];
}
NSError *error = [request error];
if (error) {
if ([[request.userInfo objectForKey:@"message"] isEqualToString:@"join"]){
[self makeAlert:@"加入房间失败"];
}
if([[request.userInfo objectForKey:@"message"] isEqualToString:@"exit"]){
[self makeAlert:@"退出房间失败"];
}
if([[request.userInfo objectForKey:@"message"] isEqualToString:@"join"]){
[self makeAlert:@"加入房间失败"];
}
}
}
-(void)makeAlert:(NSString*)name
{
UIAlertView *alert=[[[UIAlertView alloc]initWithTitle:name
message:@"一只蜗牛向你奔来"
delegate:self
cancelButtonTitle:@"确定"
otherButtonTitles:nil, nil]autorelease];
[alert show];
}
- (void)dealloc
{
[_queue cancelAllOperations];
[_queue release];
[_user release];
[super dealloc];
self = nil;
}
@end