You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
125 lines
4.8 KiB
125 lines
4.8 KiB
4 years ago
|
//
|
||
|
// TFWeChatPermissionsManager.m
|
||
|
// TFReader
|
||
|
//
|
||
|
// Created by 谢腾飞 on 2020/12/5.
|
||
|
// Copyright © 2020 xtfei_2011@126.com. All rights reserved.
|
||
|
//
|
||
|
|
||
|
#import "TFWeChatPermissionsManager.h"
|
||
|
#import <ShareSDK/ShareSDK.h>
|
||
|
#import "AppDelegate.h"
|
||
|
#import "TFURLProtocol.h"
|
||
|
|
||
|
@interface TFWeChatPermissionsManager ()
|
||
|
|
||
|
@property (nonatomic ,assign) TFWeChatPermissionsType permissionsType;
|
||
|
@end
|
||
|
|
||
|
@implementation TFWeChatPermissionsManager
|
||
|
|
||
|
implementation_singleton(TFWeChatPermissionsManager)
|
||
|
|
||
|
- (void)weChatPermissionsType:(TFWeChatPermissionsType)type
|
||
|
{
|
||
|
// 注册网络请求监听。
|
||
|
[TFURLProtocol startMonitor];
|
||
|
|
||
|
WS(weakSelf)
|
||
|
[ShareSDK authorize:SSDKPlatformTypeWechat settings:nil onStateChanged:^(SSDKResponseState state, SSDKUser *user, NSError *error) {
|
||
|
|
||
|
if (state == SSDKResponseStateSuccess) {
|
||
|
|
||
|
AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
|
||
|
NSString *code = delegate.wechatCode ?: @"";
|
||
|
|
||
|
if (code.length > 0) {
|
||
|
if (type == TFWeChatPermissionsTypeLogin) {
|
||
|
[TFPromptManager showPromptViewWithStatus:TFPromptStatusLoading promptTitle:TFLocalizedString(@"微信登录中")];
|
||
|
}
|
||
|
|
||
|
if (type == TFWeChatPermissionsTypeBinding) {
|
||
|
[TFPromptManager showPromptViewWithStatus:TFPromptStatusLoading promptTitle:TFLocalizedString(@"正在获取微信信息")];
|
||
|
}
|
||
|
} else {
|
||
|
[TFPromptManager showPromptViewWithStatus:TFPromptStatusError promptTitle:TFLocalizedString(@"微信登录失败")];
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (code && code.length > 0) {
|
||
|
|
||
|
NSDictionary *params = @{
|
||
|
@"code" : code
|
||
|
};
|
||
|
|
||
|
if (type == TFWeChatPermissionsTypeLogin) {
|
||
|
|
||
|
[TFNetworkTools POST:WeChat_Login parameters:params model:TFUserInfoManager.class success:^(BOOL isSuccess, TFUserInfoManager *_Nullable t_model, TFNetworkRequestModel *requestModel) {
|
||
|
[TFURLProtocol stopMonitor];
|
||
|
if (isSuccess) {
|
||
|
[weakSelf wechatRequestSuccess:t_model];
|
||
|
return;
|
||
|
}
|
||
|
[TFPromptManager showPromptViewWithStatus:TFPromptStatusError promptTitle:requestModel.msg];
|
||
|
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
|
||
|
[TFURLProtocol stopMonitor];
|
||
|
[weakSelf wechatRequestFail:TFLocalizedString(@"微信登录失败")];
|
||
|
}];
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (type == TFWeChatPermissionsTypeBinding) {
|
||
|
[TFNetworkTools POST:WeChat_Binding parameters:params model:TFUserInfoManager.class success:^(BOOL isSuccess, TFUserInfoManager *_Nullable t_model, TFNetworkRequestModel *requestModel) {
|
||
|
[TFURLProtocol stopMonitor];
|
||
|
if (isSuccess) {
|
||
|
[weakSelf wechatRequestSuccess:t_model];
|
||
|
} else {
|
||
|
[TFPromptManager showPromptViewWithStatus:TFPromptStatusError promptTitle:requestModel.msg];
|
||
|
}
|
||
|
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
|
||
|
[TFURLProtocol stopMonitor];
|
||
|
[weakSelf wechatRequestFail:TFLocalizedString(@"微信绑定失败")];
|
||
|
}];
|
||
|
return;
|
||
|
}
|
||
|
} else if (state == SSDKResponseStateCancel) {
|
||
|
TFLog(@"授权取消");
|
||
|
} else if (state == SSDKResponseStateFail) {
|
||
|
TFLog(@"=== 授权错误%@",error);
|
||
|
}
|
||
|
}
|
||
|
}];
|
||
|
|
||
|
[ShareSDK getUserInfo:SSDKPlatformTypeWechat onStateChanged:^(SSDKResponseState state, SSDKUser *user, NSError *error) {
|
||
|
if (state == SSDKResponseStateSuccess) {
|
||
|
|
||
|
TFLog(@"uid=%@",user.uid);
|
||
|
TFLog(@"%@",user.credential);
|
||
|
TFLog(@"token=%@",user.credential.token);
|
||
|
TFLog(@"nickname=%@",user.nickname);
|
||
|
} else {
|
||
|
TFLog(@"%@",error);
|
||
|
}
|
||
|
}];
|
||
|
}
|
||
|
|
||
|
+ (BOOL)isInstallWechat
|
||
|
{
|
||
|
return YES;//[WXApi isWXAppInstalled];
|
||
|
}
|
||
|
|
||
|
- (void)wechatRequestSuccess:(TFUserInfoManager *)userData
|
||
|
{
|
||
|
if (self.delegate && [self.delegate respondsToSelector:@selector(wechatResponseSuccess:)]) {
|
||
|
[self.delegate wechatResponseSuccess:userData];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
- (void)wechatRequestFail:(NSString *)error
|
||
|
{
|
||
|
if (self.delegate && [self.delegate respondsToSelector:@selector(wechatResponseFail:)]) {
|
||
|
[self.delegate wechatResponseFail:error ? : @""];
|
||
|
}
|
||
|
}
|
||
|
@end
|