flutter_kinetra/lib/kt_utils/kt_utils.dart

87 lines
3.4 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:in_app_purchase/in_app_purchase.dart';
import 'package:in_app_purchase_android/billing_client_wrappers.dart';
import 'package:in_app_purchase_android/in_app_purchase_android.dart';
import 'package:in_app_purchase_storekit/in_app_purchase_storekit.dart';
//工具类
class KtUtils {
///获取时区
static String getTimeZoneOffset(DateTime dateTime) {
Duration offset = dateTime.timeZoneOffset;
String sign = offset.isNegative ? '-' : '+';
int hours = offset.inHours.abs();
int minutes = (offset.inMinutes.abs() % 60);
return 'GMT$sign${hours.toString().padLeft(2, '0')}:${minutes.toString().padLeft(2, '0')}';
}
static bool isEmpty(dynamic value) {
return value == null ||
(value is Map && value.isEmpty) ||
(value is String && value.isEmpty) ||
(value is Iterable && value.isEmpty);
}
static bool isNotEmpty(dynamic value) {
return !isEmpty(value);
}
///获取iOS内购优惠价格安卓为获取原价
static String getDiscountPrice(ProductDetails? product, {bool showDiscount = false}) {
String price = product?.price ?? '';
if (product == null) return price;
if (Platform.isIOS) {
if (product is AppStoreProductDetails) {
debugPrint("skProduct优惠价格: ${product.skProduct.price}");
for (final discount in product.skProduct.discounts) {
debugPrint("优惠ID: ${discount.identifier}");
debugPrint("优惠价格: ${discount.price}");
debugPrint("priceLocale: ${discount.priceLocale.currencySymbol}-${discount.priceLocale.currencyCode}");
debugPrint("优惠周期: ${discount.subscriptionPeriod.numberOfUnits}");
debugPrint("支付模式: ${discount.paymentMode}"); // freeTrial, payAsYouGo, payUpFront
if (isNotEmpty(discount.price)) price = discount.priceLocale.currencySymbol + discount.price;
break;
}
}
} else if (Platform.isAndroid) {
if (product is GooglePlayProductDetails) {
for (SubscriptionOfferDetailsWrapper? offer in product.productDetails.subscriptionOfferDetails ?? []) {
debugPrint('Base plan ID: ${offer?.basePlanId}');
debugPrint('Offer ID: ${offer?.offerId}');
if (offer?.pricingPhases.isEmpty ?? true) continue;
for (PricingPhaseWrapper phase in offer!.pricingPhases) {
debugPrint('价格: ${phase.formattedPrice}');
debugPrint('周期: ${phase.billingPeriod}');
debugPrint('周期内收费次数: ${phase.recurrenceMode}');
if (isNotEmpty(phase.formattedPrice)) {
price = phase.formattedPrice;
if (showDiscount) return price;
}
}
}
}
}
return price;
}
static Map<String, dynamic> filterVipType(String? type) {
if (isEmpty(type)) {
return {};
}
List<Map<String, dynamic>> vipTypes = [
{'type': 'week', 'name': 'Weekly', 'shortName': 'week'},
{'type': 'month', 'name': 'Monthly', 'shortName': 'month'},
{'type': 'three_months', 'name': 'Quarterly', 'shortName': 'quarter'},
{'type': 'yearly', 'name': 'Yearly', 'shortName': 'year'},
{'type': 'year', 'name': 'Yearly', 'shortName': 'year'},
];
final filterType = vipTypes.where((item) => item['type'] == type).cast<Map<String, dynamic>?>().firstOrNull;
if (filterType != null) {
return filterType;
}
return {};
}
}