356 lines
14 KiB
Dart
356 lines
14 KiB
Dart
import 'dart:io';
|
||
|
||
import 'package:flutter/material.dart';
|
||
import 'package:flutter_screenutil/flutter_screenutil.dart';
|
||
import 'package:get/get.dart';
|
||
import 'package:jverify/jverify.dart';
|
||
import 'package:star_lock/app_settings/app_colors.dart';
|
||
|
||
import '../app_settings/app_settings.dart';
|
||
import '../common/XSConstantMacro/XSConstantMacro.dart';
|
||
import '../flavors.dart';
|
||
|
||
class JverifyOneClickLoginManage {
|
||
factory JverifyOneClickLoginManage() => shareManager()!;
|
||
|
||
JverifyOneClickLoginManage._init() {
|
||
_initSDK();
|
||
}
|
||
|
||
static JverifyOneClickLoginManage? _manager;
|
||
|
||
static JverifyOneClickLoginManage? shareManager() {
|
||
_manager ??= JverifyOneClickLoginManage._init();
|
||
return _manager;
|
||
}
|
||
|
||
JverifyOneClickLoginManage? get manager => shareManager();
|
||
Jverify jverify = Jverify();
|
||
|
||
/// 统一 key
|
||
String f_result_key = 'result';
|
||
|
||
/// 错误码
|
||
String f_code_key = 'code';
|
||
|
||
/// 回调的提示信息,统一返回 flutter 为 message
|
||
String f_msg_key = 'message';
|
||
|
||
/// 运营商信息
|
||
String f_opr_key = 'operator';
|
||
|
||
Future<void> _initSDK() async {
|
||
try {
|
||
await initPlatformState();
|
||
|
||
isInitSuccess();
|
||
|
||
getToken();
|
||
|
||
preLogin();
|
||
} catch (e) {
|
||
AppLog.log('SDK 初始化错误: $e');
|
||
}
|
||
}
|
||
|
||
Future<void> initPlatformState() async {
|
||
// 初始化 SDK 之前添加监听
|
||
jverify.addSDKSetupCallBackListener((JVSDKSetupEvent event) {
|
||
print('receive sdk setup call back event :${event.toMap()}');
|
||
});
|
||
|
||
jverify.setDebugMode(true); // 打开调试模式
|
||
jverify.setCollectionAuth(true);
|
||
String appKey;
|
||
if (F.isSKY) {
|
||
appKey = '7ff37d174c1a568a89e98dad';
|
||
AppLog.log('appKey:7ff37d174c1a568a89e98dad');
|
||
} else {
|
||
appKey = '251fc8074820d122b6de58d2';
|
||
AppLog.log('appKey:251fc8074820d122b6de58d2');
|
||
}
|
||
jverify.setup(
|
||
appKey: appKey, //"你自己应用的 AppKey",
|
||
channel: 'devloper');
|
||
|
||
/// 授权页面点击时间监听
|
||
jverify.addAuthPageEventListener((JVAuthPageEvent event) {
|
||
print('receive auth page event :${event.toMap()}');
|
||
});
|
||
}
|
||
|
||
/// sdk 初始化是否完成
|
||
void isInitSuccess() {
|
||
jverify.isInitSuccess().then((map) {
|
||
final bool result = map[f_result_key];
|
||
AppLog.log('sdk 初始化结果:$map');
|
||
if (result) {
|
||
AppLog.log('sdk 初始化成功');
|
||
} else {
|
||
AppLog.log('sdk 初始化失败');
|
||
}
|
||
});
|
||
}
|
||
|
||
/// 判断当前网络环境是否可以发起认证
|
||
Future<bool> checkVerifyEnable() async {
|
||
final Map map = await jverify.checkVerifyEnable();
|
||
AppLog.log('一家登录 sdk 初始化结果:$map');
|
||
final bool result = map[f_result_key];
|
||
return result;
|
||
// state.jverify.checkVerifyEnable().then((map) {
|
||
// final bool result = map[f_result_key];
|
||
// if (result) {
|
||
// AppLog.log('当前网络环境【支持认证】!');
|
||
// } else {
|
||
// AppLog.log('当前网络环境【不支持认证】!');
|
||
// }
|
||
// return result;
|
||
// });
|
||
}
|
||
|
||
void getToken() {
|
||
jverify.checkVerifyEnable().then((map) {
|
||
final bool result = map[f_result_key];
|
||
if (result) {
|
||
jverify.getToken().then((map) {
|
||
final int code = map[f_code_key];
|
||
final String token = map[f_msg_key];
|
||
final String operator = map[f_opr_key];
|
||
AppLog.log('getToken code:$code token:$token operator:$operator');
|
||
});
|
||
} else {
|
||
AppLog.log('[2016],msg = 当前网络环境不支持认证');
|
||
}
|
||
});
|
||
}
|
||
|
||
/// 登录预取号
|
||
void preLogin() {
|
||
jverify.checkVerifyEnable().then((map) {
|
||
final bool result = map[f_result_key];
|
||
if (result) {
|
||
jverify.preLogin().then((map) {
|
||
AppLog.log('预取号接口回调:${map.toString()}');
|
||
final int code = map[f_code_key];
|
||
final String message = map[f_msg_key];
|
||
});
|
||
} else {
|
||
AppLog.log('[2016],msg = 当前网络环境不支持认证');
|
||
}
|
||
});
|
||
}
|
||
|
||
/// SDK 请求授权一键登录
|
||
Future<void> loginAuth(BuildContext context,
|
||
Function(JVListenerEvent jvListenerEvent) action) async {
|
||
Map map = await jverify.checkVerifyEnable();
|
||
final bool result = map[f_result_key];
|
||
print('checkVerifyEnable $map');
|
||
//需要使用sms的时候不检查result
|
||
// if (result) {
|
||
// return await jverify.loginAuth(true);
|
||
// }
|
||
// return null;
|
||
|
||
if (result) {
|
||
final screenSize = MediaQuery.of(context).size;
|
||
final screenWidth = screenSize.width;
|
||
final screenHeight = screenSize.height;
|
||
bool isiOS = Platform.isIOS;
|
||
|
||
/// 自定义授权的 UI 界面,以下设置的图片必须添加到资源文件里,
|
||
/// android项目将图片存放至drawable文件夹下,可使用图片选择器的文件名,例如:btn_login.xml,入参为"btn_login"。
|
||
/// ios项目存放在 Assets.xcassets。
|
||
JVUIConfig uiConfig = JVUIConfig();
|
||
// uiConfig.authBGGifPath = "main_gif";
|
||
// uiConfig.authBGVideoPath="main_vi";
|
||
// uiConfig.authBGVideoPath = 'http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4';
|
||
// uiConfig.authBackgroundImage = 'images/icon_left_grey.png';
|
||
|
||
// uiConfig.navHidden = !isiOS;
|
||
uiConfig.navColor = AppColors.mainColor.value;
|
||
uiConfig.navText = '一键登录'.tr;
|
||
uiConfig.navTextColor = Colors.white.value;
|
||
uiConfig.navReturnImgPath = 'icon_left_grey'; //图片必须存在
|
||
|
||
uiConfig.logoWidth = 100;
|
||
uiConfig.logoHeight = 100;
|
||
//uiConfig.logoOffsetX = isiOS ? 0 : null;//(screenWidth/2 - uiConfig.logoWidth/2).toInt();
|
||
uiConfig.logoOffsetY = 100;
|
||
uiConfig.logoVerticalLayoutItem = JVIOSLayoutItem.ItemSuper;
|
||
uiConfig.logoHidden = false;
|
||
uiConfig.logoImgPath = 'logo';
|
||
|
||
uiConfig.numberFieldWidth = 200;
|
||
uiConfig.numberFieldHeight = 40;
|
||
//uiConfig.numFieldOffsetX = isiOS ? 0 : null;//(screenWidth/2 - uiConfig.numberFieldWidth/2).toInt();
|
||
uiConfig.numFieldOffsetY = isiOS ? 40 : 220;
|
||
uiConfig.numberVerticalLayoutItem = JVIOSLayoutItem.ItemLogo;
|
||
uiConfig.numberColor = AppColors.mainColor.value;
|
||
uiConfig.numberSize = 18;
|
||
|
||
uiConfig.sloganOffsetY = isiOS ? 10 : 300;
|
||
uiConfig.sloganVerticalLayoutItem = JVIOSLayoutItem.ItemNumber;
|
||
uiConfig.sloganTextColor = Colors.black.value;
|
||
uiConfig.sloganTextSize = 15;
|
||
// uiConfig.slogan
|
||
//uiConfig.sloganHidden = 0;
|
||
|
||
// uiConfig.logBtnOffsetX = 20;
|
||
uiConfig.logBtnWidth = double.parse('${1.sw - 20 * 2}').toInt();
|
||
uiConfig.logBtnHeight = 50;
|
||
//uiConfig.logBtnOffsetX = isiOS ? 0 : null;//(screenWidth/2 - uiConfig.logBtnWidth/2).toInt();
|
||
uiConfig.logBtnOffsetY = isiOS ? 30 : 330;
|
||
uiConfig.logBtnVerticalLayoutItem = JVIOSLayoutItem.ItemSlogan;
|
||
uiConfig.logBtnText = '一键登录'.tr;
|
||
uiConfig.logBtnTextColor = AppColors.mainColor.value;
|
||
uiConfig.logBtnTextSize = 16;
|
||
if (isiOS) {
|
||
uiConfig.loginBtnNormalImage =
|
||
'${F.loginBtnImagePrefix}_login_btn_normal'; //图片必须存在
|
||
uiConfig.loginBtnPressedImage =
|
||
'${F.loginBtnImagePrefix}_login_btn_press'; //图片必须存在
|
||
uiConfig.loginBtnUnableImage =
|
||
'${F.loginBtnImagePrefix}_login_btn_normal'; //图片必须存在
|
||
uiConfig.logBtnBackgroundPath =
|
||
'${F.loginBtnImagePrefix}_login_btn_normal';
|
||
}
|
||
// uiConfig.logBtnTextBold = true;
|
||
|
||
uiConfig.privacyHintToast =
|
||
true; //only android 设置隐私条款不选中时点击登录按钮默认显示toast。
|
||
|
||
uiConfig.privacyState = false; //设置默认勾选
|
||
uiConfig.privacyCheckboxSize = 22;
|
||
if (isiOS) {
|
||
uiConfig.checkedImgPath = 'check'; //图片必须存在
|
||
uiConfig.uncheckedImgPath = 'uncheck'; //图片必须存在
|
||
}
|
||
|
||
uiConfig.privacyCheckboxInCenter = true;
|
||
uiConfig.privacyCheckboxHidden = false;
|
||
uiConfig.isAlertPrivacyVc = false;
|
||
|
||
//uiConfig.privacyOffsetX = isiOS ? (20 + uiConfig.privacyCheckboxSize) : null;
|
||
uiConfig.privacyNavReturnBtnImage = 'icon_left_grey'; //图片必须存在;
|
||
uiConfig.privacyOffsetY = 30; // 距离底部距离
|
||
uiConfig.privacyOffsetX = 15; // 距离底部距离
|
||
uiConfig.privacyVerticalLayoutItem = JVIOSLayoutItem.ItemSuper;
|
||
uiConfig.clauseName = '协议1';
|
||
uiConfig.clauseUrl = 'http://www.baidu.com';
|
||
uiConfig.clauseBaseColor = Colors.black.value;
|
||
uiConfig.clauseNameTwo = '协议二';
|
||
uiConfig.clauseUrlTwo = 'http://www.hao123.com';
|
||
uiConfig.clauseColor = AppColors.mainColor.value;
|
||
uiConfig.privacyText = ['我已阅读并同意'.tr];
|
||
uiConfig.privacyTextSize = 13;
|
||
uiConfig.privacyItem = [
|
||
JVPrivacy('用户协议'.tr, XSConstantMacro.userAgreementURL,
|
||
beforeName: '', afterName: '', separator: ''),
|
||
JVPrivacy('隐私政策'.tr, XSConstantMacro.privacyPolicyURL, separator: '')
|
||
];
|
||
uiConfig.textVerAlignment = 1;
|
||
//uiConfig.privacyWithBookTitleMark = true;
|
||
//uiConfig.privacyTextCenterGravity = false;
|
||
uiConfig.authStatusBarStyle = JVIOSBarStyle.StatusBarStyleDarkContent;
|
||
uiConfig.privacyStatusBarStyle = JVIOSBarStyle.StatusBarStyleDefault;
|
||
uiConfig.modelTransitionStyle = JVIOSUIModalTransitionStyle.CrossDissolve;
|
||
|
||
uiConfig.statusBarColorWithNav = true;
|
||
// uiConfig.virtualButtonTransparent = true;
|
||
|
||
uiConfig.privacyStatusBarColorWithNav = true;
|
||
uiConfig.privacyVirtualButtonTransparent = true;
|
||
|
||
uiConfig.needStartAnim = true;
|
||
uiConfig.needCloseAnim = true;
|
||
uiConfig.enterAnim = 'activity_slide_enter_bottom';
|
||
uiConfig.exitAnim = 'activity_slide_exit_bottom';
|
||
|
||
uiConfig.privacyNavColor = Colors.white.value;
|
||
uiConfig.privacyNavTitleTextColor = Colors.white.value;
|
||
uiConfig.privacyNavTitleTextSize = 16;
|
||
|
||
//协议二次弹窗内容设置 -Android
|
||
// final JVPrivacyCheckDialogConfig privacyCheckDialogConfig =
|
||
// JVPrivacyCheckDialogConfig();
|
||
// privacyCheckDialogConfig.width = 300; //协议⼆次弹窗本身的宽
|
||
// privacyCheckDialogConfig.height = 220; //协议⼆次弹窗本身的⾼
|
||
// privacyCheckDialogConfig.privacyBackgroundColor = Colors.grey.value;
|
||
// privacyCheckDialogConfig.title = '请阅读并同意一下条款'; //弹窗标题
|
||
// privacyCheckDialogConfig.offsetX = 0; // 窗口相对屏幕中心的x轴偏移量
|
||
// privacyCheckDialogConfig.offsetY = 0; // 窗口相对屏幕中心的y轴偏移量
|
||
//
|
||
// privacyCheckDialogConfig.logBtnText = '同意并登录'; //弹窗登录按钮
|
||
// privacyCheckDialogConfig.titleTextSize = 16; // 弹窗标题字体大小
|
||
// privacyCheckDialogConfig.gravity = 'center'; //弹窗对齐方式
|
||
// privacyCheckDialogConfig.titleTextColor = Colors.black.value; // 弹窗标题字体颜色
|
||
// privacyCheckDialogConfig.contentTextGravity = 'left'; //协议⼆次弹窗协议内容对⻬⽅式
|
||
// privacyCheckDialogConfig.contentTextSize = 14; //协议⼆次弹窗协议内容字体⼤⼩
|
||
// privacyCheckDialogConfig.logBtnImgPath =
|
||
// 'log_btn_background'; //协议⼆次弹窗登录按钮的背景图⽚
|
||
// privacyCheckDialogConfig.logBtnTextColor =
|
||
// Colors.black.value; //协议⼆次弹窗登录按钮的字体颜⾊
|
||
// privacyCheckDialogConfig.logBtnMarginT = 40; //协议⼆次弹窗登录按钮上边距
|
||
// privacyCheckDialogConfig.logBtnMarginB = 20; //协议⼆次弹窗登录按钮下边距
|
||
// privacyCheckDialogConfig.logBtnMarginL = 90; //协议⼆次弹窗登录按钮左边距
|
||
// privacyCheckDialogConfig.logBtnWidth = 140; //协议⼆次弹窗登录按钮宽
|
||
// privacyCheckDialogConfig.logBtnHeight = 40; //协议⼆次弹窗登录按高
|
||
// /// 添加自定义的 控件 到dialog
|
||
// final List<JVCustomWidget> dialogWidgetList = <JVCustomWidget>[];
|
||
// const String btnDialogWidgetid = 'jv_add_custom_dialog_button'; // 标识控件 id
|
||
// final JVCustomWidget buttonDialogWidget =
|
||
// JVCustomWidget(btnDialogWidgetid, JVCustomWidgetType.button);
|
||
// buttonDialogWidget.title = '取消';
|
||
// buttonDialogWidget.titleFont = 16;
|
||
// buttonDialogWidget.left = 80;
|
||
// buttonDialogWidget.top = 180;
|
||
// buttonDialogWidget.width = 140;
|
||
// buttonDialogWidget.height = 40;
|
||
// buttonDialogWidget.textAlignment = JVTextAlignmentType.center;
|
||
// buttonDialogWidget.titleColor = Colors.black.value;
|
||
// buttonDialogWidget.btnNormalImageName = "log_btn_background";
|
||
// buttonDialogWidget.btnPressedImageName = "log_btn_background";
|
||
//
|
||
// // 添加点击事件监听
|
||
// jverify.addClikWidgetEventListener(btnDialogWidgetid, (eventId) {
|
||
// print('receive listener - click dialog widget event :$eventId');
|
||
// if (btnDialogWidgetid == eventId) {
|
||
// print('receive listener - 点击【新加 dialog button】');
|
||
// }
|
||
// });
|
||
// dialogWidgetList.add(buttonDialogWidget);
|
||
// privacyCheckDialogConfig.widgets = dialogWidgetList;
|
||
// uiConfig.privacyCheckDialogConfig = privacyCheckDialogConfig;
|
||
// // iOS 二次弹窗添加取消按钮
|
||
// uiConfig.isAlertPrivacyVc = true;
|
||
// uiConfig.agreementAlertViewWidgets = dialogWidgetList;
|
||
// uiConfig.agreementAlertViewLogBtnTextFontSize = 16;
|
||
// uiConfig.agreementAlertViewTitleTexSize = 18;
|
||
// uiConfig.agreementAlertViewContentTextFontSize = 14;
|
||
// final alertLeft = (screenWidth / 2 - 300 / 2).toInt();
|
||
// uiConfig.agreementAlertViewUIFrames = {
|
||
// 'superViewFrame': [alertLeft, 100, 300, 220],
|
||
// 'titleFrame': [20, 10, 260, 40],
|
||
// 'contentFrame': [25, 70, 250, 120],
|
||
// 'buttonFrame': [80, 142, 140, 40]
|
||
// };
|
||
|
||
/// 调用接口设置 UI
|
||
jverify.setCustomAuthorizationView(false, uiConfig,
|
||
landscapeConfig: uiConfig);
|
||
jverify.loginAuthSyncApi2(
|
||
autoDismiss: true,
|
||
loginAuthcallback: (event) {
|
||
action(event);
|
||
// AppLog.log('获取到 loginAuthSyncApi 接口返回数据,code=${event.code},message = ${event.message},operator = ${event.operator}');
|
||
});
|
||
} else {
|
||
// -10000 表示登录失败
|
||
action(JVListenerEvent.fromJson(
|
||
<String, dynamic>{'code': -10000, 'message': ''}));
|
||
}
|
||
}
|
||
}
|