From c97e52bfa5302ac47019accd086a7e28f2df320f Mon Sep 17 00:00:00 2001 From: Weller <1812208341@qq.com> Date: Mon, 21 Sep 2020 16:40:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=96=B0=E6=B7=98=E5=AE=9D?= =?UTF-8?q?=E8=AF=A6=E6=83=85=E5=9B=BE=E5=8A=A0=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/lib/main.dart | 10 ++--- example/lib/taobao_image_viewer.dart | 40 +++++++++++++++++++ ....dart => taobao_image_webview_loader.dart} | 9 +++-- lib/util/taobao/taobao_loader.dart | 25 ++++++++++++ lib/zhiying_comm.dart | 3 +- 5 files changed, 75 insertions(+), 12 deletions(-) create mode 100644 example/lib/taobao_image_viewer.dart rename lib/util/taobao/{taobao_image_loader.dart => taobao_image_webview_loader.dart} (92%) create mode 100644 lib/util/taobao/taobao_loader.dart diff --git a/example/lib/main.dart b/example/lib/main.dart index c2b8332..2a02f40 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -5,6 +5,8 @@ import 'package:jdsdk/jdsdk.dart'; import 'package:url_launcher/url_launcher.dart'; import 'package:zhiying_comm/zhiying_comm.dart'; +import 'taobao_image_viewer.dart'; + void main() => runApp(MyApp()); class MyApp extends StatefulWidget { @@ -145,14 +147,8 @@ class HomePage extends StatelessWidget { RaisedButton( onPressed: () { Navigator.push(context, MaterialPageRoute(builder: (_) { - return TaobaoImageLoader( - 'https://h5.m.taobao.com/app/detail/desc.html?_isH5Des=true#!id=592787622422&type=1&f=&sellerType=B', - onImagesLoad: (images) { - Logger.debug(images.toString()); - }, - ); + return TaobaoImageView(); })); - // TaobaoUtil.getImage(); }, child: Text('抓淘宝图片'), ), diff --git a/example/lib/taobao_image_viewer.dart b/example/lib/taobao_image_viewer.dart new file mode 100644 index 0000000..4e37432 --- /dev/null +++ b/example/lib/taobao_image_viewer.dart @@ -0,0 +1,40 @@ +import 'package:cached_network_image/cached_network_image.dart'; +import 'package:flutter/material.dart'; +import 'package:zhiying_comm/zhiying_comm.dart'; + +class TaobaoImageView extends StatefulWidget { + @override + _TaobaoImageViewState createState() => _TaobaoImageViewState(); +} + +class _TaobaoImageViewState extends State { + List _images = List(); + + @override + void initState() { + TaobaoLoader.loadImages('592787622422').then((images) { + _images = images; + setState(() {}); + }); + super.initState(); + } + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: Text('淘宝图片加载'), + ), + body: SingleChildScrollView( + child: Column( + children: _images.map((imageUrl) { + return CachedNetworkImage( + imageUrl: imageUrl ?? '', + fit: BoxFit.fitWidth, + ); + }).toList(), + ), + ), + ); + } +} diff --git a/lib/util/taobao/taobao_image_loader.dart b/lib/util/taobao/taobao_image_webview_loader.dart similarity index 92% rename from lib/util/taobao/taobao_image_loader.dart rename to lib/util/taobao/taobao_image_webview_loader.dart index 9f96d25..255d419 100644 --- a/lib/util/taobao/taobao_image_loader.dart +++ b/lib/util/taobao/taobao_image_webview_loader.dart @@ -5,12 +5,12 @@ import 'package:webview_flutter/webview_flutter.dart'; import 'package:zhiying_comm/zhiying_comm.dart'; // 淘宝详情图加载 -class TaobaoImageLoader extends StatefulWidget { +class TaobaoImageWebviewLoader extends StatefulWidget { final String url; String script; final ValueChanged> onImagesLoad; - TaobaoImageLoader(this.url, {Key key, this.script, this.onImagesLoad}) + TaobaoImageWebviewLoader(this.url, {Key key, this.script, this.onImagesLoad}) : super(key: key) { this.script = script; if (EmptyUtil.isEmpty(this.script)) { @@ -20,10 +20,11 @@ class TaobaoImageLoader extends StatefulWidget { } @override - _TaobaoImageLoaderState createState() => _TaobaoImageLoaderState(); + _TaobaoImageWebviewLoaderState createState() => + _TaobaoImageWebviewLoaderState(); } -class _TaobaoImageLoaderState extends State { +class _TaobaoImageWebviewLoaderState extends State { WebViewController _webViewController; List _images = []; diff --git a/lib/util/taobao/taobao_loader.dart b/lib/util/taobao/taobao_loader.dart new file mode 100644 index 0000000..14b4adc --- /dev/null +++ b/lib/util/taobao/taobao_loader.dart @@ -0,0 +1,25 @@ +import 'package:dio/dio.dart'; + +class TaobaoLoader { + static Future> loadImages(String id) async { + String url = + 'http://h5api.m.taobao.com/h5/mtop.taobao.detail.getdesc/6.0/?api=mtop.taobao.detail.getdesc&appkey=12574478&data={"type":"0","f":"TB1AsvbfnqWBKNjSZFx8qwpLpla","id":"${id.toString()}"}&dataType=json&jsv=2.4.11&sign=e93a97b7e9a9a459297f4e689051c895&t=1538180732308&type=json'; + Dio dio = Dio(); + Response response = await dio.get(url); +// Logger.debug(response.data.toString()); + Map json = Map.from(response.data); + List list = json['data']['wdescContent']['pages']; + List images = List(); + for (var item in list) { + RegExp exp = new RegExp("(?<=)(.*?)(?=)"); + String image = exp.stringMatch(item.toString()); + if (image.startsWith('//')) { + images.add('http:' + image); + } else if (image.startsWith('https://') || image.startsWith('http://')) { + images.add(image); + } + } + + return images; + } +} diff --git a/lib/zhiying_comm.dart b/lib/zhiying_comm.dart index 55d1ba8..b15eafa 100644 --- a/lib/zhiying_comm.dart +++ b/lib/zhiying_comm.dart @@ -26,6 +26,7 @@ export 'util/net_util.dart'; export 'util/page_factory.dart'; export 'util/router_util.dart'; export 'util/taobao/taobao_auth.dart'; -export 'util/taobao/taobao_image_loader.dart'; +export 'util/taobao/taobao_image_webview_loader.dart'; +export 'util/taobao/taobao_loader.dart'; export 'util/widget_creater.dart'; export 'util/widget_factory.dart';