import 'package:dio/dio.dart';
import 'package:zhiying_comm/zhiying_comm.dart';

class TaobaoLoader {
  static Future<List<String>> 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<String, dynamic> json = Map<String, dynamic>.from(response.data);
    List<String> images = List();
    try {
      Map<String, dynamic> data = json['data'];
      List list = json['data']['wdescContent']['pages'];
      for (var item in list) {
        Logger.debug(item);
        RegExp exp = new RegExp("(?<=<img(.*?)>)(.*?)(?=</img>)");
        String image = exp.stringMatch(item.toString());
        if (image != null && image != '') {
          if (image.startsWith('//')) {
            images.add('http:' + image);
          } else
          if (image.startsWith('https://') || image.startsWith('http://')) {
            images.add(image);
          }
        }
      }
    } catch (err) {
      Logger.error(err);
    }


    return images;
  }
}
