JXAccountBindingVC.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. //
  2. // JXAccountBindingVC.m
  3. // shiku_im
  4. //
  5. // Created by 1 on 2019/3/14.
  6. // Copyright © 2019年 Reese. All rights reserved.
  7. //
  8. #import "JXAccountBindingVC.h"
  9. #import "WXApi.h"
  10. #import <TencentOpenAPI/TencentOAuth.h>
  11. #import <TencentOpenAPI/QQApiInterface.h>
  12. #define HEIGHT 50
  13. #define MY_INSET 0 // 每行左右间隙
  14. #define TOP_ADD_HEIGHT 400 // 顶部添加的高度,防止下拉顶部空白
  15. typedef NS_ENUM(NSInteger, JXBindType) {
  16. JXBindQQ = 1, // QQ绑定
  17. JXBindWX, // 微信绑定
  18. };
  19. @interface JXAccountBindingVC () <UIAlertViewDelegate,WXApiDelegate,WXApiManagerDelegate,TencentSessionDelegate,TencentLoginDelegate>
  20. @property (nonatomic, strong) UIButton *wxBindStatus;
  21. @property (nonatomic, strong) UIButton *qqBindStatus;
  22. @property (nonatomic, retain)TencentOAuth *oauth;
  23. @property (nonatomic, assign) JXBindType type;
  24. @end
  25. @implementation JXAccountBindingVC
  26. - (void)viewDidLoad {
  27. [super viewDidLoad];
  28. self.title = Localized(@"JX_AccountAndBindSettings");
  29. self.isGotoBack = YES;
  30. self.heightHeader = JX_SCREEN_TOP;
  31. self.heightFooter = 0;
  32. [self createHeadAndFoot];
  33. [self getServerData];
  34. [self setupViews];
  35. // 微信登录回调
  36. [WXApiManager sharedManager].delegate = self;
  37. [g_notify addObserver:self selector:@selector(authRespNotification:) name:kWxSendAuthRespNotification object:nil];
  38. }
  39. - (void)getServerData {
  40. [g_server getBindInfo:self];
  41. }
  42. - (void)setupViews {
  43. self.tableBody.backgroundColor = HEXCOLOR(0xF2F2F2);
  44. UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 120, 18)];
  45. title.text = Localized(@"JX_OtherLogin");
  46. title.font = SYSFONT(16);
  47. [self.tableBody addSubview:title];
  48. // 微信绑定
  49. JXImageView* wxIv = [self createButton:Localized(@"JX_WeChat") drawTop:YES drawBottom:YES icon:@"wechat_icon" click:@selector(bindWXAcount)];
  50. wxIv.frame = CGRectMake(MY_INSET,CGRectGetMaxY(title.frame)+20, JX_SCREEN_WIDTH, HEIGHT);
  51. self.wxBindStatus = [[UIButton alloc] initWithFrame:CGRectMake(JX_SCREEN_WIDTH-20-60, 16, 60, 20)];
  52. [self.wxBindStatus setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
  53. [self.wxBindStatus.titleLabel setFont:SYSFONT(15)];
  54. [self.wxBindStatus.titleLabel setTextAlignment:NSTextAlignmentCenter];
  55. [self.wxBindStatus setTitle:Localized(@"JX_Unbounded") forState:UIControlStateNormal];
  56. [self.wxBindStatus setTitle:Localized(@"JX_Binding") forState:UIControlStateSelected];
  57. [wxIv addSubview:self.wxBindStatus];
  58. //QQ绑定
  59. JXImageView* qqIv = [self createButton:@"QQ" drawTop:YES drawBottom:YES icon:@"qq_login" click:@selector(bindQQAcount)];
  60. qqIv.frame = CGRectMake(MY_INSET,CGRectGetMaxY(wxIv.frame)+20, JX_SCREEN_WIDTH, HEIGHT);
  61. self.qqBindStatus = [[UIButton alloc] initWithFrame:CGRectMake(JX_SCREEN_WIDTH-20-60, 16, 60, 20)];
  62. [self.qqBindStatus setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
  63. [self.qqBindStatus.titleLabel setFont:SYSFONT(15)];
  64. [self.qqBindStatus.titleLabel setTextAlignment:NSTextAlignmentCenter];
  65. [self.qqBindStatus setTitle:Localized(@"JX_Unbounded") forState:UIControlStateNormal];
  66. [self.qqBindStatus setTitle:Localized(@"JX_Binding") forState:UIControlStateSelected];
  67. [qqIv addSubview:self.qqBindStatus];
  68. }
  69. - (void)bindWXAcount {
  70. self.type = JXBindWX;
  71. if (self.wxBindStatus.selected) {
  72. [g_App showAlert:Localized(@"JX_UnbindWeChat?") delegate:self tag:1001 onlyConfirm:NO];
  73. }else {
  74. [g_App showAlert:Localized(@"JX_BindWeChat?") delegate:self tag:1002 onlyConfirm:NO];
  75. }
  76. }
  77. - (void)bindQQAcount {
  78. self.type = JXBindQQ;
  79. if (self.qqBindStatus.selected) {
  80. [g_App showAlert:@"是否确认解绑QQ" delegate:self tag:1003 onlyConfirm:NO];
  81. }else {
  82. [g_App showAlert:@"是否确认绑定QQ" delegate:self tag:1004 onlyConfirm:NO];
  83. }
  84. }
  85. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
  86. if (buttonIndex == 1) {
  87. if (alertView.tag == 1001) {
  88. [g_server setAccountUnbind:2 toView:self];
  89. }
  90. if (alertView.tag == 1002) {
  91. SendAuthReq* req = [[SendAuthReq alloc] init];
  92. req.scope = @"snsapi_userinfo"; // @"post_timeline,sns"
  93. req.state = @"login";
  94. req.openID = @"";
  95. [WXApi sendAuthReq:req
  96. viewController:self
  97. delegate:[WXApiManager sharedManager]];
  98. }
  99. if (alertView.tag == 1003) {
  100. [g_server setAccountUnbind:1 toView:self];
  101. }
  102. if (alertView.tag == 1004) {
  103. NSString *appid = g_App.QQ_LOGIN_APPID;
  104. _oauth = [[TencentOAuth alloc] initWithAppId:appid
  105. andDelegate:self];
  106. _oauth.authMode = kAuthModeClientSideToken;
  107. [_oauth authorize:[self getPermissions] inSafari:NO];
  108. }
  109. }
  110. }
  111. - (void)authRespNotification:(NSNotification *)notif {
  112. SendAuthResp *response = notif.object;
  113. NSString *strMsg = [NSString stringWithFormat:@"Auth结果 code:%@,state:%@,errcode:%d", response.code, response.state, response.errCode];
  114. NSLog(@"-------%@",strMsg);
  115. [g_server getWxOpenId:response.code toView:self];
  116. }
  117. // QQ登录成功回调
  118. - (void)tencentDidLogin {
  119. NSString *qqOpenId = _oauth.openId;
  120. g_server.openId = qqOpenId;
  121. if (qqOpenId.length > 0) {
  122. [self bindTel];
  123. }
  124. }
  125. - (NSMutableArray *)getPermissions {
  126. NSMutableArray * g_permissions = [[NSMutableArray alloc] initWithObjects:kOPEN_PERMISSION_GET_USER_INFO,
  127. kOPEN_PERMISSION_GET_SIMPLE_USER_INFO,
  128. kOPEN_PERMISSION_ADD_ALBUM,
  129. kOPEN_PERMISSION_ADD_TOPIC,
  130. kOPEN_PERMISSION_CHECK_PAGE_FANS,
  131. kOPEN_PERMISSION_GET_INFO,
  132. kOPEN_PERMISSION_GET_OTHER_INFO,
  133. kOPEN_PERMISSION_LIST_ALBUM,
  134. kOPEN_PERMISSION_UPLOAD_PIC,
  135. kOPEN_PERMISSION_GET_VIP_INFO,
  136. kOPEN_PERMISSION_GET_VIP_RICH_INFO, nil];
  137. return g_permissions;
  138. }
  139. - (void)bindTel {
  140. JXUserObject *user = [[JXUserObject alloc] init];
  141. if ([g_default objectForKey:kMY_USER_PASSWORD]) {
  142. user.password = [g_default objectForKey:kMY_USER_PASSWORD];
  143. }
  144. NSString *areaCode = [g_default objectForKey:kMY_USER_AREACODE];
  145. user.areaCode = areaCode.length > 0 ? areaCode : @"86";
  146. if ([g_default objectForKey:kMY_USER_LoginName]) {
  147. user.telephone = [g_default objectForKey:kMY_USER_LoginName];
  148. }
  149. // [g_server thirdLogin:user type:2 openId:g_server.openId isLogin:YES toView:self];
  150. [g_server userBindWXAccount:user type:self.type openId:g_server.openId isLogin:YES toView:self];
  151. }
  152. -(void) didServerResultSucces:(JXConnection*)aDownload dict:(NSDictionary*)dict array:(NSArray*)array1{
  153. [_wait stop];
  154. if([aDownload.action isEqualToString:act_unbind]){
  155. if (self.type == JXBindWX) {
  156. self.wxBindStatus.selected = NO;
  157. }else {
  158. self.qqBindStatus.selected = NO;
  159. }
  160. [g_server showMsg:Localized(@"JX_UnboundSuccessfully")];
  161. }
  162. if ([aDownload.action isEqualToString:act_UserBindWXAccount]) {
  163. g_server.openId = nil;
  164. if (self.type == JXBindWX) {
  165. self.wxBindStatus.selected = YES;
  166. }else {
  167. self.qqBindStatus.selected = YES;
  168. }
  169. [g_server showMsg:Localized(@"JX_BindingSuccessfully")];
  170. }
  171. if ([aDownload.action isEqualToString:act_GetWxOpenId]) {
  172. g_server.openId = [dict objectForKey:@"openid"];
  173. [self bindTel];
  174. }
  175. if( [aDownload.action isEqualToString:act_getBindInfo] ){
  176. if (array1.count > 0) {
  177. for (NSDictionary *dict in array1) {
  178. if ([[dict objectForKey:@"type"] intValue] == 2) {
  179. //微信绑定
  180. self.wxBindStatus.selected = YES;
  181. }
  182. if ([[dict objectForKey:@"type"] intValue] == 1) {
  183. //QQ绑定
  184. self.qqBindStatus.selected = YES;
  185. }
  186. }
  187. }
  188. }
  189. }
  190. -(int) didServerResultFailed:(JXConnection*)aDownload dict:(NSDictionary*)dict{
  191. [_wait stop];
  192. if ([aDownload.action isEqualToString:act_UserBindWXAccount]) {
  193. g_server.openId = nil;
  194. }
  195. return show_error;
  196. }
  197. -(int) didServerConnectError:(JXConnection*)aDownload error:(NSError *)error{//error为空时,代表超时
  198. [_wait stop];
  199. if ([aDownload.action isEqualToString:act_UserBindWXAccount]) {
  200. g_server.openId = nil;
  201. }
  202. return show_error;
  203. }
  204. -(void) didServerConnectStart:(JXConnection*)aDownload{
  205. [_wait start];
  206. }
  207. -(JXImageView*)createButton:(NSString*)title drawTop:(BOOL)drawTop drawBottom:(BOOL)drawBottom icon:(NSString*)icon click:(SEL)click{
  208. JXImageView* btn = [[JXImageView alloc] init];
  209. btn.backgroundColor = [UIColor whiteColor];
  210. btn.userInteractionEnabled = YES;
  211. btn.didTouch = click;
  212. btn.delegate = self;
  213. [self.tableBody addSubview:btn];
  214. JXLabel* p = [[JXLabel alloc] initWithFrame:CGRectMake(20*2+20, 0, self_width-35-20-5, HEIGHT)];
  215. p.text = title;
  216. p.font = g_factory.font16;
  217. p.backgroundColor = [UIColor clearColor];
  218. p.textColor = HEXCOLOR(0x323232);
  219. [btn addSubview:p];
  220. if(icon){
  221. UIImageView* iv = [[UIImageView alloc] initWithFrame:CGRectMake(20, (HEIGHT-20)/2, 21, 21)];
  222. iv.image = [UIImage imageNamed:icon];
  223. [btn addSubview:iv];
  224. }
  225. if(drawTop){
  226. UIView* line = [[UIView alloc] initWithFrame:CGRectMake(0,0,JX_SCREEN_WIDTH,LINE_WH)];
  227. line.backgroundColor = THE_LINE_COLOR;
  228. [btn addSubview:line];
  229. }
  230. if(drawBottom){
  231. UIView* line = [[UIView alloc] initWithFrame:CGRectMake(0,HEIGHT-0.3,JX_SCREEN_WIDTH,LINE_WH)];
  232. line.backgroundColor = THE_LINE_COLOR;
  233. [btn addSubview:line];
  234. }
  235. // if(click){
  236. // UIImageView* iv;
  237. // iv = [[UIImageView alloc] initWithFrame:CGRectMake(JX_SCREEN_WIDTH-INSETS-20-3-MY_INSET, 16, 20, 20)];
  238. // iv.image = [UIImage imageNamed:@"set_list_next"];
  239. // [btn addSubview:iv];
  240. //
  241. // }
  242. return btn;
  243. }
  244. @end