first commit
This commit is contained in:
105
untitled/lib/utils/Tool.dart
Normal file
105
untitled/lib/utils/Tool.dart
Normal file
@@ -0,0 +1,105 @@
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user