mobile_skt/lib/util/common.util.dart

52 lines
1.4 KiB
Dart

import 'dart:math';
import 'package:crypto/crypto.dart';
import 'dart:convert';
class CommonUtil {
static String generateMd5(String input) {
List<int> bytes = utf8.encode(input); // 将字符串转换为字节码
Digest md5Hash = md5.convert(bytes); // 对字节码进行MD5加密
String encryptedString = md5Hash.toString(); // 获取加密后的字符串
return encryptedString;
}
static String generateTicketNumber() {
Random random = Random();
int randomTwoDigitNumber = random.nextInt(99);
String twoDigitNumber = randomTwoDigitNumber.toString().padLeft(2, '0');
// 格式化数字串
String formattedNumber =
'${random.nextInt(9999999).toString().padLeft(7, '0')}${random.nextInt(9999999).toString().padLeft(7, '0')}-$twoDigitNumber';
return formattedNumber;
}
static String firstUppercase(String text) {
return '${text[0].toUpperCase()}${text.substring(1)}';
}
static List<T> flattenTree<T extends TreeNode>(List<T> children) {
List<T> result = children;
for (T child in children) {
if (child.children != null) {
result.addAll(flattenTree(child.children as List<T>));
}
}
return result;
}
static String toNumberWithoutZero(num num) {
return num.toStringAsFixed(num.truncateToDouble() == num ? 0 : 2);
}
static String addZeroOnFront(num num) {
return num.toString().padLeft(2, '0');
}
}
class TreeNode<T> {
List<T>? children;
}