項目環(huán)境是flutter開發(fā)的,然而阿里云官網(wǎng)并沒有flutter相關(guān)的SDK,所以選擇了集成的插件,這里踩過的坑不多敘述,開始正題。
阿里云號碼認(rèn)證文檔:https://help.aliyun.com/product/75010.html
一、插件選擇:ali_auth: ^0.2.3
pubspec.yaml
dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
ali_auth: ^0.2.3
二、main.dart 初始化SDK
void main() {
runApp(const MyApp());
if (!kIsWeb) { //判斷是否是web環(huán)境
initAliLogin();
}
}
void initAliLogin() async {
WidgetsFlutterBinding.ensureInitialized();
final result;
if (Platform.isAndroid) {
result = await AliAuthPlugin.initSdk(
sk: 'andriodKey',
config: getConfig(),
);
} else {
result = await AliAuthPlugin.initSdk(
sk: 'iosKey',
config: getConfig(), //getDislogConfig(),彈框模式與全屏模式
);
}
print(result);
}
login.dart 事件綁定及監(jiān)聽
import 'package:ali_auth/ali_auth.dart';
import 'package:flutter/material.dart';
/// 我的-首頁
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
BuildContext? mContext;
@override
void initState() {
super.initState();
/// 執(zhí)行相關(guān)登錄
login();
}
/// 相關(guān)登錄
login() async {
/// 登錄監(jiān)聽
AliAuthPlugin.loginListen(
type: false, onEvent: _onEvent, onError: _onError);
}
/// 登錄成功處理
void _onEvent(event) async {
print("-------------成功分割線------------$event");
if (event != null && event['code'] != null) {
if (event['code'] == '600024') {
await AliAuthPlugin.login;
} else if (event['code'] == '600000') {
print('獲取到的token${event["data"]}');
}
}
}
/// 登錄錯誤處理
void _onError(error) {
print("-------------失敗分割線------------$error");
}
@override
Widget build(BuildContext context) {
return Container(
padding: const EdgeInsets.only(top: 100),
child: InkWell(
child: const Text("一鍵登錄"),
onTap: () async {
final result = await AliAuthPlugin.login;
// ignore: avoid_print
print(result);
},
),
);
}
}
補充
1、需要按照阿里云文檔,對andriod進行相關(guān)配置(ios應(yīng)該也要)
https://help.aliyun.com/document_detail/189787.html#section-vxb-jwt-sou
2、注意包名、簽名、密鑰的一致性。
3、重點:修改源碼,直接搜索setLoggerEnable,括號里參數(shù)都改為true?。。?/p>
setLoggerEnable(true)//調(diào)試時,出現(xiàn)600015,手機終端不安全時修改
至此,可以正常喚起一鍵登錄頁面。