WardOpenView.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. //
  2. // WardOpenView.m
  3. // BuguLive
  4. //
  5. // Created by 范东 on 2019/1/29.
  6. // Copyright © 2019 xfg. All rights reserved.
  7. //
  8. #import "WardOpenView.h"
  9. #import "WardPriceButton.h"
  10. #import "WardPrivilegeButton.h"
  11. #define kWardOpenViewPriceBtnBaseTag 200
  12. #define kWardOpenViewPrivilegeBtnBaseTag 300
  13. #define kWardOpenViewOpenBtnTag 1107
  14. @interface WardOpenView()
  15. @property (nonatomic, strong) UIView *shadowView;
  16. @property (nonatomic, strong) UIImageView *topBackView;
  17. @property (nonatomic, strong) UIImageView *iconImageView;
  18. @property (nonatomic, strong) UIImageView *wardImageView;
  19. @property (nonatomic, strong) UILabel *diamondLabel;
  20. @property (nonatomic, strong) UILabel *timeLabel;
  21. @property(nonatomic, strong) QMUIButton *diamondBtn;
  22. @property (nonatomic, copy) clickFAQBtnBlock clickFAQBtnBlock;
  23. @property (nonatomic, copy) clickWardListBtnBlock clickWardListBtnBlock;
  24. @property (nonatomic, copy) clickOpenViewBtnBlock clickOpenViewBtnBlock;
  25. @property (nonatomic, copy) clickPrivilegeBtnBlock clickPrivilegeBtnBlock;
  26. @property (nonatomic, copy) NSString *userId;
  27. @property (nonatomic, copy) NSString *currentId;
  28. @property (nonatomic, strong) NSArray *privilegeArray;
  29. @end
  30. @implementation WardOpenView
  31. - (instancetype)initWithFrame:(CGRect)frame UserId:(nonnull NSString *)userId{
  32. if (self = [super initWithFrame:frame]) {
  33. self.backgroundColor = KMGMainBGColor;
  34. UIBezierPath* rounded = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(15, 15)];
  35. CAShapeLayer* shape = [[CAShapeLayer alloc] init];
  36. [shape setPath:rounded.CGPath];
  37. self.layer.mask = shape;
  38. self.userId = userId;
  39. [self initSubview];
  40. [self requestData];
  41. }
  42. return self;
  43. }
  44. - (void)initSubview{
  45. UIImageView *topBackView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.width, 160 + 30)];
  46. // topBackView.image = [UIImage imageNamed:@"lr_img_ward_open_back_blue"];
  47. topBackView.backgroundColor = kRedColor;
  48. [self addSubview:topBackView];
  49. self.topBackView = topBackView;
  50. UIButton *faqBtn = [[UIButton alloc]initWithFrame:CGRectMake(10, 10, 20, 20)];
  51. [faqBtn setBackgroundImage:[UIImage imageNamed:@"lr_btn_ward_faq"] forState:UIControlStateNormal];
  52. [faqBtn addTarget:self action:@selector(faqBtnAction) forControlEvents:UIControlEventTouchUpInside];
  53. [self addSubview:faqBtn];
  54. [self addSubview:self.iconImageView];
  55. [self addSubview:self.wardImageView];
  56. UIButton *closeBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 10, kRealValue(25), kRealValue(25))];
  57. closeBtn.right = self.right - kRealValue(10);
  58. [closeBtn setImage:[UIImage imageNamed:@"lr_btn_ward_close"] forState:UIControlStateNormal];
  59. [closeBtn addTarget:self action:@selector(closeBtn:) forControlEvents:UIControlEventTouchUpInside];
  60. [self addSubview:closeBtn];
  61. CGSize wardListBtnSize = [ASLocalizedString(@"TA的守护")textSizeIn:CGSizeMake(MAXFLOAT, MAXFLOAT) font:[UIFont systemFontOfSize:15]];
  62. UIButton *wardListBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 10, wardListBtnSize.width, 20)];
  63. wardListBtn.right = closeBtn.left - 15;
  64. [wardListBtn setTitle:ASLocalizedString(@"TA的守护")forState:UIControlStateNormal];
  65. [wardListBtn setTitleColor:[UIColor colorWithHexString:@"#666666"] forState:UIControlStateNormal];
  66. [wardListBtn.titleLabel setFont:[UIFont systemFontOfSize:15]];
  67. [wardListBtn addTarget:self action:@selector(wardListBtnAction) forControlEvents:UIControlEventTouchUpInside];
  68. [self addSubview:wardListBtn];
  69. closeBtn.centerY = wardListBtn.centerY;
  70. UILabel *tipLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, topBackView.bottom + 15, self.width, 20)];
  71. tipLabel.text = ASLocalizedString(@"守护特权");
  72. // ASLocalizedString(@"成为Ta的守护,享受独一无二的特权");
  73. tipLabel.font = [UIFont systemFontOfSize:17];
  74. tipLabel.textColor = [UIColor colorWithHexString:@"#333333"];
  75. // RGB(120, 120, 120);
  76. tipLabel.backgroundColor =KMGMainBGColor;
  77. [self addSubview:tipLabel];
  78. UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, self.height - 50 - 10, self.width, 0.5)];
  79. lineView.backgroundColor = RGB(230, 230, 230);
  80. [self addSubview:lineView];
  81. UIButton *openBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, lineView.bottom + 5, 80, 34)];
  82. openBtn.tag = kWardOpenViewOpenBtnTag;
  83. openBtn.right = self.right - 10;
  84. openBtn.layer.cornerRadius = 17;
  85. [openBtn setBackgroundImage:[UIImage imageNamed:@"lr_btn_ward_bg"] forState:UIControlStateNormal];
  86. openBtn.clipsToBounds = YES;
  87. [openBtn setTitle:ASLocalizedString(@"开通守护")forState:UIControlStateNormal];
  88. [openBtn setTitleColor:kWhiteColor forState:UIControlStateNormal];
  89. [openBtn.titleLabel setFont:[UIFont systemFontOfSize:15]];
  90. [openBtn addTarget:self action:@selector(openBtnAction) forControlEvents:UIControlEventTouchUpInside];
  91. [self addSubview:openBtn];
  92. UIImageView *moreImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 8, 15)];
  93. moreImageView.image = [UIImage imageNamed:@"lr_btn_ward_right"];
  94. moreImageView.centerY = wardListBtn.centerY;
  95. moreImageView.right = wardListBtn.right + 10;
  96. [self addSubview:moreImageView];
  97. [self addSubview:self.diamondLabel];
  98. self.diamondLabel.centerY = openBtn.centerY;
  99. [self addSubview:self.timeLabel];
  100. self.timeLabel.centerY = openBtn.centerY;
  101. [self.diamondLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  102. make.top.equalTo(lineView.mas_bottom);
  103. make.left.equalTo(self).offset(10);
  104. make.height.mas_equalTo(@44);
  105. }];
  106. [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  107. make.right.equalTo(self).offset(-100);
  108. make.top.equalTo(lineView.mas_bottom);
  109. make.left.equalTo(self.diamondLabel.mas_right).offset(10);
  110. make.height.mas_equalTo(@44);
  111. }];
  112. }
  113. -(void)closeBtn:(UIButton *)sender{
  114. [self hide];
  115. }
  116. - (void)requestData{
  117. NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
  118. [dict setValue:@"guardians_old" forKey:@"ctl"];
  119. [dict setValue:@"duardian_index" forKey:@"act"];
  120. [dict setValue:self.userId forKey:@"host_id"];
  121. [self.httpsManager POSTWithParameters:dict SuccessBlock:^(NSDictionary *responseJson) {
  122. if ([responseJson toInt:@"status"] == 1) {
  123. [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:responseJson[@"head_image"]] placeholderImage:kDefaultPreloadHeadImg];
  124. NSString *is_guaritian = responseJson[@"is_guartian"];
  125. NSString *diamond = responseJson[@"diamonds"];
  126. if (is_guaritian.integerValue == 1) {
  127. UIButton *openBtn = [self viewWithTag:kWardOpenViewOpenBtnTag];
  128. [openBtn setTitle:ASLocalizedString(@"续费守护")forState:UIControlStateNormal];
  129. NSString *time = responseJson[@"guartian_time"];
  130. NSDateFormatter *originFormatter = [[NSDateFormatter alloc]init];
  131. [originFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
  132. NSDateFormatter *newFormatter = [[NSDateFormatter alloc]init];
  133. [newFormatter setDateFormat:@"yyyy-MM-dd"];
  134. NSDate *originDate = [originFormatter dateFromString:time];
  135. NSString *newDateString = [newFormatter stringFromDate:originDate];
  136. NSMutableAttributedString *attr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@%@",ASLocalizedString(@"到期: "),newDateString]];
  137. [attr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#399ADF"] range:NSMakeRange(ASLocalizedString(@"到期: ").length, newDateString.length)];
  138. self.timeLabel.attributedText = attr;
  139. }
  140. NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:ASLocalizedString(@"我的钻石: %@"),diamond]];
  141. [attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#399ADF"] range:NSMakeRange(ASLocalizedString(@"我的钻石: ").length, diamond.length)];
  142. [self.diamondLabel setAttributedText:attrStr];
  143. if ([is_guaritian isEqualToString:@"1"]) {
  144. //已开通守护
  145. self.wardImageView.hidden = NO;
  146. }else{
  147. //未开通守护
  148. self.wardImageView.hidden = YES;
  149. }
  150. [self setDataWithPriceArray:responseJson[@"data"] PrivilegeArray:responseJson[@"guardian_type"]];
  151. }else{
  152. NSLog(ASLocalizedString(@"守护类型接口请求失败responseJson:%@"),responseJson);
  153. }
  154. } FailureBlock:^(NSError *error) {
  155. NSLog(ASLocalizedString(@"守护类型接口请求失败error:%@"),error);
  156. }];
  157. }
  158. - (void)setDataWithPriceArray:(NSArray *)priceArray PrivilegeArray:(NSArray *)privilegeArray{
  159. self.privilegeArray = privilegeArray;
  160. CGFloat viewWidth = (self.width - 14 * 2 - 19 * 2) / 3;
  161. for (NSInteger i = 0; i < priceArray.count; i++) {
  162. WardPriceButton *button = [[WardPriceButton alloc]initWithFrame:CGRectMake( 14 + i * (viewWidth + 19), 35 + 10, viewWidth, 80)];
  163. button.type_name = priceArray[i][@"type_name"];
  164. button.dict = priceArray[i];
  165. if (i == 0) button.layer.borderWidth = 0.8f;
  166. button.layer.borderColor = [UIColor colorWithHexString:@"#F5A623"].CGColor;
  167. button.layer.cornerRadius = 10;
  168. [button setSelected:NO];
  169. [button addTarget:self action:@selector(wardPriceButtonAction:) forControlEvents:UIControlEventTouchUpInside];
  170. button.tag = kWardOpenViewPriceBtnBaseTag + i;
  171. [self addSubview:button];
  172. }
  173. WardPriceButton *button = [self viewWithTag:kWardOpenViewPriceBtnBaseTag];
  174. [button setSelected:YES];
  175. self.currentId = button.dict[@"id"];
  176. for (NSInteger i = 0; i < privilegeArray.count; i++) {
  177. NSDictionary *dict = privilegeArray[i];
  178. WardPrivilegeButton *button = [[WardPrivilegeButton alloc]initWithFrame:CGRectMake( i * 85, 220, 85, 85)];
  179. [button setTitle:dict[@"name"]];
  180. [button setImageUrl:dict[@"default_icon"]];
  181. [button setId:dict[@"id"]];
  182. [button setDict:privilegeArray[i]];
  183. button.tag = kWardOpenViewPrivilegeBtnBaseTag + i;
  184. [button addTarget:self action:@selector(privilegeButtonAction:) forControlEvents:UIControlEventTouchUpInside];
  185. [self addSubview:button];
  186. }
  187. for (UIView *subView in self.subviews) {
  188. if ([subView isKindOfClass:[WardPrivilegeButton class]]) {
  189. WardPrivilegeButton *subBtn = (WardPrivilegeButton *)subView;
  190. if ([button.type_name containsObject:subBtn.id]) {
  191. [subBtn setImageUrl:subBtn.dict[@"select_icon"]];
  192. }
  193. }
  194. }
  195. }
  196. - (void)wardListBtnAction{
  197. [self hide];
  198. if (self.clickWardListBtnBlock) {
  199. self.clickWardListBtnBlock();
  200. }
  201. }
  202. - (void)faqBtnAction{
  203. [self hide];
  204. if (self.clickFAQBtnBlock) {
  205. self.clickFAQBtnBlock();
  206. }
  207. }
  208. - (void)privilegeButtonAction:(WardPrivilegeButton *)sender{
  209. //TODO
  210. if (self.clickPrivilegeBtnBlock) {
  211. self.clickPrivilegeBtnBlock(sender.dict[@"id"],sender,sender.tag - kWardOpenViewPrivilegeBtnBaseTag == (self.privilegeArray.count - 1));
  212. }
  213. }
  214. - (void)wardPriceButtonAction:(WardPriceButton *)sender{
  215. self.currentId = sender.dict[@"id"];
  216. // if (sender.tag - kWardOpenViewPriceBtnBaseTag == 2) {
  217. // self.topBackView.image = [UIImage imageNamed:@"lr_img_ward_open_back_purple"];
  218. // }else{
  219. // self.topBackView.image = [UIImage imageNamed:@"lr_img_ward_open_back_blue"];
  220. // }
  221. NSLog(ASLocalizedString(@"点击了第%ld个按钮"),sender.tag - kWardOpenViewPriceBtnBaseTag);
  222. //选中或者x不选中
  223. [sender setSelected:YES];
  224. for (UIView *subView in self.subviews) {
  225. if ([subView isKindOfClass:[WardPriceButton class]]) {
  226. WardPriceButton *subBtn = (WardPriceButton *)subView;
  227. subBtn.layer.borderWidth = 0;
  228. if (subBtn == sender) {
  229. sender.layer.borderWidth = 0.5f;
  230. }
  231. [subBtn setSelected:(subBtn == sender)];
  232. }
  233. if ([subView isKindOfClass:[WardPrivilegeButton class]]) {
  234. WardPrivilegeButton *subBtn = (WardPrivilegeButton *)subView;
  235. if ([sender.type_name containsObject:subBtn.id]) {
  236. [subBtn setImageUrl:subBtn.dict[@"select_icon"]];
  237. }else{
  238. [subBtn setImageUrl:subBtn.dict[@"default_icon"]];
  239. }
  240. }
  241. }
  242. }
  243. - (void)openBtnAction{
  244. NSLog(ASLocalizedString(@"点击了开通守护按钮"));
  245. [self hide];
  246. if (self.clickOpenViewBtnBlock) {
  247. self.clickOpenViewBtnBlock(self.currentId);
  248. }
  249. }
  250. - (void)show:(UIView *)superView{
  251. [superView addSubview:self.shadowView];
  252. [superView addSubview:self];
  253. [UIView animateWithDuration:0.25 animations:^{
  254. self.frame = CGRectMake(0, kScreenH - self.height, self.width, self.height);
  255. self.shadowView.alpha = 1;
  256. }];
  257. }
  258. - (void)hide{
  259. [UIView animateWithDuration:0.25 animations:^{
  260. self.frame = CGRectMake(0, kScreenH, self.width, self.height);
  261. self.shadowView.alpha = 0;
  262. } completion:^(BOOL finished) {
  263. [self removeFromSuperview];
  264. [self.shadowView removeFromSuperview];
  265. }];
  266. }
  267. - (void)setClickFAQBtnBlock:(clickFAQBtnBlock)clickFAQBtnBlock{
  268. _clickFAQBtnBlock = clickFAQBtnBlock;
  269. }
  270. - (void)setClickWardListBtnBlock:(clickWardListBtnBlock)clickWardListBtnBlock{
  271. _clickWardListBtnBlock = clickWardListBtnBlock;
  272. }
  273. - (void)setClickOpenViewBtnBlock:(clickOpenViewBtnBlock)clickOpenViewBtnBlock{
  274. _clickOpenViewBtnBlock = clickOpenViewBtnBlock;
  275. }
  276. - (void)setClickPrivilegeBtnBlock:(clickPrivilegeBtnBlock)clickPrivilegeBtnBlock{
  277. _clickPrivilegeBtnBlock = clickPrivilegeBtnBlock;
  278. }
  279. #pragma mark - Lazy Load
  280. - (UIView *)shadowView{
  281. if (!_shadowView) {
  282. _shadowView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScaleH)];
  283. _shadowView.backgroundColor = [kBlackColor colorWithAlphaComponent:0.3];
  284. _shadowView.alpha = 0;
  285. _shadowView.userInteractionEnabled = YES;
  286. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hide)];
  287. [_shadowView addGestureRecognizer:tap];
  288. }
  289. return _shadowView;
  290. }
  291. - (UIImageView *)iconImageView{
  292. if (!_iconImageView) {
  293. _iconImageView = [[UIImageView alloc]initWithFrame:CGRectMake((self.width - 60 ) / 2, 35, 60, 60)];
  294. _iconImageView.layer.cornerRadius = 30;
  295. _iconImageView.clipsToBounds = YES;
  296. _iconImageView.hidden = YES;
  297. }
  298. return _iconImageView;
  299. }
  300. - (UIImageView *)wardImageView{
  301. if (!_wardImageView) {
  302. _wardImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 15, 15)];
  303. _wardImageView.right = self.iconImageView.right;
  304. _wardImageView.top = self.iconImageView.top;
  305. _wardImageView.image = [UIImage imageNamed:@"lr_img_corner_ward_vip"];
  306. _wardImageView.hidden = YES;
  307. }
  308. return _wardImageView;
  309. }
  310. - (UILabel *)diamondLabel{
  311. if (!_diamondLabel) {
  312. _diamondLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, self.width - 10 - 10 - 80 - 10, 44)];
  313. [_diamondLabel setText:ASLocalizedString(@"我的钻石: 0")];
  314. [_diamondLabel setFont:[UIFont systemFontOfSize:15]];
  315. [_diamondLabel setTextColor:RGB(150, 150, 150)];
  316. }
  317. return _diamondLabel;
  318. }
  319. -(QMUIButton *)diamondBtn{
  320. if (!_diamondBtn) {
  321. _diamondBtn = [QMUIButton buttonWithType:UIButtonTypeCustom];
  322. [_diamondBtn setTitleColor:KMGMainBlueFontColor forState:UIControlStateNormal];
  323. // _diamondBtn settitl
  324. }
  325. return _diamondBtn;
  326. }
  327. - (UILabel *)timeLabel{
  328. if (!_timeLabel) {
  329. _timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.diamondLabel.right + 10, 0, self.width - 10 - 10 - 80 - 10 - (self.width - 10 - 10 - 80 - 10) - 20, 44)];
  330. [_timeLabel setFont:[UIFont systemFontOfSize:15]];
  331. [_timeLabel setTextColor:RGB(150, 150, 150)];
  332. [_timeLabel setTextAlignment:NSTextAlignmentRight];
  333. }
  334. return _timeLabel;
  335. }
  336. /*
  337. // Only override drawRect: if you perform custom drawing.
  338. // An empty implementation adversely affects performance during animation.
  339. - (void)drawRect:(CGRect)rect {
  340. // Drawing code
  341. }
  342. */
  343. @end