【Flutter】阿里云號碼認(rèn)證一鍵登錄

項目環(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,手機終端不安全時修改

至此,可以正常喚起一鍵登錄頁面。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容