my_tools.dart 13 KB

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