YOUPAIMessageTemplateEdit.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. //
  2. // YOUPAIMessageTemplateEdit.m
  3. // MSYOUPAI
  4. //
  5. // Created by admin on 2022/3/11.
  6. // Copyright © 2022 MS. All rights reserved.
  7. //
  8. #import "YOUPAIMessageTemplateEdit.h"
  9. #import "UIViewController+YOUPAIVideo.h"
  10. #import "UIViewController+YOUPAIUPImage.h"
  11. #import "YOUPAIZYEditUserShowAlertWindow.h"
  12. #import "UIViewController+TFPresent.h"
  13. #import "UIViewController+YOUPAIYBImageBrowser.h"
  14. @interface YOUPAIMessageTemplateEdit ()<ZYEditUserShowAlertWindowDelegate>
  15. @property (strong, nonatomic) NSMutableDictionary* youpaipRequestData;
  16. @property (strong, nonatomic) UIBarButtonItem* rightItem;
  17. @end
  18. @implementation YOUPAIMessageTemplateEdit
  19. - (void)viewDidDisappear:(BOOL)animated{
  20. [super viewDidDisappear:animated];
  21. [self.soundRecordView youpaifstopSoundRecord];
  22. }
  23. - (void)viewDidLoad {
  24. [super viewDidLoad];
  25. [self.soundRecordView youpaifstopSoundRecord];
  26. self.youpaipRequestData = [[NSMutableDictionary alloc] init];
  27. self.view.backgroundColor = UIColor.whiteColor;
  28. self.title = @"新建模板";
  29. self.navigationItem.rightBarButtonItem = self.rightItem;
  30. [self youpaifMakeUI];
  31. [self youpaifMakeLayout];
  32. }
  33. -(void)viewWillAppear:(BOOL)animated{
  34. [super viewWillAppear:animated];
  35. [self reloadData];
  36. }
  37. -(UIBarButtonItem *)rightItem{
  38. if (_rightItem == nil){
  39. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  40. [button addTarget:self action:@selector(youpaifcommit) forControlEvents:UIControlEventTouchUpInside];
  41. button.frame = CGRectMake(0, 0, 56, 24);
  42. [button setTitle:@"提交审核" forState:0];
  43. [button setTitleColor:LZ7C69FEColor forState:0];
  44. button.titleLabel.font = LCFont14;
  45. button.layer.cornerRadius = 12;
  46. button.layer.masksToBounds = YES;
  47. [self.view addSubview:button];
  48. _rightItem = [[UIBarButtonItem alloc] initWithCustomView:button];
  49. }
  50. return _rightItem;
  51. }
  52. -(UIScrollView *)youpaipScrollView{
  53. if(_youpaipScrollView == nil){
  54. _youpaipScrollView = [UIScrollView new];
  55. }
  56. return _youpaipScrollView;
  57. }
  58. -(YOUPAILZTextView *)youpaiptextView{
  59. if(_youpaiptextView == nil){
  60. _youpaiptextView = [[YOUPAILZTextView alloc] initWithMaxCount:50];
  61. _youpaiptextView.frame = CGRectMake(19,62, KScreenWidth-38,170);
  62. [_youpaiptextView ms_radius:8];
  63. [_youpaiptextView setBackgroundColor:LZF7F8FAColor];
  64. _youpaiptextView.youpaiptextViewPadding = UIEdgeInsetsMake(12, 12, 12, 12);
  65. _youpaiptextView.youpaiptextLengthDefaultColor = LZD3D1D7Color;
  66. _youpaiptextView.youpaiptextLengthHigtColor = LZ7C69FEColor;
  67. _youpaiptextView.youpaiptextLengthFont = LCFont(14.0f);
  68. _youpaiptextView.youpaiptextLengthRight = 14.0f;
  69. _youpaiptextView.youpaiptextLengthBottom = 14.0f;
  70. _youpaiptextView.youpaiptextView.placeholderFont = LCFont(14.0f);
  71. _youpaiptextView.youpaiptextView.placeholderTextColor = LZD3D1D7Color;
  72. _youpaiptextView.youpaiptextView.placeholderText = @"请输入文字内容文案";
  73. _youpaiptextView.youpaiptextView.font = LCFont(14.0f);
  74. _youpaiptextView.tintColor = LZ7C69FEColor;
  75. _youpaiptextView.youpaiptextView.textColor = UIColor.blackColor;
  76. }
  77. return _youpaiptextView;
  78. }
  79. -(YOUPAIMessageEditHeaderView *)youpaipimageHeader{
  80. if(_youpaipimageHeader == nil){
  81. _youpaipimageHeader = [YOUPAIMessageEditHeaderView new];
  82. _youpaipimageHeader.youpaiptitleLab.text = @"添加图片";
  83. _youpaipimageHeader.youpaipremarkLab.text = @"(仅可添加一张)";
  84. }
  85. return _youpaipimageHeader;
  86. }
  87. - (UIImageView *)youpaipimageView{
  88. if(_youpaipimageView == nil){
  89. _youpaipimageView = [UIImageView new];
  90. _youpaipimageView.image = [UIImage imageNamed:@"icon_180_up_plus"];
  91. _youpaipimageView.contentMode = UIViewContentModeCenter;
  92. _youpaipimageView.backgroundColor = LZF7F8FAColor;
  93. [_youpaipimageView ms_radius:10];
  94. _youpaipimageView.userInteractionEnabled = true;
  95. UITapGestureRecognizer *gest = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(youpaifSelectImage:)];
  96. [_youpaipimageView addGestureRecognizer:gest];
  97. }
  98. return _youpaipimageView;
  99. }
  100. -(YOUPAIMessageEditHeaderView *)youpaipvideoHeader{
  101. if(_youpaipvideoHeader == nil){
  102. _youpaipvideoHeader = [YOUPAIMessageEditHeaderView new];
  103. _youpaipvideoHeader.youpaiptitleLab.text = @"添加视频";
  104. _youpaipvideoHeader.youpaipremarkLab.text = @"(限制15s以内)";
  105. }
  106. return _youpaipvideoHeader;
  107. }
  108. - (UIImageView *)youpaipvideoView{
  109. if(_youpaipvideoView == nil){
  110. _youpaipvideoView = [UIImageView new];
  111. _youpaipvideoView.image = [UIImage imageNamed:@"icon_180_up_plus"];
  112. [_youpaipvideoView ms_radius:10];
  113. _youpaipvideoView.userInteractionEnabled = true;
  114. _youpaipvideoView.contentMode = UIViewContentModeCenter;
  115. _youpaipvideoView.backgroundColor = LZF7F8FAColor;
  116. UITapGestureRecognizer *gest = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(youpaiSelectVideo:)];
  117. [_youpaipvideoView addGestureRecognizer:gest];
  118. }
  119. return _youpaipvideoView;
  120. }
  121. -(UIImageView *)youpaipplayerImageView{
  122. if (_youpaipplayerImageView == nil){
  123. _youpaipplayerImageView = [UIImageView new];
  124. [_youpaipplayerImageView ms_radius:10];
  125. UIImage *img = [UIImage imageNamed:@"vqu_images_D_video_play"];
  126. _youpaipplayerImageView.image = img;
  127. [_youpaipplayerImageView ms_radius:img.size.width/2];
  128. _youpaipplayerImageView.contentMode = UIViewContentModeCenter;
  129. }
  130. return _youpaipplayerImageView;
  131. }
  132. -(YOUPAIMessageEditHeaderView *)youpaipvoiceHeader{
  133. if(_youpaipvoiceHeader == nil){
  134. _youpaipvoiceHeader = [YOUPAIMessageEditHeaderView new];
  135. _youpaipvoiceHeader.youpaiptitleLab.text = @"添加语音";
  136. _youpaipvoiceHeader.youpaipremarkLab.text = @"(限制5~15秒)";
  137. }
  138. return _youpaipvoiceHeader;
  139. }
  140. -(YOUPAILZSoundRecordView *)soundRecordView{
  141. if(_soundRecordView == nil){
  142. _soundRecordView = [[YOUPAILZSoundRecordView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, KScreenWidth, 195.0f)];
  143. _soundRecordView.youpaipminTimer = 5;
  144. _soundRecordView.youpaipmaxTimer = 15;
  145. _soundRecordView.youpaiptype = LZAudioTypeWithAAC;
  146. }
  147. return _soundRecordView;
  148. }
  149. -(void)youpaifMakeUI{
  150. UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(keyboardHide:)];
  151. tapGestureRecognizer.cancelsTouchesInView = NO;
  152. [self.view addGestureRecognizer:tapGestureRecognizer];
  153. [self.view addSubview:self.youpaipScrollView];
  154. [self.youpaipScrollView addSubview:self.youpaiptextView];
  155. [self.youpaipScrollView addSubview:self.youpaipvideoView];
  156. [self.youpaipScrollView addSubview:self.youpaipvoiceHeader];
  157. [self.youpaipScrollView addSubview:self.youpaipimageHeader];
  158. [self.youpaipScrollView addSubview:self.youpaipimageView];
  159. [self.youpaipScrollView addSubview:self.youpaipvideoHeader];
  160. [self.youpaipScrollView addSubview:self.youpaipvideoView];
  161. [self.youpaipScrollView addSubview:self.youpaipvoiceHeader];
  162. [self.youpaipScrollView addSubview:self.soundRecordView];
  163. [self.youpaipvideoView addSubview:self.youpaipplayerImageView];
  164. }
  165. -(void)keyboardHide:(UITapGestureRecognizer*)tap{
  166. dispatch_async(dispatch_get_main_queue(), ^{
  167. [self.view endEditing:YES];
  168. });
  169. }
  170. -(void)reloadData{
  171. NSString *videostr = self.youpaipRequestData[@"video_file"];
  172. if ([videostr length]>0){
  173. NSURL *videourl = [LCTools getImageUrlWithAddress:videostr];
  174. _youpaipvideoView.image = [UIImage imageWithVideo:videourl];
  175. self.youpaipvideoView.contentMode = UIViewContentModeScaleAspectFill;
  176. [self.youpaipplayerImageView setHidden:false];
  177. }else{
  178. _youpaipvideoView.image = [UIImage imageNamed:@"icon_180_up_plus"];
  179. _youpaipvideoView.contentMode = UIViewContentModeCenter;
  180. [self.youpaipplayerImageView setHidden:true];
  181. }
  182. NSString *imagestr = self.youpaipRequestData[@"file"];
  183. if ([imagestr length]>0){
  184. NSURL *imagesurl = [[LCTools getImageUrlWithAddress:imagestr] urlWithImageScale:60];
  185. [_youpaipimageView sd_setImageWithURL:imagesurl placeholderImage:[UIImage imageNamed:@"icon_180_up_plus"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
  186. if(error){
  187. self.youpaipimageView.contentMode = UIViewContentModeCenter;
  188. }else{
  189. self.youpaipimageView.contentMode = UIViewContentModeScaleAspectFill;
  190. }
  191. }];
  192. }else{
  193. _youpaipimageView.image = [UIImage imageNamed:@"icon_180_up_plus"];
  194. _youpaipimageView.contentMode = UIViewContentModeCenter;
  195. }
  196. }
  197. -(void)youpaifMakeLayout{
  198. [_youpaiptextView mas_makeConstraints:^(MASConstraintMaker *make) {
  199. make.top.mas_equalTo(25);
  200. make.left.mas_equalTo(12);
  201. make.right.mas_equalTo(-12);
  202. make.height.mas_equalTo(148);
  203. make.width.mas_equalTo(KScreenWidth-24);
  204. }];
  205. [_youpaipimageHeader mas_makeConstraints:^(MASConstraintMaker *make) {
  206. make.top.mas_equalTo(self.youpaiptextView.mas_bottom);
  207. make.left.mas_equalTo(12);
  208. make.right.mas_equalTo(-12);
  209. make.height.mas_equalTo(54);
  210. }];
  211. [_youpaipimageView mas_makeConstraints:^(MASConstraintMaker *make) {
  212. make.top.mas_equalTo(self.youpaipimageHeader.mas_bottom);
  213. make.left.mas_equalTo(12);
  214. make.size.mas_equalTo(111);
  215. }];
  216. [_youpaipvideoHeader mas_makeConstraints:^(MASConstraintMaker *make) {
  217. make.top.mas_equalTo(self.youpaipimageView.mas_bottom);
  218. make.left.mas_equalTo(12);
  219. make.right.mas_equalTo(-12);
  220. make.height.mas_equalTo(54);
  221. }];
  222. [_youpaipvideoView mas_makeConstraints:^(MASConstraintMaker *make) {
  223. make.top.mas_equalTo(self.youpaipvideoHeader.mas_bottom);
  224. make.left.mas_equalTo(12);
  225. make.size.mas_equalTo(111);
  226. }];
  227. [_youpaipvoiceHeader mas_makeConstraints:^(MASConstraintMaker *make) {
  228. make.top.mas_equalTo(self.youpaipvideoView.mas_bottom);
  229. make.left.mas_equalTo(12);
  230. make.right.mas_equalTo(-12);
  231. make.height.mas_equalTo(54);
  232. }];
  233. [_youpaipScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
  234. make.left.right.bottom.mas_equalTo(0);
  235. make.width.mas_equalTo(KScreenWidth);
  236. make.height.mas_equalTo(self.view);
  237. make.top.mas_equalTo(self.view).offset(NavBarHeight);
  238. }];
  239. [_soundRecordView mas_makeConstraints:^(MASConstraintMaker *make) {
  240. make.centerX.mas_equalTo(self.view);
  241. make.height.mas_equalTo(195.0f);
  242. make.width.mas_equalTo(self.view);
  243. make.top.mas_equalTo(self.youpaipvoiceHeader.mas_bottom).offset(10);
  244. // make.bottom.mas_equalTo(0).offset(-(90+SafeHeight));
  245. }];
  246. [_youpaipplayerImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  247. make.edges.mas_equalTo(0);
  248. }];
  249. [self.view layoutIfNeeded];
  250. self.youpaipScrollView.contentSize = CGSizeMake(0.0f, CGRectGetMaxY(_soundRecordView.frame) + SafeHeight + NavBarHeight + 30.0f);
  251. }
  252. // MARK: 选择图片
  253. -(void)youpaifSelectImage:(UITapGestureRecognizer*)gestureRecognizer{
  254. NSString * file = _youpaipRequestData[@"file"];
  255. if (file.length>0){
  256. YOUPAIZYEditUserShowAlertWindow *vc= [[YOUPAIZYEditUserShowAlertWindow alloc]init];
  257. vc.youpaipbtntitleArr = @[@"查看",@"更换",@"取消"];
  258. vc.youpaipdelegate = self;
  259. vc.isTouchDismiss = YES;
  260. vc.info = @{@"tag":@(1002)};
  261. [self TFPresentVC:vc completion:^{}];
  262. }else{
  263. [self youpaifgetImage];
  264. }
  265. }
  266. -(void)youpaifgetImage{
  267. @weakify(self);
  268. [self youpaifUploadImgType:@"greet/img" configTZ:nil uploadBlock:nil fineshBlock:^(NSArray * _Nullable imgurls, NSError * _Nullable error) {
  269. @strongify(self);
  270. if (error){
  271. [ZCHUDHelper showTitle:@"上传失败"];
  272. }else{
  273. self.youpaipRequestData[@"file"] = imgurls.firstObject;
  274. [self reloadData];
  275. }
  276. }];
  277. }
  278. // MARK: 选择视频
  279. - (void)youpaiSelectVideo:(UITapGestureRecognizer *)gestureRecognizer
  280. {
  281. NSString * video_file = _youpaipRequestData[@"video_file"];
  282. if (video_file.length>0){
  283. YOUPAIZYEditUserShowAlertWindow *vc= [[YOUPAIZYEditUserShowAlertWindow alloc]init];
  284. vc.youpaipbtntitleArr = @[@"查看",@"更换",@"取消"];
  285. vc.youpaipdelegate = self;
  286. vc.isTouchDismiss = YES;
  287. vc.info = @{@"tag":@(1001)};
  288. [self TFPresentVC:vc completion:^{}];
  289. }else{
  290. [self youpaifgetVideo];
  291. }
  292. }
  293. -(void)youpaifgetVideo{
  294. @weakify(self);
  295. [self youpaifTZSelectVideo:nil didSelect:^(UIImage * _Nullable coverImage, AVURLAsset * _Nullable asset, NSError * _Nullable error) {
  296. @strongify(self);
  297. if(error){
  298. [ZCHUDHelper showTitle:error.localizedDescription];
  299. return;
  300. }
  301. // 视频时长
  302. CMTime time = [asset duration];
  303. int seconds = ceil(time.value/time.timescale);
  304. seconds = MIN(15, seconds);
  305. [ZCHUDHelper showWithStatus:@"正在裁剪..."];
  306. [self youpaifCutVideo:asset start:0 end:seconds completion:^(NSURL * outputURL, Float64 videoDuration, NSError * error) {
  307. @strongify(self);
  308. if(error){
  309. [ZCHUDHelper showTitle:error.localizedDescription];
  310. return;
  311. }
  312. [ZCHUDHelper showTitle:@"裁剪完成"];
  313. [self updateVideo:outputURL coverImage:coverImage];
  314. }];
  315. } ];
  316. }
  317. -(void)updateVideo:(NSURL*)path coverImage:(UIImage*) coverImage{
  318. NSString *urlstr = path.resourceSpecifier;
  319. [ZCHUDHelper showWithStatus:@"正在上传..."];
  320. @weakify(self);
  321. [LCCommonHttp uploadWithVideoPath:urlstr Type:@"greet/video" successBlock:^(NSString *ossFilePath) {
  322. @strongify(self);
  323. self.youpaipRequestData[@"video_file"] = ossFilePath;
  324. [ZCHUDHelper showTitle:@"上传完成"];
  325. [self reloadData];
  326. }];
  327. }
  328. // mark: 提交
  329. -(void)youpaifcommit{
  330. dispatch_async(dispatch_get_main_queue(), ^{
  331. [self.view endEditing:YES];
  332. });
  333. NSString *title = _youpaiptextView.youpaiptextView.text;
  334. _youpaipRequestData[@"is_multi"] = @(1);
  335. _youpaipRequestData[@"title"] = title;
  336. //youpaipaudioTime
  337. NSURL *voiceurl = [self.soundRecordView getSubmitSound];
  338. NSInteger voicelength = self.soundRecordView.youpaipaudioTime;
  339. @weakify(self);
  340. [ZCHUDHelper showWithStatus:@"上传中..."];
  341. dispatch_group_t group = dispatch_group_create();
  342. __block NSInteger code = 0;
  343. if (voiceurl){
  344. dispatch_group_enter(group);
  345. dispatch_group_async(group, dispatch_get_main_queue(), ^{
  346. [LCCommonHttp uploadWithAudioPath:voiceurl.resourceSpecifier Type:@"greet/mp3" successBlock:^(NSString *ossFilePath) {
  347. @strongify(self);
  348. if (ossFilePath.length != 0) {
  349. self.youpaipRequestData[@"voice_file"] = ossFilePath;
  350. self.youpaipRequestData[@"length"] = @(voicelength);
  351. }else{
  352. code = -1;
  353. }
  354. dispatch_group_leave(group);
  355. }];
  356. });
  357. }
  358. dispatch_group_notify(group, dispatch_get_main_queue(), ^{
  359. if (code!=0) {
  360. [ZCHUDHelper showTitle:@"语音上传失败"];
  361. return;
  362. }
  363. [LCHttpHelper requestWithURLString:GreetAdd parameters:self.youpaipRequestData needToken:YES type:HttpRequestTypePost success:^(id responseObject) {
  364. @strongify(self);
  365. NSString *code = responseObject[@"code"];
  366. NSString *msg = responseObject[@"msg"];
  367. if (code.integerValue == 0){
  368. [ZCHUDHelper dismiss];
  369. [self.navigationController popViewControllerAnimated:true];
  370. }else{
  371. [ZCHUDHelper showTitle:msg];
  372. }
  373. } failure:^(NSError *error) {
  374. [ZCHUDHelper showTitle:error.localizedDescription];
  375. }];
  376. });
  377. }
  378. // MARK: ZYEditUserShowAlertWindowDelegate
  379. -(void)youpaifimSessionMoreWindowAction:(YOUPAIZYEditUserShowAlertWindow *)alert
  380. type:(NSString *)type
  381. tag:(NSInteger)tag
  382. andUserId:(NSString *)user_id{
  383. NSDictionary *info = alert.info;
  384. NSInteger tagvalue = [info[@"tag"] intValue];
  385. // 视频
  386. if (tagvalue == 1001){
  387. if (tag==1){
  388. NSString * video_file = _youpaipRequestData[@"video_file"];
  389. NSURL *url = [LCTools getImageUrlWithAddress:video_file];
  390. [self youpaifAVPlayer:url];
  391. }else if (tag==2){
  392. [self youpaifgetVideo];
  393. }
  394. }
  395. // 照片
  396. else{
  397. if (tag==1){
  398. NSString * file = _youpaipRequestData[@"file"];
  399. // NSURL *url = [LCTools getImageUrlWithAddress:file];
  400. [self youpaiScanImages:@[file] currentPage:0 view:nil];
  401. }else if (tag==2){
  402. [self youpaifgetImage];
  403. }
  404. }
  405. }
  406. @end