FeedbackController.m 17.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
//
//  FeedbackController.m
//  DreamSleep
//
//  Created by peter on 2022/4/22.
//

#import "FeedbackController.h"
#import "MyFeedListController.h"
#import "FeedbackRequestModel.h"
#import "TZImagePickerController.h"
#import "FeedImageCollectionCell.h"

// 最大图片上传数量
static int MaxUpdateImgCount = 3;
// 相册显示列数
static int AlbumColumnCount = 4;

@interface FeedbackController () <MyFeedListControllerDelegate, UITextViewDelegate, TZImagePickerControllerDelegate, UICollectionViewDataSource, UICollectionViewDelegate, UINavigationControllerDelegate>
@property (strong, nonatomic) UILabel *redLab;
@property (assign, nonatomic) int unreadCount;
@property (strong, nonatomic) UITextView *feedTV;
@property (strong, nonatomic) UILabel *wordLimitLab;
@property (strong, nonatomic) UICollectionView *imgCollectionView;
@property (strong, nonatomic) UIButton *commitBtn;
@end

@implementation FeedbackController {
    NSMutableArray *_selectedPhotos;
    NSMutableArray *_selectedAssets;
    BOOL _isSelectOriginalPhoto;
    NSString *_result_imgurls;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"意见反馈";
    self.view.dk_backgroundColorPicker = DKColorPickerWithKey(VCViewBG);
    
    _selectedPhotos = [NSMutableArray array];
    _selectedAssets = [NSMutableArray array];
    
    [self setNaviRightItem];
    
    [self.view addSubview:self.feedTV];
    [self.view addSubview:self.wordLimitLab];
    [self.view addSubview:self.imgCollectionView];
    [self.view addSubview:self.commitBtn];
    
    [self layoutUI];
    
    [self getUnreadMessageRequest];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.feedTV resignFirstResponder];
}

#pragma mark - MyFeedListControllerDelegate
- (void)didClickMessage {
    if (self.unreadCount == 0) { return; }
    self.unreadCount--;
    self.redLab.text = [NSString stringWithFormat:@"%d", self.unreadCount];
    self.redLab.hidden = self.unreadCount == 0;
}

#pragma mark - UITextViewDelegate
- (void)textViewDidEndEditing:(UITextView *)textView {
    [self dealWorldLimitAttTextWithChangeLength:(int)textView.text.length];
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    int textLength = (int)(textView.text.length - range.length + text.length);
    [self dealWorldLimitAttTextWithChangeLength:textLength];
    
    if (textLength > 400) { return NO; }
    return YES;
}

- (void)dealWorldLimitAttTextWithChangeLength:(int)changeLength {
    NSString *changeStr = [NSString stringWithFormat:@"%d", changeLength];
    NSString *fixStr = @"/400";
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@%@", changeStr, fixStr]];
    UIColor *changeColor = changeLength > 400 ? ColorFromHex(0xF04B77) : BrandColor;
    [attStr addAttribute:NSForegroundColorAttributeName value:changeColor range:NSMakeRange(0, changeStr.length)];
    UIColor *fixColor = [self.dk_manager.themeVersion isEqualToString:DKThemeVersionNormal] ? SmallTextColor : DarkTextColor;
    [attStr addAttribute:NSForegroundColorAttributeName value:fixColor range:NSMakeRange(changeStr.length, fixStr.length)];
    self.wordLimitLab.attributedText = attStr;
}

#pragma mark - 品牌模式
- (NaviStyle)navigationBarStyle {
    return NaviStyleDefault;
}

#pragma mark - 关闭侧滑
- (BOOL)enableInteractivePopGestureRecognizer {
    return NO;
}

#pragma mark - Actions
- (void)myFeedAction {
    MyFeedListController *myFeedVC = [[MyFeedListController alloc] initWithDelegate:self];
    [self.navigationController pushViewController:myFeedVC animated:YES];
}

