猎豆优选

LDMyWechatViewController.m 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. //
  2. // LDMyWechatViewController.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/8/9.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "LDMyWechatViewController.h"
  9. #import "STPhotoKitController.h"
  10. #import "UIImagePickerController+ST.h"
  11. #import <AdSupport/AdSupport.h>
  12. @interface LDMyWechatViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate,STPhotoKitDelegate>
  13. {
  14. UIButton *_complete;
  15. UILabel *_desLabel;
  16. NSURLSessionUploadTask *_uploadTask;
  17. NSString *_changeImgUrl;
  18. UIImage *_selectImg;
  19. BOOL selectedImg;
  20. }
  21. @property (nonatomic, strong) UITextField *textField;
  22. @property (nonatomic, strong) UIButton *switchBtn;
  23. @property (nonatomic, strong) UIImageView *imgView;
  24. @property (nonatomic, strong) UILabel *textLb;
  25. @property (nonatomic, strong) UIActivityIndicatorView *indicatorView;
  26. @end
  27. @implementation LDMyWechatViewController
  28. - (void)viewWillDisappear:(BOOL)animated {
  29. [super viewWillDisappear:animated];
  30. [LoadingView dismiss];
  31. }
  32. - (void)viewWillAppear:(BOOL)animated {
  33. [super viewWillAppear:animated];
  34. [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
  35. }
  36. - (void)viewDidLoad {
  37. [super viewDidLoad];
  38. [self configNavigationBar];
  39. [self initSubUI];
  40. [self request];
  41. }
  42. - (void)configNavigationBar {
  43. [self.navigationBar setNavTitle:@"我的微信号"];
  44. self.navigationBar.backgroundColor = [UIColor changeColor];
  45. self.navigationBar.navTitleLabel.textColor = [UIColor whiteColor];
  46. UIButton *leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
  47. [leftBtn setImage:[UIImage imageNamed:@"back_white"] forState:UIControlStateNormal];
  48. [leftBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
  49. [self.navigationBar setCustomLeftButtons:@[leftBtn]];
  50. self.view.backgroundColor = [UIColor yhGrayColor];
  51. }
  52. - (void)backAction {
  53. [self.navigationController popViewControllerAnimated:YES];
  54. }
  55. - (void)initSubUI {
  56. // UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, NavBarHeight+15, 10, 25)];
  57. // label.textColor = [UIColor YHColorWithHex:0x333333];
  58. // label.font = [UIFont systemFontOfSize:Fitsize(16)];
  59. // label.text = @"我的微信号:";
  60. // [label sizeToFit];
  61. // [self.view addSubview:label];
  62. self.imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, NavBarHeight+Fitsize(70), Fitsize(170), Fitsize(170))];
  63. self.imgView.backgroundColor = [UIColor whiteColor];
  64. self.imgView.centerX = self.view.width/2;
  65. self.imgView.userInteractionEnabled = YES;
  66. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView:)];
  67. [self.imgView addGestureRecognizer:tap];
  68. [self.imgView addSubview:self.indicatorView];
  69. [self.view addSubview:self.imgView];
  70. UILabel *textLb = [[UILabel alloc] initWithFrame:CGRectMake(0, self.imgView.bottom+7, SCREEN_WIDTH, 20)];
  71. textLb.font = [UIFont systemFontOfSize:13];
  72. textLb.textColor = [UIColor YHColorWithHex:0x6E6E6E];
  73. textLb.text = @"点击上传微信二维码";
  74. textLb.textAlignment = NSTextAlignmentCenter;
  75. self.textLb = textLb;
  76. [self.view addSubview:textLb];
  77. UIView *fieldbg = [[UIView alloc] initWithFrame:CGRectMake(0, self.textLb.bottom+Fitsize(100), SCREEN_WIDTH, 50)];
  78. fieldbg.backgroundColor = [UIColor clearColor];
  79. UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(40, 0, 60, 25)];
  80. title.text = @"微信号";
  81. title.font = [UIFont systemFontOfSize:16];
  82. title.textColor = [UIColor YHColorWithHex:0x333333];
  83. [fieldbg addSubview:title];
  84. [self.view addSubview:fieldbg];
  85. self.textField = [[UITextField alloc] initWithFrame:CGRectMake(title.right,0, SCREEN_WIDTH-140, 33)];
  86. self.textField.centerY = title.centerY;
  87. self.textField.font = [UIFont systemFontOfSize:14];
  88. self.textField.textColor = [UIColor YHColorWithHex:0x6E6E6E];
  89. self.textField.backgroundColor = [UIColor clearColor];
  90. self.textField.keyboardType = UIKeyboardTypeASCIICapable;
  91. self.textField.placeholder = @"请输入微信号,方便下级联系";
  92. self.textField.clearButtonMode = UITextFieldViewModeAlways;
  93. [fieldbg addSubview:self.textField];
  94. UIView *line = [[UIView alloc] initWithFrame:CGRectMake(40, self.textField.bottom+5, SCREEN_WIDTH-80, 1)];
  95. line.backgroundColor = [UIColor YHColorWithHex:0x979797];
  96. [fieldbg addSubview:line];
  97. // self.switchBtn = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-15-60, 0, 60, 30)];
  98. // self.switchBtn.centerY = self.textField.centerY;
  99. // [self.switchBtn setImage:[UIImage imageNamed:@"yincang"] forState:UIControlStateNormal];
  100. // [self.switchBtn setImage:[UIImage imageNamed:@"gongkai"] forState:UIControlStateSelected];
  101. // [self.switchBtn addTarget:self action:@selector(changeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
  102. // [self.view addSubview:self.switchBtn];
  103. //
  104. // UILabel *desLb = [[UILabel alloc] initWithFrame:CGRectMake(10, self.textField.bottom+10, SCREEN_WIDTH-30, 20)];
  105. // desLb.textColor = [UIColor YHColorWithHex:0x999999];
  106. // desLb.font = [UIFont systemFontOfSize:Fitsize(11)];
  107. // desLb.text = @"填写微信号后,您可以选择公开此信息,方便下级联系您";
  108. // _desLabel = desLb;
  109. // [self.view addSubview:desLb];
  110. UIButton *complete = [[UIButton alloc] initWithFrame:CGRectMake(0, fieldbg.bottom+33, SCREEN_WIDTH-80, Fitsize(40))];
  111. complete.backgroundColor = [UIColor homeRedColor];
  112. [complete setTitle:@"完成" forState:UIControlStateNormal];
  113. complete.titleLabel.font = [UIFont systemFontOfSize:Fitsize(15)];
  114. [complete setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  115. [complete addTarget:self action:@selector(completeAction) forControlEvents:UIControlEventTouchUpInside];
  116. complete.layer.cornerRadius = 6;
  117. complete.backgroundColor = [UIColor homeRedColor];
  118. complete.centerX = self.view.centerX;
  119. _complete = complete;
  120. [self.view addSubview:complete];
  121. }
  122. - (void)tapImageView:(UITapGestureRecognizer *)tap {
  123. [self editImageSelected];
  124. }
  125. #pragma mark - --- event response 事件相应 ---
  126. - (void)editImageSelected
  127. {
  128. UIImagePickerController *controller = [UIImagePickerController imagePickerControllerWithSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
  129. [controller setDelegate:self];
  130. controller.modalPresentationStyle = UIModalPresentationFullScreen;
  131. if ([controller isAvailablePhotoLibrary]) {
  132. [self presentViewController:controller animated:YES completion:^{
  133. [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
  134. }];
  135. }else {
  136. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"您还没有开启访问相册权限" preferredStyle:UIAlertControllerStyleAlert];
  137. UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"去设置" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
  138. [alertController dismissViewControllerAnimated:YES completion:nil];
  139. [self gotoSetting];
  140. }];
  141. UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  142. [alertController dismissViewControllerAnimated:YES completion:nil];
  143. }];
  144. [alertController addAction:action1];
  145. [alertController addAction:action2];
  146. alertController.modalPresentationStyle = UIModalPresentationFullScreen;
  147. [self presentViewController:alertController animated:YES completion:nil];
  148. }
  149. }
  150. - (void)gotoSetting {
  151. NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
  152. if([[UIApplication sharedApplication]canOpenURL:url] ) {
  153. if (@available(iOS 10.0, *)) {
  154. [[UIApplication sharedApplication] openURL:url options:@{}completionHandler:^(BOOL success) {
  155. }];
  156. } else {
  157. [[UIApplication sharedApplication]openURL:url];
  158. }
  159. }
  160. }
  161. #pragma mark - 2.UIImagePickerController的委托
  162. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
  163. {
  164. [picker dismissViewControllerAnimated:YES completion:^{
  165. UIImage *imageOriginal = [info objectForKey:UIImagePickerControllerOriginalImage];
  166. STPhotoKitController *photoVC = [STPhotoKitController new];
  167. [photoVC setDelegate:self];
  168. [photoVC setImageOriginal:imageOriginal];
  169. [photoVC setSizeClip:CGSizeMake(Fitsize(300), Fitsize(300))];
  170. photoVC.modalPresentationStyle = UIModalPresentationFullScreen;
  171. [self presentViewController:photoVC animated:YES completion:nil];
  172. }];
  173. }
  174. - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
  175. [picker dismissViewControllerAnimated:YES completion:^(){
  176. }];
  177. }
  178. - (void)photoKitController:(STPhotoKitController *)photoKitController resultImage:(UIImage *)resultImage
  179. {
  180. _selectImg = resultImage;
  181. selectedImg = YES;
  182. self.imgView.image = resultImage;
  183. }
  184. - (void)upLoadImage:(UIImage *)image
  185. {
  186. NSString *url = [NSString stringWithFormat:@"%@/api/v2/adzone/addQrcode",BaseURL];
  187. /* 创建网络请求对象 */
  188. AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
  189. /* 设置请求和接收的数据编码格式 */
  190. mgr.requestSerializer = [AFJSONRequestSerializer serializer]; // 设置请求数据为 JSON 数据
  191. mgr.responseSerializer = [AFJSONResponseSerializer serializer]; // 设置接收数据为 JSON 数据
  192. /* 设置请求头 */
  193. mgr.requestSerializer.timeoutInterval = 10;
  194. NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
  195. NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
  196. [mgr.requestSerializer setValue:app_Version forHTTPHeaderField:@"version"];
  197. [mgr.requestSerializer setValue:Channel_id forHTTPHeaderField:@"source"];
  198. [mgr.requestSerializer setValue:@"ios" forHTTPHeaderField:@"platform"];
  199. if ([AccountTool isLogin]) {
  200. NSString *token = [AccountTool account].token;
  201. // NSLog(@"%@",token);
  202. [mgr.requestSerializer setValue:token forHTTPHeaderField:@"token"];
  203. }
  204. NSString *deviceUUID = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
  205. NSString *uuid = [NSString stringWithFormat:@"I-%@",deviceUUID];
  206. [mgr.requestSerializer setValue:uuid forHTTPHeaderField:@"idfa"];
  207. [mgr POST:url parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
  208. NSData *data = UIImageJPEGRepresentation(image,1);
  209. NSString *name = [self getNowTimeTimestamp];
  210. NSString *fileName = [NSString stringWithFormat:@"%@.png",name];
  211. [formData appendPartWithFileData:data name:@"img" fileName:fileName mimeType:@"image/png"];
  212. } progress:^(NSProgress * _Nonnull uploadProgress) {
  213. if (uploadProgress) {
  214. CGFloat progress = uploadProgress.completedUnitCount/uploadProgress.totalUnitCount;
  215. [SVProgressHUD showProgress:progress status:@"图片上传中..."];
  216. }
  217. } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
  218. _changeImgUrl = responseObject[@"rst"][@"img"];
  219. self.imgView.image = image;
  220. [SVProgressHUD dismiss];
  221. [self requestCompleteWith:_changeImgUrl];
  222. } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
  223. NSData * data = error.userInfo[@"com.alamofire.serialization.response.error.data"];
  224. NSString * str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
  225. NSLog(@"服务器的错误原因:%@",str);
  226. [SVProgressHUD dismiss];
  227. [MBProgressHUD showMessage:@"上传失败"];
  228. }];
  229. }
  230. - (void)request {
  231. [self.indicatorView startAnimating];
  232. NSString *url = [NSString stringWithFormat:@"%@/api/v2/adzoneCreate/displayWeChat",BaseURL];
  233. [LDHttp post:url params:nil success:^(id json) {
  234. NSString *wechat;
  235. NSString *str = json[@"weChat"];
  236. NSString *qrcode = json[@"qrcode"];
  237. if (str.length > 0) {
  238. wechat = [NSString stringWithFormat:@"%@",json[@"weChat"]];
  239. self.textField.text = wechat;
  240. }else {
  241. self.textField.placeholder = @"请输入微信号,方便下级联系";
  242. }
  243. if (qrcode.length > 0) {
  244. [self.imgView sd_setImageWithURL:[NSURL URLWithString:qrcode] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
  245. [self.indicatorView stopAnimating];
  246. }];
  247. selectedImg = YES;
  248. }else {
  249. self.imgView.image = [UIImage imageNamed:@"addImg"];
  250. [self.indicatorView stopAnimating];
  251. }
  252. // [self addRacObserver:wechat];
  253. } failure:^(NSError *error) {
  254. [self.indicatorView stopAnimating];
  255. }];
  256. }
  257. //
  258. //- (void)addRacObserver:(NSString *)wechat {
  259. // RACSignal *textSingal = [self.textField.rac_textSignal map:^id(NSString * value) {
  260. // BOOL first = value.length > 0;
  261. // BOOL second = self.imgView.image != [UIImage imageNamed:@"addImg"];
  262. // BOOL succ = first && second;
  263. // return @(succ);
  264. // }];
  265. //
  266. //
  267. //
  268. // [textSingal subscribeNext:^(NSNumber *textActionSignal) {
  269. // _complete.backgroundColor = textActionSignal.boolValue?[UIColor homeRedColor]:[UIColor lightGrayColor];
  270. // _complete.enabled = textActionSignal.boolValue;
  271. // }];
  272. //}
  273. //- (void)changeButtonClick:(UIButton *)sender {
  274. // [LoadingView show];
  275. // sender.enabled = NO;
  276. // NSString *url = [NSString stringWithFormat:@"%@/api/v2/adzoneCreate/openFlag",BaseURL];
  277. // NSNumber *state = sender.selected?@0:@1;
  278. // NSDictionary *para = @{@"openStat":state};
  279. // [LDHttp post:url params:para success:^(id json) {
  280. // sender.enabled = YES;
  281. // if ([json[@"flag"] boolValue]) {
  282. // sender.selected = !sender.selected;
  283. // NSString *text = sender.selected?@"您的微信号已公开,下级可以看到了":@"您的微信号已关闭,点击按钮公开";
  284. // _desLabel.text = text;
  285. // }else {
  286. // [MBProgressHUD showMessage:json[@"msg"]];
  287. // }
  288. //
  289. // [LoadingView dismiss];
  290. // } failure:^(NSError *error) {
  291. // [LoadingView dismiss];
  292. // }];
  293. //}
  294. - (void)completeAction {
  295. if (self.textField.text.length == 0 && !selectedImg) {
  296. [MBProgressHUD showMessage:@"请输入微信号或上传二维码"];
  297. return;
  298. }
  299. if (selectedImg) {
  300. [self upLoadImage:self.imgView.image];
  301. }else {
  302. [self requestCompleteWith:nil];
  303. }
  304. }
  305. - (void)requestCompleteWith:(NSString *)newQRUrl {
  306. _complete.enabled = NO;
  307. NSString *url = [NSString stringWithFormat:@"%@/api/v2/adzoneCreate/addWeChat",BaseURL];
  308. NSMutableDictionary *dict = [NSMutableDictionary dictionary];
  309. if (newQRUrl) {
  310. [dict setObject:newQRUrl forKey:@"imgUrl"];
  311. }else {
  312. [dict setObject:@"" forKey:@"imgUrl"];
  313. }
  314. if (self.textField.text.length > 0) {
  315. [dict setObject:self.textField.text forKey:@"weChat"];
  316. }else {
  317. [dict setObject:@"" forKey:@"weChat"];
  318. }
  319. [LDHttp post:url params:dict success:^(id json) {
  320. _complete.enabled = YES;
  321. [MBProgressHUD showMessage:json[@"msg"]];
  322. if ([json[@"flag"] boolValue]) {
  323. [self backAction];
  324. }
  325. } failure:^(NSError *error) {
  326. _complete.enabled = YES;
  327. [MBProgressHUD showMessage:@"加载失败"];
  328. }];
  329. }
  330. - (UIActivityIndicatorView *)indicatorView {
  331. if (!_indicatorView) {
  332. _indicatorView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:(UIActivityIndicatorViewStyleGray)];
  333. _indicatorView.frame= CGRectMake(0, 0, 50, 50);
  334. _indicatorView.color = [UIColor homeRedColor];
  335. _indicatorView.center = CGPointMake(self.imgView.width/2, self.imgView.height/2);
  336. _indicatorView.hidesWhenStopped = YES;
  337. }
  338. return _indicatorView;
  339. }
  340. - (NSString *)getNowTimeTimestamp{
  341. NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
  342. NSTimeInterval a=[dat timeIntervalSince1970];
  343. NSString*timeString = [NSString stringWithFormat:@"%0.f", a];//转为字符型
  344. ;
  345. return timeString;
  346. }
  347. - (void)didReceiveMemoryWarning {
  348. [super didReceiveMemoryWarning];
  349. // Dispose of any resources that can be recreated.
  350. }
  351. /*
  352. #pragma mark - Navigation
  353. // In a storyboard-based application, you will often want to do a little preparation before navigation
  354. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  355. // Get the new view controller using [segue destinationViewController].
  356. // Pass the selected object to the new view controller.
  357. }
  358. */
  359. @end