You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

177 lines
5.5 KiB

//
// Contacts.m
// myWatch
//
// Created by xTT on 15/11/5.
// Copyright © 2015年 xTT. All rights reserved.
//
#import "Contacts.h"
#import "myHttp.h"
#import "User.h"
#import "Device.h"
#import "CJFileUtility.h"
@implementation Contacts
- (void)setDefaultValue{
self.name = @"";
self.phone = @"";
}
-(NSMutableArray *)contact_arry{
if (!_contact_arry) {
_contact_arry = [NSMutableArray array];
}
return _contact_arry;
}
+ (void)getObjsSuccess:(void (^)(NSMutableArray *arr))success
failure:(void (^)(NSError *error))failure{
NSString *urlStr = [MyHttp getURL:@"getway/devices/[imei]/contact"
objArr:@[cUser.cDevice]];
[xMyHttp URL:urlStr
method:@"GET" parameters:@{}
success:^(NSURLSessionDataTask *task, id responseObject)
{
if ([responseObject[@"code"] intValue] == HTTP_SUCCESS) {
NSMutableArray *arr = [Contacts mj_objectArrayWithKeyValuesArray:responseObject[@"whiteContacts"]];
//把当前用户提到数组第一个
__block Contacts *tmpObj = nil;
__block Contacts *adminObj = nil;
[arr enumerateObjectsUsingBlock:^(Contacts * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([obj.openid isEqualToString:cUser.openid]) {
//是自己的联系人
tmpObj = obj;
//更新一下权限信息
cUser.cDevice.identity = tmpObj.identity;
}
if ([obj.identity isEqualToString:@"admin"]){
//是管理员的联系人
adminObj = obj;
}
}];
if(adminObj){
[arr removeObject:adminObj];
[arr insertObject:adminObj atIndex:0];
}
if(tmpObj){
[arr removeObject:tmpObj];
[arr insertObject:tmpObj atIndex:0];
}
//
//// NSMutableArray *arr = [NSMutableArray array];
// [arr addObjectsFromArray:[Contacts mj_objectArrayWithKeyValuesArray:responseObject[@"whiteContacts"]]];
success(arr);
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
if (failure) {
failure(error);
}
}];
}
- (void)saveManyPerson:(void (^)())success
failure:(void (^)())failure{
NSString *urlStr = [MyHttp getURL:HTTP_DEVICES__MANYCONTACT
objArr:@[cUser.cDevice]];
NSString *method = @"POST";
if (self.id.length > 0) {
method = @"PATCH";
}
// xMyHttp.responseSerializer = [AFHTTPResponseSerializer serializer];
NSMutableDictionary *mDic = [self mj_keyValues];
[mDic removeObjectForKey:@"name"];
[mDic removeObjectForKey:@"phone"];
[mDic setValue:[self.contact_arry mj_JSONString] forKey:@"contact_arry"];
[xMyHttp URL:urlStr
method:method parameters:mDic
success:^(NSURLSessionDataTask *task, id responseObject)
{
if ([responseObject[@"code"] intValue] == HTTP_SUCCESS) {
success();
}else{
failure();
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
if (failure) {
failure();
}
}];
}
- (void)saveSuccess:(void (^)())success
failure:(void (^)())failure{
NSString *urlStr = [MyHttp getURL:@"getway/devices/[imei]/contact"
objArr:@[cUser.cDevice]];
NSString *method = @"POST";
if (self.id.length > 0) {
method = @"PATCH";
}
[xMyHttp URL:urlStr
method:method parameters:[self mj_keyValues]
success:^(NSURLSessionDataTask *task, id responseObject)
{
if ([responseObject[@"code"] intValue] == HTTP_SUCCESS) {
success();
}else{
failure();
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
if (failure) {
failure();
}
}];
}
- (void)deleteSuccess:(void (^)())success
failure:(void (^)())failure{
NSString *urlStr = [MyHttp getURL:@"getway/devices/[imei]/contact"
objArr:@[cUser.cDevice]];
[xMyHttp URL:urlStr
method:@"DELETE" parameters:@{@"id":self.id}
success:^(NSURLSessionDataTask *task, id responseObject)
{
if ([responseObject[@"code"] intValue] == HTTP_SUCCESS) {
success();
}else{
failure();
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
if (failure) {
failure();
}
}];
}
+ (void)deleteManyPersonWithIDs:(NSArray*)ids success:(void (^)())success failure:(void (^)())failure{
NSString *urlStr = [MyHttp getURL:@"getway/devices/[imei]/mancontact_class"
objArr:@[cUser.cDevice]];
NSMutableDictionary *parameter = [NSMutableDictionary dictionary];
[parameter setValue:[ids mj_JSONString] forKey:@"ContactIds"];
[xMyHttp URL:urlStr
method:@"DELETE" parameters:parameter
success:^(NSURLSessionDataTask *task, id responseObject)
{
if ([responseObject[@"code"] intValue] == HTTP_SUCCESS) {
success();
}else{
failure();
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
if (failure) {
failure();
}
}];
}
@end