139 lines
3.9 KiB
Dart
139 lines
3.9 KiB
Dart
// 工具函数
|
||
import 'dart:io';
|
||
import 'dart:math';
|
||
import 'package:get/get_utils/get_utils.dart';
|
||
import 'package:path_provider/path_provider.dart';
|
||
|
||
class Utils {
|
||
static Future<String> getCookiePath() async {
|
||
Directory tempDir = await getApplicationSupportDirectory();
|
||
String tempPath = "${tempDir.path}/.plpl/";
|
||
Directory dir = Directory(tempPath);
|
||
bool b = await dir.exists();
|
||
if (!b) {
|
||
dir.createSync(recursive: true);
|
||
}
|
||
return tempPath;
|
||
}
|
||
|
||
static String numFormat(int number) {
|
||
String res = (number / 10000).toString();
|
||
if (int.parse(res.split('.')[0]) >= 1) {
|
||
return '${(number / 10000).toPrecision(1)}万';
|
||
} else {
|
||
return number.toString();
|
||
}
|
||
}
|
||
|
||
static String timeFormat(int time) {
|
||
// 1小时内
|
||
if (time < 3600) {
|
||
int minute = time ~/ 60;
|
||
double res = time / 60;
|
||
if (minute != res) {
|
||
return '$minute:${(time - minute * 60) < 10 ? '0${(time - minute * 60)}' : (time - minute * 60)}';
|
||
} else {
|
||
return minute.toString();
|
||
}
|
||
} else {
|
||
return '';
|
||
}
|
||
}
|
||
|
||
// 时间显示,刚刚,x分钟前
|
||
static String dateFormat(timeStamp, {formatType = 'list'}) {
|
||
// 当前时间
|
||
int time = (DateTime.now().millisecondsSinceEpoch / 1000).round();
|
||
// 对比
|
||
int _distance = (time - timeStamp).toInt();
|
||
// 当前年日期
|
||
String currentYearStr = 'MM月DD日 hh:mm';
|
||
String lastYearStr = 'YY年MM月DD日 hh:mm';
|
||
if (formatType == 'detail') {
|
||
currentYearStr = 'MM-DD hh:mm';
|
||
lastYearStr = 'YY-MM-DD hh:mm';
|
||
return CustomStamp_str(
|
||
timestamp: timeStamp,
|
||
date: lastYearStr,
|
||
toInt: false,
|
||
formatType: formatType);
|
||
}
|
||
if (_distance <= 60) {
|
||
return '刚刚';
|
||
} else if (_distance <= 3600) {
|
||
return '${(_distance / 60).floor()}分钟前';
|
||
} else if (_distance <= 43200) {
|
||
return '${(_distance / 60 / 60).floor()}小时前';
|
||
} else if (DateTime.fromMillisecondsSinceEpoch(time * 1000).year ==
|
||
DateTime.fromMillisecondsSinceEpoch(timeStamp * 1000).year) {
|
||
return CustomStamp_str(
|
||
timestamp: timeStamp,
|
||
date: currentYearStr,
|
||
toInt: false,
|
||
formatType: formatType);
|
||
} else {
|
||
return CustomStamp_str(
|
||
timestamp: timeStamp,
|
||
date: lastYearStr,
|
||
toInt: false,
|
||
formatType: formatType);
|
||
}
|
||
}
|
||
|
||
// 时间戳转时间
|
||
static String CustomStamp_str(
|
||
{int? timestamp, // 为空则显示当前时间
|
||
String? date, // 显示格式,比如:'YY年MM月DD日 hh:mm:ss'
|
||
bool toInt = true, // 去除0开头
|
||
String? formatType}) {
|
||
timestamp ??= (DateTime.now().millisecondsSinceEpoch / 1000).round();
|
||
String timeStr =
|
||
(DateTime.fromMillisecondsSinceEpoch(timestamp * 1000)).toString();
|
||
|
||
dynamic dateArr = timeStr.split(' ')[0];
|
||
dynamic timeArr = timeStr.split(' ')[1];
|
||
|
||
String YY = dateArr.split('-')[0];
|
||
String MM = dateArr.split('-')[1];
|
||
String DD = dateArr.split('-')[2];
|
||
|
||
String hh = timeArr.split(':')[0];
|
||
String mm = timeArr.split(':')[1];
|
||
String ss = timeArr.split(':')[2];
|
||
|
||
ss = ss.split('.')[0];
|
||
|
||
// 去除0开头
|
||
if (toInt) {
|
||
MM = (int.parse(MM)).toString();
|
||
DD = (int.parse(DD)).toString();
|
||
hh = (int.parse(hh)).toString();
|
||
mm = (int.parse(mm)).toString();
|
||
}
|
||
|
||
if (date == null) {
|
||
return timeStr;
|
||
}
|
||
|
||
if (formatType == 'list' && int.parse(DD) > DateTime.now().day - 2) {
|
||
return '昨天';
|
||
}
|
||
|
||
date = date
|
||
.replaceAll('YY', YY)
|
||
.replaceAll('MM', MM)
|
||
.replaceAll('DD', DD)
|
||
.replaceAll('hh', hh)
|
||
.replaceAll('mm', mm)
|
||
.replaceAll('ss', ss);
|
||
if (int.parse(DD) < DateTime.now().day) {
|
||
return date.split(' ')[0];
|
||
}
|
||
return date;
|
||
}
|
||
|
||
static String makeHeroTag(v) {
|
||
return v.toString() + Random().nextInt(9999).toString();
|
||
}
|
||
}
|