Files

37 lines
1.0 KiB
Dart

// Author: fruit.com.cn
import 'package:dio/dio.dart';
import '../config/app_config.dart';
final RegExp _articlePattern = RegExp(
'<article>([\\s\\S]*)</article>',
caseSensitive: false,
);
/// 从协议页里取出正文。没有 article 时退回整页。
String legalArticle(String html) {
final match = _articlePattern.firstMatch(html);
final body = match?.group(1);
if (body == null || body.trim().isEmpty) return html;
return body.trim();
}
/// 拉取协议页 HTML。不走业务 JSON 包装。
Future<String> fetchLegalArticle(String url) async {
final dio = Dio(
BaseOptions(
connectTimeout: AppConfig.requestTimeout,
receiveTimeout: AppConfig.requestTimeout,
responseType: ResponseType.plain,
validateStatus: (code) => code != null && code >= 200 && code < 300,
),
);
final response = await dio.get<String>(url);
final body = response.data;
if (body == null || body.trim().isEmpty) {
throw StateError('empty legal page');
}
return legalArticle(body);
}