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.
 
 
 
 

1248 lines
44 KiB

//
// User.m
// tianyin
//
// Created by xTT on 15/6/7.
// Copyright (c) 2015年 xTT. All rights reserved.
//
#import "User.h"
#import "AppDelegate.h"
#import "myHttp.h"
#import "MyMQTT.h"
//#import "bleObject.h"
#import "Circle.h"
#import "CJFileUtility.h"
#import "Locate.h"
#import "Device.h"
static User *currentUser = nil;
@implementation User
@synthesize ownedDevices = _ownedDevices;
- (Device *)cDevice{
if (!_cDevice && self.myDevices.count > 0) {
_cDevice = [self.myDevices firstObject];
}
return _cDevice;
}
- (NSMutableArray *)myDevices{
if (!_myDevices) {
_myDevices = [[NSMutableArray alloc] init];
return _myDevices;
}
return _myDevices;
}
- (void)setOwnedDevices:(NSMutableArray *)ownedDevices{
if (!_ownedDevices) {
_ownedDevices = [[NSMutableArray alloc] init];
}
NSMutableArray *arr = [NSMutableArray array];
[ownedDevices enumerateObjectsUsingBlock:^(Device *obj, NSUInteger idx, BOOL * _Nonnull stop) {
Device *device = [self getDeviceWithImei:obj.imei];
if (device) {
[device mj_setKeyValues:obj.mj_JSONObject];
}else{
device = obj;
}
[device getDeviceInfoWithParameters:[NSMutableDictionary dictionaryWithDictionary:@{@"imei": device.imei,@"vendor":device.vendor}]
success:nil failure:nil];
[arr addObject:device];
}];
[_ownedDevices removeAllObjects];
[_ownedDevices addObjectsFromArray:arr];
[self.myDevices removeAllObjects];
[self.myDevices addObjectsFromArray:self.ownedDevices];
[self.myDevices addObjectsFromArray:self.chatGroupDevices];
}
- (void)setChatGroupDevices:(NSMutableArray *)chatGroupDevices{
if (!_chatGroupDevices) {
_chatGroupDevices = [[NSMutableArray alloc] init];
}
[self.myDevices removeAllObjects];
NSMutableArray *arr = [NSMutableArray array];
[chatGroupDevices enumerateObjectsUsingBlock:^(Device *obj, NSUInteger idx, BOOL * _Nonnull stop) {
Device *device = [self getDeviceWithImei:obj.imei];
if (device) {
[device mj_setKeyValues:obj.mj_JSONObject];
}else{
device = obj;
}
[device getDeviceInfoWithParameters:[NSMutableDictionary dictionaryWithDictionary:@{@"imei": device.imei,@"vendor":device.vendor}]
success:nil failure:nil];
[arr addObject:device];
}];
[_chatGroupDevices removeAllObjects];
[_chatGroupDevices addObjectsFromArray:arr];
[self.myDevices addObjectsFromArray:self.ownedDevices];
[self.myDevices addObjectsFromArray:self.chatGroupDevices];
}
- (Device *)getDeviceWithImei:(NSString *)imei{
__block Device *device = nil;
[self.myDevices enumerateObjectsUsingBlock:^(Device *obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([obj.imei isEqualToString:imei]) {
device = obj;
*stop = YES;
}
}];
return device;
}
- (void)setChatGroups:(NSMutableArray *)chatGroups{
if (!_chatGroups) {
_chatGroups = [[NSMutableArray alloc] init];
}
NSMutableArray *arr = [NSMutableArray array];
[chatGroups enumerateObjectsUsingBlock:^(Circle *obj, NSUInteger idx, BOOL * _Nonnull stop) {
Circle *circle = [currentUser getCircleWithId:obj.groupid];
if (circle) {
[circle mj_setKeyValues:obj.mj_JSONObject];
}else{
circle = obj;
}
[circle getCircleInfoSuccess:^{} failure:^{}];
[arr addObject:circle];
}];
[_chatGroups removeAllObjects];
[_chatGroups addObjectsFromArray:arr];
}
#pragma 获取圈子
- (Circle *)getCircleWithId:(NSString *)Id{
__block Circle *c = nil;
[_chatGroups enumerateObjectsUsingBlock:^(Circle * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([obj.groupid isEqualToString:Id]) {
c = obj;
*stop = YES;
}
}];
return c;
}
- (Circle *)getCircleWithImei:(NSString *)imei{
__block Circle *c = nil;
[_chatGroups enumerateObjectsUsingBlock:^(Circle * _Nonnull circle, NSUInteger idx, BOOL * _Nonnull stop) {
[circle.devices enumerateObjectsUsingBlock:^(Device * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([obj.imei isEqualToString:imei]) {
c = circle;
*stop = YES;
}
}];
if (c) {
*stop = YES;
}
}];
return c;
}
+ (void)saveDefaultImei:(NSString *)imei{
[[NSUserDefaults standardUserDefaults] setObject:imei forKey:@"default_imei"];
}
+ (NSString *)getDefaultImei{
return [[NSUserDefaults standardUserDefaults] stringForKey:@"default_imei"];
}
+ (void)saveAgreeImei:(NSString *)imei {
[[NSUserDefaults standardUserDefaults] setObject:imei forKey:@"agree_imei"];
}
+ (NSString *)agreeImei {
return [[NSUserDefaults standardUserDefaults] stringForKey:@"agree_imei"];
}
+ (void)removeAgreeImei {
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"agree_imei"];
}
+ (User *)currentUser{
return currentUser;
}
- (NSString *)id{
return _openid;
}
//MARK: 上传微信授权 原始数据 和登录 新的登录接口
+ (void)upWXDataAndLoginParameters:(id)parameters
success:(void (^)(id))success
failure:(void (^)(id faiObject))failure{
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:parameters];
[dic setValue:@"ios" forKey: @"mobile_type"];
[xMyHttp URL:HTTP_POST_ACCOUNTS_WXDATAUP method:@"POST" parameters:dic
success:^(NSURLSessionDataTask *task, id responseObject)
{
if ([responseObject[@"code"] intValue] == HTTP_SUCCESS) {
User *currentUser = [User mj_objectWithKeyValues:parameters];
[currentUser mj_setKeyValues:responseObject];
[User setCurrentUser:currentUser];
if(success){success(responseObject);}
}else{
if(failure){failure(responseObject);}
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
if (failure) {
failure(error);
}
}];
}
#pragma 登录
+ (void)loginWithParameters:(id)parameters
success:(void (^)(id))success
failure:(void (^)(id faiObject))failure{
[[MyHttp sharedClient] URL:HTTP_POST_ACCOUNTS_LOGIN method:@"POST" parameters:parameters
success:^(NSURLSessionDataTask *task, id responseObject)
{
if ([responseObject[@"code"] intValue] == HTTP_SUCCESS) {
User *currentUser = [User mj_objectWithKeyValues:parameters];
[currentUser mj_setKeyValues:responseObject];
[User setCurrentUser:currentUser];
if(success){success(responseObject);}
}else{
if(failure){failure(responseObject);}
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
if (failure) {
failure(error);
}
}];
}
#pragma 主动退出登录
+ (void)logoutWithSuccess:(void (^)(id))success
failure:(void (^)(id faiObject))failure{
if(currentUser){
NSString *urlStr = [MyHttp getURL:HTTP_POST_ACCOUNTS_LOGOUT
objArr:@[currentUser]];
[[MyHttp sharedClient] URL:urlStr method:@"DELETE" parameters:nil
success:^(NSURLSessionDataTask *task, id responseObject)
{
if ([responseObject[@"code"] intValue] == HTTP_SUCCESS) {
if(success){success(responseObject);}
}else{
if(failure){failure(responseObject);}
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
if (failure) {
failure(error);
}
}];
}
}
#pragma 注销用户
+ (void)unregisterWithSuccess:(void (^)(id))success
failure:(void (^)(id faiObject))failure {
if(currentUser){
NSString *urlStr = [MyHttp getURL:HTTP_POST_ACCOUNTS_UNREGISTER
objArr:@[currentUser]];
[[MyHttp sharedClient] URL:urlStr method:@"POST" parameters:@{@"accesstoken":currentUser.accesstoken}
success:^(NSURLSessionDataTask *task, id responseObject)
{
if ([responseObject[@"code"] intValue] == HTTP_SUCCESS) {
if(success){success(responseObject);}
}else{
if(failure){failure(responseObject);}
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
if (failure) {
failure(error);
}
}];
}
}
#pragma 其他的 登录
+ (void)loginWithParameters:(id)parameters
type:(int)type
success:(void (^)(id))success
failure:(void (^)(id faiObject))failure{
[parameters setValue:@(type) forKey:@"type"];
[parameters setValue:@"ios" forKey:@"mobile_type"];
[[MyHttp sharedClient] URL:HTTP_POST_ACCOUNTS method:@"POST" parameters:parameters
success:^(NSURLSessionDataTask *task, id responseObject)
{
if ([responseObject[@"code"] intValue] == HTTP_SUCCESS) {
User *currentUser = [User mj_objectWithKeyValues:parameters];
[currentUser mj_setKeyValues:responseObject];
[User setCurrentUser:currentUser];
if(success){success(responseObject);}
}else{
if(failure){failure(responseObject);}
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
if (failure) {
failure(error);
}
}];
}
#pragma 注册用户
+ (void)newUserWithParameters:(id)parameters
success:(void (^)(id responseObject))success
failure:(void (^)(id faiObject))failure{
[[MyHttp sharedClient] URL:HTTP_POST_REGISTER method:@"POST" parameters:parameters
success:^(NSURLSessionDataTask *task, id responseObject)
{
if ([responseObject[@"code"] intValue] == HTTP_SUCCESS) {
User *currentUser = [User mj_objectWithKeyValues:parameters];
[currentUser mj_setKeyValues:responseObject];
[User setCurrentUser:currentUser];
if(success){success(responseObject);}
}else{
if(failure){failure(responseObject);}
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
if (failure) {
failure(error);
}
}];
}
#pragma 获取用户信息
- (void)getUserInfoSuccess:(void (^)(id responseObject))success
failure:(void (^)(id faiObject))failure{
NSString *urlStr = [MyHttp getURL:HTTP_ACCOUNTS_INFO
objArr:@[self]];
[xMyHttp URL:urlStr method:@"GET" parameters:@{}
success:^(NSURLSessionDataTask *task, id responseObject)
{
if ([responseObject[@"code"] intValue] == HTTP_SUCCESS) {
[self mj_setKeyValues:responseObject];
if(success){success(responseObject);}
[User saveToFile];
}else{
if(failure){failure(responseObject);}
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
if (failure) {
failure(error);
}
} showHUD:NO];
}
#pragma 获取用户未读消息
-(void)getMsgUnreadCount:(NSString *)msgType
Success:(void (^)(id responseObject))success
failure:(void (^)(void))failure{
NSString *urlStr = [MyHttp getURL:HTTP_ACCOUNTS__UNREAD_MSG
objArr:@[self]];
[xMyHttp URL:urlStr method:@"GET" parameters:@{@"type":msgType}
success:^(NSURLSessionDataTask *task, id responseObject)
{
if ([responseObject[@"code"] intValue] == HTTP_SUCCESS) {
if(success){success(responseObject);}
}else{
if(failure){failure();}
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
if (failure) {
failure();
}
} showHUD:NO];
}
-(void)updateMsg2Read:(NSString *)msgId
MsgType:(NSString *)msgType
Success:(void (^)(id responseObject))success
failure:(void (^)(void))failure{
NSString *urlStr = [MyHttp getURL:HTTP_ACCOUNTS__UNREAD_MSG
objArr:@[self]];
[xMyHttp URL:urlStr method:@"POST" parameters:@{@"type":msgType,@"id":msgId}
success:^(NSURLSessionDataTask *task, id responseObject)
{
if ([responseObject[@"code"] intValue] == HTTP_SUCCESS) {
if(success){success(responseObject);}
}else{
if(failure){failure();}
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
if (failure) {
failure();
}
} showHUD:NO];
}
/**
MARK:获取公众号模板消息开关状态
*/
- (void)getGZHSwitchSuccess:(void (^)(id responseObject))success
failure:(void (^)(id faiObject))failure{
NSString *urlStr = [MyHttp getURL:HTTP_ACCOUNTS_GZH_SWITCH
objArr:@[self]];
[xMyHttp URL:urlStr method:@"GET" parameters:@{}
success:^(NSURLSessionDataTask *task, id responseObject)
{
if ([responseObject[@"code"] intValue] == HTTP_SUCCESS) {
if(success){success(responseObject);}
}else{
if(failure){failure(responseObject);}
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
if (failure) {
failure(error);
}
} showHUD:NO];
}
/**
MARK:设置公众号模板消息开关状态
*/
- (void)setGZHSwitchStatus:(NSNumber*)status
Success:(void (^)(id responseObject))success
failure:(void (^)(id faiObject))failure;{
NSString *urlStr = [MyHttp getURL:HTTP_ACCOUNTS_GZH_SWITCH
objArr:@[self]];
[xMyHttp URL:urlStr method:@"POST" parameters:@{@"status":status}
success:^(NSURLSessionDataTask *task, id responseObject)
{
if ([responseObject[@"code"] intValue] == HTTP_SUCCESS) {
if(success){success(responseObject);}
}else{
if(failure){failure(responseObject);}
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
if (failure) {
failure(error);
}
} showHUD:NO];
}
#pragma 编辑用户信息
- (void)saveUserInfoSuccess:(void (^)(id responseObject))success
failure:(void (^)(id faiObject))failure{
NSString *urlStr = [MyHttp getURL:HTTP_ACCOUNTS_INFO
objArr:@[self]];
NSMutableDictionary *parameters = [self mj_keyValues];
[parameters removeObjectForKey:@"image"];
[xMyHttp URL:urlStr method:@"PATCH" parameters:parameters
constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
{
if (self.image) {
[formData appendPartWithFileData:UIImageJPEGRepresentation(self.image, 0.1)
name:@"image"
fileName:@"image.jpg"
mimeType:@"image/jpeg"];
}
}
success:^(NSURLSessionDataTask *task, id responseObject)
{
if ([responseObject[@"code"] intValue] == HTTP_SUCCESS) {
[self mj_setKeyValues:responseObject];
if(success){success(responseObject);}
[User saveToFile];
}else{
if(failure){failure(responseObject);}
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
if (failure) {
failure(error);
}
}];
}
#pragma 上传jjm
- (void)updatajkCodeSuccess:(void (^)(id responseObject))success
failure:(void (^)(id faiObject))failure{
NSString *urlStr = [MyHttp getURL:HTTP_POST_IOSCODE
objArr:@[cUser.cDevice]];
[xMyHttp UploadPictureByFileName:urlStr Path:UIImageJPEGRepresentation(self.image, 0.1) success:^(id responseObject) {
if(success)
success(responseObject);
} failure:^(id faiObject) {
if(faiObject)
failure(faiObject);
}];
}
#pragma 获取jjm信息
- (void)getjkCodeSuccess:(void (^)(id responseObject))success
failure:(void (^)(id faiObject))failure{
NSString *urlStr = [MyHttp getURL:HTTP_POST_IOSCODE
objArr:@[cUser.cDevice]];
[xMyHttp URL:urlStr method:@"GET" parameters:@{}
success:^(NSURLSessionDataTask *task, id responseObject)
{
if ([responseObject[@"code"] intValue] == HTTP_SUCCESS) {
[self mj_setKeyValues:responseObject];
if(success){success(responseObject);}
[User saveToFile];
}else{
if(failure){failure(responseObject);}
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
if (failure) {
failure(error);
}
} showHUD:NO];
}
#pragma mark --获取首页轮播图
- (void)getSlideshowInfoSuccess:(void (^)(id responseObject))success
failure:(void (^)(id faiObject))failure{
NSString *urlStr = [MyHttp getURL:HTTP_GET_SLIDESHOW
objArr:@[self]];
[xMyHttp URL:urlStr method:@"GET" parameters:@{}
success:^(NSURLSessionDataTask *task, id responseObject)
{
if ([responseObject[@"code"] intValue] == HTTP_SUCCESS) {
if(success){success(responseObject);}
}else{
if(failure){failure(responseObject);}
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
if (failure) {
failure(error);
}
} showHUD:NO];
}
#pragma mark --表盘列表分页查询
- (void)getDialMsgInfo:(id)parameters
success:(void (^)(id responseObject))success
failure:(void (^)(id faiObject))failure
{
NSString *urlStr = [MyHttp getURL:HTTP_POST_PAGELIST
objArr:@[self]];
[xMyHttp URL:urlStr method:@"POST" parameters:parameters
success:^(NSURLSessionDataTask *task, id responseObject)
{
if ([responseObject[@"code"] intValue] == HTTP_SUCCESS) {
if(success){success(responseObject);}
}else{
if(failure){failure(responseObject);}
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
if (failure) {
failure(error);
}
} showHUD:NO];
}
- (void)postSlideshowInfoWithID:(NSString *)slideID
Success:(void (^)(id responseObject))success
failure:(void (^)(id faiObject))failure{
NSString *urlStr = [MyHttp getURL:HTTP_GET_SLIDESHOW
objArr:@[self]];
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
[dic setValue:slideID forKey:@"id"];
[xMyHttp URL:urlStr method:@"POST" parameters:dic
success:^(NSURLSessionDataTask *task, id responseObject)
{
if ([responseObject[@"code"] intValue] == HTTP_SUCCESS) {
if(success){success(responseObject);}
}else{
if(failure){failure(responseObject);}
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
if (failure) {
failure(error);
}
} showHUD:NO];
}
+ (void)wxUserApplyPhone:(NSString*)phone
success:(void (^)(id responseObject))success
failure:(void (^)(id faiObject))failure{
NSString *url = [MyHttp getURL:HTTP_POST_ACCOUNTS_APPLY_BIND_PHONE
objArr:@[currentUser]];
[[MyHttp sharedClient] URL:url
method:@"POST" parameters:@{@"phone":phone}
success:^(NSURLSessionDataTask *task, id responseObject)
{
if ([responseObject[@"code"] intValue] == HTTP_SUCCESS) {
[[User currentUser] mj_setKeyValues:responseObject];
if(success){success(responseObject);}
[User saveToFile];
}else{
if(failure){failure(responseObject);}
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
if (failure) {
failure(error);
}
} showHUD:NO];
}
#pragma 手机账号 绑定微信账号
+ (void)userBindWX:(NSMutableDictionary*)param
success:(void (^)(id responseObject))success
failure:(void (^)(id faiObject))failure{
NSString *url = [MyHttp getURL:HTTP_ACCOUNTS_BIND_AND_UNBIND_WX
objArr:@[currentUser]];
[[MyHttp sharedClient] URL:url
method:@"POST" parameters:param
success:^(NSURLSessionDataTask *task, id responseObject)
{
if ([responseObject[@"code"] intValue] == HTTP_SUCCESS) {
[[User currentUser] mj_setKeyValues:responseObject];
if(success){success(responseObject);}
[User saveToFile];
}else{
if(failure){failure(responseObject);}
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
if (failure) {
failure(error);
}
} showHUD:YES];
}
#pragma 手机账号 解绑微信账号
+ (void)userUNBindWXSuccess:(void (^)(id responseObject))success
failure:(void (^)(id faiObject))failure{
NSString *url = [MyHttp getURL:HTTP_ACCOUNTS_BIND_AND_UNBIND_WX
objArr:@[currentUser]];
[[MyHttp sharedClient] URL:url
method:@"DELETE" parameters:nil
success:^(NSURLSessionDataTask *task, id responseObject)
{
if ([responseObject[@"code"] intValue] == HTTP_SUCCESS) {
[[User currentUser] mj_setKeyValues:responseObject];
if(success){success(responseObject);}
[User saveToFile];
}else{
if(failure){failure(responseObject);}
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
if (failure) {
failure(error);
}
} showHUD:YES];
}
#pragma 获取用户关联设备
- (void)getRelatedDeviceSuccess:(void (^)(id responseObject))success
failure:(void (^)(id faiObject))failure{
NSString *url = [MyHttp getURL:HTTP_ACCOUNTS__RELATEDDEVICE
objArr:@[currentUser]];
[[MyHttp sharedClient] URL:url
method:@"GET" parameters:@{}
success:^(NSURLSessionDataTask *task, id responseObject)
{
if ([responseObject[@"code"] intValue] == HTTP_SUCCESS) {
[[User currentUser] mj_setKeyValues:responseObject];
[User saveToFile];
//MARK: 添加广告开关 代码
if(responseObject[@"ad_rate"]){
if ([responseObject[@"ad_rate"] intValue] == 2) {
[cUser getADConfig];
}
}
if(success){success(responseObject);}
}else{
if(failure){failure(responseObject);}
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
if (failure) {
failure(error);
}
} showHUD:NO];
}
/// 获取未读消息
- (void)GetNotificationCountWithSuccess:(void (^)(id responseObject))success failure:(void (^)(NSError *error))failure
{
NSString *urlStr = [MyHttp getURL:HTTP_GET_NotificationCount
objArr:@[currentUser]];
[xMyHttp URL:urlStr
method:@"GET" parameters:nil
success:^(NSURLSessionDataTask *task, id responseObject)
{
if ([responseObject[@"code"] intValue] == 0) {
success(responseObject);
} else {
if (failure) {
NSString *domain = @"com.zuoyebang.iot.watch";
NSDictionary *userInfo = @{@"msg" : responseObject[@"msg"]};
NSError *err = [NSError errorWithDomain:domain code:-1 userInfo:userInfo];
failure(err);
}
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
if (failure) {
failure(error);
}
}];
}
- (void)getRelatedChatGroupSuccess:(void (^)(id responseObject))success
failure:(void (^)(id faiObject))failure{
NSString *url = [MyHttp getURL:HTTP_CHATGROUPS__RELATECHATGROP
objArr:@[currentUser]];
[[MyHttp sharedClient] URL:url
method:@"GET" parameters:@{}
success:^(NSURLSessionDataTask *task, id responseObject)
{
if ([responseObject[@"code"] intValue] == HTTP_SUCCESS) {
[[User currentUser] mj_setKeyValues:responseObject];
if(success){success(responseObject);}
[User saveToFile];
}else{
if(failure){failure(responseObject);}
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
if (failure) {
failure(error);
}
} showHUD:NO];
}
#pragma mark --- 获取环信MI 用户 (与设备对应的 MI 用户)
- (void)getHXAccoutSuccess:(void (^)(id responseObject))success
failure:(void (^)())failure{
// accounts/[imei]/video
NSString *urlStr = [MyHttp getURL:HTTP_POST_GETHXID
objArr:@[self.cDevice]];
[xMyHttp URL:urlStr
method:@"GET" parameters:nil
success:^(NSURLSessionDataTask *task, id responseObject)
{
if ([responseObject[@"code"] intValue] == HTTP_SUCCESS) {
if(responseObject[@"username"] &&
responseObject[@"password"] &&
responseObject[@"appkey"]){
success(responseObject);
}else{
xLog(@"缺少返回参数username password appkey 之一");
if (failure) {
failure();
}
}
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
if (failure) {
failure();
}
} showHUD:YES];
}
#pragma 添加关联设备
- (void)addDeviceWithDevice:(Device *)device
success:(void (^)(id responseObject))success
failure:(void (^)(id faiObject))failure{
NSString *urlStr = [MyHttp getURL:HTTP_ACCOUNTS__RELATEDDEVICE
objArr:@[device,self]];
NSMutableDictionary *parameters = [device mj_keyValues];
[xMyHttp URL:urlStr
method:@"POST" parameters:parameters
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
if (device.image) {
[formData appendPartWithFileData:UIImageJPEGRepresentation(device.image, 0.1)
name:@"image"
fileName:@"image.jpg"
mimeType:@"image/jpeg"];
}
}
success:^(NSURLSessionDataTask *task, id responseObject)
{
if ([responseObject[@"code"] intValue] == HTTP_SUCCESS) {
if(success){success(responseObject);}
[User saveToFile];
}else{
if(failure){failure(responseObject);}
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
if (failure) {
failure(error);
}
}];
}
- (void)localDeleteDevice{
if ([self.ownedDevices containsObject:self.cDevice]) {
[self.ownedDevices removeObject:self.cDevice];
}else if([self.chatGroupDevices containsObject:self.cDevice]){
[self.chatGroupDevices removeObject:self.cDevice];
}
if([self.myDevices containsObject:self.cDevice]){
[self.myDevices removeObject:self.cDevice];
}
if(self.myDevices.count > 0){
self.cDevice = [self.myDevices firstObject];
}else{
self.cDevice = nil;
}
//清空 一下 聊天群组
self.chatGroups = nil;
}
#pragma 扫码查询设备是否存在管理员,如果存在则得到管理员的 openid 和 phone
/**
扫码查询设备是否存在管理员,如果存在则得到管理员的 openid 和 phone
@param params @{@"imei":@"XXXX"}
@param success <#success description#>
@param failure <#failure description#>
*/
- (void)getDeviceIsHaveAdmin:(NSMutableDictionary *)params
success:(void (^)(id responseObject))success
failure:(void (^)(id faiObject))failure{
NSString *urlStr = [MyHttp getURL:HTTP_ACCOUNTS__AFTER
objArr:@[currentUser]];
[xMyHttp URL:urlStr
method:@"POST" parameters:params
success:^(NSURLSessionDataTask *task, id responseObject)
{
if ([responseObject[@"code"] intValue] == HTTP_SUCCESS) {
// if (![cUser getDeviceWithImei:params[@"imei"]])
// {
// Device *de = [Device new];
// de.imei = params[@"imei"];
// de.deviceType = responseObject[@"deviceType"];
// de.name = @"";
// [self setOwnedDevices:[NSMutableArray arrayWithArray:@[de]]];
// }
// //self.cDevice.deviceType = [NSNumber numberWithInteger:3];
// NSLog(@"%@",self.cDevice);
if(success){success(responseObject);}
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
if (failure) {
failure(error);
}
}];
}
#pragma 发送关注申请的接口 需要管理员的 openid 和 phone
/**
发送关注申请的接口 需要管理员的 openid 和 phone
*/
- (void)postDeviceInAdmin:(NSMutableDictionary *)params
success:(void (^)(id responseObject))success
failure:(void (^)(id faiObject))failure{
NSString *urlStr = [MyHttp getURL:HTTP_ACCOUNTS__APPLY_BIND
objArr:@[currentUser]];
[xMyHttp URL:urlStr
method:@"POST" parameters:params
success:^(NSURLSessionDataTask *task, id responseObject)
{
if ([responseObject[@"code"] intValue] == HTTP_SUCCESS) {
if(success){success(responseObject);}
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
if (failure) {
failure(error);
}
}];
}
#pragma 删除关联设备
- (void)deleteDeviceSuccess:(void (^)(id responseObject))success
failure:(void (^)(id faiObject))failure{
NSString *urlStr = [MyHttp getURL:HTTP_ACCOUNTS__RELATEDDEVICE
objArr:@[self.cDevice,[User currentUser]]];
[xMyHttp URL:urlStr
method:@"DELETE" parameters:@{@"imei":cUser.cDevice.imei}
success:^(NSURLSessionDataTask *task, id responseObject)
{
if ([responseObject[@"code"] intValue] == HTTP_SUCCESS) {
[self localDeleteDevice];
if(success){success(responseObject);}
[User saveToFile];
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
if (failure) {
failure(error);
}
}];
}
+ (void)setDeviceTokenWithNet:(NSString *)deviceToken AndVoIPToken:(NSString *)VoIPToken method:(NSString *)method{
NSString *version = @"produce";
#if defined(DEBUG)||defined(_DEBUG)
version = @"develop";
#endif
NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithDictionary:@{
@"version":version,
@"package":[[NSBundle mainBundle] bundleIdentifier]
}];
[parameters setValue:VoIPToken forKey:@"voip_devicetoken"];
[xMyHttp URL:[NSString stringWithFormat:HTTP_DEVICETOKEN__,deviceToken]
method:method
parameters:parameters constructingBodyWithBlock:nil
success:^(NSURLSessionDataTask *task, id responseObject) {}
failure:^(NSURLSessionDataTask *task, NSError *error) {}
showHUD:NO
showError:NO];
}
#pragma mark --- 获取问题类别和热门问题
+ (void)getCategoryQAndHotQSuccess:(void (^)(id responseObject))success
failure:(void (^)(id faiObject))failure{
NSString *url = [MyHttp getURL:HTTP_GET_CQ_AND_HOTQ
objArr:@[currentUser]];
[[MyHttp sharedClient] URL:url
method:@"GET" parameters:@{}
success:^(NSURLSessionDataTask *task, id responseObject)
{
if ([responseObject[@"code"] intValue] == HTTP_SUCCESS) {
if (success) {success(responseObject);}
}else{
if(failure){failure(responseObject);}
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
if (failure) {
failure(error);
}
} showHUD:YES];
}
#pragma mark --- 获取某个问题类别中的所有问题
+ (void)getAllCategoryQWithCID:(NSString*)cid
success:(void (^)(id responseObject))success
failure:(void (^)(id faiObject))failure{
NSString *url = [MyHttp getURL:HTTP_GET_ALL_CQ
objArr:@[currentUser]];
url = [url stringByReplacingOccurrencesOfString:@"[categoryid]" withString:cid];
[[MyHttp sharedClient] URL:url
method:@"GET" parameters:@{}
success:^(NSURLSessionDataTask *task, id responseObject)
{
if ([responseObject[@"code"] intValue] == HTTP_SUCCESS) {
if (success) {success(responseObject);}
}else{
if(failure){failure(responseObject);}
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
if (failure) {
failure(error);
}
} showHUD:YES];
}
+ (void)setCurrentUser:(User *)user
{
AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
if (user && app.deviceTokenStr) {
currentUser = user;//先赋值,不然上传没有用户数据
[self setDeviceTokenWithNet:app.deviceTokenStr AndVoIPToken:app.deviceVoIPTokenStr method:@"POST"];
}else if(!user && currentUser){
[self setDeviceTokenWithNet:app.deviceTokenStr AndVoIPToken:app.deviceVoIPTokenStr method:@"DELETE"];
}
currentUser = user;
// [xMyMQTT setSubscriptions:nil];
[User saveToFile];
}
+ (void)setDeviceToken:(NSString *)deviceToken AndVoIPToken:(NSString *)VoIPToken{
if (currentUser) {
[self setDeviceTokenWithNet:deviceToken AndVoIPToken:VoIPToken method:@"POST"];
}else{
[self setDeviceTokenWithNet:deviceToken AndVoIPToken:VoIPToken method:@"DELETE"];
}
}
+ (void)loadFormFile{
[baseModel mj_setupIgnoredPropertyNames:^NSArray *{
return @[@"block",@"image"];
}];
[User mj_setupIgnoredPropertyNames:^NSArray *{
return @[@"curLocation",@"cDevice",@"myDevices"];
}];
[User mj_setupObjectClassInArray:^NSDictionary *{
return @{@"ownedDevices":@"Device",
@"chatGroupDevices":@"Device",
@"chatGroups":@"Circle",
@"questions":@"CSQuestion"};
}];
[Circle mj_setupObjectClassInArray:^NSDictionary *{
return @{@"members":@"User",
@"devices":@"Device"};
}];
[Device mj_setupObjectClassInArray:^NSDictionary *{
return @{@"alerts":@"Clock",
@"chatGroupList":@"Circle",
@"contacts":@"Contacts"};
}];
NSString *path = [CJFileUtility documentsPathSubDir:@"user"
andFile:@"user_data"];
NSData *data = [NSData dataWithContentsOfFile:path];
if (data) {
NSMutableDictionary *userDic = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:path]
options:NSJSONReadingMutableContainers
error:nil];
xLog(@"%@",userDic);
NSMutableDictionary *tmp = [NSMutableDictionary dictionary];
[userDic enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
if ([obj isKindOfClass:[NSArray class]]) {
[tmp setObject:obj forKey:key];
[userDic removeObjectForKey:key];
}
}];
[User setCurrentUser:[User mj_objectWithKeyValues:userDic]];
[[User currentUser] mj_setKeyValues:tmp];
}
}
+ (void)deleteFormFile{
// if ([[User currentUser].admin boolValue] && [bleObject defaultManager].peripheral) {
// [[bleObject defaultManager].manager cancelPeripheralConnection:[bleObject defaultManager].peripheral];
// }
NSString *path = [CJFileUtility documentsPathSubDir:@"user" andFile:@"user_data"];
[[NSFileManager defaultManager] removeItemAtPath:path error:nil];
}
+ (void)saveToFile{
// NSString* libraryDir = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES)[0];
// NSString* filePath = [libraryDir stringByAppendingPathComponent:@"user.plist"];
// NSLog(@"%@",currentUser.mj_keyValues);
// NSMutableArray *tmp = [NSMutableArray arrayWithArray:currentUser.myCircles];
// currentUser.babies = [NSMutableArray arrayWithArray:@[]];
if (currentUser) {
NSString *path = [CJFileUtility documentsPathSubDir:@"user"
andFile:@"user_data"];
NSData *data = [NSJSONSerialization dataWithJSONObject:[currentUser mj_keyValues]
options:NSJSONWritingPrettyPrinted error:nil];
BOOL isSave = [CJFileUtility writeToFile:path data:data];
xLog(@"isSave = %d %@ %@",isSave,currentUser.openid,currentUser.accesstoken);
if (!isSave) {
xLog(@"%@",[currentUser mj_keyValues]);
}
}else{
[User deleteFormFile];
}
// currentUser.babies = tmp;
}
- (void)getADConfig{
NSString *urlStr = [MyHttp getURL:HTTP_ADS_CONFIG
objArr:@[self]];
[xMyHttp URL:urlStr method:@"GET" parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
if ([responseObject[@"code"] intValue] == HTTP_SUCCESS) {
[UserDefaults setObject:responseObject forKey:kNewADKey];
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
}];
}
- (void)setSwitchDataState:(NSInteger)state
success:(void (^)(id responseObject))success
failure:(void (^)())failure{
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
[parameters setValue:@(state) forKey:@"switch"];
[parameters setValue:cUser.cDevice.imei forKey:@"imei"];
[parameters setValue:cUser.openid forKey:@"openid"];
[parameters setValue:cUser.accesstoken forKey:@"accesstoken"];
NSString *urlStr = [MyHttp getURL:HTTP_DEVICES_GpsSwitch objArr:@[cUser.cDevice]];
[xMyHttp URL:urlStr
method:@"POST" parameters:parameters
success:^(NSURLSessionDataTask *task, id responseObject)
{
if ([responseObject[@"code"] intValue] == HTTP_SUCCESS) {
success(responseObject);
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
if (failure) {
failure(error);
}
}];
}
/**
根据imei获取供应商的配置
*/
- (void)getSupplierConfigWithDevice:(Device *)device{
NSString *urlStr = [MyHttp getURL:HTTP_GETSUPPLIER_CONFIG
objArr:@[device]];
[xMyHttp URL:urlStr method:@"GET" parameters:@{@"type":@"ios"} success:^(NSURLSessionDataTask *task, id responseObject) {
if ([responseObject[@"code"] intValue] == HTTP_SUCCESS) {
[UserDefaults setObject:responseObject forKey:kSupplierConfigKey];
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
}];
}
//MARK: 上传声网视频通话的通话时长和视频通话的分辨率
- (void)postVideoCallTimeParame:(NSDictionary*) parameters
deviceModel:(Device *)deviceModel
success:(void (^)(id responseObject))success
failure:(void (^)())failure{
// accounts/[imei]/video
NSString *urlStr = [MyHttp getURL:HTTP_POST_VideoOperation
objArr:@[deviceModel]];
[xMyHttp URL:urlStr
method:@"POST" parameters:parameters
success:^(NSURLSessionDataTask *task, id responseObject)
{
if ([responseObject[@"code"] intValue] == HTTP_SUCCESS) {
success(responseObject);
}else{
if (failure) {
failure();
}
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
if (failure) {
failure();
}
} showHUD:YES];
}
/**
MARK: 主动挂断通话
@param videoID 要挂断的通话 ID
*/
- (void)activeHangUpVideoID:(NSString*)videoID
WithDevice:(Device*)device
WithSuccess:(void (^)(id responseObject))success failure:(void (^)())failure{
// accounts/[imei]/video
NSString *urlStr = [MyHttp getURL:HTTP_POST_VideoOperation
objArr:@[device]];
NSMutableDictionary * dic = [NSMutableDictionary dictionary];
[dic setValue:videoID forKey:@"video_id"];
[xMyHttp URL:urlStr
method:@"DELETE" parameters:dic
success:^(NSURLSessionDataTask *task, id responseObject)
{
if ([responseObject[@"code"] intValue] == HTTP_SUCCESS) {
success(responseObject);
}else{
if (failure) {
failure();
}
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
if (failure) {
failure();
}
} showHUD:YES];
}
#pragma mark --------- 视频获取信息接口(声网和菊风共用) ----------
- (void)getVideoAccoutCallType:(int)callType
deviceModel:(Device *)deviceModel
Success:(void (^)(id responseObject))success
failure:(void (^)())failure{
// getway/accounts/[imei]/video
NSString *urlStr = [MyHttp getURL:HTTP_POST_VideoOperation
objArr:@[deviceModel]];
NSMutableDictionary * param = [NSMutableDictionary dictionary];
[param setValue:@(callType) forKey:@"callType"];
[xMyHttp URL:urlStr
method:@"GET" parameters:param
success:^(NSURLSessionDataTask *task, id responseObject)
{
if ([responseObject[@"code"] intValue] == HTTP_SUCCESS) {
success(responseObject);
}else{
if (failure) {
failure();
}
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
if (failure) {
failure();
}
} showHUD:YES];
}
- (void)getVideoInfoWithDeviceImei:(NSString*)deviceImei
CallType:(int)callType
Success:(void (^)(id responseObject))success
failure:(void (^)(void))failure{
// getway/accounts/[imei]/video
NSString *urlStr = [HTTP_POST_VideoOperation stringByReplacingOccurrencesOfString:@"[imei]" withString:deviceImei];
NSMutableDictionary * param = [NSMutableDictionary dictionary];
[param setValue:@(callType) forKey:@"callType"];
[param setValue:@"getInfo" forKey:@"type"];
[xMyHttp URL:urlStr
method:@"GET" parameters:param
success:^(NSURLSessionDataTask *task, id responseObject)
{
if ([responseObject[@"code"] intValue] == HTTP_SUCCESS) {
success(responseObject);
}else{
if (failure) {
failure();
}
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
if (failure) {
failure();
}
} showHUD:YES];
}
#pragma mark --------- Juphoon获取信息接口 End----------
@end