Files
featherVoice/QXLive/Mine(音域)/Controller/个人主页/QXCreatPhotosViewController.m
2025-10-24 10:52:40 +08:00

304 lines
13 KiB
Objective-C

//
// QXCreatPhotosViewController.m
// QXLive
//
// Created by 启星 on 2025/5/29.
//
#import "QXCreatPhotosViewController.h"
#import "QXUserInfoEditFooterView.h"
#import "QXMineNetwork.h"
#import "QXPhotosDetailVC.h"
#import <TZImagePickerController/TZImageManager.h>
#import <TZImagePickerController/TZImagePickerController.h>
#import "QXOSSManager.h"
#import "UIImage+QX.h"
@interface QXCreatPhotosViewController ()<QXUserInfoImageCellDelegate,UITextFieldDelegate>
@property (nonatomic,strong)UIView *topBgView;
@property (nonatomic,strong)UITextField *textField;
@property (nonatomic,strong)UIButton *selectedBtn;
@property (nonatomic,strong)UILabel *messageLabel;
@property (nonatomic,strong)UILabel *priviteLabel;
@property (nonatomic,strong)UIButton *priviteBtn;
@property (nonatomic,strong)UIButton *commitBtn;
@property (nonatomic,strong)UIButton *deleteBtn;
@property (nonatomic,strong)NSString *avartar;
@property (nonatomic,strong)UIView *bottomBgView;
@property (nonatomic,strong)UITextField *passwordTextField;
@end
@implementation QXCreatPhotosViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
-(void)setNavgationItems{
[super setNavgationItems];
self.navigationItem.title = QXText(@"新建相册");
}
- (void)initSubViews{
self.selectedBtn = [[UIButton alloc] initWithFrame:CGRectMake((SCREEN_WIDTH-150)/2, NavContentHeight+12, 150, 150)];
[self.selectedBtn setBackgroundImage:[UIImage imageNamed:@"mine_room_cover_add"] forState:(UIControlStateNormal)];
self.selectedBtn.imageView.contentMode = UIViewContentModeScaleAspectFill;
[self.selectedBtn addTarget:self action:@selector(selectAlbumPhoto) forControlEvents:(UIControlEventTouchUpInside)];
[self.selectedBtn addRoundedCornersWithRadius:6];
[self.view addSubview:self.selectedBtn];
self.messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, self.selectedBtn.bottom+8, SCREEN_WIDTH-16-16, 20)];
self.messageLabel.textAlignment = NSTextAlignmentCenter;
self.messageLabel.textColor = QXConfig.textColor;
self.messageLabel.font = [UIFont systemFontOfSize:12];
self.messageLabel.text = QXText(@"相册封面");
[self.view addSubview:self.messageLabel];
self.topBgView = [[UIView alloc] initWithFrame:CGRectMake(16, self.messageLabel.bottom+24, SCREEN_WIDTH-32, 44)];
[self.topBgView addRoundedCornersWithRadius:11];
self.topBgView.backgroundColor = RGB16(0xEFF2F8);
[self.view addSubview:self.topBgView];
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(12, 0, self.topBgView.width-34, 44)];
self.textField.placeholder = QXText(@"请输入相册名称");
self.textField.font = [UIFont systemFontOfSize:16];
self.textField.delegate = self;
[self.topBgView addSubview:self.textField];
self.priviteBtn = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-16-40, self.topBgView.bottom+2, 40, 35)];
[self.priviteBtn setImage:[UIImage imageNamed:@"home_switch_off"] forState:(UIControlStateNormal)];
[self.priviteBtn setImage:[UIImage imageNamed:@"home_switch_on"] forState:(UIControlStateSelected)];
[self.priviteBtn addTarget:self action:@selector(priviteAction) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:self.priviteBtn];
self.priviteLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, self.priviteBtn.top, SCREEN_WIDTH-16-40-5-16, 35)];
self.priviteLabel.textAlignment = NSTextAlignmentRight;
self.priviteLabel.textColor = QXConfig.textColor;
self.priviteLabel.font = [UIFont systemFontOfSize:13];
self.priviteLabel.text = QXText(@"是否设置密码");
[self.view addSubview:self.priviteLabel];
self.bottomBgView = [[UIView alloc] initWithFrame:CGRectMake(16, self.priviteBtn.bottom+2, SCREEN_WIDTH-32, 44)];
[self.bottomBgView addRoundedCornersWithRadius:11];
self.bottomBgView.backgroundColor = RGB16(0xEFF2F8);
[self.view addSubview:self.bottomBgView];
self.passwordTextField = [[UITextField alloc] initWithFrame:CGRectMake(12, 0, self.bottomBgView.width-34, 44)];
self.passwordTextField.placeholder = QXText(@"请输入相册密码");
self.passwordTextField.font = [UIFont systemFontOfSize:16];
self.passwordTextField.secureTextEntry = YES;
self.passwordTextField.delegate = self;
[self.bottomBgView addSubview:self.passwordTextField];
self.bottomBgView.hidden = YES;
self.commitBtn = [[UIButton alloc] initWithFrame:CGRectMake(38, self.view.height-kSafeAreaBottom-10-42, SCREEN_WIDTH-38*2, 42)];
[self.commitBtn setTitle:QXText(@"确认") forState:(UIControlStateNormal)];
[self.commitBtn addRoundedCornersWithRadius:21];
[self.commitBtn setTitleColor:QXConfig.btnTextColor forState:(UIControlStateNormal)];
self.commitBtn.backgroundColor = QXConfig.themeColor;
self.commitBtn.needEventInterval = 0.5;
self.commitBtn.titleLabel.font = [UIFont systemFontOfSize:14];
[self.commitBtn addTarget:self action:@selector(commitAction) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:self.commitBtn];
if (self.model != nil) {
self.avartar = self.model.image;
[self.selectedBtn sd_setImageWithURL:[NSURL URLWithString:self.model.image] forState:(UIControlStateNormal)];
self.textField.text = self.model.name;
if (self.model.pwd.length > 0) {
self.priviteBtn.selected = YES;
self.bottomBgView.hidden = NO;
self.passwordTextField.text = self.model.pwd;
}else{
self.priviteBtn.selected = NO;
self.bottomBgView.hidden = YES;
self.passwordTextField.text = @"";
}
self.deleteBtn = [[UIButton alloc] initWithFrame:CGRectMake(38, self.view.height-kSafeAreaBottom-10-42-12-42, SCREEN_WIDTH-38*2, 42)];
[self.deleteBtn setTitle:QXText(@"删除") forState:(UIControlStateNormal)];
[self.deleteBtn addRoundedCornersWithRadius:21];
[self.deleteBtn setTitleColor:UIColor.whiteColor forState:(UIControlStateNormal)];
self.deleteBtn.backgroundColor = QXConfig.btnTextColor;
self.deleteBtn.needEventInterval = 0.5;
self.deleteBtn.titleLabel.font = [UIFont systemFontOfSize:14];
[self.deleteBtn addTarget:self action:@selector(deleteAction) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:self.deleteBtn];
}
}
-(void)setModel:(QXPhotoDetailModel *)model{
_model = model;
}
-(void)deleteAction{
MJWeakSelf
[QXMineNetwork delePhotosWithAlbum_id:self.model.id successBlock:^(NSDictionary * _Nonnull dict) {
for (UIViewController*vc in weakSelf.navigationController.viewControllers) {
if ([vc isKindOfClass:[NSClassFromString(@"QXPhotosDetailVC") class]]) {
[vc removeFromParentViewController];
}
}
[[NSNotificationCenter defaultCenter] postNotificationName:noticePhotosStatusChange object:nil];
[weakSelf.navigationController popViewControllerAnimated:YES];
} failBlock:^(NSError * _Nonnull error, NSString * _Nonnull msg) {
}];
}
-(void)priviteAction{
self.priviteBtn.selected = !self.priviteBtn.selected;
if (self.priviteBtn.selected) {
self.bottomBgView.hidden = NO;
}else{
self.bottomBgView.hidden = YES;
self.passwordTextField.text = @"";
}
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if (textField.text.length == 8) {
if ([string isEqualToString:@""]) {
return YES;
}else{
return NO;
}
}else{
return YES;
}
}
-(void)selectAlbumPhoto {
TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:1 delegate:nil];
imagePickerVc.maxImagesCount = 1;
imagePickerVc.allowCameraLocation = NO;
imagePickerVc.allowPickingOriginalPhoto = NO;
imagePickerVc.allowTakeVideo = NO;
imagePickerVc.allowPickingVideo = NO;
imagePickerVc.showSelectBtn = NO;
imagePickerVc.allowCrop = NO;
MJWeakSelf
[imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
if (photos.count) {
NSData *imageData = [photos.firstObject qx_compressImageQualityWithToByte:(SCREEN_WIDTH*SCREEN_HEIGHT*2)];
[weakSelf OSSUploadPhotoWithFileData:imageData contentType:[NSString contentTypeWithImageData:imageData]];
}
}];
imagePickerVc.modalPresentationStyle = UIModalPresentationFullScreen;
[imagePickerVc.navigationBar setBackgroundImage:[UIImage qx_imageWithColor:RGB16(0x333333)] forBarMetrics:UIBarMetricsDefault];
[self presentViewController:imagePickerVc animated:YES completion:nil];
}
- (void)OSSUploadPhotoWithFileData:(NSData *)fileData contentType:(NSString *)contentType {
[self OSSUploadPhotoWithFileData:fileData contentType:contentType isVideoCover:NO];
}
- (void)OSSUploadPhotoWithFileData:(NSData *)fileData contentType:(NSString *)contentType isVideoCover:(BOOL)isVideoCover {
if (!fileData || !contentType) {
showToastInView(QXText(@"资源加载错误"), self.view);
return;
}
if (fileData.length > 1024*1024 && [contentType isEqualToString:IMG_FILE_BASE_PATH]) {
showToastInView(QXText(@"请不要上传超过1M的头像"), self.view);
return;
}
NSMutableArray *files = [[NSMutableArray alloc] initWithObjects:fileData, nil];
// NSString *fileBasePath = IMG_FILE_BASE_PATH;// 默认图片上传
// NSString *fileName = [NSString stringWithFormat:@"%@%@/%@.%@",fileBasePath,[[QXOSSManager sharedInstance] currentDate],[NSUUID UUID].UUIDString,contentType];
// NSMutableArray *fileNames = [[NSMutableArray alloc] initWithObjects:fileName, nil];
// __weak typeof(self)weakSelf = self;
// showLoadingInView(self.view);
// [[QXOSSManager sharedInstance] uploadFile:files withObjectKey:fileNames isAsync:YES complete:^(NSArray<NSString *> *names, UploadImageState state) {
// dispatch_async(dispatch_get_main_queue(), ^{
// hideLoadingInView(weakSelf.view);
// });
// if (state == UploadImageSuccess) {
// NSString *fileName = [names lastObject];
// NSString *fileUrl =[NSString stringWithFormat:@"https://%@.%@/%@",OSS_BUCKET_NAME,OSSEndPoint,fileName];
// weakSelf.avartar = fileUrl;
// [weakSelf.selectedBtn sd_setImageWithURL:[NSURL URLWithString:fileUrl] forState:(UIControlStateNormal)];
// }else{
// showToastInView(QXText(@"文件上传失败,请重新尝试"), weakSelf.view);
// }
// }];
__weak typeof(self)weakSelf = self;
showLoadingInView(self.view);
NSString *fileName = [NSString stringWithFormat:@"%@.%@",[[QXCOSUploadManager shareManager] currentDate],contentType];
[[QXCOSUploadManager shareManager] uploadFile:files withObjectKey:@[fileName] isAsync:YES complete:^(NSArray<NSString *> * _Nonnull names, QXCOSUploadImageState state) {
dispatch_async(dispatch_get_main_queue(), ^{
hideLoadingInView(weakSelf.view);
});
if (state == QXCOSUploadImageSuccess) {
weakSelf.avartar = names.firstObject;
[weakSelf.selectedBtn sd_setImageWithURL:[NSURL URLWithString:weakSelf.avartar] forState:(UIControlStateNormal)];
}else{
showToastInView(QXText(@"文件上传失败,请重新尝试"), weakSelf.view);
}
}];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self.view endEditing:YES];
}
-(void)commitAction{
if (self.textField.text.length == 0) {
showToast(@"请输入相册名称");
return;
}
if (self.avartar.length == 0) {
showToast(@"您还未上传封面");
return;
}
if (self.priviteBtn.selected) {
if (self.passwordTextField.text.length < 4) {
showToast(@"密码至少需要4位");
return;
}
}
MJWeakSelf
if (self.model != nil) {
[QXMineNetwork photosEditWithNewAlbumId:self.model.id name:self.textField.text pwd:self.passwordTextField.text image:self.avartar successBlock:^(NSDictionary * _Nonnull dict) {
if (weakSelf.createSuccessBlock) {
weakSelf.createSuccessBlock();
}
[[NSNotificationCenter defaultCenter] postNotificationName:noticePhotosStatusChange object:nil];
[weakSelf.navigationController popViewControllerAnimated:YES];
} failBlock:^(NSError * _Nonnull error, NSString * _Nonnull msg) {
}];
}else{
[QXMineNetwork createPhotosWithName:self.textField.text
pwd:self.passwordTextField.text
image:self.avartar
successBlock:^(NSDictionary * _Nonnull dict) {
if (weakSelf.createSuccessBlock) {
weakSelf.createSuccessBlock();
}
[weakSelf.navigationController popViewControllerAnimated:YES];
// QXPhotosDetailVC *vc = [[QXPhotosDetailVC alloc] init];
}
failBlock:^(NSError * _Nonnull error, NSString * _Nonnull msg) {
}];
}
}
@end