zjf
2023-03-06 392b76515f40376b6d36f40a114850ef63650384
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
//
//  PBLoadModelDisplayController.m
//  IphoneBIMe
//
//  Created by zjf on 2018/7/24.
//  Copyright © 2018年 ProBIM. All rights reserved.
//
 
#import "PBLoadModelDisplayController.h"
#import "PBAddIssueController.h"
#import "PBNavigationController.h"
#import <WebKit/WebKit.h>
#import "PBProjectModel.h"
#import <JavaScriptCore/JavaScriptCore.h>
#import "PBBrowseViewController.h"
@interface PBLoadModelDisplayController ()<WKUIDelegate, WKNavigationDelegate,WKScriptMessageHandler, UIGestureRecognizerDelegate>
@property (nonatomic, strong) WKWebView *webView;
//@property (nonatomic, strong) JSContext *context;
@end
 
@implementation PBLoadModelDisplayController
 
- (void)viewDidLoad {
    [super viewDidLoad];
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.f) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
        self.navigationController.interactivePopGestureRecognizer.delegate = self;
    }
    [self setupUI];
}
- (void)setupUI {
    self.title = @"三维浏览";
    WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
    configuration.userContentController = [WKUserContentController new];
    [configuration.userContentController addScriptMessageHandler:self name:@"finshLoad"];
    [configuration.userContentController addScriptMessageHandler:self name:@"base64Image"];
    [configuration.userContentController addScriptMessageHandler:self name:@"preView"];
    WKPreferences *preferences = [WKPreferences new];
    preferences.javaScriptCanOpenWindowsAutomatically = YES;
    configuration.preferences = preferences;
    self.webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:configuration];
    self.webView.scrollView.bounces = NO;
    self.webView.navigationDelegate = self;
    self.webView.UIDelegate = self;
    [self.view addSubview:self.webView];
    [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.view);
    }];
    PBBackNavItem *backNav = [PBBackNavItem backNacItem];
    backNav.title = @"返回";
    [backNav addTarget:self action:@selector(backNavAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backNavItem = [[UIBarButtonItem alloc] initWithCustomView:backNav];
    self.navigationItem.leftBarButtonItem = backNavItem;
 
    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithTitle:@"新增问题" style:UIBarButtonItemStylePlain target:self action:@selector(rightAction)];
    self.navigationItem.rightBarButtonItem = right;
    self.navigationItem.rightBarButtonItem.enabled = NO;
    [self loadWeb];
}
 
