common_util.dart 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /// 截取后几位
  2. String stringExpand(String phone) {
  3. if (phone.length < 4) {
  4. return phone;
  5. }
  6. return phone.substring(phone.length - 4);
  7. }
  8. ///截取前几位
  9. String stringShort(String phone) {
  10. if (phone.length < 4) {
  11. return phone;
  12. }
  13. return phone.substring(0, 4);
  14. }
  15. String userNickName(String? remark, String phoneNumber, bool isIntercept) {
  16. if (remark != null && remark.isNotEmpty) {
  17. return isIntercept ? stringShort(remark) : remark;
  18. } else {
  19. return isIntercept ? stringExpand(phoneNumber) : phoneNumber;
  20. }
  21. }
  22. String time2TimeDesc(int? timeMillis) {
  23. if (timeMillis == 0 || timeMillis == null) {
  24. return "未知";
  25. }
  26. int currentTimeMillis = DateTime.now().millisecondsSinceEpoch;
  27. int timeDiff = currentTimeMillis - timeMillis;
  28. if (timeDiff < 0) {
  29. return "刚刚";
  30. }
  31. int minute = timeDiff ~/ 60 ~/ 1000;
  32. if (minute < 60) {
  33. if (minute == 0) {
  34. return "刚刚";
  35. }
  36. return "$minute分钟前";
  37. }
  38. int hour = minute ~/ 60;
  39. if (hour < 24) {
  40. return "$hour小时前";
  41. }
  42. int day = hour ~/ 24;
  43. if (day < 30) {
  44. return "$day天前";
  45. }
  46. int month = day ~/ 30;
  47. if (month < 12) {
  48. return "$month月前";
  49. }
  50. int year = month ~/ 12;
  51. return "$year年前";
  52. }
  53. String addressCheck(String? address) {
  54. if (address == null || address.isEmpty) {
  55. return "未开启定位";
  56. }
  57. return address;
  58. }