From 1ebc5bbfa4f47e850326116066ae820eb0ab4418 Mon Sep 17 00:00:00 2001 From: lxm <1274844347@qq.com> Date: Mon, 16 Mar 2026 15:11:31 +0800 Subject: [PATCH] =?UTF-8?q?feat(utils):=20=E6=B7=BB=E5=8A=A0=E6=96=87?= =?UTF-8?q?=E6=A1=A3URL=E5=8A=A9=E6=89=8B=E4=BB=A5=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E5=A4=9A=E8=AF=AD=E8=A8=80=E6=96=87=E6=A1=A3=E9=93=BE=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增 doc_url_helper.dart 工具文件,提供 docLangPath、privacyPolicyUrl 和 userAgreementUrl 函数 根据当前语言环境动态生成对应的文档链接,支持多种语言版本 替换登录页、个人资料页和隐私政策弹窗中的硬编码URL,提高代码复用性和可维护性 --- lib/core/utils/doc_url_helper.dart | 27 +++++++++++++++++++ .../common/widgets/privacy_policy_dialog.dart | 19 +++---------- lib/modules/login/views/login_view.dart | 16 +++-------- lib/modules/profile/views/profile_view.dart | 19 +++---------- 4 files changed, 36 insertions(+), 45 deletions(-) create mode 100644 lib/core/utils/doc_url_helper.dart diff --git a/lib/core/utils/doc_url_helper.dart b/lib/core/utils/doc_url_helper.dart new file mode 100644 index 000000000..951ac88bf --- /dev/null +++ b/lib/core/utils/doc_url_helper.dart @@ -0,0 +1,27 @@ +import 'package:get/get.dart'; + +/// 根据当前语言返回 doc 目录名,如 "zh_cn"、"en"、"fr" +String docLangPath() { + final lang = Get.locale?.languageCode ?? 'en'; + final country = Get.locale?.countryCode?.toUpperCase() ?? ''; + if (lang == 'zh') { + if (country == 'CN') return 'zh_cn'; + if (country == 'HK') return 'zh_hk'; + return 'zh_tw'; + } + const supported = [ + 'ar', 'bg', 'cs', 'da', 'de', 'el', 'es', 'et', 'fa', 'fi', 'fr', + 'hi', 'hr', 'hu', 'id', 'is', 'it', 'ja', 'ko', 'lt', 'lv', 'ms', + 'nb', 'nl', 'pl', 'pt', 'ro', 'ru', 'sk', 'sl', 'sr', 'sv', 'th', + 'tl', 'tr', 'uk', 'vi', + ]; + return supported.contains(lang) ? lang : 'en'; +} + +/// 构建隐私政策 URL +String privacyPolicyUrl() => + 'https://www.voitrans.net/doc/${docLangPath()}/privacy_policy.html'; + +/// 构建用户协议 URL +String userAgreementUrl() => + 'https://www.voitrans.net/doc/${docLangPath()}/user_agreement.html'; diff --git a/lib/modules/common/widgets/privacy_policy_dialog.dart b/lib/modules/common/widgets/privacy_policy_dialog.dart index 16ecc154e..70d461b32 100644 --- a/lib/modules/common/widgets/privacy_policy_dialog.dart +++ b/lib/modules/common/widgets/privacy_policy_dialog.dart @@ -3,6 +3,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:get/get.dart'; import '../webview_page.dart'; +import '../../../core/utils/doc_url_helper.dart'; /// 隐私政策弹窗组件 class PrivacyPolicyDialog extends StatelessWidget { @@ -77,15 +78,8 @@ class PrivacyPolicyDialog extends StatelessWidget { alignment: PlaceholderAlignment.middle, child: GestureDetector( onTap: () { - String url = - 'https://www.voitrans.net/doc/privacy_policy.html'; - if (Get.locale?.languageCode != 'zh' || - Get.locale?.countryCode != 'CN') { - url = - 'https://www.voitrans.net/doc/privacy_policy_en.html'; - } Get.to(() => WebViewPage( - url: url, + url: privacyPolicyUrl(), title: 'privacyPolicy'.tr, )); }, @@ -109,15 +103,8 @@ class PrivacyPolicyDialog extends StatelessWidget { alignment: PlaceholderAlignment.middle, child: GestureDetector( onTap: () { - String url = - 'https://www.voitrans.net/doc/user_agreement.html'; - if (Get.locale?.languageCode != 'zh' || - Get.locale?.countryCode != 'CN') { - url = - 'https://www.voitrans.net/doc/user_agreement_en.html'; - } Get.to(() => WebViewPage( - url: url, + url: userAgreementUrl(), title: 'userAgreement'.tr, // 用户协议 )); }, diff --git a/lib/modules/login/views/login_view.dart b/lib/modules/login/views/login_view.dart index b5a82f869..a8c05a542 100644 --- a/lib/modules/login/views/login_view.dart +++ b/lib/modules/login/views/login_view.dart @@ -4,6 +4,7 @@ import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:font_awesome_flutter/font_awesome_flutter.dart'; import '../controllers/login_controller.dart'; import '../../common/webview_page.dart'; +import '../../../core/utils/doc_url_helper.dart'; class LoginView extends GetView { const LoginView({Key? key}) : super(key: key); @@ -596,13 +597,8 @@ class LoginView extends GetView { children: [ GestureDetector( onTap: () { - String url = 'https://www.voitrans.net/doc/user_agreement.html'; - if (Get.locale?.languageCode != 'zh' || - Get.locale?.countryCode != 'CN') { - url = 'https://www.voitrans.net/doc/user_agreement_en.html'; - } Get.to(() => WebViewPage( - url: url, + url: userAgreementUrl(), title: 'userAgreement'.tr, )); }, @@ -623,14 +619,8 @@ class LoginView extends GetView { ), GestureDetector( onTap: () { - String url = 'https://www.voitrans.net/doc/privacy_policy.html'; - if (Get.locale?.languageCode != 'zh' || - Get.locale?.countryCode != 'CN') { - url = 'https://www.voitrans.net/doc/privacy_policy_en.html'; - } - Get.to(() => WebViewPage( - url: url, + url: privacyPolicyUrl(), title: 'privacyPolicy'.tr, )); }, diff --git a/lib/modules/profile/views/profile_view.dart b/lib/modules/profile/views/profile_view.dart index d020d1290..1061cb75a 100644 --- a/lib/modules/profile/views/profile_view.dart +++ b/lib/modules/profile/views/profile_view.dart @@ -7,6 +7,7 @@ import '../../../routes/app_routes.dart'; import 'dart:io'; import 'dart:ui'; import '../../common/webview_page.dart'; +import '../../../core/utils/doc_url_helper.dart'; import 'unified_avatar.dart'; // ProfileView: 用户设置页面 @@ -695,15 +696,8 @@ class EditProfileView extends GetView { isDarkMode ? Colors.purple[300]! : Colors.purple[600]!, titleColor: isDarkMode ? Colors.white : Colors.black87, onTap: () { - String url = - 'https://www.voitrans.net/doc/user_agreement.html'; - if (Get.locale?.languageCode != 'zh' || - Get.locale?.countryCode != 'CN') { - url = - 'https://www.voitrans.net/doc/user_agreement_en.html'; - } Get.to(() => WebViewPage( - url: url, + url: userAgreementUrl(), title: 'userAgreement'.tr, )); }, @@ -726,15 +720,8 @@ class EditProfileView extends GetView { isDarkMode ? Colors.purple[300]! : Colors.purple[600]!, titleColor: isDarkMode ? Colors.white : Colors.black87, onTap: () { - String url = - 'https://www.voitrans.net/doc/privacy_policy.html'; - if (Get.locale?.languageCode != 'zh' || - Get.locale?.countryCode != 'CN') { - url = - 'https://www.voitrans.net/doc/privacy_policy_en.html'; - } Get.to(() => WebViewPage( - url: url, + url: privacyPolicyUrl(), title: 'privacyPolicy'.tr, )); },