my_tools.dart 12 KB

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