YBYoungManager.m 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. //
  2. // YBYoungManager.m
  3. // YBVideo
  4. //
  5. // Created by YB007 on 2022/6/2.
  6. // Copyright © 2022 cat. All rights reserved.
  7. //
  8. #import "YBYoungManager.h"
  9. #import "YBYoungSmall.h"
  10. #import "YBYoungModeVC.h"
  11. @interface YBYoungManager()
  12. @property(nonatomic,strong)YBYoungSmall *smallPop;
  13. @property(nonatomic,assign)BOOL ispwd; // 是否设置密码
  14. @property(nonatomic,assign)BOOL isstate; // 是否开启青少年模式
  15. @property(nonatomic,strong)NSString *promptStr; // 小窗提示内容
  16. @property(nonatomic,strong)NSArray *youngList; // 青少年模式说明
  17. @property(nonatomic,strong)NSTimer *youngTimer;
  18. @property(nonatomic,assign)int interval; // 定时请求间隔
  19. @property(nonatomic,strong)NSDictionary *youngInfo; // 青少年模式详情【isuser:0-可以使用 1-时长上限 2-非规定时间 msg:对应提示】
  20. @property(nonatomic,strong)YBAlertView *youngAlert;
  21. @end
  22. @implementation YBYoungManager
  23. static YBYoungManager *_youngManager = nil;
  24. +(instancetype)shareInstance{
  25. static dispatch_once_t onceToken;
  26. dispatch_once(&onceToken, ^{
  27. _youngManager = [[super allocWithZone:NULL] init];
  28. });
  29. return _youngManager;
  30. }
  31. + (instancetype)allocWithZone:(struct _NSZone *)zone{
  32. return [self shareInstance];
  33. }
  34. - (BOOL)youngSwitch{
  35. return self.isstate;
  36. }
  37. /// 检测
  38. -(void)checkYoungStatus:(YoungFrom)youngFrom;{
  39. if ([[Config getOwnID] intValue]<=0) {
  40. return;
  41. }
  42. [YBNetworking postWithUrl:@"User.checkTeenagers" Dic:@{} Suc:^(int code, id info, NSString *msg) {
  43. if (code == 0) {
  44. NSDictionary *infoDic = [info firstObject];
  45. // 是否设置密码
  46. self.ispwd = [minstr([infoDic valueForKey:@"ispwd"])intValue];
  47. // 是否开启青少年模式
  48. self.isstate = [minstr([infoDic valueForKey:@"isstate"])intValue];
  49. // 小窗提示语
  50. self.promptStr = minstr([infoDic valueForKey:@"prompt"]);
  51. // 青少年模式说明数组
  52. self.youngList = [NSArray arrayWithArray:[infoDic valueForKey:@"lists"]];
  53. // 青少年模式下:定时间隔
  54. self.interval = [minstr([infoDic valueForKey:@"duration"])intValue];
  55. // 青少年模式说明【isuser:0-可以使用 1-时长上限 2-非规定时间 msg:对应提示】
  56. self.youngInfo = [infoDic valueForKey:@"useinfo"];
  57. [self changeBanStatus];
  58. // 首页、个人中心跳转页面
  59. if (youngFrom == YoungFrom_Home) {
  60. if (self.isstate == 0 && self.needShowPop == YES) {
  61. // 未开启、提示开启
  62. [self showYoungSmallPop];
  63. }else if(self.isstate == 1){
  64. // 已开启、判断弹窗还是开启计时
  65. [self judgeShowAlertAndNeedResetTimer:NO];
  66. [[NSNotificationCenter defaultCenter]postNotificationName:@"hideHomeVideoRed" object:nil];
  67. }
  68. self.needShowPop = NO;
  69. }else if(youngFrom == YoungFrom_Center){
  70. [self enterYoungMode];
  71. }
  72. }else{
  73. [MBProgressHUD showError:msg];
  74. }
  75. } Fail:^(id fail) {
  76. }];
  77. }
  78. #pragma mark - 开启青少年模式
  79. -(void)enterYoungMode {
  80. YBYoungModeVC *modeVC = [[YBYoungModeVC alloc]init];
  81. modeVC.ispwd = self.ispwd;
  82. modeVC.isstate = self.isstate;
  83. modeVC.youngList = self.youngList;
  84. [[YBBaseAppDelegate sharedAppDelegate] pushViewController:modeVC animated:YES];
  85. }
  86. #pragma mark - 小窗开始
  87. -(void)showYoungSmallPop {
  88. self.needShowPop = NO;
  89. [self destroySamllPop];
  90. _smallPop = [YBYoungSmall showYoungPop:self.promptStr];
  91. }
  92. -(void)destroySamllPop {
  93. if (_smallPop) {
  94. [_smallPop removeFromSuperview];
  95. _smallPop = nil;
  96. }
  97. }
  98. -(void)smallEnterYoungModel;{
  99. [self destroySamllPop];
  100. [self enterYoungMode];
  101. }
  102. #pragma mark - 小窗结束
  103. #pragma mark -
  104. /// 开启-关闭
  105. -(void)changeYoungSwitch:(int)youngSwitch youngInfo:(NSDictionary *)youngInfo;{
  106. self.isstate = youngSwitch;
  107. self.youngInfo = youngInfo;
  108. [self changeBanStatus];
  109. if (youngSwitch == 1) {
  110. [self judgeShowAlertAndNeedResetTimer:YES];
  111. [[NSNotificationCenter defaultCenter] postNotificationName:ybYoungModelSwitchChage object:nil];
  112. [[NSNotificationCenter defaultCenter]postNotificationName:@"hideHomeVideoRed" object:nil];
  113. [[NSNotificationCenter defaultCenter]postNotificationName:@"shopYongChange" object:nil];
  114. }else{
  115. [self destroyTimer];
  116. [[NSNotificationCenter defaultCenter]postNotificationName:@"showHomeVideoRed" object:nil];
  117. }
  118. }
  119. // 改变受限状态
  120. -(void)changeBanStatus {
  121. // isuser:0-可以使用 1-时长上限 2-非规定时间 msg:对应提示
  122. int isuser = [minstr([_youngInfo valueForKey:@"isuser"]) intValue];
  123. if (isuser == 0 || self.isstate == 0) {
  124. self.youngBan = NO;
  125. }else{
  126. self.youngBan = YES;
  127. }
  128. }
  129. /**
  130. * 注意:频繁切前、后台会存在可接受误差
  131. * 0.checkTeenagers返回请求频率、是否需要弹窗【上限、时间段】
  132. * 1.定时请求接口,服务端累计使用时间-->返回是否需要弹窗
  133. * 2.前台启动定时,后台暂停计时
  134. */
  135. /// 前台
  136. -(void)appActive;{
  137. if (self.isstate == NO) {
  138. return;
  139. }
  140. [self createTimer];
  141. }
  142. /// 后台
  143. -(void)appResignActive;{
  144. [self destroyTimer];
  145. }
  146. /// 杀进程
  147. -(void)appKilled;{
  148. [self destroyTimer];
  149. }
  150. /// 定时任务
  151. -(void)createTimer {
  152. [self destroyTimer];
  153. /// 避免太频繁,约定最小时间10s
  154. if (_interval < 10) {
  155. _interval = 10;
  156. }
  157. _youngTimer = [NSTimer scheduledTimerWithTimeInterval:_interval target:self selector:@selector(youngTaskCount) userInfo:nil repeats:YES];
  158. }
  159. -(void)youngTaskCount {
  160. if([[Config getOwnID] intValue] <= 0){
  161. //用户退出、被顶掉等等销毁计时器
  162. [self destroyTimer];
  163. return;
  164. }
  165. //NSLog(@"rk_young_timer=======>");
  166. NSDictionary *postDic = @{@"duration":@(_interval)};
  167. [YBNetworking postWithUrl:@"User.reduceTeenagers" Dic:postDic Suc:^(int code, id info, NSString *msg) {
  168. if (code == 0) {
  169. //NSLog(@"rk_young_timer=======>finish");
  170. if ([info isKindOfClass:[NSArray class]]) {
  171. self.youngInfo = [info firstObject];
  172. [self changeBanStatus];
  173. [self judgeShowAlertAndNeedResetTimer:NO];
  174. }
  175. }
  176. } Fail:^(id fail) {
  177. }];
  178. }
  179. -(void)destroyTimer {
  180. if (_youngTimer) {
  181. [_youngTimer invalidate];
  182. _youngTimer = nil;
  183. }
  184. }
  185. /// 判断是否到达限制【总时长、时间区间】
  186. -(void)judgeShowAlertAndNeedResetTimer:(BOOL)reset {
  187. dispatch_async(dispatch_get_main_queue(), ^{
  188. // isuser:0-可以使用 1-时长上限 2-非规定时间 msg:对应提示
  189. int isuser = [minstr([_youngInfo valueForKey:@"isuser"]) intValue];
  190. NSString *youngAlert = minstr([_youngInfo valueForKey:@"msg"]);
  191. if (isuser > 0) {
  192. [[NSNotificationCenter defaultCenter]postNotificationName:ybYoungModelTimeEndEvent object:nil];
  193. // 提示
  194. [self destroyTimer];
  195. [self showAlert:youngAlert];
  196. }else{
  197. // 计时
  198. if (reset == YES || (reset == NO && !_youngTimer)) {
  199. [self createTimer];
  200. }
  201. }
  202. });
  203. }
  204. -(void)showAlert:(NSString *)alertStr {
  205. [self destroyAlert];
  206. NSDictionary *contentDic = @{
  207. @"title":YZMsg(@"提示"),
  208. @"msg":alertStr,
  209. @"left":YZMsg(@"知道了"),
  210. @"right":YZMsg(@"去关闭"),
  211. };
  212. YBWeakSelf;
  213. _youngAlert = [YBAlertView showAlertView:contentDic complete:^(int eventType) {
  214. dispatch_async(dispatch_get_main_queue(), ^{
  215. if (eventType == 0) {
  216. // 杀掉app
  217. [PublicObj appDestroy];
  218. }else{
  219. // 去关闭页面
  220. [weakSelf enterYoungMode];
  221. }
  222. });
  223. }];
  224. _youngAlert.alertFrom = AlertFrom_YoungModel;
  225. _youngAlert.forbidSureDismiss = YES;
  226. }
  227. -(void)destroyAlert {
  228. if (_youngAlert) {
  229. [_youngAlert removeFromSuperview];
  230. _youngAlert = nil;
  231. }
  232. }
  233. @end