- (void)loadWeb {
    NSString *url = [NSString stringWithFormat:@"%@/BIMComposer/index.html?projectId=%@&model=%@&ver=",WebUrl,self.projectIID, self.modelID];
    NSLog(@"url:%@",url);
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
}
- (void)backNavAction {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)setProjectModel:(PBProjectModel *)projectModel {
    _projectModel = projectModel;
}
- (void)setModelID:(NSString *)modelID {
    _modelID = modelID;
}
- (void)setViewID:(NSString *)viewID {
    _viewID = viewID;
}
- (void)setViewPointID:(NSString *)viewPointID {
    _viewPointID = viewPointID;
}
- (void)setProjectIID:(NSString *)projectIID {
    _projectIID = projectIID;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
// 页面加载完成之后调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    NSLog(@"加载完成");
    NSString *ocToJs = @"window.model.BIM365API.Events.finishRender.on('default',()=>{window.top.webkit.messageHandlers.finshLoad.postMessage(null)})";
    [self.webView evaluateJavaScript:ocToJs completionHandler:^(id _Nullable name, NSError * _Nullable error) {
        NSLog(@"方法调用完成回调%@",error);
    }];
}
- (void)rightAction {
    NSString *ocToJs = @"window.model.BIM365API.Context.asynCapturePicture().then((image_base64)=>{window.top.webkit.messageHandlers.base64Image.postMessage(image_base64)})";
    [self.webView evaluateJavaScript:ocToJs completionHandler:^(id name, NSError * error) {
        NSLog(@"获取截图%@ - %@",name, error);
    }];
}
#pragma mark - WKScriptMessageHandler
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
    //    message.body  --  Allowed types are NSNumber, NSString, NSDate, NSArray,NSDictionary, and NSNull.
    if ([message.name isEqualToString:@"finshLoad"]) {
        if (self.viewPointID) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                NSString *ocToJs = [NSString stringWithFormat:@"window.model.BIM365API.Context.setViewPointBasicInfo(%@)",self.viewPointID];
                [self.webView evaluateJavaScript:ocToJs completionHandler:^(id _Nullable name, NSError * _Nullable error) {
                    NSLog(@"相机位置设置完成");
                }];
            });
        }else {
            self.navigationItem.rightBarButtonItem.enabled = YES;
        }
    }else if ([message.name isEqualToString:@"base64Image"]) {
        NSLog(@"图片回来了%@",message.body);
//        NSRange range = [message.body rangeOfString:@"base64,"];
//        NSString *base64 = [message.body substringFromIndex:range.location + range.length];
//        UIImage *image = [NSString imageDecoding:base64];
 
        PBAddIssueController *addIssueVC = [[PBAddIssueController alloc] init];
        addIssueVC.isAddIssue = YES;
        addIssueVC.projectModel = self.projectModel;
//        addIssueVC.statusAndTypeData = self.statusAndTypeData;
        addIssueVC.modelID = self.modelID;
        NSLog(@"获取相机位置");
        NSString *ocToJs = @"window.model.BIM365API.Context.getViewPointBasicInfo(true)";
        [self.webView evaluateJavaScript:ocToJs completionHandler:^(id name, NSError * error) {
            NSLog(@"方法调用完成回调%@ - %@",name, error);
            NSDictionary *dict = (NSDictionary *)name;
            NSString *str = [NSString convertToJson:dict];
            addIssueVC.viewPointStr = str;
        }];
        addIssueVC.base64Image = message.body;
        addIssueVC.SaveSuccessBlock = ^{
            
            
        };
        PBNavigationController *nav = [[PBNavigationController alloc] initWithRootViewController:addIssueVC];
        nav.modalPresentationStyle = UIModalPresentationFullScreen;
        [self presentViewController:nav animated:YES completion:nil];
    }else if ([message.name isEqualToString:@"preView"]) {
        NSLog(@"图片回来了%@",message.body);
        NSString *str =  (NSString *)message.body;
        NSDictionary *dict = [NSString convertTodictionaryOrArr:str];
        NSLog(@"%@",dict);
        PBBrowseViewController *browseVC = [[PBBrowseViewController alloc] init];
        browseVC.details = dict;
        [self.navigationController pushViewController:browseVC animated:YES];
    }
}
 
/*
#pragma mark - Navigation
 
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
 
 
- (void)encodeWithCoder:(nonnull NSCoder *)coder {
    
}
 
- (void)traitCollectionDidChange:(nullable UITraitCollection *)previousTraitCollection {
    
}
 
- (void)preferredContentSizeDidChangeForChildContentContainer:(nonnull id<UIContentContainer>)container {
    
}
 
 
 
- (void)systemLayoutFittingSizeDidChangeForChildContentContainer:(nonnull id<UIContentContainer>)container {
    
}
 
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(nonnull id<UIViewControllerTransitionCoordinator>)coordinator {
    
}
 
- (void)willTransitionToTraitCollection:(nonnull UITraitCollection *)newCollection withTransitionCoordinator:(nonnull id<UIViewControllerTransitionCoordinator>)coordinator {
    
}
 
- (void)didUpdateFocusInContext:(nonnull UIFocusUpdateContext *)context withAnimationCoordinator:(nonnull UIFocusAnimationCoordinator *)coordinator {
    
}
 
- (void)setNeedsFocusUpdate {
    
}
 
 
 
- (void)updateFocusIfNeeded {
    
}
 
@end