// // KBUserInfoView.m // YouHuiProject // // Created by 小花 on 2018/5/22. // Copyright © 2018年 kuxuan. All rights reserved. // #import "KBUserInfoView.h" @interface KBUserInfoView() @property (nonatomic, strong) UIImageView *iconView; //用户头像 @property (nonatomic, strong) UILabel *nickName; //用户昵称 @property (nonatomic, strong) UILabel *inviteCode; //邀请码 @property (nonatomic, strong) UIImageView *userTypeIcon; //用户类型图标 @property (nonatomic, strong) UIButton *inviteCopyBtn;//复制按钮 @property (nonatomic, strong) UILabel *unLonginLabel;//未登录文字 @property (nonatomic, strong) UILabel *unLoginDes;//未登录描述 @property (nonatomic, strong) UILabel *vipGuideLabel; @end @implementation KBUserInfoView - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self initSubViews]; } return self; } - (void)initSubViews { [self addSubview:self.iconView]; [self addSubview:self.nickName]; [self addSubview:self.userTypeIcon]; [self addSubview:self.inviteCode]; [self addSubview:self.inviteCopyBtn]; [self addSubview:self.unLonginLabel]; [self addSubview:self.unLoginDes]; [self addSubview:self.vipGuideLabel]; [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_offset(28); make.centerY.mas_equalTo(self); make.width.height.mas_equalTo(60); }]; [self.unLonginLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.iconView.mas_right).mas_offset(14); make.top.mas_equalTo(self.iconView.mas_top).mas_offset(10); }]; [self.unLoginDes mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.unLonginLabel); make.bottom.mas_equalTo(self.iconView.mas_bottom).mas_offset(-10); }]; [self.nickName mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(self.iconView.mas_top).mas_offset(8); make.left.mas_equalTo(self.iconView.mas_right).mas_offset(14); }]; [self.inviteCode mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.nickName); make.top.mas_equalTo(self.nickName.mas_bottom).mas_offset(10); }]; [self.vipGuideLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.nickName); make.width.mas_equalTo(250); make.top.mas_equalTo(self.nickName.mas_bottom).mas_offset(10); }]; [self.userTypeIcon mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.nickName.mas_right).mas_offset(10); make.centerY.mas_equalTo(self.nickName); make.width.height.mas_equalTo(20); }]; [self.inviteCopyBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.inviteCode.mas_right).mas_offset(10); make.centerY.mas_equalTo(self.inviteCode); make.width.mas_equalTo(34); make.height.mas_equalTo(14); }]; self.inviteCode.hidden = YES; self.inviteCopyBtn.hidden = YES; self.vipGuideLabel.hidden = YES; [self setIsLogInView]; } - (void)setUserInfo:(KBUserInfo *)userInfo { _userInfo = userInfo; [self setIsLogInView]; self.nickName.text = userInfo.name; if (userInfo.name.length > 10) { [self.nickName mas_updateConstraints:^(MASConstraintMaker *make) { make.width.mas_equalTo(150); }]; } self.inviteCode.text = [NSString stringWithFormat:@"邀请码:%@",userInfo.invite_code]; NSString *imgStr; switch ([userInfo.user_level integerValue]) { case 1: { imgStr = @"vip_icon"; } break; case 2: { imgStr = @"svip_icon"; } break; case 3: { imgStr = @"operator_icon"; } break; default: break; } UIImage *image = [UIImage imageNamed:imgStr]; self.userTypeIcon.image = image; [self.iconView sd_setImageWithURL:[NSURL URLWithString:userInfo.img] placeholderImage:[UIImage imageNamed:@"login"]]; [self.userTypeIcon mas_updateConstraints:^(MASConstraintMaker *make) { make.width.mas_equalTo(image.size.width); make.height.mas_equalTo(image.size.height); }]; BOOL showVipGuideLabel = [userInfo.user_level isEqualToString:@"1"]; self.vipGuideLabel.hidden = !showVipGuideLabel; self.inviteCode.hidden = showVipGuideLabel; self.inviteCopyBtn.hidden = showVipGuideLabel; } - (void)setIsLogInView { self.vipGuideLabel.hidden = YES; BOOL isLogin = [AccountTool isLogin]; self.unLonginLabel.hidden = isLogin; self.unLoginDes.hidden = isLogin; self.nickName.hidden = !isLogin; self.userTypeIcon.hidden = !isLogin; // self.vipGuideLabel.hidden = !isLogin; self.inviteCode.hidden = !isLogin; self.inviteCopyBtn.hidden = !isLogin; if (!isLogin) self.iconView.image = [UIImage imageNamed:@"unlogin"]; } #pragma mark ---- /** 点击登录 */ - (void)goToLoginPage { if (self.loginClick) { self.loginClick(); } } - (void)settingAction { if (self.settingClick) { self.settingClick(); } } - (void)tapTypeIcon { if (self.tapUserType) { self.tapUserType(); } } - (void)copyCodeAction { UIPasteboard * pastboard = [UIPasteboard generalPasteboard]; if (_userInfo.invite_code) { pastboard.string = _userInfo.invite_code; [MBProgressHUD showMessage:@"复制成功"]; }else { [MBProgressHUD showMessage:@"复制失败"]; } } - (UIImageView *)iconView { if (!_iconView) { _iconView = [[UIImageView alloc] init]; _iconView.layer.cornerRadius = 30; _iconView.layer.masksToBounds = YES; NSString *imgStr = [AccountTool isLogin] ? @"login" : @"unlogin"; _iconView.image = [UIImage imageNamed:imgStr]; _iconView.userInteractionEnabled = YES; _iconView.layer.borderWidth = 2; _iconView.layer.borderColor = [UIColor whiteColor].CGColor; UITapGestureRecognizer *settingTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(settingAction)]; [_iconView addGestureRecognizer:settingTap]; } return _iconView; } - (UILabel *)nickName { if (!_nickName) { _nickName = [[UILabel alloc] init]; _nickName.font = [UIFont systemFontOfSize:16]; _nickName.textColor = [UIColor whiteColor]; _nickName.userInteractionEnabled = YES; _nickName.text = @"省钱小达人"; UITapGestureRecognizer *settingTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(settingAction)]; [_nickName addGestureRecognizer:settingTap]; } return _nickName; } - (UIImageView *)userTypeIcon { if (!_userTypeIcon) { _userTypeIcon = [[UIImageView alloc] init]; _userTypeIcon.userInteractionEnabled = YES; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTypeIcon)]; [_userTypeIcon addGestureRecognizer:tap]; } return _userTypeIcon; } - (UILabel *)inviteCode { if (!_inviteCode) { _inviteCode = [[UILabel alloc] init]; _inviteCode.font = [UIFont systemFontOfSize:14]; _inviteCode.textColor = [UIColor whiteColor]; _inviteCode.text = @"邀请码:"; } return _inviteCode; } - (UIButton *)inviteCopyBtn { if (!_inviteCopyBtn) { _inviteCopyBtn = [UIButton buttonWithType:UIButtonTypeCustom]; _inviteCopyBtn.layer.cornerRadius = 7; _inviteCopyBtn.layer.borderColor = [UIColor whiteColor].CGColor; _inviteCopyBtn.layer.borderWidth = 1; _inviteCopyBtn.titleLabel.font = [UIFont systemFontOfSize:11]; [_inviteCopyBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [_inviteCopyBtn setTitle:@"复制" forState:UIControlStateNormal]; [_inviteCopyBtn addTarget:self action:@selector(copyCodeAction) forControlEvents:UIControlEventTouchUpInside]; } return _inviteCopyBtn; } - (UILabel *)unLonginLabel { if (!_unLonginLabel) { _unLonginLabel = [[UILabel alloc] init]; _unLonginLabel.text = @"点击登录"; _unLonginLabel.textColor = [UIColor whiteColor]; _unLonginLabel.font = [UIFont systemFontOfSize:15]; _unLonginLabel.userInteractionEnabled = YES; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(goToLoginPage)]; [_unLonginLabel addGestureRecognizer:tap]; } return _unLonginLabel; } - (UILabel *)unLoginDes { if (!_unLoginDes) { _unLoginDes = [[UILabel alloc] init]; _unLoginDes.text = @"登录后可以领取优惠券哦"; _unLoginDes.textColor = [UIColor whiteColor]; _unLoginDes.font = [UIFont systemFontOfSize:13]; _unLoginDes.userInteractionEnabled = YES; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(goToLoginPage)]; [_unLoginDes addGestureRecognizer:tap]; } return _unLoginDes; } - (UILabel *)vipGuideLabel { if (!_vipGuideLabel) { _vipGuideLabel = [[UILabel alloc] init]; NSString *text = @"成为超级会员购物领佣金"; _vipGuideLabel.text = text; _vipGuideLabel.textColor = [UIColor whiteColor]; _vipGuideLabel.font = [UIFont systemFontOfSize:13]; _vipGuideLabel.userInteractionEnabled = YES; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTypeIcon)]; [_vipGuideLabel addGestureRecognizer:tap]; UIButton *right = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 34, 14)]; right.layer.cornerRadius = 7; right.layer.borderColor = [UIColor whiteColor].CGColor; right.layer.borderWidth = 1; right.titleLabel.font = [UIFont systemFontOfSize:11]; [right setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [right setTitle:@"升级" forState:UIControlStateNormal]; CGSize size = [PublicFunction getAutoWidthWith:text andSize:CGSizeMake(MAXFLOAT, 20) andFont:13]; right.left = size.width+5; right.centerY = 8; [right addTarget:self action:@selector(tapTypeIcon) forControlEvents:UIControlEventTouchUpInside]; [_vipGuideLabel addSubview:right]; _vipGuideLabel.hidden = YES; } return _vipGuideLabel; } @end