common.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import ajax from '@/utils/request.js'
  2. export default {
  3. getuid() {
  4. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
  5. var r = Math.random() * 16 | 0,
  6. v = c == 'x' ? r : (r & 0x3 | 0x8);
  7. return v.toString(16);
  8. });
  9. },
  10. // 提示信息
  11. toast(msg, icon = 'none', duration = 2000, mask) {
  12. uni.showToast({
  13. "icon": icon,
  14. "title": msg,
  15. "mask": mask || false,
  16. "duration": duration
  17. })
  18. },
  19. showLoading(msg) {
  20. uni.showLoading({
  21. 'title': msg,
  22. 'mask': true
  23. })
  24. },
  25. hidingLoading() {
  26. uni.hideLoading();
  27. },
  28. // 提示信息 底部
  29. toastBottom(msg, icon = 'none', duration = 2000, mask = false) {
  30. uni.showToast({
  31. "icon": icon,
  32. "title": msg,
  33. "mask": mask,
  34. "duration": duration,
  35. "position": 'bottom'
  36. })
  37. },
  38. // 路由跳转
  39. to(url) {
  40. uni.navigateTo({
  41. url: url,
  42. animationType: 'slide-in-right',
  43. animationDuration: 300
  44. })
  45. },
  46. toBar(url) {
  47. uni.switchTab({
  48. url: url
  49. })
  50. },
  51. // 返回上一页
  52. back() {
  53. uni.navigateBack({
  54. delta: 1,
  55. animationType: 'pop-out',
  56. animationDuration: 200
  57. })
  58. },
  59. // 验证邮箱
  60. isEmail(email) {
  61. let reg = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/;
  62. if (!reg.test(email)) {
  63. this.$common.toast('请输入正确邮箱格式')
  64. }
  65. return reg.test(email)
  66. },
  67. // 正则验证是否为手机号
  68. isPhone(str) {
  69. str = str + '';
  70. if ((/^1[3456789]\d{9}$/.test(str))) {
  71. return true;
  72. }
  73. return false;
  74. },
  75. isNum(str) {
  76. str = str + '';
  77. if ((/^[0-9]*$/.test(str)) || (/^[0-9]+(.[0-9]{1})?$/.test(str))) {
  78. return true;
  79. }
  80. return false;
  81. },
  82. isCarNo(str) {
  83. str = str + '';
  84. let reg =
  85. /^(([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z](([0-9]{5}[DF])|([DF]([A-HJ-NP-Z0-9])[0-9]{4})))|([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z][A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳使领]))$/
  86. if (reg.test(str)) {
  87. return true;
  88. }
  89. return false;
  90. },
  91. removeNull(obj) {
  92. var newObj = {};
  93. if (obj != undefined && obj != null) {
  94. for (var key in obj) {
  95. if (obj[key] === undefined || obj[key] === null || obj[key] === '') {
  96. //
  97. } else {
  98. newObj[key] = obj[key];
  99. }
  100. }
  101. }
  102. return newObj;
  103. },
  104. isAuth(code) {
  105. let list = uni.getStorageSync('perList');
  106. return list.indexOf(code) !== -1;
  107. },
  108. // 将时间戳转化为指定时间
  109. // way:方式(1=年月日,2=年月日时分秒)默认1, 也可以指定格式:yyyy-MM-dd HH:mm:ss
  110. forDate(inputTime, way) {
  111. var date = new Date(inputTime);
  112. var y = date.getFullYear();
  113. var m = date.getMonth() + 1;
  114. m = m < 10 ? ('0' + m) : m;
  115. var d = date.getDate();
  116. d = d < 10 ? ('0' + d) : d;
  117. var h = date.getHours();
  118. h = h < 10 ? ('0' + h) : h;
  119. var minute = date.getMinutes();
  120. var second = date.getSeconds();
  121. minute = minute < 10 ? ('0' + minute) : minute;
  122. second = second < 10 ? ('0' + second) : second;
  123. var ms = date.getMilliseconds();
  124. way = way || 1;
  125. // way == 1 年月日
  126. if (way === 1) {
  127. return y + '-' + m + '-' + d;
  128. }
  129. // way == 1 年月日时分秒
  130. if (way === 2) {
  131. return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;
  132. }
  133. // way == 具体格式 标准格式: yyyy-MM-dd HH:mm:ss
  134. if (typeof way == 'string') {
  135. return way.replace("yyyy", y).replace("MM", m).replace("dd", d).replace("HH", h).replace("mm", minute).replace("ss", second).replace("ms", ms);
  136. }
  137. return y + '-' + m + '-' + d;
  138. }
  139. }