ifish/Ifish/controllers/IfishTabControllers/探索/lookafterServer/MineKanHuViewController.m

608 lines
20 KiB
Objective-C

//
// MineKanHuViewController.m
// Ifish
//
// Created by imac on 16/8/23.
// Copyright © 2016年 lianxiang. All rights reserved.
//
#import "MineKanHuViewController.h"
#import "MineKanHuNotBindFirstViewCell.h"
#import "MineKanHuNotBindSecondViewCell.h"
#import "MineKanHuNormalViewCell.h"
#import "PushMasssageWebViewController.h"
#import "YooseConnectFirstViewController.h"
//#import "IfishSelectCardViewController.h"
#import "IfishBindDeviceSelectViewController.h"
#import "IfishCameraModel.h"
#define CELL_KANHURAG1 555
#import "UIImageView+WebCache.h"
#import "NIMKit.h"
//自定义
#import "IfishSessionViewController.h"
//抽离demo
#import "NIMSessionViewController.h"
@interface MineKanHuViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate,UIActionSheetDelegate>
@property(nonatomic,strong) UITableView *tableView;
@property(nonatomic,strong) NSArray *myCameraArr;
@end
@implementation MineKanHuViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//self.title = @"我的看护";
[self addTitleViewWithTitle:@"我的看护"];
[self testCamerArr];
[self initUI];
}
-(void)testCamerArr{
if (!_myCameraArr) {
_myCameraArr =[[NSArray alloc] init];
_myCameraArr =[[DataCenter defaultDtacenter] valueForKey:@"cameraArr"];
}
}
-(void)goBackAction{
//
[self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)initUI{
//self.rightItem = [[UIBarButtonItem alloc] initWithTitle:@"管理" style:UIBarButtonItemStylePlain target:self action:@selector(guanliActon)];
UIBarButtonItem*rightItem= [[UIBarButtonItem alloc] initWithTitle:@"管理" style:UIBarButtonItemStyleDone target:self action:@selector(guanliActon)];
self.navigationItem.rightBarButtonItem= rightItem;
self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
CGFloat BtnH = self.view.frame.size.width * 0.12;
self.tableView = [[UITableView alloc] init ];
self.tableView.frame =CGRectMake(0,0, self.view.frame.size.width,self.view.frame.size.height - BtnH);
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.showsVerticalScrollIndicator = NO;
self.tableView.backgroundColor = TABLE_BACKGROUD_COLOR;
[self.view addSubview:self.tableView];
NSString *str= @" 打给TA";
NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:str];
NSTextAttachment *attch = [[NSTextAttachment alloc] init];
attch.image = [UIImage imageNamed:@"businesslistings_call"];
attch.bounds = CGRectMake(0,-3,16,17);//33*35
NSAttributedString *attristr=[NSAttributedString attributedStringWithAttachment:attch];
[attri insertAttributedString:attristr atIndex:0];
UIButton *tel =[UIButton buttonWithType:UIButtonTypeCustom];
[tel setAttributedTitle:attri forState:UIControlStateNormal];
tel.frame = CGRectMake(0,CGRectGetMaxY(self.tableView.frame),self.view.frame.size.width/2, BtnH);
tel.backgroundColor = RGB(0, 180,239);
[attri addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0,str.length+1)];
[attri addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15] range:NSMakeRange(0,str.length+1)];
[tel addTarget:self action:@selector(telAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:tel];
NSString *str2= @" 咨询TA";
NSMutableAttributedString *attri2 = [[NSMutableAttributedString alloc] initWithString:str2];
NSTextAttachment *attch2 = [[NSTextAttachment alloc] init];
attch2.image = [UIImage imageNamed:@"businesslistings_comments"];
attch2.bounds = CGRectMake(0,-4,17,16);;//35*33
NSAttributedString *attristr2=[NSAttributedString attributedStringWithAttachment:attch2];
[attri2 insertAttributedString:attristr2 atIndex:0];
UIButton *zixunbtn =[UIButton buttonWithType:UIButtonTypeCustom];
[zixunbtn setAttributedTitle:attri2 forState:UIControlStateNormal];
zixunbtn.frame = CGRectMake(CGRectGetMaxX(tel.frame),CGRectGetMinY(tel.frame), self.view.frame.size.width/2, BtnH);
zixunbtn.backgroundColor = RGB(255, 115, 92);
[attri2 addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0,str2.length+1)];
[attri2 addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15] range:NSMakeRange(0,str2.length+1)];
[zixunbtn addTarget:self action:@selector(chatBtnAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:zixunbtn];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if ([_myCameraArr count] != 0) {
return 1 + _myCameraArr.count;
}else{
return 2;
}
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if ([_myCameraArr count]!=0) {
//有摄像头
if (indexPath.row == 0) {
MineKanHuNotBindFirstViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MineKanHuNotBindFirstViewCell"];
if (!cell) {
cell = [[[NSBundle mainBundle]loadNibNamed:@"MineKanHuNotBindFirstViewCell" owner:self options:nil]lastObject];
}
cell.shopName.text = self.xinXiModel.shopsName;
cell.shopAddress.text = [NSString stringWithFormat:@"%@%@%@%@",self.xinXiModel.shopsProvince,self.xinXiModel.shopsCity,self.xinXiModel.shopsArea,self.xinXiModel.shopsAddress];;
// [cell.geiShopDaDianHuaBtn addTarget:self action:@selector(telAction:) forControlEvents:UIControlEventTouchUpInside];
cell.geiShopDaDianHuaBtn.hidden = YES;
//商户头像 原写成招牌 不想改了暂时用这 只是个名字而已
NSString *imgUrl=[NSString stringWithFormat:@"%@%@",kGetIconUrl,self.xinXiModel.userImg];
[cell.shopZhaoPai sd_setImageWithURL:[NSURL URLWithString:imgUrl] placeholderImage:[UIImage imageNamed:@""]];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.sChatBtn.hidden = YES;
// [cell.sChatBtn addTarget:self action:@selector(chatBtnAction:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}else {
MineKanHuNormalViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MineKanHuNormalViewCell"];
if (!cell) {
cell = [[[NSBundle mainBundle]loadNibNamed:@"MineKanHuNormalViewCell" owner:self options:nil]lastObject];
}
IfishCameraModel *mdoel = _myCameraArr[indexPath.row -1];
NSString *filePath = [Utils getHeaderFilePathWithId:mdoel.cameraId];
UIImage *headImg = [UIImage imageWithContentsOfFile:filePath];
if(headImg==nil){
cell.cameraImg.image = LXImageWithImageName(@"ic_header.png");
}else{
cell.cameraImg.image = headImg;
}
cell.yuGangMing.text = mdoel.showName;
if (!mdoel.isLook) {
mdoel.isLook = @"0";
}
if ([mdoel.isLook isEqualToString:@"1"]) {
[cell.kanHuBtn setImage:[UIImage imageNamed:@"Switch-On"] forState:UIControlStateNormal];
cell.kanHuBtn.selected = YES;
}else{
[cell.kanHuBtn setImage:[UIImage imageNamed:@"Switch-Off"] forState:UIControlStateNormal];
cell.kanHuBtn.selected = NO;
}
cell.kanHuBtn.tag = indexPath.row + CELL_KANHURAG1;
[cell.kanHuBtn addTarget:self action:@selector(kanHuAction:) forControlEvents:UIControlEventTouchUpInside];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
}else{
//未绑定摄像头
if (indexPath.row == 0) {
MineKanHuNotBindFirstViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MineKanHuNotBindFirstViewCell"];
if (!cell) {
cell = [[[NSBundle mainBundle]loadNibNamed:@"MineKanHuNotBindFirstViewCell" owner:self options:nil]lastObject];
}
cell.shopName.text = self.xinXiModel.shopsName;
cell.shopAddress.text = self.xinXiModel.shopsAddress;
[cell.geiShopDaDianHuaBtn addTarget:self action:@selector(telAction:) forControlEvents:UIControlEventTouchUpInside];
NSString *imgUrl=[NSString stringWithFormat:@"%@%@",kGetIconUrl,self.xinXiModel.userImg];
[cell.shopZhaoPai sd_setImageWithURL:[NSURL URLWithString:imgUrl] placeholderImage:[UIImage imageNamed:@""]];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[cell.sChatBtn addTarget:self action:@selector(chatBtnAction:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}else {
MineKanHuNotBindSecondViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MineKanHuNotBindSecondViewCell"];
if (!cell) {
cell = [[[NSBundle mainBundle]loadNibNamed:@"MineKanHuNotBindSecondViewCell" owner:self options:nil]lastObject];
}
[cell.mineKanHuGoumai addTarget:self action:@selector(goumaiAction:) forControlEvents:UIControlEventTouchUpInside];
[cell.mineKanHuBangDing addTarget:self action:@selector(bangDingAction:) forControlEvents:UIControlEventTouchUpInside];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
}
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row ==0) {
return 200;
}else{
if ([_myCameraArr count]!=0) {
return 290;
}
return 220;
}
}
#pragma mark - 购买按钮
-(void)goumaiAction:(UIButton *)btn{
NSURL *url=[NSURL URLWithString:TABAO_URL];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}else{
self.hidesBottomBarWhenPushed = YES;
PushMasssageWebViewController*cameraShopVC=[[PushMasssageWebViewController alloc]init];
cameraShopVC.pushlink =IfishCameraTaoBaoShop;
cameraShopVC.pushtitle = @"购买摄像头";
[self.navigationController pushViewController:cameraShopVC animated:YES];
}
}
#pragma mark - 绑定按钮
-(void)bangDingAction:(UIButton *)btn{
self.hidesBottomBarWhenPushed = YES;
IfishBindDeviceSelectViewController *fistVC=[[IfishBindDeviceSelectViewController alloc] init];
[self.navigationController pushViewController:fistVC animated:YES];
}
#pragma mark -打电话
-(void)telAction:(UIButton *)btn{
//拨打电话
if ([UIApplication instancesRespondToSelector:@selector(canOpenURL:)]) {
NSString *telNumber = [NSString stringWithFormat:@"telprompt://%@",self.xinXiModel.shopsPhone];
NSURL *aURL = [NSURL URLWithString: telNumber];
if ([[UIApplication sharedApplication] canOpenURL:aURL])
{
[[UIApplication sharedApplication] openURL:aURL];
}
}
}
#pragma mark - 看护开启关闭按钮事件
-(void)kanHuAction:(UIButton*)btn{
//最多五个摄像头
IfishCameraModel *camera = nil;
switch (btn.tag) {
case CELL_KANHURAG1 + 1:
{
camera = _myCameraArr[0];
}
break;
case CELL_KANHURAG1 + 2:
{
camera= _myCameraArr[1];
}
break;
case CELL_KANHURAG1 + 3:
{
camera = _myCameraArr[2];
}
break;
case CELL_KANHURAG1 + 4:
{
camera = _myCameraArr[3];
}
break;
case CELL_KANHURAG1 + 5:
{
camera = _myCameraArr[4];
}
break;
default:
break;
}
if ([camera.isActive isEqualToString:@"0"]) {
[self.view makeToast:@"请先激活摄像头"];
return;
}
if ([camera.isLook isEqualToString:@"1"]) {
UIAlertController*ac=[UIAlertController alertControllerWithTitle:@"提示" message:@"关闭看护后,商家将再无法看到您的水族箱,确认关闭吗?" preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:ac animated:YES completion:nil];
[ac addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction*action){
[self kaiGuanKanHuWith:camera];
}]];
[ac addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction*action){
}]];
}else{
[self kaiGuanKanHuWith:camera];
}
}
-(void)kaiGuanKanHuWith:(IfishCameraModel *)camera
{
if (!camera.isLook) {
[self.view makeToast:@"错误看护开关未响应"];
return;
}
[self.view makeToastActivity];
UserModel*umodel=[[DataCenter defaultDtacenter]valueForKey:@"UserLogIn"];
NSString*userId=[NSString stringWithFormat:@"%@",umodel.userId];
NSString *status = nil;
if ([camera.isLook isEqualToString:@"0"]) {
status = @"1";
}else{
status = @"0";
}
[AFNOHeaderHttpTool kanHuKaiGuanWith:camera.cameraId userId:userId status:status success:^(id response) {
NSDictionary *reDic=[NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableContainers error:nil];
if ([reDic[@"result"] isEqualToString:@"100"]) {
[self.view hideToastActivity];
if ([camera.isLook isEqualToString:@"0"]) {
// [self.view makeToast:@"已开启看护"];
}else{
//[self.view makeToast:@"已关闭看护"];
}
NSMutableArray *newCamerArr =[[NSMutableArray alloc] init];
for (IfishCameraModel *camer in _myCameraArr) {
if ([camer.cameraId isEqualToString:camera.cameraId]) {
if ([camera.isLook isEqualToString:@"0"]) {
camer.isLook = @"1";
}else{
camer.isLook = @"0";
}
}
[newCamerArr addObject:camer];
}
//存储摄像头数组
[[DataCenter defaultDtacenter]setValue: newCamerArr forKey:@"cameraArr"];
_myCameraArr = newCamerArr;
[self.tableView reloadData];
}else if ([reDic[@"result"] isEqualToString:@"101"]){
[self.view hideToastActivity];
[self.view makeToast:@"开启失败错无码101"];
}else if ([reDic[@"result"] isEqualToString:@"210"]){
[self.view hideToastActivity];
[self.view makeToast:@"摄像头不存在错无码210"];
}
} failure:^(NSError *err) {
[self.view hideToastActivity];
[self.view makeToast:@"网络出现问题未请求成功"];
}];
}
#pragma mark- 聊天
-(void)chatBtnAction:(UIButton *)btn{
UserModel*model=[[DataCenter defaultDtacenter]valueForKey:@"UserLogIn"];
NSString *myAcount = [NSString stringWithFormat:@"%@",model.userId];
NSString *tarGetAcount = [NSString stringWithFormat:@"%@",self.xinXiModel.userId];
[[NIMSDK sharedSDK].loginManager login:myAcount token:model.neteaseToken completion:^(NSError *error) {
if (!error) {
NSLog(@"登录成功");
//创建session
NIMSession *session = [NIMSession session:tarGetAcount type:NIMSessionTypeP2P];
//创建聊天页,这个页面继承自 NIMKit 中的组件 NIMSessionViewController
self.hidesBottomBarWhenPushed = YES;
IfishSessionViewController *vc = [[IfishSessionViewController alloc] initWithSession:session];
//vc.targetChatname = self.xinXiModel.userName;
[self.navigationController pushViewController:vc animated:YES];
// NIMSessionViewController *vc = [[NIMSessionViewController alloc] initWithSession:session];
// [self.navigationController pushViewController:vc animated:YES];
}else{
NSLog(@"登录失败");
}
}];
}
#pragma mark - guanliActon
-(void)guanliActon
{
UIActionSheet *shet =[[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"解除看护", nil];
[shet showInView:self.view];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex==actionSheet.cancelButtonIndex) {
return;
}
switch (buttonIndex) {
case 0:
{
UIAlertController*ac=[UIAlertController alertControllerWithTitle:@"解除看护" message:@"解除看护后,该商家将不再为您看护,您可以在看护列表中重新选择一个商家。确定解除吗?" preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:ac animated:YES completion:nil];
[ac addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction*action){
}]];
[ac addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction*action){
[self removeLook];
}]];
}
break;
default:
break;
}
}
-(void)removeLook{
NSString *userId = [dataContorl dataControlGetUserIdInfo];
[AFHttpTool removeLookUserId:userId success:^(id response) {
NSDictionary *reDic=[NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableContainers error:nil];
if ([reDic[@"result"] isEqualToString:@"100"]) {
CertificationShopModel *shopInfo2=[[CertificationShopModel alloc] init];
[[DataCenter defaultDtacenter]setValue:shopInfo2 forKey:@"ShopsInfo2"];
[self.navigationController popToRootViewControllerAnimated:YES];
}
} failure:^(NSError *err) {
}];
}
@end