From 6172a08d3f17fab1a4698ddedaadb43f51beb34c Mon Sep 17 00:00:00 2001 From: jack ning Date: Tue, 28 Jun 2022 06:43:22 +0800 Subject: [PATCH] update --- bytedesk_demo/lib/main.dart | 12 ++ bytedesk_demo/lib/page/switch_user_page.dart | 109 ++++++++++++++++++ bytedesk_demo/pubspec.yaml | 2 +- .../flutter/generated_plugin_registrant.cc | 3 + .../windows/flutter/generated_plugins.cmake | 1 + bytedesk_kefu/lib/bytedesk_kefu.dart | 16 ++- 6 files changed, 139 insertions(+), 4 deletions(-) create mode 100755 bytedesk_demo/lib/page/switch_user_page.dart diff --git a/bytedesk_demo/lib/main.dart b/bytedesk_demo/lib/main.dart index a5926fc..e840897 100644 --- a/bytedesk_demo/lib/main.dart +++ b/bytedesk_demo/lib/main.dart @@ -13,6 +13,8 @@ import 'package:flutter/material.dart'; // import 'package:vibration/vibration.dart'; import 'package:audioplayers/audioplayers.dart'; +import 'page/switch_user_page.dart'; + void main() { // runApp(MyApp()); runApp(OverlaySupport( @@ -139,6 +141,16 @@ class _MyAppState extends State with WidgetsBindingObserver { })); }, ), + ListTile( + title: const Text('切换用户'), + trailing: const Icon(Icons.keyboard_arrow_right), + onTap: () { + Navigator.of(context) + .push(MaterialPageRoute(builder: (context) { + return SwitchUserPage(); + })); + }, + ), // ListTile( // title: Text('退出登录'), // onTap: () { diff --git a/bytedesk_demo/lib/page/switch_user_page.dart b/bytedesk_demo/lib/page/switch_user_page.dart new file mode 100755 index 0000000..dced185 --- /dev/null +++ b/bytedesk_demo/lib/page/switch_user_page.dart @@ -0,0 +1,109 @@ +import 'package:bytedesk_kefu/bytedesk_kefu.dart'; +import 'package:flutter/material.dart'; +import 'package:fluttertoast/fluttertoast.dart'; + +import 'user_info_page.dart'; + +// 切换用户 +class SwitchUserPage extends StatefulWidget { + SwitchUserPage({Key? key}) : super(key: key); + + @override + _SwitchUserPageState createState() => _SwitchUserPageState(); +} + +class _SwitchUserPageState extends State { + // 获取appkey,登录后台->渠道管理->Flutter->添加应用->获取appkey + String _appKey = '81f427ea-4467-4c7c-b0cd-5c0e4b51456f'; + // 获取subDomain,也即企业号:登录后台->客服管理->客服账号->企业号 + String _subDomain = "vip"; + + @override + void initState() { + super.initState(); + } + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: Text('切换用户'), + elevation: 0, + ), + body: ListView( + children: ListTile.divideTiles( + context: context, + tiles: [ + ListTile( + title: Text('用户信息'), + trailing: Icon(Icons.keyboard_arrow_right), + onTap: () { + _userInfo(); + }, + ), + ListTile( + title: Text('用户1男'), + onTap: () { + _userBoyLogin(); + }, + ), + ListTile( + title: Text('用户2女'), + onTap: () { + _userGirlLogin(); + }, + ), + ListTile( + title: Text('退出登录'), + onTap: () { + _userLogout(); + }, + ), + ], + ).toList()), + ); + } + + void _userInfo() { + Navigator.of(context).push(new MaterialPageRoute(builder: (context) { + return new UserInfoPage(); + })); + } + + void _userBoyLogin() { + if (BytedeskKefu.isLogin()) { + Fluttertoast.showToast(msg: '请先退出登录'); + return; + } + _initWithUsernameAndNicknameAndAvatar( + "myflutteruserboy", + "我是帅哥flutter", + "https://bytedesk.oss-cn-shenzhen.aliyuncs.com/avatars/boy.png", + _appKey, + _subDomain); + } + + void _userGirlLogin() { + if (BytedeskKefu.isLogin()) { + Fluttertoast.showToast(msg: '请先退出登录'); + return; + } + Fluttertoast.showToast(msg: '登录中'); + _initWithUsernameAndNicknameAndAvatar( + "myflutterusergirl", + "我是美女flutter", + "https://bytedesk.oss-cn-shenzhen.aliyuncs.com/avatars/girl.png", + _appKey, + _subDomain); + } + + void _initWithUsernameAndNicknameAndAvatar(String username, String nickname, + String avatar, String appKey, String subDomain) { + BytedeskKefu.initWithUsernameAndNicknameAndAvatar(username, nickname, avatar, appKey, subDomain); + } + + void _userLogout() { + Fluttertoast.showToast(msg: '退出中'); + BytedeskKefu.logout(); + } +} diff --git a/bytedesk_demo/pubspec.yaml b/bytedesk_demo/pubspec.yaml index 503ca76..e96f51b 100644 --- a/bytedesk_demo/pubspec.yaml +++ b/bytedesk_demo/pubspec.yaml @@ -46,7 +46,7 @@ dependencies: # 请在ios/Podfile中添加:use_frameworks! vibration: ^1.7.3 # 在线客服 https://pub.dev/packages/bytedesk_kefu - bytedesk_kefu: ^1.4.0 + bytedesk_kefu: ^1.4.1 # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. diff --git a/bytedesk_demo/windows/flutter/generated_plugin_registrant.cc b/bytedesk_demo/windows/flutter/generated_plugin_registrant.cc index 50989f9..2146005 100644 --- a/bytedesk_demo/windows/flutter/generated_plugin_registrant.cc +++ b/bytedesk_demo/windows/flutter/generated_plugin_registrant.cc @@ -7,8 +7,11 @@ #include "generated_plugin_registrant.h" #include +#include void RegisterPlugins(flutter::PluginRegistry* registry) { BytedeskKefuPluginRegisterWithRegistrar( registry->GetRegistrarForPlugin("BytedeskKefuPlugin")); + UrlLauncherWindowsRegisterWithRegistrar( + registry->GetRegistrarForPlugin("UrlLauncherWindows")); } diff --git a/bytedesk_demo/windows/flutter/generated_plugins.cmake b/bytedesk_demo/windows/flutter/generated_plugins.cmake index bf26b7e..406ac4c 100644 --- a/bytedesk_demo/windows/flutter/generated_plugins.cmake +++ b/bytedesk_demo/windows/flutter/generated_plugins.cmake @@ -4,6 +4,7 @@ list(APPEND FLUTTER_PLUGIN_LIST bytedesk_kefu + url_launcher_windows ) set(PLUGIN_BUNDLED_LIBRARIES) diff --git a/bytedesk_kefu/lib/bytedesk_kefu.dart b/bytedesk_kefu/lib/bytedesk_kefu.dart index 032ec9e..32ffd7b 100644 --- a/bytedesk_kefu/lib/bytedesk_kefu.dart +++ b/bytedesk_kefu/lib/bytedesk_kefu.dart @@ -74,6 +74,15 @@ class BytedeskKefu { } } + // 判断是否已经登录 + static bool isLogin() { + String? accessToken = SpUtil.getString(BytedeskConstants.accessToken); + if (accessToken!.isEmpty) { + return false; + } + return true; + } + // 访客匿名登录接口 static void anonymousLogin(String appKey, String subDomain) async { /// sp初始化 @@ -231,8 +240,8 @@ class BytedeskKefu { } // 默认设置商品信息和附言为空 - static void startChatDefault( - BuildContext context, String uuid, String type, String title, bool isV2Robot) { + static void startChatDefault(BuildContext context, String uuid, String type, + String title, bool isV2Robot) { startChat(context, uuid, type, title, '', '', isV2Robot, null); } @@ -399,7 +408,8 @@ class BytedeskKefu { } // 一个接口,设置:昵称、头像、备注 - static Future updateProfile(String nickname, String avatar, String description) async { + static Future updateProfile( + String nickname, String avatar, String description) async { return BytedeskUserHttpApi().updateProfile(nickname, avatar, description); }