Files
pte/untitled/lib/utils/Tool.dart
2024-09-27 01:31:25 +08:00

105 lines
3.0 KiB
Dart
Raw Permalink 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:convert';
import 'package:convert/convert.dart';
import 'package:crypto/crypto.dart';
import 'dart:io';
import 'dart:math';
import 'package:path_provider/path_provider.dart';
import 'package:peach/config/_.dart';
import 'package:peach/utils/_.dart';
/*
* 工具函数主要给dart逻辑使用
*/
class Tool {
/// 按照指定下标将数组分割为二维数组
static List<List<T>> splitList<T>(List<T> list, int len) {
if (len <= 1) {
return [list];
}
List<List<T>> result = List();
int index = 1;
while (true) {
if (index * len < list.length) {
List<T> temp = list.skip((index - 1) * len).take(len).toList();
result.add(temp);
index++;
continue;
}
List<T> temp = list.skip((index - 1) * len).toList();
result.add(temp);
break;
}
return result;
}
/// 生成N位随机数
static String randomBit([int len = 15]) {
String scopeF = '123456789'; //首位
String scopeC = '0123456789'; //中间
String result = '';
for (int i = 0; i < len; i++) {
if (i == 0) {
result = scopeF[Random().nextInt(scopeF.length)];
} else {
result = result + scopeC[Random().nextInt(scopeC.length)];
}
}
return result;
}
/// 检查用户是否有权限打开【单手模式】
/// 权限规则:
/// 1对弹窗给的 androidid 进行 MD5 加密
/// 2在 com.example.untitled/files 文件夹中 用第一步的密文来创建文件
/// 3向文件中 写入 设备的 androidid
/// 通过验证,正常访问..
static Future<bool> checkIsOpen() async {
String androidId = Config.androidDeviceInfo.androidId;
Directory filesPath = await getExternalStorageDirectory();
File completePath = File("${filesPath.path}/${generateMd5(androidId)}");
if (completePath.existsSync()) {
String textMessage = await completePath.readAsString();
return textMessage == androidId ? true : false;
} else {
return false;
}
}
/*
* md5 加密
*/
static String generateMd5(String data) {
var content = new Utf8Encoder().convert(data);
var digest = md5.convert(content);
// 这里其实就是 digest.toString()
return hex.encode(digest.bytes);
}
/// 从本地读取用户信息
static Map userInfo() {
String username = SPreferences().getString("username");
String objectId = SPreferences().getString("objectId");
String createdAt = SPreferences().getString("createdAt");
String userType = SPreferences().getString("userType");
String userWeiXin = SPreferences().getString("userWeiXin");
Map info = new Map();
info.addAll({
'username': username,
'objectId': objectId,
'createdAt': createdAt,
'userType': userType,
'userWeiXin': userWeiXin,
});
return info;
}
/// 验证是否为合法的手机号
static bool isPhone(String str) {
return new RegExp('^((13[0-9])|(15[^4])|(166)|(17[0-8])|(18[0-9])|(19[8-9])|(147,145))\\d{8}\$').hasMatch(str);
}
}