You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
4.0 KiB

3 years ago
  1. let _debounceTimeout = null,
  2. _throttleRunning = false
  3. /**
  4. * 防抖
  5. * @param {Function} 执行函数
  6. * @param {Number} delay 延时ms
  7. */
  8. export const debounce = (fn, delay=500) => {
  9. clearTimeout(_debounceTimeout);
  10. _debounceTimeout = setTimeout(() => {
  11. fn();
  12. }, delay);
  13. }
  14. /**
  15. * 节流
  16. * @param {Function} 执行函数
  17. * @param {Number} delay 延时ms
  18. */
  19. export const throttle = (fn, delay=500) => {
  20. if(_throttleRunning){
  21. return;
  22. }
  23. _throttleRunning = true;
  24. fn();
  25. setTimeout(() => {
  26. _throttleRunning = false;
  27. }, delay);
  28. }
  29. /**
  30. * toast
  31. */
  32. export const msg = (title = '', param={}) => {
  33. if(!title) return;
  34. uni.showToast({
  35. title,
  36. duration: param.duration || 1500,
  37. mask: param.mask || false,
  38. icon: param.icon || 'none'
  39. });
  40. }
  41. /**
  42. * 检查登录
  43. * @return {Boolean}
  44. */
  45. export const isLogin = (options={}) => {
  46. const token = uni.getStorageSync('uniIdToken');
  47. if(token){
  48. return true;
  49. }
  50. if(options.nav !== false){
  51. uni.navigateTo({
  52. url: '/pages/auth/login'
  53. })
  54. }
  55. return false;
  56. }
  57. /**
  58. * 获取页面栈
  59. * @param {Number} preIndex为1时获取上一页
  60. * @return {Object}
  61. */
  62. export const prePage = (preIndex = 1) => {
  63. const pages = getCurrentPages();
  64. const prePage = pages[pages.length - (preIndex + 1)];
  65. return prePage.$vm;
  66. }
  67. /**
  68. * 格式化时间戳 Y-m-d H:i:s
  69. * @param {String} format Y-m-d H:i:s
  70. * @param {Number} timestamp 时间戳
  71. * @return {String}
  72. */
  73. export const date = (format, timeStamp) => {
  74. if('' + timeStamp.length <= 10){
  75. timeStamp = + timeStamp * 1000;
  76. }else{
  77. timeStamp = + timeStamp;
  78. }
  79. let _date = new Date(timeStamp),
  80. Y = _date.getFullYear(),
  81. m = _date.getMonth() + 1,
  82. d = _date.getDate(),
  83. H = _date.getHours(),
  84. i = _date.getMinutes(),
  85. s = _date.getSeconds();
  86. m = m < 10 ? '0' + m : m;
  87. d = d < 10 ? '0' + d : d;
  88. H = H < 10 ? '0' + H : H;
  89. i = i < 10 ? '0' + i : i;
  90. s = s < 10 ? '0' + s : s;
  91. return format.replace(/[YmdHis]/g, key=>{
  92. return {Y,m,d,H,i,s}[key];
  93. });
  94. }
  95. //二维数组去重
  96. export const getUnique = array => {
  97. let obj = {}
  98. return array.filter((item, index) => {
  99. let newItem = item + JSON.stringify(item)
  100. return obj.hasOwnProperty(newItem) ? false : obj[newItem] = true
  101. })
  102. }
  103. // 判断类型集合
  104. export const checkStr = (str, type) => {
  105. switch (type) {
  106. case 'mobile': //手机号码
  107. return /^1[3|4|5|6|7|8|9][0-9]{9}$/.test(str);
  108. case 'tel': //座机
  109. return /^(0\d{2,3}-\d{7,8})(-\d{1,4})?$/.test(str);
  110. case 'card': //身份证
  111. return /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(str);
  112. case 'mobileCode': //6位数字验证码
  113. return /^[0-9]{6}$/.test(str)
  114. case 'pwd': //密码以字母开头,长度在6~18之间,只能包含字母、数字和下划线
  115. return /^([a-zA-Z0-9_]){6,18}$/.test(str)
  116. case 'payPwd': //支付密码 6位纯数字
  117. return /^[0-9]{6}$/.test(str)
  118. case 'postal': //邮政编码
  119. return /[1-9]\d{5}(?!\d)/.test(str);
  120. case 'QQ': //QQ号
  121. return /^[1-9][0-9]{4,9}$/.test(str);
  122. case 'email': //邮箱
  123. return /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/.test(str);
  124. case 'money': //金额(小数点2位)
  125. return /^\d*(?:\.\d{0,2})?$/.test(str);
  126. case 'URL': //网址
  127. return /(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/.test(str)
  128. case 'IP': //IP
  129. return /((?:(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d)\\.){3}(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d))/.test(str);
  130. case 'date': //日期时间
  131. return /^(\d{4})\-(\d{2})\-(\d{2}) (\d{2})(?:\:\d{2}|:(\d{2}):(\d{2}))$/.test(str) || /^(\d{4})\-(\d{2})\-(\d{2})$/
  132. .test(str)
  133. case 'number': //数字
  134. return /^[0-9]$/.test(str);
  135. case 'english': //英文
  136. return /^[a-zA-Z]+$/.test(str);
  137. case 'chinese': //中文
  138. return /^[\\u4E00-\\u9FA5]+$/.test(str);
  139. case 'lower': //小写
  140. return /^[a-z]+$/.test(str);
  141. case 'upper': //大写
  142. return /^[A-Z]+$/.test(str);
  143. case 'HTML': //HTML标记
  144. return /<("[^"]*"|'[^']*'|[^'">])*>/.test(str);
  145. default:
  146. return true;
  147. }
  148. }