- (void)commitAction {
    if (self.feedTV.text.length == 0) {
        [DSProgressHUD showDetailInfo:@"请输入遇到的问题或建议"];
        return;
    }
    if (self.feedTV.text.length < 5) {
        [DSProgressHUD showDetailInfo:@"内容不能少于5个字"];
        return;
    }
    
    // 发送提交请求
    [DSProgressHUD showProgressHUDWithInfo:@"提交中..."];
    _result_imgurls = _result_imgurls ? _result_imgurls : @"";
    NSString * feedStr = [NSString transferSpecialCharacters:self.feedTV.text];
    [FeedbackRequestModel adviceFeedbackRequestWithContent:feedStr contentImg:_result_imgurls completion:^(FeedbackRequestModel * _Nonnull requestModel) {
        [DSProgressHUD dissmissProgressHUD];
        if (requestModel.resCode == DSResCodeSuccess) {
            [DSProgressHUD showDetailInfo:@"已收到您的宝贵建议,谢谢!"];
            [self.navigationController popViewControllerAnimated:YES];
            
            [DataStatisticsUtil event:SuggestFeedback attributes:@{@"name":@"提交反馈意见"}];
            
            // 执行意见反馈任务
            [DsTaskManager excuteGeneralTaskWithType:TaskTypeDaily taskItem:TaskItemFeedback];
        }
    }];
}

- (void)deleteBtnClick:(UIButton *)sender {
    [_selectedPhotos removeObjectAtIndex:sender.tag];
    [_selectedAssets removeObjectAtIndex:sender.tag];
    
    [self.imgCollectionView performBatchUpdates:^{
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:sender.tag inSection:0];
        [self.imgCollectionView deleteItemsAtIndexPaths:@[indexPath]];
    } completion:^(BOOL finished) {
        [self.imgCollectionView reloadData];
    }];
}

#pragma mark - UICollectionViewDataSource && UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _selectedPhotos.count + 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    FeedImageCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FeedImageCollectionCell" forIndexPath:indexPath];
    if (indexPath.row == 0) {
        cell.imageView.image = nil;
        cell.addIcon.hidden = NO;
        cell.deleteBtn.hidden = YES;
    } else {
        cell.imageView.image = _selectedPhotos[indexPath.row-1];
        cell.asset = _selectedAssets[indexPath.row-1];
        cell.addIcon.hidden = YES;
        cell.deleteBtn.hidden = NO;
        cell.deleteBtn.tag = indexPath.row - 1;
        [cell.deleteBtn addTarget:self action:@selector(deleteBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) { // 打开相册
        [self pushTZImagePickerController];
    } else { // 预览照片或者视频
        id asset = _selectedAssets[(indexPath.row - 1)];
        BOOL isVideo = NO;
        if ([asset isKindOfClass:[PHAsset class]]) {
            PHAsset *phAsset = asset;
            isVideo = phAsset.mediaType == PHAssetMediaTypeVideo;
        }
        
        TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithSelectedAssets:_selectedAssets selectedPhotos:_selectedPhotos index:(indexPath.row - 1)];
        __weak TZImagePickerController * weakPicker = imagePickerVc;
        imagePickerVc.maxImagesCount = MaxUpdateImgCount;
        imagePickerVc.allowPickingOriginalPhoto = NO;
        imagePickerVc.allowPickingMultipleVideo = NO;
        imagePickerVc.showSelectedIndex = YES;
        imagePickerVc.isSelectOriginalPhoto = _isSelectOriginalPhoto;
        WS(weakSelf);
        [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
            self->_selectedPhotos = [NSMutableArray arrayWithArray:photos];
            self->_selectedAssets = [NSMutableArray arrayWithArray:assets];
            self->_isSelectOriginalPhoto = isSelectOriginalPhoto;
            [weakSelf.imgCollectionView reloadData];
            [weakPicker dismissViewControllerAnimated:YES completion:nil];
        }];
        [self presentViewController:imagePickerVc animated:YES completion:nil];
    }
}

