Flutter快速通过验证插件quickpass_yidun_flutter的使用
Flutter快速通过验证插件quickpass_yidun_flutter的使用
直连三大运营商,一步校验手机号与当前 SIM 卡号一致性。优化注册/登录/支付等场景验证流程。
平台支持(兼容性)
Android | iOS |
---|---|
适用版本区间:4.4以上 | 适用版本区间:9 - 14 |
环境准备
安装 CocoaPods:
https://guides.cocoapods.org/using/getting-started.html
资源引入/集成
在 pubspec.yaml
中添加:
dependencies:
quickpass_yidun_flutter: ^1.4.3
项目开发配置
1.0.3 版本之前需要在 flutter 工程对应的 android/app/build.gradle
文件的 android
域中添加:
repositories {
flatDir {
dirs project(':quickpass_yidun_flutter').file('libs')
}
}
release
包需要添加混淆规则:
-dontwarn com.cmic.sso.**
-keep class com.cmic.sso.**{*;}
-dontwarn com.sdk.**
-keep class com.sdk.** { *;}
-keep class cn.com.chinatelecom.account.**{*;}
-keep public class * extends android.view.View
-keep class com.netease.nis.quicklogin.entity.**{*;}
-keep class com.netease.nis.quicklogin.listener.**{*;}
-keep class com.netease.nis.quicklogin.QuickLogin{
public <methods>;
public <fields>;
}
-keep class com.netease.nis.quicklogin.helper.UnifyUiConfig{*;}
-keep class com.netease.nis.quicklogin.helper.UnifyUiConfig$Builder{
public <methods>;
public <fields>;
}
-keep class com.netease.nis.quicklogin.utils.LoginUiHelper$CustomViewListener{
public <methods>;
public <fields>;
}
-keep class com.netease.nis.basesdk.**{
public *;
protected *;
}
调用示例
import 'dart:convert';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:modal_progress_hud_nsn/modal_progress_hud_nsn.dart';
import 'package:quickpass_yidun_flutter/quickpass_flutter_plugin.dart';
void main() => runApp(new MaterialApp(
title: "易盾号码认证Demo",
theme: new ThemeData(primaryColor: Colors.white),
home: MyApp(),
));
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
/// 统一 key
final String f_result_key = "success";
String phoneNumber = "";
String _result = "token=";
bool _loading = false;
final QuickpassFlutterPlugin quickLoginPlugin = new QuickpassFlutterPlugin();
var eventChannel = const EventChannel("yd_quicklogin_flutter_event_channel");
@override
void initState() {
super.initState();
eventChannel.receiveBroadcastStream().listen(_onData, onError: _onError);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('易盾一键登录'),
),
body: ModalProgressHUD(child: _buildContent(), inAsyncCall: _loading),
),
);
}
void _onData(response) {
if (response is Map) {
var action = (response as Map)["action"];
if (action == "handleCustomEvent1") {
print("点击微信");
} else if (action == "handleCustomEvent2") {
print("点击QQ");
quickLoginPlugin.authLoginButtonClick();
} else if (action == "handleCustomEvent3") {
quickLoginPlugin.checkedSelected("true");
print("点击微博");
} else if (action == "handleCustomLabel") {
print("点击其他登录方式");
} else if (action == "authViewWillDisappear") {
print("授权页将要消失");
} else if (action == "authViewDidDisappear") {
print("授权页已经消失");
} else if (action == "authViewDealloc") {
print("授权页销毁");
} else if (action == "authViewDidLoad") {
print("加载授权页");
} else if (action == "authViewWillAppear") {
print("授权页将要出现");
} else if (action == "authViewDidAppear") {
print("授权页已经出现");
} else if (action == "backAction") {
print("点击返回按钮");
} else if (action == "loginAction") {
print("点击登录按钮");
} else if (action == "checkedAction") {
print("点击复选框");
} else if (action == "appDPrivacy") {
print("点击默认协议");
} else if (action == "appFPrivacy") {
print("点击第一个协议");
} else if (action == "appSPrivacy") {
print("点击第二个协议");
}
}
}
_onError(dynamic error) {
print('Received error: ${error.message}');
}
Widget _buildContent() {
return SingleChildScrollView(
child: new Column(
children: <Widget>[
Container(
margin: EdgeInsets.all(20),
color: Colors.white,
child: Text(_result),
width: 300,
height: 100,
),
new Container(
margin: EdgeInsets.fromLTRB(40, 5, 40, 5),
child: new Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new CustomButton(
onPressed: () {
isInitSuccess();
},
title: "初始化状态"),
new Text(" "),
Expanded(
child: new CustomButton(
onPressed: () {
checkVerifyEnable();
},
title: "网络环境是否支持",
)),
],
),
),
new Container(
child: SizedBox(
child: new CustomButton(
onPressed: () {
preLogin();
},
title: "预取号",
),
width: double.infinity,
),
margin: EdgeInsets.fromLTRB(40, 5, 40, 5),
),
new Container(
child: SizedBox(
child: new CustomButton(
onPressed: () {
quickLogin();
},
title: "一键登录",
),
width: double.infinity,
),
margin: EdgeInsets.fromLTRB(40, 5, 40, 5),
),
new TextField(
keyboardType: TextInputType.phone,
maxLines: 1,
autofocus: false,
cursorColor: Colors.blue,
maxLength: 11,
onChanged: (text) {
phoneNumber = text;
print("phone=$text");
},
decoration: InputDecoration(
hintText: "请输入手机号码",
labelText: "手机号",
// 未获得焦点下划线设为灰色
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.grey),
),
//获得焦点下划线设为蓝色
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.blue),
),
),
),
new Container(
child: SizedBox(
child: new CustomButton(
onPressed: () {
verifyPhoneNumber();
},
title: "本机校验",
),
width: double.infinity,
),
margin: EdgeInsets.fromLTRB(40, 5, 40, 5),
),
],
mainAxisAlignment: MainAxisAlignment.start,
),
);
}
/// SDK 初始化是否完成
void isInitSuccess() {
quickLoginPlugin
.init("易盾业务id", 4, true)
.then((map) {
bool result = map?[f_result_key];
setState(() {
if (result) {
_result = "SDK 初始化成功";
} else {
_result = "SDK 初始化失败";
}
});
});
}
/// 判断当前网络环境是否可以发起认证
void checkVerifyEnable() {
quickLoginPlugin.checkVerifyEnable().then((map) {
bool result = map?[f_result_key];
setState(() {
if (result) {
_result = "当前网络环境【支持认证】!";
} else {
_result = "当前网络环境【不支持认证】!";
}
});
});
}
/// 登录预取号
void preLogin() async {
setState(() {
_loading = true;
});
Map<dynamic, dynamic>? map = await quickLoginPlugin.preFetchNumber();
if (map?[f_result_key] == true) {
var ydToken = map?['token'];
setState(() {
_loading = false;
_result = "Token = $ydToken";
});
} else {
var ydToken = map?['token'];
var errorMsg = map?['errorMsg'];
setState(() {
_loading = false;
_result = "$errorMsg";
});
}
}
void quickLogin() {
var configMap;
var param = "";
String file = "";
if (Platform.isIOS) {
file = "asserts/ios-light-config.json";
} else if (Platform.isAndroid) {
file = "asserts/android-light-config.json";
}
rootBundle.loadString(file).then((value) async {
configMap = {"uiConfig": json.decode(value)};
quickLoginPlugin.setUiConfig(configMap);
Map<dynamic, dynamic>? map = await quickLoginPlugin.onePassLogin();
if (map?[f_result_key]) {
var accessToken = map?["accessToken"];
setState(() {
_result = "取号成功, 运营商授权码:" + accessToken;
});
quickLoginPlugin.closeLoginAuthView();
} else {
var errorMsg = map?["msg"];
setState(() {
_result = "取号失败, 出错原因:" + errorMsg;
});
quickLoginPlugin.closeLoginAuthView();
}
});
}
void verifyPhoneNumber() {
quickLoginPlugin
.verifyPhoneNumber("易盾业务id", phoneNumber)
.then((map) {
if (map?[f_result_key]) {
var accessToken = map?["accessToken"];
print("运营商token$accessToken");
} else {
var errorMsg = map?["msg"];
print("错误信息$errorMsg");
}
var ydToken = map?["ydToken"];
print("易盾token$ydToken");
});
}
}
/// 封装 按钮
class CustomButton extends StatelessWidget {
final VoidCallback onPressed;
final String title;
const CustomButton({required this.onPressed, required this.title});
@override
Widget build(BuildContext context) {
return new ElevatedButton(
onPressed: onPressed,
child: new Text(
"$title",
style: TextStyle(fontSize: 13),
),
);
}
}
SDK方法说明
1. 创建对象
import 'package:quickpass_yidun_flutter/quickpass_flutter_plugin.dart';
QuickpassFlutterPlugin quickLoginPlugin = new QuickpassFlutterPlugin();
2. 是否具备一键登录能力
quickLoginPlugin.checkVerifyEnable().then((map) {
bool result = map['success'];
});
参数说明:
回调参数 | 类型 | 描述 |
---|---|---|
success | Boolean | 是否具备一键登录能力 |
3. 初始化
quickLoginPlugin.init(String businessId, int timeout).then((map) {
bool result = map['success'];
});
参数说明:
参数 | 类型 | 是否必填 | 默认值 | 描述 |
---|---|---|---|---|
businessId | String | 是 | 无 | 易盾分配的业务 id |
isDebug | boolean | 否 | false | 是否打开日志开关 |
timeout | int | 否 | 3 | 运营商预取号和授权登录接口的超时时间,单位秒 |
回调参数说明:
回调参数 | 类型 | 描述 |
---|---|---|
success | Boolean | 初始化是否成功 |
4. 预取号
Map map = await quickLoginPlugin.preFetchNumber();
if (map['success'] == true) {
var ydToken = map['token'];
} else {
var ydToken = map['token'];
var errorMsg = map['errorMsg'];
}
参数说明:
回调参数 | 类型 | 描述 |
---|---|---|
success | Boolean | 预取号是否成功 |
token | String | 如果预取号成功则返回易盾 token,否则无此字段 |
errorMsg | String | 如果预取号失败,返回错误详情 |
5. 自定义授权页面
String file = "";
if (Platform.isIOS) {
file = "asserts/ios-light-config.json";
} else if (Platform.isAndroid) {
file = "asserts/android-light-config.json";
}
rootBundle.loadString(file).then((value) async {
configMap = {"uiConfig": json.decode(value)};
quickLoginPlugin.setUiConfig(configMap);
});
参数说明:
参数 | 类型 | 是否必填 | 默认值 | 描述 |
---|---|---|---|---|
config | Map | 是 | 无 | 自定义配置项 |
6. 调起授权页面
// 拉起授权页面
Map map = await quickLoginPlugin.onePassLogin(animated: true);
if (map["success"]) {
var accessToken = map["accessToken"];
} else {
var errorMsg = map["msg"];
}
参数说明:
参数 | 类型 | 是否必填 | 默认值 | 描述 |
---|---|---|---|---|
animated | bool | 否 | true | 授权页弹出是否有动画 |
回调参数说明:
回调参数 | 类型 | 描述 |
---|---|---|
success | Boolean | 授权是否成功 |
accessToken | String | 如果取号成功返回运营商授权码,否则无此字段 |
msg | String | 如果取号失败,返回错误详情 |
7. 事件监听
var eventChannel = const EventChannel("yd_quicklogin_flutter_event_channel");
@override
void initState() {
super.initState();
initPlatformState();
eventChannel.receiveBroadcastStream().listen(_onData, onError: _onError);
}
void _onData(response) {
if (response is Map) {
var action = (response as Map)["action"];
}
}
action 参数说明:
回调参数 | 类型 | 描述 |
---|---|---|
action | String | authViewDidLoad 表示正在加载授权页 authViewWillAppear 表示授权页已经出现 authViewWillDisappear 表示授权页将要消失 authViewDidDisappear表示授权页已经消失 authViewDealloc 表示授权页销毁 appDPrivacy 表示点击了默认协议 appFPrivacy 表示点击了议第一个协议点击 appSPrivacy 表示点击了第二个协议 loginAction 表示点击了登录按钮,data.checked = true 表示在点击登录按钮时复选框选已选中反之 checkedAction 表示点击了复选框,data.checked = true 表示复选框已选中反之 其他自定义的action |
8. 关闭授权页面
quickLoginPlugin.closeLoginAuthView();
9. 自动登录
方法说明: 调用该方法自动登录 注意:该方法仅限iOS
quickLoginPlugin.authLoginButtonClick();
10. 协议旁边复选框勾选
方法描述: 协议旁边复选框勾选
复选框勾选
quickLoginPlugin.checkedSelected(true);
11. 本机校验
在初始化之后执行,本机校验和一键登录可共用初始化,本机校验界面需自行实现
quickLoginPlugin.verifyPhoneNumber("易盾业务id", phoneNumber).then(map){
if (map["success"]) {
var accessToken = map["accessToken"];
} else {
var errorMsg = map["msg"];
}
var ydToken = map["ydToken"];
}
更多关于Flutter快速通过验证插件quickpass_yidun_flutter的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter快速通过验证插件quickpass_yidun_flutter的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是如何在Flutter项目中使用quickpass_yidun_flutter
插件进行快速验证的一个示例代码案例。请注意,实际使用时你可能需要根据具体的插件文档和需求进行微调。
首先,确保你已经在pubspec.yaml
文件中添加了quickpass_yidun_flutter
插件的依赖:
dependencies:
flutter:
sdk: flutter
quickpass_yidun_flutter: ^最新版本号 # 请替换为实际的最新版本号
然后,运行flutter pub get
来安装依赖。
接下来,我们创建一个简单的Flutter应用,展示如何使用quickpass_yidun_flutter
插件进行验证。以下是一个基本的示例:
import 'package:flutter/material.dart';
import 'package:quickpass_yidun_flutter/quickpass_yidun_flutter.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'QuickPass Yidun Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: QuickPassDemoPage(),
);
}
}
class QuickPassDemoPage extends StatefulWidget {
@override
_QuickPassDemoPageState createState() => _QuickPassDemoPageState();
}
class _QuickPassDemoPageState extends State<QuickPassDemoPage> {
String _verificationResult = '';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('QuickPass Yidun Flutter Demo'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Verification Result:',
style: TextStyle(fontSize: 20),
),
SizedBox(height: 20),
Text(
_verificationResult,
style: TextStyle(fontSize: 18),
),
SizedBox(height: 40),
ElevatedButton(
onPressed: _performVerification,
child: Text('Perform Verification'),
),
],
),
),
);
}
Future<void> _performVerification() async {
try {
// 假设QuickPassYidunFlutter有一个名为verify的方法,接受一些参数
// 请根据实际的插件API文档替换以下代码
bool result = await QuickPassYidunFlutter.verify(
// 示例参数,请根据实际情况替换
userId: 'user123',
password: 'password123',
// 其他可能的参数,如生物识别类型等
);
setState(() {
_verificationResult = result ? 'Verification Successful' : 'Verification Failed';
});
} catch (e) {
// 处理异常
setState(() {
_verificationResult = 'Verification Error: ${e.message}';
});
}
}
}
注意:
- 上面的代码假设
quickpass_yidun_flutter
插件有一个名为verify
的静态方法,该方法接受一些参数(如用户ID和密码)并返回一个布尔值。然而,实际的插件API可能与此不同,因此你需要查阅插件的官方文档来获取正确的使用方法。 - 插件可能还需要一些额外的配置,例如初始化插件、处理权限请求等,这些步骤也应参考插件的官方文档。
- 在真实的应用中,避免在客户端存储敏感信息,如密码,而是使用安全存储机制或服务器验证。
请确保查阅最新的quickpass_yidun_flutter
插件文档,并根据其提供的API和示例代码进行调整。