Kaynağa Gözat

1、微信登录、苹果登录的实现

tags/0.0.2+16
PH2 3 yıl önce
ebeveyn
işleme
6d56f84956
13 değiştirilmiş dosya ile 233 ekleme ve 33 silme
  1. +16
    -0
      example/android/app/build.gradle
  2. BIN
     
  3. +10
    -0
      example/ios/Runner/Runner.entitlements
  4. +1
    -1
      lib/models/base/base_tab_model.dart
  5. +2
    -2
      lib/models/base/skip_model.dart
  6. +2
    -0
      lib/models/user/user_info_model_notifier.dart
  7. +15
    -0
      lib/pages/login_page/bind/bloc/login_bind_phone_repository.dart
  8. +16
    -2
      lib/pages/login_page/bloc/login_bloc.dart
  9. +12
    -2
      lib/pages/login_page/bloc/login_event.dart
  10. +45
    -5
      lib/pages/login_page/bloc/login_repository.dart
  11. +100
    -18
      lib/pages/login_page/login_page.dart
  12. +9
    -0
      lib/util/mob_util/mob_util.dart
  13. +5
    -3
      pubspec.yaml

+ 16
- 0
example/android/app/build.gradle Dosyayı Görüntüle

@@ -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"



+ 10
- 0
example/ios/Runner/Runner.entitlements Dosyayı Görüntüle

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.developer.applesignin</key>
<array>
<string>Default</string>
</array>
</dict>
</plist>

+ 1
- 1
lib/models/base/base_tab_model.dart Dosyayı Görüntüle

@@ -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'];


+ 2
- 2
lib/models/base/skip_model.dart Dosyayı Görüntüle

@@ -16,8 +16,8 @@ class SkipModel {

void fromJson(Map<String, dynamic> 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'];
}



+ 2
- 0
lib/models/user/user_info_model_notifier.dart Dosyayı Görüntüle

@@ -77,6 +77,8 @@ class UserInfoNotifier with ChangeNotifier {
FlutterAlibc.loginOut();
// 退出QQ登录
MobUtil.cancelQQAuth();
// 退出微信登录
MobUtil.cancelWeChatAuth();
notifyListeners();
}



+ 15
- 0
lib/pages/login_page/bind/bloc/login_bind_phone_repository.dart Dosyayı Görüntüle

@@ -58,7 +58,22 @@ class LoginBindPhoneRepository {

/// 苹果登录
if(GlobalConfig.LOGIN_THIRD_APPLE == third){
// identityTokenStr
Map<String, String> appleParam = {};
appleParam['token'] = data['identityTokenStr'];
params['apple'] = appleParam;
}

/// 微信登录
if(GlobalConfig.LOGIN_THIRD_WECHAT == third){
Map<String, String> 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登录


+ 16
- 2
lib/pages/login_page/bloc/login_bloc.dart Dosyayı Görüntüle

@@ -73,7 +73,14 @@ class LoginBloc extends Bloc<LoginEvent, LoginState> {
}

/// 微信登录
Stream<LoginState> _mapLoginWeChatEventToState(LoginThirdWeChatEvent event) async* {}
Stream<LoginState> _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<LoginState> _mapLoginQQEventToState(LoginThirdQQEvent event) async* {
@@ -86,5 +93,12 @@ class LoginBloc extends Bloc<LoginEvent, LoginState> {
}

/// 苹果登录
Stream<LoginState> _mapLoginAppleEventToState(LoginThirdAppleEvent event) async* {}
Stream<LoginState> _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();
}
}
}

+ 12
- 2
lib/pages/login_page/bloc/login_event.dart Dosyayı Görüntüle

@@ -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<Object> get props => [this.identityToken];
}

/// 第三方登录:微信登录
class LoginThirdWeChatEvent extends LoginEvent {}
class LoginThirdWeChatEvent extends LoginEvent {
final Map<dynamic, dynamic> model;
LoginThirdWeChatEvent({@required this.model});
@override
List<Object> get props => [this.model];
}

/// 第三方登录:QQ登录
class LoginThirdQQEvent extends LoginEvent {


+ 45
- 5
lib/pages/login_page/bloc/login_repository.dart Dosyayı Görüntüle

@@ -16,6 +16,27 @@ class LoginRepository {
return await LoginStyleUtil.fetchCachePageData();
}

/// apple 登录
Future<UserInfoModel> appleLogin({@required final String identityToken}) async {
try{
Map<String, String> 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<UserInfoModel> 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<UserInfoModel> weChatLogin() async {
return null;
}
Future<UserInfoModel> weChatLogin(final Map<dynamic, dynamic> data) async {
try {
var dbInfo = jsonDecode(data['dbInfo']);
Map<String, String> 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<UserInfoModel> 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;
}



+ 100
- 18
lib/pages/login_page/login_page.dart Dosyayı Görüntüle

@@ -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<LoginPageContainer> {
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<LoginBloc>(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<LoginPageContainer> {
} 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<LoginBloc>(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<LoginPageContainer> {
Fluttertoast.showToast(msg: '暂不支持~');
return;
}

/// 淘宝登录
if ('taobao' == model.type) {
@@ -105,11 +151,14 @@ class _LoginPageContainerState extends State<LoginPageContainer> {
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<LoginBloc>(context).add(LoginThirdQQEvent(model: userdata));
Loading.dismiss();
try {
_qqUserData = jsonDecode(userdata['dbInfo']);
BlocProvider.of<LoginBloc>(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<LoginPageContainer> {

/// 苹果登录
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<LoginPageContainer> {

/// 跳到绑定手机号
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<LoginPageContainer> {
'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<LoginPageContainer> {
})));
}
}

/// 微信登录
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(),
})));
}
}
}

/// 跳到首页


+ 9
- 0
lib/util/mob_util/mob_util.dart Dosyayı Görüntüle

@@ -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()}');
});
}

///
/// 取消授权
///


+ 5
- 3
pubspec.yaml Dosyayı Görüntüle

@@ -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



Yükleniyor…
İptal
Kaydet