//提交到服务器
-(void) addDishDataToServer:(BOOL)modify
{
NSString *strurl =[NSStringstringWithFormat:@"%@/menus.php",HTTP_SERVER];
NSURL *url = [NSURL URLWithString:strurl];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
NSData *imageData =UIImagePNGRepresentation(_dishImageBtn.currentBackgroundImage);
NSString *imageName = [_dishName.text stringByAppendingString:@".png"];
[request setData:imageData withFileName:imageNameandContentType:@"image/png" forKey:@"uploadImage"];
//NSMutableDictionary *postDic = [[NSMutableDictionary alloc] init];
//getUserList 请求数据
if(modify)
{
[self.dishDic setObject:@"updateMenu" forKey:@"action"];
[self.dishDic setObject:_dishID.text forKey:@"MID"];
if(_isSellOutBtn.selected)
{
[self.dishDic setObject:@"1" forKey:@"MSellOut"];
}
else
{
[self.dishDic setObject:@"0" forKey:@"MSellOut"];
}
}
else
{
[self.dishDic setObject:@"saveMenu" forKey:@"action"];
}
NSData *postData = [NSJSONSerialization dataWithJSONObject:self.dishDicoptions:NSJSONWritingPrettyPrinted error:nil]; //data是字符串
NSString *postString = [[NSString alloc] initWithData:postDataencoding:NSUTF8StringEncoding];//把data格式的postdata转换成string格式
[request setPostValue:postString forKey:@"data"]; //传值
[request setCompletionBlock:^{
[mainViewController showHintAlertView:NO withMsg:@"正在上传中,请稍后..."];
//阻塞块可以吧整个函数作为变量,传到setCompletionBlock中
NSDictionary *dic = [NSJSONSerializationJSONObjectWithData:request.responseDataoptions:NSJSONReadingMutableContainers error:nil];
//把服务器返回的json字符串的data转化成dictionary
if([[dic objectForKey:@"statusID"] isEqualToString:@"0"])
{
NSString *msg = [dic objectForKey:@"msg"];
UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:@"提示"message:msg delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
[alertV show];
[alertV release];
}
else
{
NSString *msg = [dic objectForKey:@"msg"];
UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:@"提示"message:msg delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
[alertV show];
[alertV release];
}
NSLog(@"%@",request.responseString);
}]; //返回数据
[request setFailedBlock:^{
[mainViewController showHintAlertView:NO withMsg:@"正在上传中,请稍后..."];
}];
[request startAsynchronous];
[postString release];
}
//确认增加
-(IBAction) addBtnAction:(id)sender
{
if(![self checkInput])
{
return;
}
[mainViewController showHintAlertView:YES withMsg:@"正在上传中,请稍后..."];
self.dishDic = [[NSMutableDictionary alloc] init];
// [self.dishDic setObject:_dishImageBtn.currentBackgroundImage forKey:@"dishImage"];
[self.dishDic setObject:_dishName.text forKey:@"MName"];
NSString *mType = @"";
if(_fastfoodBtn.selected)
mType = _fastfoodBtn.titleLabel.text;
if(_snackBtn.selected)
mType = _snackBtn.titleLabel.text;
if(_drinkBtn.selected)
mType = _drinkBtn.titleLabel.text;
[self.dishDic setObject:mType forKey:@"MType"];
[self.dishDic setObject:_dishDescribe.text forKey:@"MDescription"];
[self.dishDic setObject:_dishPrice.text forKey:@"MPrice"];
if(_isSpecialBtn.selected)
{
[self.dishDic setObject:@"1" forKey:@"MSpecial"];
}
else
{
[self.dishDic setObject:@"0" forKey:@"MSpecial"];
}
if(self._dishID.text.length > 5)
{
[self addDishDataToServer:YES];
}
else
{
[self addDishDataToServer:NO];
}
}