OrgService.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. //
  2. // OrgService.m
  3. // WildFireChat
  4. //
  5. // Created by Heavyrain Lee on 2019/10/22.
  6. // Copyright © 2019 WildFireChat. All rights reserved.
  7. //
  8. #import "OrgService.h"
  9. #import <WFChatClient/WFCChatClient.h>
  10. #import "AFNetworking.h"
  11. #import "WFCConfig.h"
  12. #import <WFChatUIKit/WFChatUIKit.h>
  13. #import <WebKit/WebKit.h>
  14. static OrgService *sharedSingleton = nil;
  15. #define WFC_ORGSERVER_AUTH_TOKEN @"WFC_ORGSERVER_AUTH_TOKEN"
  16. #define AUTHORIZATION_HEADER @"authToken"
  17. @interface OrgService ()
  18. @property(nonatomic, assign)BOOL isServiceAvailable;
  19. @end
  20. @implementation OrgService
  21. + (OrgService *)sharedOrgService {
  22. if (sharedSingleton == nil) {
  23. @synchronized (self) {
  24. if (sharedSingleton == nil) {
  25. sharedSingleton = [[OrgService alloc] init];
  26. }
  27. }
  28. }
  29. return sharedSingleton;
  30. }
  31. - (void)login:(void(^)(void))successBlock error:(void(^)(int errCode))errorBlock {
  32. if(!ORG_SERVER_ADDRESS) {
  33. NSLog(@"没有配置组织通讯录服务!");
  34. errorBlock(-1);
  35. return;
  36. }
  37. [[WFCCIMService sharedWFCIMService] getAuthCode:@"admin" type:2 host:IM_SERVER_HOST success:^(NSString *authCode) {
  38. [self post:@"/api/user_login" data:@{@"authCode":authCode} isLogin:YES success:^(NSDictionary *dict) {
  39. if([dict[@"code"] intValue] == 0) {
  40. self.isServiceAvailable = YES;
  41. if(successBlock) successBlock();
  42. } else {
  43. if(errorBlock) errorBlock([dict[@"code"] intValue]);
  44. }
  45. } error:^(NSError * _Nonnull error) {
  46. if(errorBlock) errorBlock(-1);
  47. }];
  48. } error:^(int error_code) {
  49. if(errorBlock) errorBlock(error_code);
  50. }];
  51. }
  52. - (void)getRelationship:(NSString *)employeeId
  53. success:(void(^)(NSArray<WFCUOrgRelationship *> *))successBlock
  54. error:(void(^)(int error_code))errorBlock {
  55. [self post:@"/api/relationship/employee" data:@{@"employeeId":employeeId} isLogin:NO success:^(NSDictionary *dict) {
  56. if([dict[@"code"] intValue] == 0) {
  57. NSMutableArray *result = [[NSMutableArray alloc] init];
  58. NSArray *arr = dict[@"result"];
  59. [arr enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
  60. WFCUOrgRelationship *rs = [self relationshipFromDict:obj];
  61. [result addObject:rs];
  62. }];
  63. if(successBlock) successBlock(result);
  64. } else {
  65. if(errorBlock) errorBlock([dict[@"code"] intValue]);
  66. }
  67. } error:^(NSError * _Nonnull error) {
  68. if(errorBlock) errorBlock(-1);
  69. }];
  70. }
  71. - (WFCUOrganization *)organizationFromDict:(NSDictionary *)obj {
  72. WFCUOrganization *org = [[WFCUOrganization alloc] init];
  73. org.organizationId = [obj[@"id"] intValue];
  74. org.parentId = [obj[@"parentId"] intValue];
  75. org.managerId = obj[@"managerId"];
  76. org.name = obj[@"name"];
  77. org.desc = obj[@"desc"];
  78. org.portraitUrl = obj[@"portraitUrl"];
  79. org.tel = obj[@"tel"];
  80. org.office = obj[@"office"];
  81. org.groupId = obj[@"groupId"];
  82. org.memberCount = [obj[@"memberCount"] intValue];
  83. org.sort = [obj[@"sort"] intValue];
  84. org.updateDt = [obj[@"updateDt"] longLongValue];
  85. org.createDt = [obj[@"createDt"] longLongValue];
  86. return org;
  87. }
  88. - (WFCUEmployee *)employeeFromDict:(NSDictionary *)obj {
  89. WFCUEmployee *emp = [[WFCUEmployee alloc] init];
  90. emp.employeeId = obj[@"employeeId"];
  91. emp.organizationId = [obj[@""] intValue];
  92. emp.name = obj[@"name"];
  93. emp.title = obj[@"title"];
  94. emp.level = [obj[@"level"] intValue];
  95. emp.mobile = obj[@"mobile"];
  96. emp.email = obj[@"email"];
  97. emp.ext = obj[@"ext"];
  98. emp.office = obj[@"office"];
  99. emp.city = obj[@"city"];
  100. emp.portraitUrl = obj[@"portraitUrl"];
  101. emp.jobNumber = obj[@"jobNumber"];
  102. emp.joinTime = obj[@"joinTime"];
  103. emp.type = [obj[@"type"] intValue];
  104. emp.gender = [obj[@"gender"] intValue];
  105. emp.sort = [obj[@"sort"] intValue];
  106. emp.createDt = [obj[@"createDt"] longLongValue];
  107. emp.updateDt = [obj[@"updateDt"] longLongValue];
  108. return emp;
  109. }
  110. - (WFCUOrgRelationship *)relationshipFromDict:(NSDictionary *)obj {
  111. WFCUOrgRelationship *rs = [[WFCUOrgRelationship alloc] init];
  112. rs.employeeId = obj[@"employeeId"];
  113. rs.organizationId = [obj[@"organizationId"] intValue];
  114. rs.depth = [obj[@"depth"] intValue];
  115. rs.bottom = [obj[@"bottom"] boolValue];
  116. rs.parentOrganizationId = [obj[@"parentOrganizationId"] intValue];
  117. return rs;
  118. }
  119. - (void)getRootOrganization:(void(^)(NSArray<WFCUOrganization *> *))successBlock
  120. error:(void(^)(int error_code))errorBlock {
  121. [self post:@"/api/organization/root" data:nil isLogin:NO success:^(NSDictionary *dict) {
  122. if([dict[@"code"] intValue] == 0) {
  123. NSMutableArray *result = [[NSMutableArray alloc] init];
  124. NSArray *arr = dict[@"result"];
  125. [arr enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
  126. WFCUOrganization *org = [self organizationFromDict:obj];
  127. [result addObject:org];
  128. }];
  129. if(successBlock) successBlock(result);
  130. } else {
  131. if(errorBlock) errorBlock([dict[@"code"] intValue]);
  132. }
  133. } error:^(NSError * _Nonnull error) {
  134. if(errorBlock) errorBlock(-1);
  135. }];
  136. }
  137. - (void)getOrganizationEx:(NSInteger)organizationId
  138. success:(void(^)(WFCUOrganizationEx *ex))successBlock
  139. error:(void(^)(int error_code))errorBlock {
  140. [self post:@"/api/organization/query_ex" data:@{@"id":@(organizationId)} isLogin:NO success:^(NSDictionary *dict) {
  141. if([dict[@"code"] intValue] == 0) {
  142. NSDictionary *d = dict[@"result"];
  143. WFCUOrganization *org = [self organizationFromDict:d[@"organization"]];
  144. NSMutableArray *subOrgs = [[NSMutableArray alloc] init];
  145. if(d[@"subOrganizations"]) {
  146. NSArray *arr = d[@"subOrganizations"];
  147. [arr enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  148. WFCUOrganization *org = [self organizationFromDict:obj];
  149. [subOrgs addObject:org];
  150. }];
  151. }
  152. NSMutableArray *employees = [[NSMutableArray alloc] init];
  153. if(d[@"employees"]) {
  154. NSArray *arr = d[@"employees"];
  155. [arr enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  156. WFCUEmployee *emp = [self employeeFromDict:obj];
  157. [employees addObject:emp];
  158. }];
  159. }
  160. WFCUOrganizationEx *ex = [[WFCUOrganizationEx alloc] init];
  161. ex.organizationId = organizationId;
  162. ex.organization = org;
  163. ex.subOrganizations = subOrgs;
  164. ex.employees = employees;
  165. if(successBlock) successBlock(ex);
  166. } else {
  167. if(errorBlock) errorBlock([dict[@"code"] intValue]);
  168. }
  169. } error:^(NSError * _Nonnull error) {
  170. if(errorBlock) errorBlock(-1);
  171. }];
  172. }
  173. - (void)getOrganizations:(NSArray<NSNumber *> *)organizationIds
  174. success:(void(^)(NSArray<WFCUOrganization *> *organizations))successBlock
  175. error:(void(^)(int error_code))errorBlock {
  176. [self post:@"/api/organization/query_list" data:@{@"ids":organizationIds} isLogin:NO success:^(NSDictionary *dict) {
  177. if([dict[@"code"] intValue] == 0) {
  178. NSArray *arr = dict[@"result"];
  179. NSMutableArray *orgs = [[NSMutableArray alloc] init];
  180. [arr enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  181. WFCUOrganization *org = [self organizationFromDict:obj];
  182. [orgs addObject:org];
  183. }];
  184. if(successBlock) successBlock(orgs);
  185. } else {
  186. if(errorBlock) errorBlock([dict[@"code"] intValue]);
  187. }
  188. } error:^(NSError * _Nonnull error) {
  189. if(errorBlock) errorBlock(-1);
  190. }];
  191. }
  192. - (void)getBatchOrgEmployees:(NSArray<NSNumber *> *)orgIds
  193. success:(void(^)(NSArray<NSString *> *employeeIds))successBlock
  194. error:(void(^)(int error_code))errorBlock {
  195. [self post:@"/api/organization/batch_employees" data:@{@"ids":orgIds} isLogin:NO success:^(NSDictionary *dict) {
  196. if([dict[@"code"] intValue] == 0) {
  197. NSArray *arr = dict[@"result"];
  198. if(successBlock) successBlock(arr);
  199. } else {
  200. if(errorBlock) errorBlock([dict[@"code"] intValue]);
  201. }
  202. } error:^(NSError * _Nonnull error) {
  203. if(errorBlock) errorBlock(-1);
  204. }];
  205. }
  206. - (void)getOrgEmployees:(NSInteger)orgId
  207. success:(void(^)(NSArray<NSString *> *employeeIds))successBlock
  208. error:(void(^)(int error_code))errorBlock {
  209. [self post:@"/api/organization/employees" data:@{@"id":@(orgId)} isLogin:NO success:^(NSDictionary *dict) {
  210. if([dict[@"code"] intValue] == 0) {
  211. NSArray *arr = dict[@"result"];
  212. if(successBlock) successBlock(arr);
  213. } else {
  214. if(errorBlock) errorBlock([dict[@"code"] intValue]);
  215. }
  216. } error:^(NSError * _Nonnull error) {
  217. if(errorBlock) errorBlock(-1);
  218. }];
  219. }
  220. - (void)getEmployee:(NSString *)employeeId
  221. success:(void(^)(WFCUEmployee *employee))successBlock
  222. error:(void(^)(int error_code))errorBlock {
  223. [self post:@"/api/employee/query" data:@{@"employeeId":employeeId} isLogin:NO success:^(NSDictionary *dict) {
  224. if([dict[@"code"] intValue] == 0) {
  225. NSDictionary *emp = dict[@"result"];
  226. WFCUEmployee *employee = [self employeeFromDict:emp];
  227. if(successBlock) successBlock(employee);
  228. } else {
  229. if(errorBlock) errorBlock([dict[@"code"] intValue]);
  230. }
  231. } error:^(NSError * _Nonnull error) {
  232. if(errorBlock) errorBlock(-1);
  233. }];
  234. }
  235. - (void)getEmployeeEx:(NSString *)employeeId
  236. success:(void(^)(WFCUEmployeeEx *employeeEx))successBlock
  237. error:(void(^)(int error_code))errorBlock {
  238. [self post:@"/api/employee/query_ex" data:@{@"employeeId":employeeId} isLogin:NO success:^(NSDictionary *dict) {
  239. if([dict[@"code"] intValue] == 0) {
  240. NSDictionary *exDict = dict[@"result"];
  241. WFCUEmployee *employee = [self employeeFromDict:exDict[@"employee"]];
  242. NSArray *arr = exDict[@"relationships"];
  243. NSMutableArray *result = [[NSMutableArray alloc] init];
  244. [arr enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  245. WFCUOrgRelationship *rs = [self relationshipFromDict:obj];
  246. [result addObject:rs];
  247. }];
  248. WFCUEmployeeEx *empEx = [[WFCUEmployeeEx alloc] init];
  249. empEx.employeeId = employeeId;
  250. empEx.employee = employee;
  251. empEx.relationships = result;
  252. if(successBlock) successBlock(empEx);
  253. } else {
  254. if(errorBlock) errorBlock([dict[@"code"] intValue]);
  255. }
  256. } error:^(NSError * _Nonnull error) {
  257. if(errorBlock) errorBlock(-1);
  258. }];
  259. }
  260. - (void)searchEmployee:(NSInteger)organizationId
  261. keyword:(NSString *)keyword
  262. success:(void(^)(NSArray<WFCUEmployee *> *employees))successBlock
  263. error:(void(^)(int error_code))errorBlock {
  264. [self post:@"/api/employee/search" data:@{@"keyword":keyword, @"organizationId":@(organizationId), @"count":@(50), @"page":@(0)} isLogin:NO success:^(NSDictionary *dict) {
  265. if([dict[@"code"] intValue] == 0) {
  266. NSDictionary *exDict = dict[@"result"];
  267. int totalPages = [exDict[@"totalPages"] intValue];
  268. int totalCount = [exDict[@"totalCount"] intValue];
  269. int currentPage = [exDict[@"currentPage"] intValue];
  270. NSArray<NSDictionary *> *arr = exDict[@"contents"];
  271. NSMutableArray *result = [[NSMutableArray alloc] init];
  272. [arr enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  273. WFCUEmployee *rs = [self employeeFromDict:obj];
  274. [result addObject:rs];
  275. }];
  276. if(successBlock) successBlock(result);
  277. } else {
  278. if(errorBlock) errorBlock([dict[@"code"] intValue]);
  279. }
  280. } error:^(NSError * _Nonnull error) {
  281. if(errorBlock) errorBlock(-1);
  282. }];
  283. }
  284. - (void)post:(NSString *)path data:(id)data isLogin:(BOOL)isLogin success:(void(^)(NSDictionary *dict))successBlock error:(void(^)(NSError * _Nonnull error))errorBlock {
  285. if(!isLogin && !self.isServiceAvailable) {
  286. NSLog(@"组织通讯录服务不可用,请确保先登录再使用组织通讯录");
  287. errorBlock([NSError errorWithDomain:@"" code:401 userInfo:@{NSLocalizedDescriptionKey:@"未登录"}]);
  288. return;
  289. }
  290. AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
  291. manager.requestSerializer = [AFJSONRequestSerializer serializer];
  292. manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
  293. //在调用其他接口时需要把token传给后台,也就是设置token的过程
  294. NSString *authToken = [self getOrgServiceAuthToken];
  295. if(authToken.length) {
  296. [manager.requestSerializer setValue:authToken forHTTPHeaderField:AUTHORIZATION_HEADER];
  297. }
  298. [manager POST:[ORG_SERVER_ADDRESS stringByAppendingPathComponent:path]
  299. parameters:data
  300. progress:nil
  301. success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
  302. if(isLogin) { //鉴权信息
  303. NSString *appToken;
  304. if ([task.response isKindOfClass:[NSHTTPURLResponse class]]) {
  305. NSHTTPURLResponse *r = (NSHTTPURLResponse *)task.response;
  306. appToken = [r allHeaderFields][AUTHORIZATION_HEADER];
  307. }
  308. if(appToken.length) {
  309. [[NSUserDefaults standardUserDefaults] setObject:appToken forKey:WFC_ORGSERVER_AUTH_TOKEN];
  310. }
  311. }
  312. NSDictionary *dict = responseObject;
  313. if([dict[@"code"] intValue] > 0) {
  314. NSLog(@"API request failure:%@", dict[@"message"]);
  315. }
  316. dispatch_async(dispatch_get_main_queue(), ^{
  317. successBlock(dict);
  318. });
  319. }
  320. failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
  321. NSLog(@"Http request failure:%@", error);
  322. dispatch_async(dispatch_get_main_queue(), ^{
  323. errorBlock(error);
  324. });
  325. }];
  326. }
  327. - (NSString *)getOrgServiceAuthToken {
  328. return [[NSUserDefaults standardUserDefaults] objectForKey:WFC_ORGSERVER_AUTH_TOKEN];
  329. }
  330. - (void)clearOrgServiceAuthInfos {
  331. [[NSUserDefaults standardUserDefaults] removeObjectForKey:WFC_ORGSERVER_AUTH_TOKEN];
  332. //remove kit org cache
  333. [[WFCUOrganizationCache sharedCache] clearCaches];
  334. }
  335. @end