Files
yuyin_ios/SweetParty/主类/Dynamic/Controller/SPTrendPublishVC.m
2025-08-08 11:05:33 +08:00

332 lines
13 KiB
Objective-C
Executable File

//
// SPTrendPublishVC.m
// SweetParty
//
// Created by bj_szd on 2022/6/6.
//
#import "SPTrendPublishVC.h"
#import "SPPickPhotoCell.h"
#import "UITextView+ZWPlaceHolder.h"
#import "MLNetWorkHelper.h"
#define itemW (ScreenWidth-15*2-12*3)/3
@interface SPTrendPublishVC ()<TZImagePickerControllerDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UITextViewDelegate>
@property (nonatomic, strong) UITextView *textView;
@property (nonatomic, strong) UILabel *wordCountLabel;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UILabel *locationLab;
@property (nonatomic, strong) UISwitch *locationSwitch;
@property (nonatomic, strong) UIButton *publishBtn;
@property (nonatomic, strong) NSMutableArray *selectedPhotos;
@property (nonatomic, strong) NSMutableArray *selectedAssets;
@property(nonatomic, copy)NSString *imgsUrl;
@end
@implementation SPTrendPublishVC
- (void)viewDidLoad {
[super viewDidLoad];
self.selectedPhotos = [NSMutableArray arrayWithCapacity:0];
self.selectedAssets = [NSMutableArray arrayWithCapacity:0];
[self createUI];
}
- (void)createUI {
UIImageView *bgImgV = [[UIImageView alloc] initWithImage:ImageNamed(@"home_bg")];
[self.view addSubview:bgImgV];
[bgImgV mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.left.right.equalTo(self.view);
make.height.mas_equalTo(ScreenWidth/375*812);
}];
[self showNaviBarWithTitle:@"发布动态"];
UIScrollView *bgScrollView = [[UIScrollView alloc] init];
[self.view addSubview:bgScrollView];
[bgScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.view).offset(TOP_BAR_HEIGHT);
make.left.right.equalTo(self.view);
make.bottom.equalTo(self.view).offset(-SAFE_AREA_INSERTS_BOTTOM);
}];
UIView *contentView = [[UIView alloc] init];
[bgScrollView addSubview:contentView];
[contentView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(bgScrollView);
make.width.mas_equalTo(ScreenWidth);
}];
UIView *textBgView = [[UIView alloc] init];
textBgView.backgroundColor = kClearColor;
textBgView.layer.cornerRadius = 15;
[contentView addSubview:textBgView];
self.textView = [ControlCreator createTextView:nil rect:CGRectZero text:nil font:YBMediumFont(12) color:HEXCOLORA(0x111111, 1) backguoundColor:kClearColor];
self.textView.delegate = self;
self.textView.zw_placeHolder = @"向大家展示你最近的生活的吧~";
self.textView.zw_placeHolderColor = HEXCOLORA(0x999999, 1);
[textBgView addSubview:self.textView];
self.wordCountLabel = [ControlCreator createLabel:nil rect:CGRectZero text:@"0/200" font:YBMediumFont(12) color:HEXCOLORA(0x333333, 1) backguoundColor:nil align:NSTextAlignmentLeft lines:1];
// self.wordCountLabel.hidden = YES;
[textBgView addSubview:self.wordCountLabel];
UICollectionViewFlowLayout *collectionLayout = [[UICollectionViewFlowLayout alloc] init];
collectionLayout.itemSize = CGSizeMake(itemW, itemW);
collectionLayout.sectionInset = UIEdgeInsetsMake(0, 15, 0, 15);
collectionLayout.minimumInteritemSpacing = 0;
collectionLayout.minimumLineSpacing = 12;
UICollectionView *collectionV = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:collectionLayout];
collectionV.backgroundColor = kClearColor;
collectionV.delegate = self;
collectionV.dataSource = self;
[collectionV registerNib:[UINib nibWithNibName:@"SPPickPhotoCell" bundle:nil] forCellWithReuseIdentifier:@"SPPickPhotoCell"];
[contentView addSubview:collectionV];
self.collectionView = collectionV;
// UIView *locationBgV = [[UIView alloc] init];
// [contentView addSubview:locationBgV];
//
// UILabel *locTitleLab = [ControlCreator createLabel:locationBgV rect:CGRectZero text:@"显示位置" font:YBMediumFont(14) color:kWhiteColor backguoundColor:nil align:NSTextAlignmentLeft lines:1];
// UILabel *locLab = [ControlCreator createLabel:locationBgV rect:CGRectZero text:@"请选择" font:YBMediumFont(13) color:HEXCOLORA(0xFFFFFF, 0.6) backguoundColor:nil align:NSTextAlignmentLeft lines:1];
// self.locationLab = locLab;
// self.locationSwitch = [[UISwitch alloc] init];
// self.locationSwitch.onTintColor = mainDeepColor;
// self.locationSwitch.on = YES;
// [locationBgV addSubview:self.locationSwitch];
// UIImageView *arrowImgV = [ControlCreator createImageView:locationBgV rect:CGRectZero imageName:@"trend_right_arrow" backguoundColor:nil];
// UIView *lineV = [ControlCreator createView:locationBgV rect:CGRectZero backguoundColor:HEXCOLORA(0xFFFFFF, 0.2)];
UIButton *publishBtn = [ControlCreator createButton:nil rect:CGRectZero text:@"立即发布" font:YBBoldFont(15) color:HEXCOLOR(0x333333) backguoundColor:mainDeepColor imageName:nil target:self action:@selector(onPublish)];
publishBtn.layer.masksToBounds = YES;
publishBtn.layer.cornerRadius = 22.5;
[self.view addSubview:publishBtn];
self.publishBtn = publishBtn;
[textBgView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(contentView).offset(0);
make.right.equalTo(contentView).offset(0);
make.top.equalTo(contentView).offset(10);
make.height.mas_equalTo(150);
}];
[self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(12);
make.right.mas_equalTo(-12);
make.top.mas_equalTo(12);
make.bottom.mas_equalTo(-30);
}];
[self.wordCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.mas_equalTo(-10);
make.bottom.mas_equalTo(-8);
}];
[self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(textBgView.mas_bottom).offset(10);
make.left.right.equalTo(contentView);
make.height.mas_equalTo((itemW+12)*2);
make.bottom.equalTo(contentView).offset(-20);
}];
// [locationBgV mas_makeConstraints:^(MASConstraintMaker *make) {
// make.top.equalTo(self.collectionView.mas_bottom).offset(20);
// make.left.right.equalTo(contentView);
// make.height.mas_equalTo(44);
// }];
// [locTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
// make.left.equalTo(locationBgV).offset(15);
// make.centerY.equalTo(locationBgV);
// }];
// [self.locationSwitch mas_makeConstraints:^(MASConstraintMaker *make) {
// make.right.equalTo(locationBgV).offset(-15);
// make.centerY.equalTo(locationBgV);
// }];
// [arrowImgV mas_makeConstraints:^(MASConstraintMaker *make) {
// make.right.equalTo(locationBgV).offset(-15);
// make.centerY.equalTo(locationBgV);
// }];
// [lineV mas_makeConstraints:^(MASConstraintMaker *make) {
// make.left.equalTo(locationBgV).offset(15);
// make.right.equalTo(locationBgV).offset(-15);
// make.bottom.equalTo(locationBgV);
// make.height.mas_equalTo(0.5);
// }];
// [publishBtn mas_makeConstraints:^(MASConstraintMaker *make) {
// make.right.mas_equalTo(-14);
// make.top.mas_equalTo(yb_StatusBar_H+12);
// make.width.mas_equalTo(45);
// make.height.mas_equalTo(20);
// }];
[publishBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(contentView).offset(34);
make.right.equalTo(contentView).offset(-34);
make.top.equalTo(self.collectionView.mas_bottom).offset(50);
make.height.mas_equalTo(48);
make.bottom.equalTo(contentView).offset(-20);
}];
[publishBtn setJianBianWithCGSize:CGSizeMake(ScreenWidth-34*2, 48)];
}
- (void)textViewDidChange:(UITextView *)textView {
NSInteger maxLength = 200;
if (textView.text.length > maxLength) {
[HelpPageDefine showMessage:[NSString stringWithFormat:@"最多输入%ld字", maxLength]];
textView.text = [textView.text substringToIndex:maxLength];
}
self.wordCountLabel.text = [NSString stringWithFormat:@"%ld/200", textView.text.length];
}
#pragma mark UICollectionView 代理方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
if (_selectedAssets.count < 6) {
return _selectedAssets.count + 1;
}else {
return 6;
}
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
SPPickPhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SPPickPhotoCell" forIndexPath:indexPath];
if(indexPath.row < _selectedAssets.count) {
cell.imgV.image = _selectedPhotos[indexPath.row];
cell.deleteBtn.hidden = NO;
}else {
cell.imgV.image = [UIImage imageNamed:@"trend_pick_photo"];
cell.deleteBtn.hidden = YES;
}
cell.deleteBtn.tag = indexPath.row;
[cell.deleteBtn addTarget:self action:@selector(deleteBtnClik:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row >= _selectedAssets.count) {
[self pushTZImagePickerController];
}
}
- (void)deleteBtnClik:(UIButton *)sender {
if (sender.tag < _selectedPhotos.count) {
NSInteger index = sender.tag;
[_selectedPhotos removeObjectAtIndex:index];
[_selectedAssets removeObjectAtIndex:index];
}
[self.collectionView reloadData];
}
#pragma mark - TZImagePickerController
- (void)pushTZImagePickerController {
TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:6 columnNumber:4 delegate:self pushPhotoPickerVc:YES];
imagePickerVc.selectedAssets = _selectedAssets; // 目前已经选中的图片数组
// 4. 照片排列按修改时间升序
imagePickerVc.sortAscendingByModificationDate = YES;
imagePickerVc.statusBarStyle = UIStatusBarStyleLightContent;
imagePickerVc.showSelectedIndex = YES;
imagePickerVc.allowPickingOriginalPhoto = NO;
imagePickerVc.allowPickingImage = YES;
imagePickerVc.allowPickingVideo = NO;
// imagePickerVc.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:imagePickerVc animated:YES completion:nil];
}
#pragma mark - TZImagePickerControllerDelegate
- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray<UIImage *> *)photos sourceAssets:(NSArray *)assets isSelectOriginalPhoto:(BOOL)isSelectOriginalPhoto infos:(NSArray<NSDictionary *> *)infos {
_selectedPhotos = [NSMutableArray arrayWithArray:photos];
_selectedAssets = [NSMutableArray arrayWithArray:assets];
[_collectionView reloadData];
}
#pragma mark ------------- 发布 --------------
-(void)onPublish {
if (self.textView.text.length <= 0) {
[HelpPageDefine showMessage:@"请输入内容"];
return;
}
self.publishBtn.userInteractionEnabled = NO;
[self.view endEditing:YES];
if (_selectedAssets.count > 0) {
[self onUploadImages];
}else {
[self onCallInterface];
}
}
-(void)onUploadImages {
NSString *urlStr = [NSString stringWithFormat:@"%@api/upload/img_upload", VERSION_HTTPS_SERVER];
NSDictionary *params = @{@"login_token":GVUSER.token};
[MLNetWorkHelper uploadImagesWithURL:urlStr parameters:params name:@"file[]" images:_selectedPhotos fileNames:nil imageScale:0.5 imageType:@"png" progress:nil success:^(id responseObject) {
NSInteger code = [responseObject safeIntForKey:@"code"];
if (code == 200){
NSArray *imgArr = [responseObject safeArrayForKey:@"data"];
if (imgArr.count > 0){
self.imgsUrl = [imgArr componentsJoinedByString:@","];
[self onCallInterface];
}
}else{
[HelpPageDefine showMessage:[responseObject safeStringForKey:@"msg"]];
}
self.publishBtn.userInteractionEnabled = YES;
} failure:^(NSError *error) {
self.publishBtn.userInteractionEnabled = YES;
}];
}
-(void)onCallInterface {
NSMutableDictionary *params = [NSMutableDictionary dictionary];
[params setValue:self.textView.text forKey:@"content"];
if (self.imgsUrl.length > 0) {
[params setValue:self.imgsUrl forKey:@"images"];
}
if (self.locationSwitch.isOn) {
NSString *lng = [[NSUserDefaults standardUserDefaults] valueForKey:@"Location_Lng"];
NSString *lat = [[NSUserDefaults standardUserDefaults] valueForKey:@"Location_Lat"];
[params setValue:C_string(lng) forKey:@"lng"];
[params setValue:C_string(lat) forKey:@"lat"];
}
[AFNetworkRequset.shared postRequestWithParams:params Path:@"api/User_Zone/publish_zone" Loading:YES Hud:YES Success:^(id _Nonnull responseDic) {
self.publishBtn.userInteractionEnabled = YES;
[self dismissViewControllerAnimated:YES completion:^{
if (self.onReloadListBlock) {
self.onReloadListBlock();
}
}];
} Failure:^(id _Nonnull errorData) {
self.publishBtn.userInteractionEnabled = YES;
}];
}
@end