From 6d56f8495690d7f24fa8d52bc700f3f9598b2676 Mon Sep 17 00:00:00 2001 From: PH2 <1293456824@qq.com> Date: Wed, 16 Dec 2020 17:51:08 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E5=BE=AE=E4=BF=A1=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E3=80=81=E8=8B=B9=E6=9E=9C=E7=99=BB=E5=BD=95=E7=9A=84=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/android/app/build.gradle | 16 +++ example/android/app/demokey.keystore | Bin 0 -> 2240 bytes example/ios/Runner/Runner.entitlements | 10 ++ lib/models/base/base_tab_model.dart | 2 +- lib/models/base/skip_model.dart | 4 +- lib/models/user/user_info_model_notifier.dart | 2 + .../bloc/login_bind_phone_repository.dart | 15 +++ lib/pages/login_page/bloc/login_bloc.dart | 18 ++- lib/pages/login_page/bloc/login_event.dart | 14 ++- .../login_page/bloc/login_repository.dart | 50 +++++++- lib/pages/login_page/login_page.dart | 118 +++++++++++++++--- lib/util/mob_util/mob_util.dart | 9 ++ pubspec.yaml | 8 +- 13 files changed, 233 insertions(+), 33 deletions(-) create mode 100755 example/android/app/demokey.keystore create mode 100644 example/ios/Runner/Runner.entitlements 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 0000000000000000000000000000000000000000..e63ea63371fd7ba4570019bdee56f0d642514335 GIT binary patch literal 2240 zcmcgt`9IVP7oP8IjJ=eS>|_~fFxEoR9bDO$VVE))%4B4j>`RlSg_4~bDcQ6LQ$c+C@we~5)MECS@0nMfPtV?=+`TR_s!Dt{G@~@Eet0j zsT7}T*rJ=hg@wg@ThG7tOT-s{5%N#~`*xBpOX%+Lp2OvTU)lcP6zgv;j)#qqwDw5E z@gRywvFjrP^*iR#<9u!FvGRXlw1KbtsVv9WGW0`8{8%lbN1iQQi=W4mekV@QN-Bi2 zQXsP1MEggFaYj*f!}JaV-=wo|tOxsS#ioVWlYF@B5?&%YEAwr}FHQM2C3XRU+w(~e zAW&}p7$06|$6iu~bZok_|4P#5f%J5Zz|1CI_8@BU5nlqv?tSF8_o!oPCz@WBy}}0{ zS+%KRdUlD7k^WFHMpw3!{Xxe zw@MOVow=*D%)X)APt2{br3RHWkAt z%n}tm+2NjnP3MW0%AKd_D`VPv{y|3E*h+a#V>)=jB{C(W9?88#^V#j+5WTP;y@8j^ zM(Ji2q*7&b<1;$CtdnnL>?dpbX0iR!VQqN&**d7R8S{zjxtrCHK%Wh^Ud?Tkf{9J2iDtW*oeMGalY&EkTS zh0EJzda@FUzA8`_F*+to~NTA?8U)tcz) zXvxtwY0PjWn>e}Kn}76Ki2e;>Qx)t$o*^aAf8b+x77&;%IfqU28aV%Cc(FKCw1R#l z^l@RP)L8JdTMAW3127|rrhuV5S{NQo;##^ed@}k}k!bVv1f`Ls&7FNzgK_5>XkJQ> z*5lk|dT<+EWHwqD!)F9A?f%oOJx$nj%PI8^*c&t1$@`Un6OTWQZh}X5J;Or zT1M#m?Rq1idx*Zf3hdnaYR|M4Va93cw9rm)cSOs<3Tf#MX2#pkkRcx z#pFjhZNrFe3EnDm@=X?5PXSTq{)=R@LC4ZpIgcd~eVKrB&hobMG5f=Eq=-NBUz#OO z{b(V+=5vt&t%kY;&QHAi5-^ojmk<4eUB`Opu5)$Cg&HJAcF0BuDQ?!X?;1vT+9?P} z{QRoMJ+jtgPyy!DVSj2-;ASwBQZF=J6XEPUxt>c~>Zsz(ZeiqlSFEJWgz%UIii9EV zc+i=423b#*BkhRU4!=_7OXwwx^lKbRe7ZPS8uZ&U$ zfgm0zDujfhf-N#2U;qq;k~l7GqB!9Q*+~7!O-=w{KRB2~1$Gny4zYs5dBM8I3WrfV zzX>ZZLXScqyE=Q2UH{Qo{?-DndQcR&P#nMW*mz;yr@fs?|8VyI;gG*MO5_-)f)YyU zHRDk>7aR6*!Dgb~U7|14ON$IV4y4SGU^0u}6 zi)+Ses2_Fwyfqs>cr#%g67cgLaRlj#MbWxe#h(>Rrf?+wdZ=!_>afeTSzOh4GT!%g zRJRrlqF0hCC&AxbSlS!%`c{7u&Q5i(<*m$~HFNGX{l_l?WBMq00_twMG2z>4+bpA1?7VtQR7vS z3vT<^KZ$%VoMK+hoft7l`L~G&dvstT03y9B4HA}5tHegcGYAr@)4F=1Eb<|37H^_Y zADMT`EaH4vwN2nVQxUsli=PNySUbEvOl?Z5!?msy$}2kdC)T6y<#5(qM2qoE2pZXc zuPE?BcNq<{O_Fo^^*_@Z?#N_i`%krah{Y}9qTYiyVwl0q1pl4A2_EbAbK)hz!3E(- zJmR|T{$9(hy>)U?xMEfbjRg7wp=Avgh*aaokDAtG=lqr;Q@j&SV#io*;S!tAS3LKr zwO5@EEYjD!idOWw(-b&PV + + + + 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 ee1fa8e..714465e 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