#pragma mark - TZImagePickerController
- (void)pushTZImagePickerController {
    if (MaxUpdateImgCount <= 0) { return; }
    
    TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:MaxUpdateImgCount columnNumber:AlbumColumnCount delegate:self pushPhotoPickerVc:NO];
    imagePickerVc.isSelectOriginalPhoto = _isSelectOriginalPhoto;
    imagePickerVc.naviBgColor = [self.dk_manager.themeVersion isEqualToString:DKThemeVersionNight] ? SubNaviDarkColor : BrandColor;
    // 设置目前已经选中的图片数组
    if (MaxUpdateImgCount > 1) { imagePickerVc.selectedAssets = _selectedAssets; }
    // 在内部显示拍照按钮
    imagePickerVc.allowTakePicture = YES;
    imagePickerVc.iconThemeColor = ColorFromRGBA(31.0, 185.0, 34.0, 1.0);
    imagePickerVc.showPhotoCannotSelectLayer = YES;
    imagePickerVc.cannotSelectLayerColor = [DSWhite colorWithAlphaComponent:0.8];
    // 设置是否可以选择视频/图片/原图
    imagePickerVc.allowPickingVideo = NO;
    imagePickerVc.allowPickingImage = YES;
    imagePickerVc.allowPickingOriginalPhoto = NO;
    imagePickerVc.allowPickingGif = NO;
    // 是否可以多选视频
    imagePickerVc.allowPickingMultipleVideo = NO;
    // 照片排列按修改时间升序
    imagePickerVc.sortAscendingByModificationDate = YES;
    imagePickerVc.showSelectBtn = NO;
    imagePickerVc.allowCrop = NO;
    imagePickerVc.needCircleCrop = NO;
    // 设置竖屏下的裁剪尺寸
    NSInteger left = 30;
    NSInteger widthHeight = self.view.width - 2 * left;
    NSInteger top = (self.view.height - widthHeight) / 2;
    imagePickerVc.cropRect = CGRectMake(left, top, widthHeight, widthHeight);
    // 设置是否显示图片序号
    imagePickerVc.showSelectedIndex = YES;
    imagePickerVc.autoDismiss = NO;
    // 你可以通过block或者代理,来得到用户选择的照片.
    __weak TZImagePickerController *weakPicker = imagePickerVc;
    [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        if (photos.count) { [self uploadImgsWithPhotos:photos picker:weakPicker]; }
    }];
    [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];
    _isSelectOriginalPhoto = isSelectOriginalPhoto;
    [self.imgCollectionView reloadData];
    
    // 1.打印图片名字
    [self printAssetsName:assets];
    // 2.图片位置信息
    for (PHAsset *phAsset in assets) {
        DSLog(@"location:%@", phAsset.location);
    }
}

// 如果用户选择了一个gif图片,下面的handle会被执行
- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingGifImage:(UIImage *)animatedImage sourceAssets:(id)asset {
    _selectedPhotos = [NSMutableArray arrayWithArray:@[animatedImage]];
    _selectedAssets = [NSMutableArray arrayWithArray:@[asset]];
    [self.imgCollectionView reloadData];
}

// 决定相册显示与否
- (BOOL)isAlbumCanSelect:(NSString *)albumName result:(id)result {
    return YES;
}

