基础库
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 

83 rader
4.0 KiB

  1. import 'dart:convert';
  2. import 'package:flutter/material.dart';
  3. import 'package:webview_flutter/webview_flutter.dart';
  4. import 'package:zhiying_comm/zhiying_comm.dart';
  5. // 淘宝详情图加载,webview方式
  6. @Deprecated('使用TaobaoLoader抓取图片')
  7. class TaobaoImageWebviewLoader extends StatefulWidget {
  8. final String url;
  9. String script;
  10. final ValueChanged<List<String>> onImagesLoad;
  11. TaobaoImageWebviewLoader(this.url, {Key key, this.script, this.onImagesLoad})
  12. : super(key: key) {
  13. this.script = script;
  14. if (EmptyUtil.isEmpty(this.script)) {
  15. this.script =
  16. 'CnZhciB6cj17fTsKZnVuY3Rpb24gZ2V0aW4oc3RyMSxzdHIyLHN0cjMpewogICAgc3RyMT1zdHIxKycnO3N0cjI9c3RyMisnJztzdHIzPXN0cjMrJyc7CiAgICB2YXIgdG1wPXN0cjEuc3BsaXQoc3RyMik7CiAgICBpZih0bXBbMV09PW51bGwpdG1wWzFdPScnOwogICAgdG1wPXRtcFsxXS5zcGxpdChzdHIzKTsKICAgIHJldHVybiB0bXBbMF07Cn0KZnVuY3Rpb24gY2hlY2soKXsKICAgIC8qenIudG1wPWRvY3VtZW50LmdldEVsZW1lbnRCeUlkKCdkZXNjcmlwdGlvbicpLmlubmVySFRNTDsqLwogICAgenIudG1wPWRvY3VtZW50LmdldEVsZW1lbnRCeUlkKCdKX2Z1bGxDb250ZW50JykuaW5uZXJIVE1MOwogICAgaWYoenIudG1wPT1udWxsfHx6ci50bXA9PScnfHx6ci50bXAuaW5kZXhPZigiLmpwZyIpPT0tMSl7CiAgICAgICAgd2luZG93LnNldFRpbWVvdXQoZnVuY3Rpb24oKXtjaGVjaygpO30sMzAwKTsKICAgICAgICByZXR1cm47CiAgICB9CiAgICB6ci5hbGw9enIudG1wLnNwbGl0KCdzcmM9Jyk7CiAgICB6ci5yZXR1cm5fZGF0YT1bXTsKICAgIHpyLm49LTE7CiAgICBmb3IodmFyIGkgPSAwLGxlbiA9IHpyLmFsbC5sZW5ndGg7IGkgPCBsZW47IGkrKyl7CiAgICAgICAgaWYoaT09MCljb250aW51ZTsKICAgICAgICBpZih6ci5hbGwubGVuZ3RoLTE9PWkpY29udGludWU7CiAgICAgICAgenIudG1wdmFsPXpyLmFsbFtpXTsKICAgICAgICB6ci50bXB2YWw9Z2V0aW4oenIudG1wdmFsLCciJywnIicpOwogICAgICAgIGlmKHpyLnRtcHZhbC5pbmRleE9mKCJiYXNlNjQiKSE9LTF8fHpyLnRtcHZhbC5pbmRleE9mKCJocmVmPSIpIT0tMSljb250aW51ZTsKICAgICAgICBpZih6ci50bXB2YWwuaW5kZXhPZigiaHR0cCIpPT0tMSl6ci50bXB2YWw9Imh0dHBzOiIrenIudG1wdmFsOwogICAgICAgIHpyLm4rKzsKICAgICAgICB6ci5yZXR1cm5fZGF0YVt6ci5uXT16ci50bXB2YWw7CiAgICB9CiAgICB6ci5yZXR1cm5fZGF0YT1KU09OLnN0cmluZ2lmeSh6ci5yZXR1cm5fZGF0YSk7CiAgICAvLyBkb2N1bWVudC5ib2R5LmlubmVySFRNTD16ci5yZXR1cm5fZGF0YTsKICAgIExvYWRlci5wb3N0TWVzc2FnZSh6ci5yZXR1cm5fZGF0YSk7CiAgICAvLyBqaCh6ci5yZXR1cm5fZGF0YSwiIik7Cn0KdHJ5e3dpbmRvdy5zZXRUaW1lb3V0KGZ1bmN0aW9uKCl7Y2hlY2soKTt9LDApO30KY2F0Y2goZXJyKXtkb2N1bWVudC5ib2R5LmlubmVySFRNTD1KU09OLnN0cmluZ2lmeShlcnIpO30KCg==';
  17. }
  18. }
  19. @override
  20. _TaobaoImageWebviewLoaderState createState() =>
  21. _TaobaoImageWebviewLoaderState();
  22. }
  23. class _TaobaoImageWebviewLoaderState extends State<TaobaoImageWebviewLoader> {
  24. WebViewController _webViewController;
  25. List<String> _images = [];
  26. @override
  27. Widget build(BuildContext context) {
  28. return WebView(
  29. initialUrl: widget.url,
  30. javascriptMode: JavascriptMode.unrestricted,
  31. onWebViewCreated: (WebViewController webViewController) {
  32. _webViewController = webViewController;
  33. },
  34. javascriptChannels: <JavascriptChannel>[
  35. _imageLoaderJavascriptChannel(context),
  36. ].toSet(),
  37. navigationDelegate: (NavigationRequest request) {
  38. print('allowing navigation to $request');
  39. return NavigationDecision.navigate;
  40. },
  41. onPageStarted: (String url) {
  42. print('Page started loading: $url');
  43. },
  44. onPageFinished: (String url) {
  45. _loadImages();
  46. },
  47. gestureNavigationEnabled: true,
  48. );
  49. }
  50. void _loadImages() {
  51. String js = EncodeUtil.decodeBase64(widget.script);
  52. _webViewController.evaluateJavascript(js).then((result) {});
  53. }
  54. JavascriptChannel _imageLoaderJavascriptChannel(BuildContext context) {
  55. return JavascriptChannel(
  56. name: 'Loader',
  57. onMessageReceived: (JavascriptMessage message) {
  58. if (widget.onImagesLoad != null) {
  59. List list = jsonDecode(message.message);
  60. if (list.length != _images.length) {
  61. Future.delayed(Duration(milliseconds: 1000), () {
  62. _loadImages();
  63. });
  64. _images = list.map((item) => item.toString()).toList();
  65. widget.onImagesLoad(_images);
  66. } else {
  67. print('图片抓取结束');
  68. }
  69. }
  70. });
  71. }
  72. }