diff --git a/example/android/app/build.gradle b/example/android/app/build.gradle index 87ca655..7434614 100644 --- a/example/android/app/build.gradle +++ b/example/android/app/build.gradle @@ -52,10 +52,19 @@ MobSDK { bypassApproval false enable true } + Wechat { + appId "wx598aaef252cd78e4" + appSecret "d2fc2edf9ece746765b3bfc0bf1b230e" + withShareTicket true + bypassApproval false + enable true + } WechatMoments { + bypassApproval false enable true } WechatFavorite { + bypassApproval false enable true } QZone { @@ -78,6 +87,7 @@ android { defaultConfig { // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html). applicationId "cn.zhios.zhiying" +// applicationId "cn.sharesdk.demo" minSdkVersion 19 targetSdkVersion 28 aaptOptions.cruncherEnabled = false @@ -89,6 +99,12 @@ android { } signingConfigs{ + config { + keyAlias 'demokey.keystore' + keyPassword '123456' + storeFile file('demokey.keystore') + storePassword '123456' + } release { storeFile file("zhiying.jks") storePassword "zhioscom" diff --git a/example/android/app/demokey.keystore b/example/android/app/demokey.keystore new file mode 100755 index 0000000..e63ea63 Binary files /dev/null and b/example/android/app/demokey.keystore differ diff --git a/example/ios/Runner/Runner.entitlements b/example/ios/Runner/Runner.entitlements new file mode 100644 index 0000000..a812db5 --- /dev/null +++ b/example/ios/Runner/Runner.entitlements @@ -0,0 +1,10 @@ + + + + + com.apple.developer.applesignin + + Default + + + diff --git a/lib/models/base/base_tab_model.dart b/lib/models/base/base_tab_model.dart index 3b98063..09136e7 100644 --- a/lib/models/base/base_tab_model.dart +++ b/lib/models/base/base_tab_model.dart @@ -29,7 +29,7 @@ class BaseTabModel extends SkipModel { chooseIcon = json['choose_icon']; cateTag = json['cate_tag']; skipIdentifier = json['skip_identifier']; - requiredLogin = json['required_login']; + requiredLogin = json['required_login']?.toString(); requiredTaobaoAuth = json['required_taobao_auth']; bgColor = json['bg_color']; fontColor = json['font_color']; diff --git a/lib/models/base/skip_model.dart b/lib/models/base/skip_model.dart index d00d068..e60e8d2 100644 --- a/lib/models/base/skip_model.dart +++ b/lib/models/base/skip_model.dart @@ -16,8 +16,8 @@ class SkipModel { void fromJson(Map json) { url = json['url']; - requiredLogin = json['required_login']; - requiredTaobaoAuth = json['required_taobao_auth']; + requiredLogin = json['required_login']?.toString(); + requiredTaobaoAuth = json['required_taobao_auth']?.toString(); skipIdentifier = json['skip_identifier']; } diff --git a/lib/models/user/user_info_model_notifier.dart b/lib/models/user/user_info_model_notifier.dart index 95cb760..e5313ab 100644 --- a/lib/models/user/user_info_model_notifier.dart +++ b/lib/models/user/user_info_model_notifier.dart @@ -77,6 +77,8 @@ class UserInfoNotifier with ChangeNotifier { FlutterAlibc.loginOut(); // 退出QQ登录 MobUtil.cancelQQAuth(); + // 退出微信登录 + MobUtil.cancelWeChatAuth(); notifyListeners(); } diff --git a/lib/pages/login_page/bind/bloc/login_bind_phone_repository.dart b/lib/pages/login_page/bind/bloc/login_bind_phone_repository.dart index dcc092d..f33a8ab 100644 --- a/lib/pages/login_page/bind/bloc/login_bind_phone_repository.dart +++ b/lib/pages/login_page/bind/bloc/login_bind_phone_repository.dart @@ -58,7 +58,22 @@ class LoginBindPhoneRepository { /// 苹果登录 if(GlobalConfig.LOGIN_THIRD_APPLE == third){ + // identityTokenStr + Map appleParam = {}; + appleParam['token'] = data['identityTokenStr']; + params['apple'] = appleParam; + } + /// 微信登录 + if(GlobalConfig.LOGIN_THIRD_WECHAT == third){ + Map qqParam = {}; + qqParam['nickname'] = data['nickname']?.toString(); + qqParam['gender'] = data['gender']?.toString(); + qqParam['avatar_url'] = data['avatar_url']?.toString(); + qqParam['open_id'] = data['open_id']?.toString(); + qqParam['token'] = data['token']?.toString(); + qqParam['unionid'] = data['unionid']?.toString(); + params['wechat'] = qqParam; } /// QQ登录 diff --git a/lib/pages/login_page/bloc/login_bloc.dart b/lib/pages/login_page/bloc/login_bloc.dart index b1ba58c..2d2b607 100644 --- a/lib/pages/login_page/bloc/login_bloc.dart +++ b/lib/pages/login_page/bloc/login_bloc.dart @@ -73,7 +73,14 @@ class LoginBloc extends Bloc { } /// 微信登录 - Stream _mapLoginWeChatEventToState(LoginThirdWeChatEvent event) async* {} + Stream _mapLoginWeChatEventToState(LoginThirdWeChatEvent event) async* { + var result = await repository.weChatLogin(event?.model); + if(!EmptyUtil.isEmpty(result)){ + yield LoginThirdLoginSuccessState(model: result, thirdType: GlobalConfig.LOGIN_THIRD_WECHAT); + }else{ + yield LoginThirdLoginErrorState(); + } + } /// QQ 登录 Stream _mapLoginQQEventToState(LoginThirdQQEvent event) async* { @@ -86,5 +93,12 @@ class LoginBloc extends Bloc { } /// 苹果登录 - Stream _mapLoginAppleEventToState(LoginThirdAppleEvent event) async* {} + Stream _mapLoginAppleEventToState(LoginThirdAppleEvent event) async* { + var result = await repository.appleLogin(identityToken: event.identityToken); + if(!EmptyUtil.isEmpty(result)){ + yield LoginThirdLoginSuccessState(model: result, thirdType: GlobalConfig.LOGIN_THIRD_APPLE); + }else{ + yield LoginThirdLoginErrorState(); + } + } } diff --git a/lib/pages/login_page/bloc/login_event.dart b/lib/pages/login_page/bloc/login_event.dart index 0750294..ee32698 100644 --- a/lib/pages/login_page/bloc/login_event.dart +++ b/lib/pages/login_page/bloc/login_event.dart @@ -30,10 +30,20 @@ class LoginThirdAliEvent extends LoginEvent { } /// 第三方登录:苹果登录 -class LoginThirdAppleEvent extends LoginEvent {} +class LoginThirdAppleEvent extends LoginEvent { + final String identityToken; + LoginThirdAppleEvent({@required this.identityToken}); + @override + List get props => [this.identityToken]; +} /// 第三方登录:微信登录 -class LoginThirdWeChatEvent extends LoginEvent {} +class LoginThirdWeChatEvent extends LoginEvent { + final Map model; + LoginThirdWeChatEvent({@required this.model}); + @override + List get props => [this.model]; +} /// 第三方登录:QQ登录 class LoginThirdQQEvent extends LoginEvent { diff --git a/lib/pages/login_page/bloc/login_repository.dart b/lib/pages/login_page/bloc/login_repository.dart index 0874092..5ee5dc8 100644 --- a/lib/pages/login_page/bloc/login_repository.dart +++ b/lib/pages/login_page/bloc/login_repository.dart @@ -16,6 +16,27 @@ class LoginRepository { return await LoginStyleUtil.fetchCachePageData(); } + /// apple 登录 + Future appleLogin({@required final String identityToken}) async { + try{ + Map params = {}; + params['token'] = identityToken; + + var result = await NetUtil.post('/api/v1/sign/fast/in', + queryParameters: {'third_party_login': 'apple'}, + params: {'apple' : params}, + method: NetMethod.POST, + ); + if(NetUtil.isSuccess(result) && !EmptyUtil.isEmpty(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA])){ + return UserInfoModel.fromJson(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA]); + } + + }catch(e,s){ + Logger.error(e,s); + } + return null; + } + /// 淘宝登录 Future aliLogin( {@required final String nick, @required final String avatarUrl, @required final String openId, final String openSid, final String topAccessToken, final String topAuthCode}) async { @@ -42,13 +63,32 @@ class LoginRepository { return null; } + /// 微信登录 - Future weChatLogin() async { - return null; - } + Future weChatLogin(final Map data) async { + try { + var dbInfo = jsonDecode(data['dbInfo']); + Map params = {}; + params['nickname'] = dbInfo['nickname']?.toString(); + params['gender'] = dbInfo['gender']?.toString() == '0' ? '1' : '2'; + params['avatar_url'] = dbInfo['icon']?.toString(); + params['open_id'] = dbInfo['userID']?.toString(); + params['unionid'] = dbInfo['unionid']?.toString(); + params['token'] = dbInfo['token']?.toString(); - /// apple 登录 - Future appleLogin() async { + var result = await NetUtil.post( + '/api/v1/sign/fast/in', + params: {'wechat' :params}, + queryParameters: {'third_party_login': 'wechat'}, + method: NetMethod.POST, + ); + + if(NetUtil.isSuccess(result) && !EmptyUtil.isEmpty(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA])){ + return UserInfoModel.fromJson(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA]); + } + } catch (e, s) { + Logger.error(e, s); + } return null; } diff --git a/lib/pages/login_page/login_page.dart b/lib/pages/login_page/login_page.dart index 641b532..2de880b 100644 --- a/lib/pages/login_page/login_page.dart +++ b/lib/pages/login_page/login_page.dart @@ -1,4 +1,5 @@ import 'dart:convert'; +import 'dart:io'; import 'package:cached_network_image/cached_network_image.dart'; import 'package:flutter/cupertino.dart'; @@ -16,6 +17,7 @@ import 'package:zhiying_comm/util/empty_util.dart'; import 'package:zhiying_comm/util/mob_util/mob_util.dart'; import 'package:zhiying_comm/zhiying_comm.dart'; import 'package:provider/provider.dart'; +import 'package:ali_auth_wbq/ali_auth_wbq.dart'; import 'bloc/bloc.dart'; import 'bloc/login_repository.dart'; @@ -48,6 +50,42 @@ class LoginPageContainer extends StatefulWidget { class _LoginPageContainerState extends State { LoginModel _taoBao; var _qqUserData; + var _wxUserData; + String _appleData; + + @override + void initState() { + // 登录监听 + AliAuthPlugin.loginListen(type: false, onEvent: _onAppleLoginEvent); + super.initState(); + } + + // 返回授权信息 + // returnCode: 200成功 500失败 + // returnMsg + // user + // familyName //可能为 @"" + // givenName //可能为 @"" + // email //可能为 @"" + // identityTokenStr //需要使用的参数 + // authorizationCodeStr + void _onAppleLoginEvent(event) { + var stateCode = event['returnCode']; + String stateMsg = event['returnMsg']; + // _appleData = event['identityTokenStr']; + _appleData = event['user']; + Logger.debug('event == ' + event?.toString()); + if (stateCode == 200 && !EmptyUtil.isEmpty(_appleData)) { + Loading.dismiss(); + print("成功------$event"); + + BlocProvider.of(context).add(LoginThirdAppleEvent(identityToken: _appleData)); + } else { + Loading.dismiss(); + print("失败------$stateMsg"); + } + } + /// 微信or手机登陆 void _loginClick(String type, LoginStyleModel model) { print('登陆$type'); @@ -62,12 +100,21 @@ class _LoginPageContainerState extends State { } else if ('wechat' == type) { /// 微信登录 Loading.show(context); - SharesdkPlugin.getUserInfo(ShareSDKPlatforms.wechatSession, (SSDKResponseState state, Map user, SSDKError error) { - if(state == SSDKResponseState.Success){ - - } - }); - Fluttertoast.showToast(msg: '暂不支持~'); + SharesdkPlugin.getUserInfo(ShareSDKPlatforms.wechatSession, (SSDKResponseState state, Map userdata, SSDKError error) { + Logger.log('state = ${state?.toString()}, userInfo = ${userdata?.toString()}'); + if (state == SSDKResponseState.Success) { + try { + _wxUserData = jsonDecode(userdata['dbInfo']); + BlocProvider.of(context).add(LoginThirdWeChatEvent(model: userdata)); + Loading.dismiss(); + } catch (e) { + Fluttertoast.showToast(msg: '登录失败'); + Loading.dismiss(); + } + } else { + Loading.dismiss(); + } + }); } } @@ -78,7 +125,6 @@ class _LoginPageContainerState extends State { Fluttertoast.showToast(msg: '暂不支持~'); return; } - /// 淘宝登录 if ('taobao' == model.type) { @@ -105,11 +151,14 @@ class _LoginPageContainerState extends State { SharesdkPlugin.getUserInfo(ShareSDKPlatforms.qq, (SSDKResponseState state, Map userdata, SSDKError error) { Logger.log('state = ${state?.toString()}\nuserData = ${userdata?.toString()}'); if (state == SSDKResponseState.Success && !EmptyUtil.isEmpty(userdata)) { - try{ - _qqUserData = jsonDecode(userdata['dbInfo']);; - }catch(e){} - BlocProvider.of(context).add(LoginThirdQQEvent(model: userdata)); - Loading.dismiss(); + try { + _qqUserData = jsonDecode(userdata['dbInfo']); + BlocProvider.of(context).add(LoginThirdQQEvent(model: userdata)); + Loading.dismiss(); + } catch (e) { + Fluttertoast.showToast(msg: '登录失败'); + Loading.dismiss(); + } } else { Loading.dismiss(); } @@ -119,10 +168,14 @@ class _LoginPageContainerState extends State { /// 苹果登录 if ('apple' == model.type) { - Fluttertoast.showToast(msg: '暂不支持~'); + if (Platform.isIOS) { + Loading.show(context); + await AliAuthPlugin.appleLogin; + // final appleLogin = await AliAuthPlugin.appleLogin; + } else { + Fluttertoast.showToast(msg: '暂不支持~'); + } } - - } /// 跳到用户协议 @@ -135,6 +188,17 @@ class _LoginPageContainerState extends State { /// 跳到绑定手机号 void _jumpBindPhonePage(String thirdType) { + /// 如果是苹果登录 + if (GlobalConfig.LOGIN_THIRD_APPLE == thirdType) { + Navigator.push( + context, + CupertinoPageRoute( + builder: (_) => LoginBindPhonePage({ + 'thirdType': thirdType, + 'identityTokenStr': _appleData, + }))); + } + /// 如果是淘宝登录 if (GlobalConfig.LOGIN_THIRD_ALI == thirdType) { Navigator.push( @@ -150,14 +214,14 @@ class _LoginPageContainerState extends State { 'topAuthCode': _taoBao?.data?.topAuthCode, }))); } + /// 如果是QQ登录 - if(GlobalConfig.LOGIN_THIRD_QQ == thirdType) { + if (GlobalConfig.LOGIN_THIRD_QQ == thirdType) { if (!EmptyUtil.isEmpty(_qqUserData)) { Navigator.push( context, CupertinoPageRoute( - builder: (_) => - LoginBindPhonePage({ + builder: (_) => LoginBindPhonePage({ 'thirdType': thirdType, 'nickname': _qqUserData['nickname']?.toString(), 'avatar_url': _qqUserData['icon']?.toString(), @@ -168,6 +232,24 @@ class _LoginPageContainerState extends State { }))); } } + + /// 微信登录 + if (GlobalConfig.LOGIN_THIRD_WECHAT == thirdType) { + if (!EmptyUtil.isEmpty(_wxUserData)) { + Navigator.push( + context, + CupertinoPageRoute( + builder: (_) => LoginBindPhonePage({ + 'thirdType': thirdType, + 'nickname': _wxUserData['nickname']?.toString(), + 'avatar_url': _wxUserData['icon']?.toString(), + 'open_id': _wxUserData['userID']?.toString(), + 'gender': _wxUserData['gender']?.toString() == '0' ? '1' : '2', + 'unionid': _wxUserData['unionid']?.toString(), + 'token': _wxUserData['token']?.toString(), + }))); + } + } } /// 跳到首页 diff --git a/lib/util/mob_util/mob_util.dart b/lib/util/mob_util/mob_util.dart index a866bbc..d5b15b1 100644 --- a/lib/util/mob_util/mob_util.dart +++ b/lib/util/mob_util/mob_util.dart @@ -33,6 +33,15 @@ class MobUtil { }); } + /// + /// 退出微信登录 + /// + static void cancelWeChatAuth(){ + SharesdkPlugin.cancelAuth(ShareSDKPlatforms.wechatSession, (SSDKResponseState state, Map user, SSDKError error) { + Logger.debug('state = ${state}\n rawData = ${error?.rawData?.toString()}'); + }); + } + /// /// 取消授权 /// diff --git a/pubspec.yaml b/pubspec.yaml index 903d8a0..8dff1d7 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -56,9 +56,11 @@ dependencies: # 锁粉 moblink: ^3.4.1 # 苹果登录 -# sign_in_with_apple: ^2.5.4 - # 微信登录 -# fluwx_no_pay: 2.3.1 + ali_auth_wbq: +# path: ../zhiying_ali_auth_wbq + git: + url: 'http://192.168.0.138:3000/FnuoOS_ZhiYing/zhiying_ali_auth_wbq.git' + ref: '0.0.5' # loading 动画 loading_indicator: ^1.2.0 #权限授权