my_tools.dart 11 KB

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