CNLiveUserAgreementController.m
10.7 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
//
// CNLiveUserAgreementController.h
// CNLiveUserAgreementManager
//
// Created by 153993236@qq.com on 04/13/2019.
// Copyright (c) 2019 153993236@qq.com. All rights reserved.
//
#import "CNLiveUserAgreementController.h"
#import "QMUIKit.h"
#import "WKWebViewJavascriptBridge.h"
#import <AVFoundation/AVCaptureDevice.h>
#import <AVFoundation/AVMediaFormat.h>
#import "XFCameraController.h"
#import "TZImagePickerController.h"
#import <CNLiveRequestBastKit/CNLiveNetworking.h>
#import "CNLiveManager.h"
#import "CNLiveEnvironment.h"
#import "CNLiveWebView.h"
@interface CNLiveUserAgreementController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate,TZImagePickerControllerDelegate>
@property (nonatomic, strong) NSMutableArray *images;
@end
@implementation CNLiveUserAgreementController
#define wjjHelpAndFeedbackImage @"wjjHelpAndFeedbackImage" //帮助与反馈->产品建议:添加照片按钮
#define wjjSubmitImage @"wjjSubmitImage" //帮助与反馈->产品建议:点击提交
#define wjjDelegateImage @"wjjDelegateImage" //帮助与反馈->产品建议:删除照片
#define wjjSubmitSuccessFinal @"wjjSubmitSuccessFinal" //反馈提交(成功/失败)后隐藏loading
#define wjjFeedbackSuccess @"wjjFeedbackSuccess" //帮助与反馈 提交反馈后点击完成
#define wjjJSGetNetStatusFromClient @"JSGetNetStatusFromClient" //h5主动从客户端获取网络状态
#define OCSendNetStatusToJS @"OCSendNetStatusToJS" //网络状态变化信息
#define wjjSubmitSuccess @"wjjSubmitSuccess" //帮助与反馈->产品建议:App->h5图片urls
- (void)createSubViews {
[super createSubViews];
UIColor *color = [UIColor colorWithRed:72/255.0 green:168/255.0 blue:249/255.0 alpha:1.0];
self.webView.progressViewColor = color;
}
#pragma mark - 交互
- (void)interaction {
[super interaction];
__weak typeof(self)weakSelf = self;
// 帮助与反馈->产品建议:添加照片按钮
[self.bridge registerHandler:wjjHelpAndFeedbackImage handler:^(id data, WVJBResponseCallback responseCallback) {
NSLog(@"ObjC Echo called with: %@", data);
[weakSelf uploadPictures];
responseCallback(data);
}];
// 帮助与反馈->产品建议:删除照片
[self.bridge registerHandler:wjjDelegateImage handler:^(id data, WVJBResponseCallback responseCallback) {
NSLog(@"ObjC Echo called with: %@", data);
if ([data isKindOfClass:[NSDictionary class]]) {
NSString *index = [data objectForKey:@"index"];
[weakSelf.images removeObjectAtIndex:[index integerValue]];
}
responseCallback(data);
}];
//帮助与反馈->产品建议:点击提交
[self.bridge registerHandler:wjjSubmitImage handler:^(id data, WVJBResponseCallback responseCallback) {
NSLog(@"ObjC Echo called with: %@", data);
NSString *text = @"";
NSString *mobile = @"";
NSString *modeltype = @"";
if ([data isKindOfClass:[NSDictionary class]]) {
text = [data objectForKey:@"text"];
mobile = [data objectForKey:@"mobile"];
modeltype = [data objectForKey:@"modeltype"];
}
if (weakSelf.images.count > 0) {
[weakSelf getToken];
} else {
NSArray *array = [NSArray array];
id data = @{ @"images": array };
[QMUITips showLoading:@"正在提交" inView:weakSelf.view];
// 帮助与反馈->产品建议App->h5图片urls
[weakSelf.bridge callHandler:wjjSubmitSuccess data:data responseCallback:^(id responseData) {
NSLog(@"ObjC received response: %@", responseData);
}];
}
responseCallback(data);
}];
// 反馈提交(成功/失败)后隐藏loading
[self.bridge registerHandler:wjjSubmitSuccessFinal handler:^(id data, WVJBResponseCallback responseCallback) {
NSLog(@"ObjC Echo called with: %@", data);
[QMUITips hideAllTipsInView:weakSelf.view];
responseCallback(data);
}];
// 帮助与反馈 提交反馈后点击完成
[self.bridge registerHandler:wjjFeedbackSuccess handler:^(id data, WVJBResponseCallback responseCallback) {
NSLog(@"ObjC Echo called with: %@", data);
[weakSelf.navigationController popViewControllerAnimated:YES];
responseCallback(data);
}];
// h5主动从客户端获取网络状态
[self.bridge registerHandler:wjjJSGetNetStatusFromClient handler:^(id data, WVJBResponseCallback responseCallback) {
NSLog(@"ObjC Echo called with: %@", data);
// 向h5传递网路状态信息
NSInteger networkStatus = 0;
if (![CNLiveNetworking isNetworking]) {
networkStatus = 0;
}
else if ([CNLiveNetworking isWWANNetwork]) {
networkStatus = 1;
}
else if ([CNLiveNetworking isWiFiNetwork]) {
networkStatus = 2;
}
id answerData = @{ @"netStatus": [NSString stringWithFormat:@"%ld",(long)networkStatus] };
[weakSelf.bridge callHandler:OCSendNetStatusToJS data:answerData responseCallback:^(id responseData) {
NSLog(@"ObjC received response: %@", responseData);
}];
responseCallback(data);
}];
}
#pragma mark - Setter方法
- (void)setUrl:(NSString *)url{
if(!url||[url isEqualToString:@""]) return;
[super setUrl:[NSString stringWithFormat:@"%@",url]];
}
#pragma mark - CNLiveWebViewDelegate
- (void)webView:(CNLiveWebView *)webView didFinishLoadWithURL:(NSURL *)url {
if(self.title.length == 0){
self.title = webView.title;
}
}
- (void)webView:(CNLiveWebView *)webView didFailLoadWithError:(NSError *)error {
}
#pragma mark - 添加图片
- (void)uploadPictures{
__weak typeof(self) weakSelf = self;
QMUIAlertAction *action1 = [QMUIAlertAction actionWithTitle:@"拍照" style:QMUIAlertActionStyleDefault handler:^(__kindof QMUIAlertController *aAlertController, QMUIAlertAction *action) {
__strong typeof(weakSelf) strongSelf = weakSelf;
if(!strongSelf) return ;
__strong typeof(strongSelf) weakSelf = strongSelf;
[CNLiveAuthorizationManager getAVAuthorizationStatus:^{
__strong typeof(weakSelf) strongSelf = weakSelf;
if(!strongSelf) return ;
XFCameraController *cameraController = [[XFCameraController alloc] init];
cameraController.isCanShootVideo = NO;
cameraController.modalPresentationStyle = UIModalPresentationFullScreen;
__weak XFCameraController *weakCameraController = cameraController;
__weak typeof(strongSelf) weakSelf = strongSelf;
cameraController.takePhotosCompletionBlock = ^(UIImage *image, NSError *error) {
__strong typeof(weakSelf) strongSelf = weakSelf;
if(!strongSelf) return ;
[strongSelf.images addObject:image];
dispatch_async(dispatch_get_main_queue(), ^{
[weakCameraController dismissViewControllerAnimated:YES completion:nil];
});
};
[strongSelf presentViewController:cameraController animated:YES completion:nil];
}];
}];
QMUIAlertAction *action2 = [QMUIAlertAction actionWithTitle:@"从手机相册中选择" style:QMUIAlertActionStyleDefault handler:^(__kindof QMUIAlertController *aAlertController, QMUIAlertAction *action) {
__strong typeof(weakSelf) strongSelf = weakSelf;
if(!strongSelf) return ;
__strong typeof(strongSelf) weakSelf = strongSelf;
[CNLiveAuthorizationManager getPHAuthorizationStatus:^{
__strong typeof(weakSelf) strongSelf = weakSelf;
if(!strongSelf) return ;
TZImagePickerController *picker = [[TZImagePickerController alloc] initWithMaxImagesCount:1 delegate:strongSelf];
picker.modalPresentationStyle = UIModalPresentationFullScreen;
picker.allowPickingVideo = NO;
picker.allowPreview = NO;
__weak typeof(strongSelf) weakSelf = strongSelf;
[picker setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
__strong typeof(weakSelf) strongSelf = weakSelf;
if(!strongSelf) return ;
[strongSelf.images addObject:[photos firstObject]];
}];
[strongSelf presentViewController:picker animated:YES completion:nil];
}];
}];
QMUIAlertAction *cancel = [QMUIAlertAction actionWithTitle:@"取消" style:QMUIAlertActionStyleCancel handler:^(__kindof QMUIAlertController *aAlertController, QMUIAlertAction *action) {
__strong typeof(weakSelf) strongSelf = weakSelf;
if(!strongSelf) return ;
}];
QMUIAlertController *alertController = [QMUIAlertController alertControllerWithTitle:@"" message:@"" preferredStyle:QMUIAlertControllerStyleActionSheet];
[alertController addAction:action1];
[alertController addAction:action2];
[alertController addAction:cancel];
NSMutableDictionary *titleAttributs = [[NSMutableDictionary alloc] initWithDictionary:alertController.alertTitleAttributes];
titleAttributs[NSForegroundColorAttributeName] = [UIColor colorWithRed:72/255.0 green:168/255.0 blue:249/255.0 alpha:1.0];
NSMutableDictionary *cancelAttributs = [[NSMutableDictionary alloc] initWithDictionary:alertController.alertTitleAttributes];
cancelAttributs[NSForegroundColorAttributeName] = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1.0];
action1.buttonAttributes = titleAttributs;
action2.buttonAttributes = titleAttributs;
cancel.buttonAttributes = cancelAttributs;
[alertController showWithAnimated:YES];
}
// 获取图片的token
- (void)getToken {
NSDictionary *param = @{@"appId":AppId,
@"verb":@"POST",
@"storageType":@"2",
@"bucket":API_BucketName
};
[QMUITips showLoading:@"正在提交" inView:self.view];
[CNLiveNetworking setAllowRequestDefaultArgument:NO];
[CNLiveNetworking requestNetworkWithMethod:CNLiveRequestMethodPOST URLString:CNUploadAuthForApp Param:param CacheType:CNLiveNetworkCacheTypeNetworkOnly CompletionBlock:^(NSURLSessionTask *requestTask, id responseObject, NSError *error) {
NSMutableDictionary *data = [NSMutableDictionary dictionaryWithDictionary:responseObject[@"data"]];
NSDictionary *dic = (NSDictionary *)data;
if (!error && dic[@"token"]) {
// [self QNStartUploadWithtoken:dic[@"token"]];
}else {
[QMUITips hideAllToastInView:self.view animated:YES];
[QMUITips showError:error.localizedDescription inView:self.view hideAfterDelay:1.5];
}
}];
}
@end