- (void)tz_imagePickerControllerDidCancel:(TZImagePickerController *)picker {
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - Private
- (void)printAssetsName:(NSArray *)assets {
    /// 打印图片名字
    NSString *fileName;
    for (id asset in assets) {
        if ([asset isKindOfClass:[PHAsset class]]) {
            PHAsset *phAsset = (PHAsset *)asset;
            fileName = [phAsset valueForKey:@"filename"];
        }
    }
}

#pragma mark - 获取我的反馈回复数据
- (void)getUnreadMessageRequest {
    [FeedbackRequestModel queryUserTotalReplysRequest:^(FeedbackRequestModel * _Nonnull requestModel) {
        if (requestModel.resCode == DSResCodeSuccess) {
            self.unreadCount = requestModel.unreadCount;
            self.redLab.text = [NSString stringWithFormat:@"%d", self.unreadCount];
            self.redLab.hidden = self.unreadCount == 0;
        }
    }];
}

#pragma mark - 图片上传
- (void)uploadImgsWithPhotos:(NSArray<UIImage *> *)photos picker:(TZImagePickerController *)picker {
    // 1、处理图片
    NSMutableString *mstr = [NSMutableString string];
    [mstr appendString:@"["];
    for (int index = 0; index < photos.count; index++) {
        UIImage *img = photos[index];
        NSString *base64Str = [UIImage imageToBase64Str:img];
        NSString *sepStr = (index == (photos.count - 1)) ? @"" : @",";
        [mstr appendString:[NSString stringWithFormat:@"\"%@\"%@", base64Str, sepStr]];
    }
    [mstr appendString:@"]"];
    // 2、上传图片
    [DSProgressHUD showProgressHUDWithInfo:@""];
    [FeedbackRequestModel uploadBulkImagesWithImgStr:[mstr copy] completion:^(FeedbackRequestModel * _Nonnull requestModel) {
        [DSProgressHUD dissmissProgressHUD];
        if (requestModel.resCode == DSResCodeSuccess) {
            // 存储图片上传成功后返回的urls
            self->_result_imgurls = requestModel.result_imgurls;
            [picker dismissViewControllerAnimated:YES completion:nil];
        }
    }];
}

#pragma mark - lazy
- (UITextView *)feedTV {
    if (!_feedTV) {
        _feedTV = [[UITextView alloc] init];
        _feedTV.dk_backgroundColorPicker = DKColorPickerWithColors(ColorFromHex(0xf0f0f0), CornerViewDarkColor, DSWhite);
        _feedTV.dk_textColorPicker = DKColorPickerWithColors(SubTitleColor, DarkTextColor, DSWhite);
        _feedTV.font = SysFont(14);
        _feedTV.delegate = self;
        [_feedTV cornerRadius:12];
    }
    return _feedTV;
}

- (UILabel *)wordLimitLab {
    if (!_wordLimitLab) {
        _wordLimitLab = [UILabel labWithFont:SysFont(12)];
        _wordLimitLab.textAlignment = NSTextAlignmentRight;
        [self dealWorldLimitAttTextWithChangeLength:0];
    }
    return _wordLimitLab;
}

- (UICollectionView *)imgCollectionView {
    if (!_imgCollectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake(92, 92);
        layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        _imgCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _imgCollectionView.showsHorizontalScrollIndicator = NO;
        _imgCollectionView.dk_backgroundColorPicker = DKColorPickerWithKey(VCViewBG);
        _imgCollectionView.dataSource = self;
        _imgCollectionView.delegate = self;
        _imgCollectionView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
        [_imgCollectionView registerClass:[FeedImageCollectionCell class] forCellWithReuseIdentifier:@"FeedImageCollectionCell"];
    }
    return _imgCollectionView;
}

- (UIButton *)commitBtn {
    if (!_commitBtn) {
        _commitBtn = [UIButton btnWithTitle:@"提交" titleColor:DSWhite font:BoldFont(16) bgColor:BrandColor];
        [_commitBtn addTarget:self action:@selector(commitAction) forControlEvents:UIControlEventTouchUpInside];
        [_commitBtn cornerRadius:20];
    }
    return _commitBtn;
}

- (void)setNaviRightItem {
    UIView *cusView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 105, 32)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:cusView];
    
    UIButton *myFeedBtn = [UIButton btnWithTitle:@"      我的反馈      " titleColor:DSWhite font:SysFont(12)];
    [myFeedBtn cornerRadius:13];
    myFeedBtn.layer.borderColor = DSWhite.CGColor;
    myFeedBtn.layer.borderWidth = 1;
    [myFeedBtn addTarget:self action:@selector(myFeedAction) forControlEvents:UIControlEventTouchUpInside];
    [cusView addSubview:myFeedBtn];
    
    [myFeedBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(cusView);
        make.top.equalTo(cusView).offset(6);
    }];
    
    UILabel *lab = [UILabel labWithTextColor:DSWhite font:SysFont(12)];
    lab.textAlignment = NSTextAlignmentCenter;
    lab.backgroundColor = DSRed;
    [lab cornerRadius:10];
    lab.hidden = YES;
    self.redLab = lab;
    [cusView addSubview:lab];
    [lab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(cusView);
        make.top.equalTo(cusView);
        make.width.height.equalTo(@20);
    }];
}

- (void)layoutUI {
    [self.feedTV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@15);
        make.right.equalTo(@-15);
        make.top.equalTo(@15);
    }];
    [self.imgCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.feedTV.mas_bottom).offset(26);
        make.left.equalTo(@15);
        make.right.equalTo(@-15);
        make.height.equalTo(@92);
    }];
    [self.commitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.imgCollectionView.mas_bottom).offset(60);
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(self.view).offset(-91);
        make.size.mas_equalTo(CGSizeMake(155, 40));
    }];
    [self.wordLimitLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.feedTV).offset(-8);
        make.bottom.equalTo(self.feedTV).offset(-15);
        make.width.equalTo(@200);
    }];
}

@end