From d7d600355d92973bcfc58877a4237cdec6e42748 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=AB=A0=E6=96=87=E8=BD=A9?= <12812285557@qq.com> Date: Wed, 17 Aug 2022 17:10:29 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=A2=E5=8D=95=E6=95=B0=E6=8D=AE=E6=9B=B4?= =?UTF-8?q?=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bytedesk_demo/lib/page/chat_type_page.dart | 98 +++++++++++++------ bytedesk_kefu/lib/bytedesk_kefu.dart | 29 +++--- .../lib/ui/chat/page/chat_kf_page.dart | 66 ++++++++----- .../ui/chat/provider/chat_kf_provider.dart | 7 +- bytedesk_kefu/lib/util/bytedesk_utils.dart | 6 +- 5 files changed, 132 insertions(+), 74 deletions(-) diff --git a/bytedesk_demo/lib/page/chat_type_page.dart b/bytedesk_demo/lib/page/chat_type_page.dart index 98c6abe..c591690 100755 --- a/bytedesk_demo/lib/page/chat_type_page.dart +++ b/bytedesk_demo/lib/page/chat_type_page.dart @@ -128,43 +128,77 @@ class _ChatTypePageState extends State { "other3": "", // 可另外添加自定义字段,客服端不可见,可用于回调原样返回 }); - BytedeskKefu.startWorkGroupChatShopCallback( - context, _workGroupWid, "技能组客服-电商-回调", custom, (value) { - print('value为custom参数原样返回 $value'); + BytedeskKefu.startWorkGroupChatOrderCallback( + context, _workGroupWid, "技能组客服-电商-回调",(value){ }, () { // 主要用途:用户在聊天页面点击商品消息,回调此接口,开发者可在此打开进入商品详情页 showModalBottomSheet( context: context, builder: (context) { - return GestureDetector( - onTap: (){ - custom = json.encode({ - "type": BytedeskConstants.MESSAGE_TYPE_COMMODITY, // 不能修改 - "title": "11111", // 可自定义, 类型为字符串 - "content": "11111", // 可自定义, 类型为字符串 - "price": "11111", // 可自定义, 类型为字符串 - "url": - "https://item.m.jd.com/product/12172344.html", // 必须为url网址, 类型为字符串 - "imageUrl": - "https://bytedesk.oss-cn-shenzhen.aliyuncs.com/images/123.webp", //必须为图片网址, 类型为字符串 - "id": 123, // 可自定义 - "categoryCode": "100010003", // 可自定义, 类型为字符串 - "client": "flutter", // 可自定义, 类型为字符串 - // 可自定义添加key:value, 客服端不可见,可用于回调原样返回 - "other1": "", // 可另外添加自定义字段,客服端不可见,可用于回调原样返回 - "other2": "", // 可另外添加自定义字段,客服端不可见,可用于回调原样返回 - "other3": "", // 可另外添加自定义字段,客服端不可见,可用于回调原样返回 - }); - print('11111'); - BytedeskKefu.updateGoods(custom); - setState(() { + return Column( + children: [ + GestureDetector( + onTap: (){ + custom = json.encode({ + "type": BytedeskConstants.MESSAGE_TYPE_COMMODITY, // 不能修改 + "title": "11111", // 可自定义, 类型为字符串 + "content": "11111", // 可自定义, 类型为字符串 + "price": "11111", // 可自定义, 类型为字符串 + "url": + "https://item.m.jd.com/product/12172344.html", // 必须为url网址, 类型为字符串 + "imageUrl": + "https://bytedesk.oss-cn-shenzhen.aliyuncs.com/images/123.webp", //必须为图片网址, 类型为字符串 + "id": 123, // 可自定义 + "categoryCode": "100010003", // 可自定义, 类型为字符串 + "client": "flutter", // 可自定义, 类型为字符串 + // 可自定义添加key:value, 客服端不可见,可用于回调原样返回 + "other1": "", // 可另外添加自定义字段,客服端不可见,可用于回调原样返回 + "other2": "", // 可另外添加自定义字段,客服端不可见,可用于回调原样返回 + "other3": "", // 可另外添加自定义字段,客服端不可见,可用于回调原样返回 + }); + print('11111'); + BytedeskKefu.updateGoods(custom); + setState(() { + + }); + }, + child: Container( + width: 200, + height: 100, + color: Colors.red, + ), + ), + GestureDetector( + onTap: (){ + custom = json.encode({ + "type": BytedeskConstants.MESSAGE_TYPE_COMMODITY, // 不能修改 + "title": "222", // 可自定义, 类型为字符串 + "content": "222", // 可自定义, 类型为字符串 + "price": "222", // 可自定义, 类型为字符串 + "url": + "https://item.m.jd.com/product/12172344.html", // 必须为url网址, 类型为字符串 + "imageUrl": + "https://bytedesk.oss-cn-shenzhen.aliyuncs.com/images/123.webp", //必须为图片网址, 类型为字符串 + "id": 123, // 可自定义 + "categoryCode": "100010003", // 可自定义, 类型为字符串 + "client": "flutter", // 可自定义, 类型为字符串 + // 可自定义添加key:value, 客服端不可见,可用于回调原样返回 + "other1": "", // 可另外添加自定义字段,客服端不可见,可用于回调原样返回 + "other2": "", // 可另外添加自定义字段,客服端不可见,可用于回调原样返回 + "other3": "", // 可另外添加自定义字段,客服端不可见,可用于回调原样返回 + }); + print('11111'); + BytedeskKefu.updateGoods(custom); + setState(() { - }); - }, - child: Container( - width: 200, - height: 200, - color: Colors.red, - ), + }); + }, + child: Container( + width: 200, + height: 100, + color: Colors.blue, + ), + ), + ], ); }); }); diff --git a/bytedesk_kefu/lib/bytedesk_kefu.dart b/bytedesk_kefu/lib/bytedesk_kefu.dart index 119aa7e..c9332c0 100644 --- a/bytedesk_kefu/lib/bytedesk_kefu.dart +++ b/bytedesk_kefu/lib/bytedesk_kefu.dart @@ -195,7 +195,7 @@ class BytedeskKefu { static void startWorkGroupChatPostscript( BuildContext context, String wid, String title, String postScript) { startChat(context, wid, BytedeskConstants.CHAT_TYPE_WORKGROUP, title, '', - postScript, false, null); + postScript, false, null, null); } // 电商接口,携带商品参数 @@ -207,16 +207,23 @@ class BytedeskKefu { } + ///调用订单信息 + static void startWorkGroupChatOrderCallback(BuildContext context, String wid, + String title, ValueSetter customCallback,Function btnCallBack) { + startChatShop(context, wid, BytedeskConstants.CHAT_TYPE_WORKGROUP, title, + '', customCallback,btnCallBack: btnCallBack); + } + ///用于更新订单数据 static void updateGoods(String info) { - BytedeskUtils.goodsInfo = info; + BytedeskUtils.goodsInfo.value = info; } static void startWorkGroupChatShopCallback(BuildContext context, String wid, - String title, String commodity, ValueSetter customCallback,{Widget? widget}) { + String title, String commodity, ValueSetter customCallback) { startChatShop(context, wid, BytedeskConstants.CHAT_TYPE_WORKGROUP, title, - commodity, customCallback,widget: widget); + commodity, customCallback); } // 指定客服会话 @@ -230,7 +237,7 @@ class BytedeskKefu { static void startAppointedChatPostscript( BuildContext context, String uid, String title, String postScript) { startChat(context, uid, BytedeskConstants.CHAT_TYPE_APPOINTED, title, '', - postScript, false, null); + postScript, false, null, null); } // 电商接口,携带商品参数 @@ -249,20 +256,19 @@ class BytedeskKefu { // 默认设置商品信息和附言为空 static void startChatDefault(BuildContext context, String uuid, String type, String title, bool isV2Robot) { - startChat(context, uuid, type, title, '', '', isV2Robot, null); + startChat(context, uuid, type, title, '', '', isV2Robot, null, null); } // 电商对话-自定义类型(技能组、指定客服) static void startChatShop(BuildContext context, String uuid, String type, - String title, String commodity, ValueSetter? customCallback, - {Widget? widget}) { - startChat(context, uuid, type, title, commodity, '', false, customCallback,widget:widget); + String title, String commodity, ValueSetter? customCallback,{Function? btnCallBack}) { + startChat(context, uuid, type, title, commodity, '', false, customCallback, btnCallBack); } // 发送附言消息-自定义类型(技能组、指定客服) static void startChatPostscript(BuildContext context, String uuid, String type, String title, String postScript) { - startChat(context, uuid, type, title, '', postScript, false, null); + startChat(context, uuid, type, title, '', postScript, false, null, null); } // 客服会话-自定义类型(技能组、指定客服) @@ -275,7 +281,7 @@ class BytedeskKefu { String postScript, bool isV2Robot, ValueSetter? customCallback, - {Widget? widget} + Function? btnBack, ) { Navigator.of(context).push(new MaterialPageRoute(builder: (context) { return new ChatKFProvider( @@ -287,6 +293,7 @@ class BytedeskKefu { postscript: postScript, isV2Robot: isV2Robot, customCallback: customCallback, + btnBack: btnBack, ); })); } diff --git a/bytedesk_kefu/lib/ui/chat/page/chat_kf_page.dart b/bytedesk_kefu/lib/ui/chat/page/chat_kf_page.dart index a392248..bddc234 100755 --- a/bytedesk_kefu/lib/ui/chat/page/chat_kf_page.dart +++ b/bytedesk_kefu/lib/ui/chat/page/chat_kf_page.dart @@ -51,7 +51,7 @@ class ChatKFPage extends StatefulWidget { final bool? isThread; final Thread? thread; final ValueSetter? customCallback; - final Widget? widget; + final Function? btnBack; // ChatKFPage( {Key? key, @@ -64,7 +64,7 @@ class ChatKFPage extends StatefulWidget { this.isV2Robot, this.isThread, this.thread, - this.customCallback, this.widget}) + this.customCallback, this.btnBack,}) : super(key: key); // @override @@ -120,29 +120,45 @@ class _ChatKFPageState extends State String goodUrl = ''; bool showGood = false; String customGoods = ''; + // String lastGoods = ''; + + @override void initState() { - customGoods = widget.custom??''; - if ( - customGoods.trim().length > 0){ - showGood = true; - Map json = jsonDecode(customGoods); - json.forEach((key, value) { - //typeOne等所对应的数组数据 - if(key=='title'){ - goodName = value; - } - if(key=='price'){ - goodPrice = value; - } - if(key=='imageUrl'){ - goodUrl = value; - } - }); + + BytedeskUtils.goodsInfo.addListener(() { + print('发生改变!!!!!!!!!!'); + customGoods = BytedeskUtils.goodsInfo.value; + _goodsSubmitted(); + }); + + + if(widget.btnBack==null){ + customGoods = widget.custom??''; + + if (customGoods.trim().length > 0){ + showGood = true; + Map json = jsonDecode(customGoods); + json.forEach((key, value) { + //typeOne等所对应的数组数据 + if(key=='title'){ + goodName = value; + } + if(key=='price'){ + goodPrice = value; + } + if(key=='imageUrl'){ + goodUrl = value; + } + }); + } + }else{ } + + // BytedeskUtils.printLog('chat_kf_page init'); SpUtil.putBool(BytedeskConstants.isCurrentChatKfPage, true); // 从历史会话或者顶部通知栏进入 @@ -604,7 +620,7 @@ class _ChatKFPageState extends State SizedBox(width: 30,), GestureDetector( onTap: (){ - //showGood = false; + showGood = false; setState(() { }); _goodsSubmitted(); @@ -695,10 +711,9 @@ class _ChatKFPageState extends State void _handleShowOrders() async { print('_handleShowOrders'); - if(widget.customCallback!=null){ - widget.customCallback!('点击订单'); - customGoods = BytedeskUtils.goodsInfo; - _goodsSubmitted(); + if(widget.btnBack!=null){ + widget.btnBack!(); + } } @@ -794,8 +809,7 @@ class _ChatKFPageState extends State // 发送商品消息 void _goodsSubmitted() { print(customGoods); - if ( - customGoods.trim().length > 0) { + if (customGoods.trim().length > 0) { if (_bdMqtt.isConnected()) { if (_currentThread == null) { diff --git a/bytedesk_kefu/lib/ui/chat/provider/chat_kf_provider.dart b/bytedesk_kefu/lib/ui/chat/provider/chat_kf_provider.dart index a60d5eb..1aa137e 100755 --- a/bytedesk_kefu/lib/ui/chat/provider/chat_kf_provider.dart +++ b/bytedesk_kefu/lib/ui/chat/provider/chat_kf_provider.dart @@ -14,7 +14,7 @@ class ChatKFProvider extends StatelessWidget { final String? postscript; final bool? isV2Robot; final ValueSetter? customCallback; - final Widget? widget; + final Function? btnBack; // const ChatKFProvider( {Key? key, @@ -25,7 +25,7 @@ class ChatKFProvider extends StatelessWidget { this.custom, this.postscript, this.isV2Robot, - this.customCallback, this.widget}) + this.customCallback, this.btnBack}) : super(key: key); // @override @@ -51,7 +51,8 @@ class ChatKFProvider extends StatelessWidget { isV2Robot: isV2Robot, isThread: false, customCallback: customCallback, - widget: widget,), + btnBack: btnBack, + ), ); } } diff --git a/bytedesk_kefu/lib/util/bytedesk_utils.dart b/bytedesk_kefu/lib/util/bytedesk_utils.dart index 663e7ce..144ff96 100755 --- a/bytedesk_kefu/lib/util/bytedesk_utils.dart +++ b/bytedesk_kefu/lib/util/bytedesk_utils.dart @@ -28,8 +28,10 @@ import 'package:image_gallery_saver/image_gallery_saver.dart'; import 'package:flutter/services.dart' show rootBundle; class BytedeskUtils { - // - static String goodsInfo = ''; + + + static ValueNotifier goodsInfo = ValueNotifier(''); + static bool get isDesktop => !isWeb && (isWindows || isLinux || isMacOS); static bool get isMobile => isAndroid || isIOS; static bool get isWeb => kIsWeb;