utils.dart 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. import 'dart:convert';
  2. import 'package:barcode_scan/barcode_scan.dart';
  3. import 'package:crypto/crypto.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:flutter/services.dart';
  6. import 'package:liftmanager/utils/theme_utils.dart';
  7. import 'package:liftmanager/utils/toast.dart';
  8. import 'package:keyboard_actions/keyboard_actions.dart';
  9. import 'package:url_launcher/url_launcher.dart';
  10. import 'package:liftmanager/utils/url.dart';
  11. import 'package:liftmanager/common/user_db.dart';
  12. import 'package:liftmanager/internal/account/model/user_entity.dart';
  13. import "package:intl/intl.dart";
  14. import 'package:liftmanager/routers/fluro_navigator.dart';
  15. import 'package:liftmanager/internal/wode/wode_router.dart';
  16. class Utils {
  17. // 根据关键字获取当前用户角色是否有该功能
  18. static bool getAuthByRouter(context,String router, [bool isAbnormal = true]) {
  19. List<AppMenus> userAuth = User().getUserAuth();
  20. bool flag = false;
  21. for (int i = 0; i < userAuth.length; i++) {
  22. print(userAuth[i].router);
  23. if (userAuth[i].router == router) {
  24. flag = true;
  25. break;
  26. }
  27. }
  28. // return true;
  29. if (flag) {
  30. return true;
  31. } else {
  32. if (isAbnormal) {
  33. // toasts('暂无权限,请先开通会员!');
  34. showAlert(
  35. context,
  36. "提示",
  37. "暂无权限,请先开通会员!",
  38. "确定",
  39. () {
  40. NavigatorUtils.goBack(context);
  41. NavigatorUtils.push(context, "${WodeRouter.vip}?id=");
  42. },
  43. txt2: "取消",
  44. onPre2: () {
  45. NavigatorUtils.goBack(context);
  46. },
  47. );
  48. }
  49. return false;
  50. }
  51. }
  52. // 图片异常处理
  53. static String getImagePath(String url,{bool isWater = false}) {
  54. RegExp exp = RegExp(r"(http|https):\/\/([\w.]+\/?)\S*");
  55. String endImg = "?x-oss-process=image/watermark,size_60,t_40,g_center,rotate_30,color_FFFFFF,text_5Zu-54mH5p2l5rqQIOKAnOeUteair-euoeWutuKAnQ==";
  56. if (url == null || url == "") {
  57. return "";
  58. } else {
  59. if (exp.hasMatch(url)) {
  60. if(isWater){
  61. return url + endImg;
  62. }else {
  63. return url;
  64. }
  65. } else {
  66. if(isWater){
  67. return imgFontUrl + url + endImg;
  68. }else {
  69. return imgFontUrl + url;
  70. }
  71. }
  72. }
  73. }
  74. // md5 加密
  75. static String generateMd5(String str) {
  76. return md5.convert(utf8.encode(str)).toString();
  77. }
  78. /// 调起拨号页
  79. static void launchTelURL(context,String phone) async {
  80. String url = 'tel:' + phone;
  81. if (await canLaunch(url)) {
  82. await launch(url);
  83. } else {
  84. toasts('拨号失败!');
  85. }
  86. }
  87. /// 调起二维码扫描页
  88. static Future<String> scan(context) async {
  89. try {
  90. return await BarcodeScanner.scan();
  91. } catch (e) {
  92. if (e is PlatformException) {
  93. if (e.code == BarcodeScanner.CameraAccessDenied) {
  94. toasts("没有相机权限!");
  95. }
  96. }
  97. }
  98. return null;
  99. }
  100. static KeyboardActionsConfig getKeyboardActionsConfig(
  101. BuildContext context, List<FocusNode> list) {
  102. return KeyboardActionsConfig(
  103. keyboardActionsPlatform: KeyboardActionsPlatform.IOS,
  104. keyboardBarColor: ThemeUtils.getKeyboardActionsColor(context),
  105. nextFocus: true,
  106. actions: List.generate(
  107. list.length,
  108. (i) => KeyboardAction(
  109. focusNode: list[i],
  110. closeWidget: const Padding(
  111. padding: const EdgeInsets.all(5.0),
  112. child: const Text("关闭"),
  113. ),
  114. )),
  115. );
  116. }
  117. static String getFileType(String fileName) {
  118. fileName = fileName.toLowerCase();
  119. var index1 = fileName.lastIndexOf(".");
  120. var index2 = fileName.length;
  121. var fileType = fileName.substring(index1, index2);
  122. switch (fileType) {
  123. case ".jpg":
  124. case ".jpeg":
  125. case ".jpe":
  126. case ".png":
  127. case ".bmp":
  128. case ".gif":
  129. return "image";
  130. case ".pdf":
  131. return "pdf";
  132. case ".json":
  133. return "json";
  134. case ".svg":
  135. case ".svgz":
  136. return "svg";
  137. case ".mp3":
  138. return "audio";
  139. case ".mp4":
  140. return "video";
  141. case ".mov":
  142. return "video";
  143. case ".htm":
  144. case ".html":
  145. return "html";
  146. case ".css":
  147. return "css";
  148. case ".csv":
  149. return "csv";
  150. case ".txt":
  151. case ".text":
  152. case ".conf":
  153. case ".def":
  154. case ".log":
  155. case ".in":
  156. return "plain";
  157. }
  158. return null;
  159. }
  160. static final DateFormat _monthFormat = new DateFormat("yyyy年MM月");
  161. static final DateFormat _dayFormat = new DateFormat("dd");
  162. static final DateFormat _firstDayFormat = new DateFormat("MMM dd");
  163. static final DateFormat _fullDayFormat = new DateFormat("EEE MMM dd, yyyy");
  164. static final DateFormat _apiDayFormat = new DateFormat("yyyy-MM-dd");
  165. static String formatMonth(DateTime d) => _monthFormat.format(d);
  166. static String formatDay(DateTime d) => _dayFormat.format(d);
  167. static String formatFirstDay(DateTime d) => _firstDayFormat.format(d);
  168. static String fullDayFormat(DateTime d) => _fullDayFormat.format(d);
  169. static String apiDayFormat(DateTime d) => _apiDayFormat.format(d);
  170. static const List<String> weekdays = const [
  171. "日",
  172. "一",
  173. "二",
  174. "三",
  175. "四",
  176. "五",
  177. "六"
  178. ];
  179. static List<DateTime> daysInMonth(DateTime month) {
  180. var first = firstDayOfMonth(month);
  181. var daysBefore = first.weekday;
  182. var firstToDisplay = first.subtract(new Duration(days: daysBefore));
  183. var last = Utils.lastDayOfMonth(month);
  184. var daysAfter = 7 - last.weekday;
  185. // If the last day is sunday (7) the entire week must be rendered
  186. if (daysAfter == 0) {
  187. daysAfter = 7;
  188. }
  189. var lastToDisplay = last.add(new Duration(days: daysAfter));
  190. return daysInRange(firstToDisplay, lastToDisplay).toList();
  191. }
  192. static bool isFirstDayOfMonth(DateTime day) {
  193. return isSameDay(firstDayOfMonth(day), day);
  194. }
  195. static bool isLastDayOfMonth(DateTime day) {
  196. return isSameDay(lastDayOfMonth(day), day);
  197. }
  198. static DateTime firstDayOfMonth(DateTime month) {
  199. return new DateTime(month.year, month.month);
  200. }
  201. static DateTime firstDayOfWeek(DateTime day) {
  202. /// Handle Daylight Savings by setting hour to 12:00 Noon
  203. /// rather than the default of Midnight
  204. day = new DateTime.utc(day.year, day.month, day.day, 12);
  205. /// Weekday is on a 1-7 scale Monday - Sunday,
  206. /// This Calendar works from Sunday - Monday
  207. var decreaseNum = day.weekday % 7;
  208. return day.subtract(new Duration(days: decreaseNum));
  209. }
  210. static DateTime lastDayOfWeek(DateTime day) {
  211. /// Handle Daylight Savings by setting hour to 12:00 Noon
  212. /// rather than the default of Midnight
  213. day = new DateTime.utc(day.year, day.month, day.day, 12);
  214. /// Weekday is on a 1-7 scale Monday - Sunday,
  215. /// This Calendar's Week starts on Sunday
  216. var increaseNum = day.weekday % 7;
  217. return day.add(new Duration(days: 7 - increaseNum));
  218. }
  219. /// The last day of a given month
  220. static DateTime lastDayOfMonth(DateTime month) {
  221. var beginningNextMonth = (month.month < 12)
  222. ? new DateTime(month.year, month.month + 1, 1)
  223. : new DateTime(month.year + 1, 1, 1);
  224. return beginningNextMonth.subtract(new Duration(days: 1));
  225. }
  226. /// Returns a [DateTime] for each day the given range.
  227. ///
  228. /// [start] inclusive
  229. /// [end] exclusive
  230. static Iterable<DateTime> daysInRange(DateTime start, DateTime end) sync* {
  231. var i = start;
  232. var offset = start.timeZoneOffset;
  233. while (i.isBefore(end)) {
  234. yield i;
  235. i = i.add(new Duration(days: 1));
  236. var timeZoneDiff = i.timeZoneOffset - offset;
  237. if (timeZoneDiff.inSeconds != 0) {
  238. offset = i.timeZoneOffset;
  239. i = i.subtract(new Duration(seconds: timeZoneDiff.inSeconds));
  240. }
  241. }
  242. }
  243. /// Whether or not two times are on the same day.
  244. static bool isSameDay(DateTime a, DateTime b) {
  245. return a.year == b.year && a.month == b.month && a.day == b.day;
  246. }
  247. static bool isSameWeek(DateTime a, DateTime b) {
  248. /// Handle Daylight Savings by setting hour to 12:00 Noon
  249. /// rather than the default of Midnight
  250. a = new DateTime.utc(a.year, a.month, a.day);
  251. b = new DateTime.utc(b.year, b.month, b.day);
  252. var diff = a.toUtc().difference(b.toUtc()).inDays;
  253. if (diff.abs() >= 7) {
  254. return false;
  255. }
  256. var min = a.isBefore(b) ? a : b;
  257. var max = a.isBefore(b) ? b : a;
  258. var result = max.weekday % 7 - min.weekday % 7 >= 0;
  259. return result;
  260. }
  261. static DateTime previousMonth(DateTime m) {
  262. var year = m.year;
  263. var month = m.month;
  264. if (month == 1) {
  265. year--;
  266. month = 12;
  267. } else {
  268. month--;
  269. }
  270. return new DateTime(year, month);
  271. }
  272. static DateTime nextMonth(DateTime m) {
  273. var year = m.year;
  274. var month = m.month;
  275. if (month == 12) {
  276. year++;
  277. month = 1;
  278. } else {
  279. month++;
  280. }
  281. return new DateTime(year, month);
  282. }
  283. static DateTime previousWeek(DateTime w) {
  284. return w.subtract(new Duration(days: 7));
  285. }
  286. static DateTime nextWeek(DateTime w) {
  287. return w.add(new Duration(days: 7));
  288. }
  289. }
  290. /// 默认dialog背景色为半透明黑色,这里修改源码改为透明
  291. Future<T> showTransparentDialog<T>({
  292. @required BuildContext context,
  293. bool barrierDismissible = true,
  294. WidgetBuilder builder,
  295. }) {
  296. final ThemeData theme = Theme.of(context, shadowThemeOnly: true);
  297. return showGeneralDialog(
  298. context: context,
  299. pageBuilder: (BuildContext buildContext, Animation<double> animation,
  300. Animation<double> secondaryAnimation) {
  301. final Widget pageChild = Builder(builder: builder);
  302. return SafeArea(
  303. child: Builder(builder: (BuildContext context) {
  304. return theme != null
  305. ? Theme(data: theme, child: pageChild)
  306. : pageChild;
  307. }),
  308. );
  309. },
  310. barrierDismissible: barrierDismissible,
  311. barrierLabel: MaterialLocalizations.of(context).modalBarrierDismissLabel,
  312. barrierColor: const Color(0x00FFFFFF),
  313. transitionDuration: const Duration(milliseconds: 150),
  314. transitionBuilder: _buildMaterialDialogTransitions,
  315. );
  316. }
  317. Widget _buildMaterialDialogTransitions(
  318. BuildContext context,
  319. Animation<double> animation,
  320. Animation<double> secondaryAnimation,
  321. Widget child) {
  322. return FadeTransition(
  323. opacity: CurvedAnimation(
  324. parent: animation,
  325. curve: Curves.easeOut,
  326. ),
  327. child: child,
  328. );
  329. }
  330. Future<T> showElasticDialog<T>({
  331. @required BuildContext context,
  332. bool barrierDismissible = true,
  333. WidgetBuilder builder,
  334. }) {
  335. final ThemeData theme = Theme.of(context, shadowThemeOnly: true);
  336. return showGeneralDialog(
  337. context: context,
  338. pageBuilder: (BuildContext buildContext, Animation<double> animation,
  339. Animation<double> secondaryAnimation) {
  340. final Widget pageChild = Builder(builder: builder);
  341. return SafeArea(
  342. child: Builder(builder: (BuildContext context) {
  343. return theme != null
  344. ? Theme(data: theme, child: pageChild)
  345. : pageChild;
  346. }),
  347. );
  348. },
  349. barrierDismissible: barrierDismissible,
  350. barrierLabel: MaterialLocalizations.of(context).modalBarrierDismissLabel,
  351. barrierColor: Colors.black54,
  352. transitionDuration: const Duration(milliseconds: 550),
  353. transitionBuilder: _buildDialogTransitions,
  354. );
  355. }
  356. Widget _buildDialogTransitions(
  357. BuildContext context,
  358. Animation<double> animation,
  359. Animation<double> secondaryAnimation,
  360. Widget child) {
  361. return FadeTransition(
  362. opacity: CurvedAnimation(
  363. parent: animation,
  364. curve: Curves.easeOut,
  365. ),
  366. child: SlideTransition(
  367. position: Tween<Offset>(begin: const Offset(0.0, 0.3), end: Offset.zero)
  368. .animate(CurvedAnimation(
  369. parent: animation,
  370. curve: animation.status != AnimationStatus.forward
  371. ? Curves.easeOutBack
  372. : ElasticOutCurve(0.85),
  373. )),
  374. child: child,
  375. ),
  376. );
  377. }
  378. // Color string2Color(String colorString){
  379. // int value = 0x00000000;
  380. // if(isNotEmpty(colorString)){
  381. // if(colorString[0] == "#"){
  382. // colorString = colorString.substring(1);
  383. // }
  384. // value = int.tryParse(colorString,raidx:16);
  385. // if(value != null){
  386. // if(value < 0xFF000000){
  387. // value += 0xFF000000;
  388. // }
  389. // }
  390. // }
  391. // return Color(value);
  392. // }