SceneDelegate.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. //
  2. // SceneDelegate.m
  3. // 唔即云相册
  4. //
  5. // Created by 余衡武 on 2021/12/8.
  6. //
  7. #import "SceneDelegate.h"
  8. #import "HWWebViewController.h"
  9. #import "CalculatorViewController.h"
  10. #import "SetUsePWDViewController.h"
  11. #import "GuideViewController.h"
  12. #import "BaseNavigationController.h"
  13. #import "MySetViewController.h"
  14. #import "SafeForKey.h"
  15. #import "TipsQRCodeViewController.h"
  16. #import "SetPWDFirstViewController.h"
  17. #import "ConnectTestViewController.h"
  18. #import "PlayerViewController.h"
  19. #import "connectDeviceManager.h"
  20. #import "inputPWDViewController.h"
  21. #import "customLaunchView.h"
  22. #import "RSATool.h"
  23. @interface SceneDelegate ()<SetUsePWDViewControllerDelegate,GuideViewControllerDelegate>
  24. @property(nonatomic, strong) CalculatorViewController *calculatorVC;
  25. @property(nonatomic, strong) HWWebViewController *webVC;
  26. @property(nonatomic, strong) inputPWDViewController *inputVC;
  27. @property(nonatomic, strong) NSTimer *checkSNDataTimer;//
  28. @property(nonatomic, strong) customLaunchView *customLaunchV;//
  29. @property(nonatomic, strong) PlayerViewController *curPlayerVC;
  30. @property(nonatomic, assign) bool isQRCodeType;//第一次 扫码进来的
  31. @property (nonatomic, assign) BOOL isLoginAgainType;// 单点登录 点重新登录进来的
  32. @end
  33. @implementation SceneDelegate
  34. - (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions API_AVAILABLE(ios(13.0)){
  35. if (scene) {
  36. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(guideOk:) name:GuideOkNotification object:nil];
  37. UIWindowScene *windowScene = (UIWindowScene *)scene;
  38. self.window = [[UIWindow alloc] initWithWindowScene:windowScene];
  39. self.window.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
  40. // 初始化数据库
  41. [self initBrowserWindow];
  42. // 加载主页面
  43. [self enterMainVCFromScene];
  44. //
  45. [self getNetWorkPopViewFun];
  46. [self.window makeKeyAndVisible];
  47. }
  48. }
  49. /** 进入登录界面 - 内部使用 */
  50. - (void)enterLoginVCFromScene {
  51. // UIViewController *vc = [[UIViewController alloc] init];
  52. // vc.view.backgroundColor = [UIColor redColor];
  53. // UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:vc];
  54. // self.window.rootViewController = nvc;
  55. }
  56. /** 主界面 - 内部使用 */
  57. - (void)enterMainVCFromScene {
  58. // ConnectTestViewController *qrCodeVCNav = [[ConnectTestViewController alloc] init];
  59. // self.window.rootViewController = qrCodeVCNav;
  60. // return;/*临时测试*/
  61. /*先判断本地有无设备 无设备时需要先扫码添加设备*/
  62. NSDictionary *deviceDict = [HWDataManager getObjectWithKey:Const_Have_Add_Device];
  63. if (!deviceDict || ![[deviceDict allKeys] containsObject:Const_Have_Add_Device_SN]){
  64. /*扫码界面*/
  65. TipsQRCodeViewController *qrCodeVC = [[TipsQRCodeViewController alloc] init];
  66. BaseNavigationController *qrCodeVCNav = [[BaseNavigationController alloc] initWithRootViewController:qrCodeVC];
  67. self.window.rootViewController = qrCodeVCNav;
  68. _isQRCodeType = YES;
  69. return;
  70. }
  71. //有设备了先去做链接准备 // 80bec9c5
  72. NSString *SNStr = deviceDict[@"Const_Have_Add_Device_SN"];
  73. NSString *sdnId = [connectDeviceManager shareInstance].DeviceThirdIdMod.data.sdnId;
  74. if(SNStr && !sdnId){
  75. [[connectDeviceManager shareInstance] getThridMsgBySN:SNStr didNetEnd:^(bool didSuc) {
  76. }];
  77. }
  78. // GuideViewController
  79. BOOL haveGuide = [HWDataManager getBoolWithKey:Const_Have_Show_Guide];
  80. if (haveGuide) {
  81. ;
  82. }else {
  83. GuideViewController *guiDeVC = [[GuideViewController alloc] init];
  84. guiDeVC.delegate = self;
  85. guiDeVC.sn = SNStr;
  86. self.window.rootViewController = guiDeVC;
  87. return;
  88. }
  89. // if (![[deviceDict allKeys] containsObject:Const_Have_Add_Device_PWD]){
  90. // /*没有设置密码*/
  91. // SetPWDFirstViewController *qrCodeVC = [[SetPWDFirstViewController alloc] init];
  92. // BaseNavigationController *qrCodeVCNav = [[BaseNavigationController alloc] initWithRootViewController:qrCodeVC];
  93. // self.window.rootViewController = qrCodeVCNav;
  94. //
  95. // return;
  96. // }
  97. //确保通过SN号拿到密码了
  98. //非扫码进入 进入到这里很可能还没联网拿到设备最新信息
  99. if(![connectDeviceManager shareInstance].DeviceThirdIdMod){
  100. if(!_checkSNDataTimer){
  101. _checkSNDataTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(enterMainVCFromScene) userInfo:nil repeats:YES];
  102. BOOL haveOpenMask = [HWDataManager getBoolWithKey:Const_Mask_View_Open];
  103. //添加默认启动图片
  104. if(!_customLaunchV && !haveOpenMask){
  105. _customLaunchV = [[customLaunchView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H)];
  106. [self.window addSubview:_customLaunchV];
  107. }
  108. }
  109. return;
  110. }
  111. if(_checkSNDataTimer){
  112. [_checkSNDataTimer invalidate];
  113. [_customLaunchV removeFromSuperview];
  114. _customLaunchV = nil;
  115. }
  116. //是否已经有密码了 有就是输入密码 没有就是设置密码
  117. NSString *curPwd = [connectDeviceManager shareInstance].DeviceThirdIdMod.data.password;
  118. sdnId = [connectDeviceManager shareInstance].DeviceThirdIdMod.data.sdnId;
  119. if(!sdnId){
  120. HLog(@"没有拿到sdnId");
  121. [[connectDeviceManager shareInstance] getThridMsgBySN:SNStr didNetEnd:^(bool didSuc) {
  122. }];
  123. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  124. mainBlock(^{
  125. [self enterMainVCFromScene];
  126. });
  127. });
  128. return;
  129. }
  130. if(curPwd && curPwd.length > 0){
  131. /*设置密码*/
  132. // inputPWDViewController *nextVC = [[inputPWDViewController alloc] init];
  133. // BaseNavigationController *nextVCNav = [[BaseNavigationController alloc] initWithRootViewController:nextVC];
  134. // self.window.rootViewController = nextVCNav;
  135. }
  136. else{
  137. /*没有设置密码*/
  138. SetPWDFirstViewController *qrCodeVC = [[SetPWDFirstViewController alloc] init];
  139. BaseNavigationController *qrCodeVCNav = [[BaseNavigationController alloc] initWithRootViewController:qrCodeVC];
  140. self.window.rootViewController = qrCodeVCNav;
  141. return;
  142. }
  143. if(_curPlayerVC){
  144. [_curPlayerVC disconnectVideoServer];
  145. }
  146. //每次进入
  147. PlayerViewController *playerRootVC = [[PlayerViewController alloc] init];
  148. if(self.isLoginAgainType){
  149. playerRootVC.isLoginAgainType = self.isLoginAgainType;
  150. }
  151. playerRootVC.isPwdVCShow = YES;
  152. _curPlayerVC = playerRootVC;
  153. BaseNavigationController *playerNav = [[BaseNavigationController alloc] initWithRootViewController:playerRootVC];
  154. self.window.rootViewController = playerNav;
  155. self.isLoginAgainType = NO;
  156. if(!_isQRCodeType){
  157. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  158. mainBlock(^{
  159. [playerRootVC setShowImgAndVoiceTypeFun:NO];
  160. });
  161. });
  162. [self showCalculatorVC];
  163. _isQRCodeType = NO;
  164. }
  165. // MySetViewController *playerRootVC = [[MySetViewController alloc] init];
  166. // BaseNavigationController *playerNav = [[BaseNavigationController alloc] initWithRootViewController:playerRootVC];
  167. // self.window.rootViewController = playerNav;
  168. }
  169. - (void)setPwdOk{
  170. [self enterMainVCFromScene];
  171. // NSArray *dataArray = [BaseModel bg_findAll:DB_BrowserWindows_TableName];
  172. //
  173. // if (dataArray.count == 0) { // 浏览器没有窗口
  174. // // 加载首页
  175. // HWWebViewController *vc = [[HWWebViewController alloc] init];
  176. // vc.webUrl = Const_HomeUrl;
  177. // UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:vc];
  178. // self.window.rootViewController = nvc;
  179. //
  180. // }else {
  181. // // 浏览器当前窗口索引ID
  182. // NSInteger ID = [HWDataManager getIntegerWithKey:BrowserWindowsCurrentID];
  183. // NSString *where = [NSString stringWithFormat:@"where %@=%ld",bg_sqlKey(@"ID"),(long)ID];
  184. // NSArray* finfAlls = [BaseModel bg_find:DB_BrowserWindows_TableName where:where];
  185. // BaseModel *model = finfAlls.firstObject;
  186. //
  187. // // 加载网页
  188. // HWWebViewController *vc = [[HWWebViewController alloc] init];
  189. // vc.webUrl = model.webUrl;
  190. // UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:vc];
  191. // self.window.rootViewController = nvc;
  192. // }
  193. }
  194. /** 登录界面 - 外部使用*/
  195. + (void)enterLoginVC {
  196. // SetPWDViewController *vc = [[SetPWDViewController alloc] init];
  197. // UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:vc];
  198. // UIWindow *keyWindow = [iTools getKeyWindow];
  199. // keyWindow.rootViewController = nvc;
  200. }
  201. /** 主界面 - 外部使用 */
  202. + (void)enterMainVC {
  203. // AlbumHomeViewController *vc = [[AlbumHomeViewController alloc] init];
  204. // UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:vc];
  205. // UIWindow *keyWindow = [iTools getKeyWindow];
  206. // keyWindow.rootViewController = nvc;
  207. }
  208. - (void)sceneDidDisconnect:(UIScene *)scene API_AVAILABLE(ios(13.0)){
  209. }
  210. - (void)sceneDidBecomeActive:(UIScene *)scene API_AVAILABLE(ios(13.0)){
  211. BOOL haveGuide = [HWDataManager getBoolWithKey:Const_Have_Show_Guide];
  212. if (haveGuide) {
  213. ;
  214. }else {
  215. return;
  216. }
  217. [self showCalculatorVC];
  218. }
  219. - (void)sceneWillResignActive:(UIScene *)scene API_AVAILABLE(ios(13.0)){
  220. BOOL haveGuide = [HWDataManager getBoolWithKey:Const_Have_Show_Guide];
  221. if (haveGuide) {
  222. ;
  223. }else {
  224. return;
  225. }
  226. [self showCalculatorVC];
  227. }
  228. - (void)sceneWillEnterForeground:(UIScene *)scene API_AVAILABLE(ios(13.0)){
  229. }
  230. - (void)sceneDidEnterBackground:(UIScene *)scene API_AVAILABLE(ios(13.0)){
  231. BOOL haveGuide = [HWDataManager getBoolWithKey:Const_Have_Show_Guide];
  232. if (haveGuide) {
  233. ;
  234. }else {
  235. return;
  236. }
  237. [self showCalculatorVC];
  238. }
  239. #pragma mark 初始化数据库-DB_BrowserWindows_TableName
  240. - (void)initBrowserWindow {
  241. // 浏览器当前所有窗口
  242. NSArray *dataArray = [BaseModel bg_findAll:DB_BrowserWindows_TableName];
  243. // 浏览器当前窗口索引ID
  244. NSInteger ID = [HWDataManager getIntegerWithKey:BrowserWindowsCurrentID];
  245. NSString *IDValue = [NSString stringWithFormat:@"%ld", ID];
  246. NSString *where = [NSString stringWithFormat:@"where %@=%@",bg_sqlKey(@"ID"),bg_sqlValue(IDValue)];
  247. NSArray *modelArr = [BaseModel bg_find:DB_BrowserWindows_TableName where:where];
  248. if (dataArray.count == 0) { // 浏览器没有窗口
  249. BaseModel *model = [[BaseModel alloc] init];
  250. model.ID = 0;
  251. model.name = @"主页";
  252. // model.iconFile = imageUrl;
  253. model.webUrl = Const_HomeUrl;
  254. // 写入数据库
  255. model.bg_tableName = DB_BrowserWindows_TableName;
  256. [model bg_saveOrUpdateAsync:^(BOOL isSuccess) {
  257. HLog(@"BaseModel写入:%@", isSuccess ? @"成功":@"失败");
  258. }];
  259. // 浏览器当前窗口索引ID
  260. [HWDataManager setIntegerWithKey:BrowserWindowsCurrentID value:0];
  261. }else if (modelArr.count == 0) { // 新建窗口
  262. BaseModel *model = [[BaseModel alloc] init];
  263. model.ID = ID;
  264. model.name = @"主页";
  265. // model.iconFile = imageUrl;
  266. model.webUrl = Const_HomeUrl;
  267. // 写入数据库
  268. model.bg_tableName = DB_BrowserWindows_TableName;
  269. [model bg_saveOrUpdateAsync:^(BOOL isSuccess) {
  270. HLog(@"BaseModel写入:%@", isSuccess ? @"成功":@"失败");
  271. }];
  272. }else { // 更新窗口数据
  273. // 浏览器当前窗口索引ID
  274. // NSInteger ID = [HWDataManager getIntegerWithKey:BrowserWindowsCurrentID];
  275. //
  276. // BaseModel *model = [[BaseModel alloc] init];
  277. // model.ID = ID;
  278. // model.name = @"主页";
  279. //// model.iconFile = imageUrl;
  280. // model.webUrl = Const_HomeUrl;
  281. //
  282. // // 更新数据库
  283. // model.bg_tableName = DB_BrowserWindows_TableName;
  284. // [model bg_saveOrUpdateAsync:^(BOOL isSuccess) {
  285. // HLog(@"BaseModel更新: %@", isSuccess ? @"成功":@"失败");
  286. // }];
  287. }
  288. }
  289. #pragma mark 获取当前屏幕的截图
  290. //- (UIImage *)getScreenShotImage {
  291. // CGSize size = [UIScreen mainScreen].bounds.size;
  292. // CGFloat scale = [UIScreen mainScreen].scale;
  293. // UIGraphicsBeginImageContextWithOptions(size, YES, scale);
  294. // [self.window.layer renderInContext:UIGraphicsGetCurrentContext()];
  295. // UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
  296. // UIGraphicsEndImageContext();
  297. // return image;
  298. //}
  299. #pragma mark 计算器伪装视图
  300. /*CalculatorViewController*/
  301. - (void)showCalculatorVC{
  302. /*无有效时长直接返回不加载任何加密界面*/
  303. NSString *str = nil;
  304. NSDictionary *deviceDict = [HWDataManager getObjectWithKey:Const_Have_Add_Device];
  305. //本地密码补在判断 密码改在服务器保持
  306. if ([[deviceDict allKeys] containsObject:Const_Have_Add_Device_PWD]){
  307. str = [deviceDict objectForKey:Const_Have_Add_Device_PWD];
  308. }
  309. if (!str || [str isEqualToString:@""])
  310. {
  311. return;
  312. }
  313. // BOOL haveOpenMask = [HWDataManager getBoolWithKey:Const_Mask_View_Open];
  314. // if (!haveOpenMask){
  315. // //输入密码界面
  316. // [self closeCalculatorVC];
  317. // _inputVC = [[inputPWDViewController alloc] init];
  318. // [self.window addSubview:_inputVC.view];
  319. // return;
  320. // }
  321. BOOL haveVaildTime = [HWDataManager getBoolWithKey:Const_Have_No_VaildTime];
  322. if (haveVaildTime == YES){/*无有效时间*/
  323. return;
  324. }
  325. NSInteger maskModel = [HWDataManager getIntegerWithKey:Const_Mask_View_Model];
  326. if (maskModel == 0){
  327. //输入密码界面
  328. [self closeCalculatorVC];
  329. _inputVC = [[inputPWDViewController alloc] init];
  330. [self.window addSubview:_inputVC.view];
  331. }
  332. else if (maskModel == 2){
  333. /*浏览器*/
  334. [self closeCalculatorVC];
  335. _webVC = [[HWWebViewController alloc] init];
  336. _webVC.pwd = str;
  337. _webVC.webUrl = @"https://baidu.com";
  338. [self.window addSubview:_webVC.view];
  339. }else{
  340. /*计算器*/
  341. [self closeCalculatorVC];
  342. _calculatorVC = [[CalculatorViewController alloc] init];
  343. _calculatorVC.pwd = str;
  344. [self.window addSubview:_calculatorVC.view];
  345. }
  346. [[NSNotificationCenter defaultCenter] postNotificationName:ShowPwdVCNotification object:nil];
  347. }
  348. - (void)closeCalculatorVC{
  349. if (_calculatorVC){
  350. [_calculatorVC.view removeFromSuperview];
  351. }
  352. /*浏览器*/
  353. if (_webVC){
  354. [_webVC.view removeFromSuperview];
  355. }
  356. if(_inputVC){
  357. [_inputVC.view removeFromSuperview];
  358. }
  359. }
  360. - (NSString *)documentPathForAccount:(NSString *)account fileFolder:(NSString *)fileFolder {
  361. NSString *path = DocumentPath;
  362. if (account.length != 0) {
  363. path = [NSString stringWithFormat:@"%@/%@/",DocumentPath,account];
  364. }
  365. if ([fileFolder containsString:@"/"]) {
  366. NSString *path1 = [path stringByAppendingPathComponent:fileFolder];
  367. path1 = [path1 stringByDeletingLastPathComponent];
  368. NSError *error;
  369. if (![[NSFileManager defaultManager] fileExistsAtPath:path1]) {
  370. [[NSFileManager defaultManager] createDirectoryAtPath:path1 withIntermediateDirectories:YES attributes:nil error:&error];
  371. }
  372. }else{
  373. NSError *error;
  374. if (![[NSFileManager defaultManager] fileExistsAtPath:path]) {
  375. [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error];
  376. }
  377. }
  378. return [path stringByAppendingPathComponent:fileFolder];
  379. }
  380. - (void)startUseBtnBePressed{
  381. [self enterMainVCFromScene];
  382. // NSString *launchAppPWDPath = [self documentPathForAccount:@"" fileFolder:@"LaunchPWD"];
  383. // NSString *str = [[NSString alloc] initWithContentsOfFile:launchAppPWDPath encoding:(NSUTF8StringEncoding) error:nil];
  384. // if (!str || [str isEqualToString:@"0"])
  385. // {
  386. // SetUsePWDViewController *loginVC = [[SetUsePWDViewController alloc] init];
  387. // loginVC.delegate = self;
  388. // self.window.rootViewController = loginVC;
  389. //
  390. // return;
  391. // }
  392. //
  393. // NSArray *dataArray = [BaseModel bg_findAll:DB_BrowserWindows_TableName];
  394. //
  395. // if (dataArray.count == 0) { // 浏览器没有窗口
  396. // // 加载首页
  397. // HWWebViewController *vc = [[HWWebViewController alloc] init];
  398. // vc.webUrl = Const_HomeUrl;
  399. // UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:vc];
  400. // self.window.rootViewController = nvc;
  401. //
  402. // }else {
  403. // // 浏览器当前窗口索引ID
  404. // NSInteger ID = [HWDataManager getIntegerWithKey:BrowserWindowsCurrentID];
  405. // NSString *where = [NSString stringWithFormat:@"where %@=%ld",bg_sqlKey(@"ID"),(long)ID];
  406. // NSArray* finfAlls = [BaseModel bg_find:DB_BrowserWindows_TableName where:where];
  407. // BaseModel *model = finfAlls.firstObject;
  408. //
  409. // // 加载网页
  410. // HWWebViewController *vc = [[HWWebViewController alloc] init];
  411. // vc.webUrl = model.webUrl;
  412. // UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:vc];
  413. // self.window.rootViewController = nvc;
  414. // }
  415. }
  416. - (void)loginOk{
  417. [self enterMainVCFromScene];
  418. }
  419. - (void)guideOk:(NSNotification*)not{
  420. NSString *object = not.object;
  421. if(object && [object isEqualToString:@"isLoginAgainType"]){
  422. self.isLoginAgainType = YES;
  423. }
  424. [self enterMainVCFromScene];
  425. }
  426. #pragma mark 调起系统网络允许
  427. -(void)getNetWorkPopViewFun
  428. {
  429. NSMutableDictionary *paraDict = [NSMutableDictionary new];
  430. // KWeakSelf
  431. [[netWorkManager shareInstance] CommonGetWithCallBackCode:@"" Parameters:paraDict success:^(id _Nonnull responseObject) {
  432. } failure:^(NSError * _Nonnull error) {
  433. }];
  434. }
  435. @end