my_tools.dart 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. import 'dart:convert' as convert;
  2. import 'dart:convert';
  3. import 'dart:io';
  4. import 'package:bbyyy/beans/address_bean_entity.dart';
  5. import 'package:bbyyy/beans/new_msg_bean_entity.dart';
  6. import 'package:bbyyy/my_tools/easy_loading/easy_loading.dart';
  7. import 'package:bbyyy/my_tools/my_cookie.dart';
  8. import 'package:convert/convert.dart';
  9. import 'package:crypto/crypto.dart';
  10. import 'package:dio/dio.dart';
  11. import 'package:flutter/material.dart';
  12. import 'package:flutter/services.dart';
  13. import 'package:flutter_image_compress/flutter_image_compress.dart';
  14. import 'package:image_cropper/image_cropper.dart';
  15. import 'package:image_picker/image_picker.dart';
  16. import 'package:pull_to_refresh/pull_to_refresh.dart';
  17. import 'package:uuid/uuid.dart';
  18. import 'event_bus.dart';
  19. import 'month_day.dart';
  20. import 'my_colors.dart';
  21. class MyTools {
  22. toPage(context, page, then, {bool noBack = false}) {
  23. if (noBack) {
  24. Navigator.pushAndRemoveUntil(
  25. context,
  26. MaterialPageRoute(builder: (context) => page),
  27. (route) => route == null);
  28. } else {
  29. Navigator.push(context, MaterialPageRoute(builder: (context) => page))
  30. .then(then);
  31. }
  32. }
  33. hideKeyboard(BuildContext context) {
  34. FocusScope.of(context).requestFocus(FocusNode());
  35. }
  36. static String base64Encode(String data) {
  37. var content = convert.utf8.encode(data);
  38. var digest = convert.base64Encode(content);
  39. return digest;
  40. }
  41. }
  42. showToast(
  43. String status,
  44. ) {
  45. EasyLoading.showToast(status);
  46. }
  47. const _regExp = r"^[ZA-ZZa-z0-9_]+$";
  48. class onlyInputNumberAndWorkFormatter extends TextInputFormatter {
  49. @override
  50. TextEditingValue formatEditUpdate(
  51. TextEditingValue oldValue, TextEditingValue newValue) {
  52. if (newValue.text.length > 0) {
  53. if (RegExp(_regExp).firstMatch(newValue.text) != null) {
  54. return newValue;
  55. }
  56. return oldValue;
  57. }
  58. return newValue;
  59. }
  60. }
  61. final picker = ImagePicker();
  62. selectImage({bool isCover = true}) async {
  63. final pickedFile = await picker.getImage(source: ImageSource.gallery);
  64. if (pickedFile != null) {
  65. File cropperImg;
  66. if (isCover) {
  67. cropperImg = await ImageCropper.cropImage(
  68. sourcePath: pickedFile.path,
  69. cropStyle: CropStyle.rectangle,
  70. aspectRatio: CropAspectRatio(ratioX: 1, ratioY: 1),
  71. aspectRatioPresets: [
  72. isCover
  73. ? CropAspectRatioPreset.square
  74. : CropAspectRatioPreset.original,
  75. ],
  76. androidUiSettings: AndroidUiSettings(
  77. toolbarTitle: '图片剪裁',
  78. toolbarColor: MyColors.cFF4233,
  79. toolbarWidgetColor: Colors.white,
  80. initAspectRatio: isCover
  81. ? CropAspectRatioPreset.square
  82. : CropAspectRatioPreset.original,
  83. lockAspectRatio: isCover),
  84. iosUiSettings: IOSUiSettings(
  85. minimumAspectRatio: 1.0,
  86. ));
  87. } else {
  88. cropperImg = await ImageCropper.cropImage(
  89. sourcePath: pickedFile.path,
  90. aspectRatioPresets: [
  91. isCover
  92. ? CropAspectRatioPreset.square
  93. : CropAspectRatioPreset.original,
  94. ],
  95. androidUiSettings: AndroidUiSettings(
  96. toolbarTitle: '图片剪裁',
  97. toolbarColor: MyColors.cFF4233,
  98. toolbarWidgetColor: Colors.white,
  99. initAspectRatio: isCover
  100. ? CropAspectRatioPreset.square
  101. : CropAspectRatioPreset.original,
  102. lockAspectRatio: isCover),
  103. iosUiSettings: IOSUiSettings(
  104. minimumAspectRatio: 1.0,
  105. ));
  106. }
  107. if (cropperImg != null) {
  108. cropperImg = await compression(cropperImg, 1000000);
  109. return cropperImg;
  110. } else {
  111. return null;
  112. }
  113. }
  114. }
  115. int quality = 90;
  116. Future<File> compression(File cropperImg, int maxSize) async {
  117. int l = await cropperImg.length();
  118. print('$l');
  119. if (l > maxSize) {
  120. List<String> s = cropperImg.path.split('.');
  121. String type = s[s.length - 1];
  122. print(type);
  123. File f = await FlutterImageCompress.compressAndGetFile(
  124. cropperImg.path, cropperImg.path.replaceAll('.$type', '1.$type'),
  125. quality: quality);
  126. int l1 = await f.length();
  127. print('$l1');
  128. if (l > maxSize) {
  129. quality -= 10;
  130. return await compression(f, maxSize);
  131. } else {
  132. quality = 90;
  133. return f;
  134. }
  135. } else {
  136. return cropperImg;
  137. }
  138. }
  139. selectPicturesIndividually() async {
  140. final pickedFile = await picker.getImage(source: ImageSource.gallery);
  141. return await compression(File(pickedFile.path), 1000000);
  142. }
  143. endRe(RefreshController reController) {
  144. reController.refreshCompleted();
  145. reController.loadComplete();
  146. }
  147. String generateMd5(String data) {
  148. var content = new Utf8Encoder().convert(data);
  149. var digest = md5.convert(content);
  150. // 这里其实就是 digest.toString()
  151. return hex.encode(digest.bytes);
  152. }
  153. String reOS(String orderString) {
  154. orderString = orderString.replaceAll('\n', '');
  155. orderString = orderString.replaceAll(' ', '');
  156. orderString = orderString.replaceAll('\r', '');
  157. orderString = orderString.replaceAll('\t', '');
  158. orderString = orderString.replaceAll('\s', '');
  159. return orderString;
  160. }
  161. String getUUID() {
  162. var uuid = Uuid();
  163. var uuID = uuid.v4();
  164. return uuID.replaceAll('-', '');
  165. }
  166. getMessageContent(String c) {
  167. print(c);
  168. NewMsgBeanEntity entity = NewMsgBeanEntity().fromJson(json.decode(c));
  169. if (entity.content.type == '文字') {
  170. return entity.content.content;
  171. } else {
  172. return '[${entity.content.type}]';
  173. }
  174. }
  175. getTime(int timeIndex) {
  176. List<String> paidAt = [];
  177. String sTime = '00';
  178. String eTime = '23';
  179. var dateTime = DateTime.now();
  180. if (timeIndex == 0) {
  181. if (sTime != '00') {
  182. if (dateTime.hour >= int.parse(sTime)) {
  183. paidAt.add(
  184. '${dateTime.year}-${numberComplement('${dateTime.month}')}-${numberComplement('${dateTime.day}')} $sTime:00:00');
  185. paidAt.add(
  186. '${dateTime.add(Duration(days: 1)).year}-${numberComplement('${dateTime.add(Duration(days: 1)).month}')}-${numberComplement('${dateTime.add(Duration(days: 1)).day}')} $eTime:59:59');
  187. } else {
  188. paidAt.add(
  189. '${dateTime.subtract(Duration(days: 1)).year}-${numberComplement('${dateTime.subtract(Duration(days: 1)).month}')}-${numberComplement('${dateTime.subtract(Duration(days: 1)).day}')} $sTime:00:00');
  190. paidAt.add(
  191. '${dateTime.year}-${numberComplement('${dateTime.month}')}-${numberComplement('${dateTime.day}')} $eTime:59:59');
  192. }
  193. } else {
  194. paidAt.add(
  195. '${dateTime.year}-${numberComplement('${dateTime.month}')}-${numberComplement('${dateTime.day}')} $sTime:00:00');
  196. paidAt.add(
  197. '${dateTime.year}-${numberComplement('${dateTime.month}')}-${numberComplement('${dateTime.day}')} $eTime:59:59');
  198. }
  199. } else if (timeIndex == 1) {
  200. if (sTime == '00') {
  201. paidAt.add(
  202. '${dateTime.subtract(Duration(days: 1)).year}-${numberComplement('${dateTime.subtract(Duration(days: 1)).month}')}-${numberComplement('${dateTime.subtract(Duration(days: 1)).day}')} $sTime:00:00');
  203. paidAt.add(
  204. '${dateTime.subtract(Duration(days: 1)).year}-${numberComplement('${dateTime.subtract(Duration(days: 1)).month}')}-${numberComplement('${dateTime.subtract(Duration(days: 1)).day}')} $eTime:59:59');
  205. } else {
  206. if (dateTime.hour >= int.parse(sTime)) {
  207. paidAt.add(
  208. '${dateTime.subtract(Duration(days: 1)).year}-${numberComplement('${dateTime.subtract(Duration(days: 1)).month}')}-${numberComplement('${dateTime.subtract(Duration(days: 1)).day}')} $sTime:00:00');
  209. paidAt.add(
  210. '${dateTime.year}-${numberComplement('${dateTime.month}')}-${numberComplement('${dateTime.day}')} $eTime:59:59');
  211. } else {
  212. paidAt.add(
  213. '${dateTime.subtract(Duration(days: 2)).year}-${numberComplement('${dateTime.subtract(Duration(days: 2)).month}')}-${numberComplement('${dateTime.subtract(Duration(days: 2)).day}')} $sTime:00:00');
  214. paidAt.add(
  215. '${dateTime.subtract(Duration(days: 1)).year}-${numberComplement('${dateTime.subtract(Duration(days: 1)).month}')}-${numberComplement('${dateTime.subtract(Duration(days: 1)).day}')} $eTime:59:59');
  216. }
  217. }
  218. } else if (timeIndex == 2) {
  219. paidAt.add(
  220. '${dateTime.subtract(Duration(days: dateTime.weekday - 1)).year}-${numberComplement('${dateTime.subtract(Duration(days: dateTime.weekday - 1)).month}')}-${numberComplement('${dateTime.subtract(Duration(days: dateTime.weekday - 1)).day}')} 00:00:00');
  221. paidAt.add(
  222. '${dateTime.add(Duration(days: 7 - dateTime.weekday)).year}-${numberComplement('${dateTime.add(Duration(days: 7 - dateTime.weekday)).month}')}-${numberComplement('${dateTime.add(Duration(days: 7 - dateTime.weekday)).day}')} 23:59:59');
  223. } else if (timeIndex == 3) {
  224. DateTime lastWeek = dateTime.subtract(Duration(days: 7));
  225. paidAt.add(
  226. '${lastWeek.subtract(Duration(days: lastWeek.weekday - 1)).year}-${numberComplement('${lastWeek.subtract(Duration(days: lastWeek.weekday - 1)).month}')}-${numberComplement('${lastWeek.subtract(Duration(days: lastWeek.weekday - 1)).day}')} 00:00:00');
  227. paidAt.add(
  228. '${lastWeek.add(Duration(days: 7 - lastWeek.weekday)).year}-${numberComplement('${lastWeek.add(Duration(days: 7 - lastWeek.weekday)).month}')}-${numberComplement('${lastWeek.add(Duration(days: 7 - lastWeek.weekday)).day}')} 23:59:59');
  229. } else if (timeIndex == 4) {
  230. paidAt.add(
  231. '${dateTime.year}-${numberComplement('${dateTime.month}')}-01 00:00:00');
  232. paidAt.add(
  233. '${dateTime.year}-${numberComplement('${dateTime.month}')}-${numberComplement('${MonthDay().getDays(dateTime)}')} 23:59:59');
  234. } else if (timeIndex == 5) {
  235. DateTime firstMonth = dateTime.subtract(Duration(days: dateTime.day));
  236. paidAt.add('${firstMonth.year}-${firstMonth.month}-01 00:00:00');
  237. paidAt.add(
  238. '${firstMonth.year}-${numberComplement('${firstMonth.month}')}-${numberComplement('${MonthDay().getDays(firstMonth)}')} 23:59:59');
  239. }
  240. print(json.encode(paidAt));
  241. return paidAt;
  242. }
  243. numberComplement(String s) {
  244. if (s.length == 1) {
  245. return '0$s';
  246. } else {
  247. return '$s';
  248. }
  249. }
  250. String hideUID(String uid) {
  251. if(uid.isEmpty||uid.length<=4){
  252. return '';
  253. }
  254. return uid.replaceRange(0, 4, '****');
  255. }
  256. getAddressByLatitudeAndLongitude(double latitude, double longitude) {
  257. print('getAddressByLatitudeAndLongitude');
  258. Dio()
  259. .get(
  260. 'https://restapi.amap.com/v3/geocode/regeo?output=json&location=$longitude,$latitude&key=5dcd9f0ed7d51aefb5b2f73dba1069cb&radius=10&extensions=all')
  261. .then((value) {
  262. AddressBeanEntity addressBeanEntity =
  263. AddressBeanEntity().fromJson(json.decode(value.toString()));
  264. MyCookie().location = MyLocation(
  265. province: addressBeanEntity.regeocode.addressComponent.province,
  266. city: addressBeanEntity.regeocode.addressComponent.city,
  267. formattedAddress: addressBeanEntity.regeocode.formattedAddress,
  268. district: addressBeanEntity.regeocode.addressComponent.district,
  269. latitude: latitude,
  270. longitude: longitude);
  271. if (addressBeanEntity.regeocode.addressComponent.province != '四川省' ||
  272. addressBeanEntity.regeocode.addressComponent.city != '成都市') {
  273. EventBus().emit('getLocation');
  274. }
  275. });
  276. }