util.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. export default{
  2. // 密码只能是6位数字
  3. sixNum(str){
  4. let reg=/^\d{6}$/;
  5. if(!reg.test(str)){
  6. return false
  7. }else{
  8. return true
  9. }
  10. },
  11. // 密码由6-12位数字和字母组成
  12. numLetter(str){
  13. let reg=/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/
  14. if(!reg.test(str)){
  15. return false
  16. }else{
  17. return true
  18. }
  19. },
  20. // 字符串只能由1-10位中文、数字、英文组成,且必须有中文
  21. chineseNumLetter(str){
  22. let reg = /^([\u4E00-\uFA29]|[\uE7C7-\uE7F3]|[a-zA-Z0-9]){1,10}$/
  23. if(reg.test(str)){
  24. // console.log('第一层通过')
  25. let reg1 = new RegExp("[\\u4E00-\\u9FFF]+","g");
  26. if(reg1.test(str)){
  27. // console.log('第二层通过')
  28. return true
  29. }else{
  30. return false
  31. }
  32. }else{
  33. return false
  34. }
  35. },
  36. // 节流
  37. throttle(fn, wait = 200) {
  38. let last, timer,now;
  39. return function() {
  40. now = Date.now();
  41. if (last && now - last < wait) {
  42. clearTimeout(timer);
  43. timer = setTimeout(function() {
  44. last = now;
  45. fn.call(this, ...arguments);
  46. }, wait);
  47. } else {
  48. last = now;
  49. fn.call(this, ...arguments);
  50. }
  51. };
  52. },
  53. // 防抖
  54. debounce(fn, wait=200) {
  55. let timer;
  56. return function () {
  57. let context = this;
  58. let args = arguments;
  59. if (timer) clearTimeout(timer);
  60. timer = setTimeout(() => {
  61. fn.apply(context, args);
  62. }, wait)
  63. }
  64. },
  65. }
  66. // export function debounce (func, wait, immediate = true) {
  67. // let timeout
  68. // return function () {
  69. // if (timeout) clearTimeout(timeout)
  70. // if (immediate) {
  71. // var callNow = !timeout
  72. // timeout = setTimeout(() => {
  73. // timeout = null
  74. // }, wait)
  75. // if (callNow) func.apply(this, arguments)
  76. // } else {
  77. // timeout = setTimeout(function () {
  78. // func.apply(context, args)
  79. // }, wait)
  80. // }
  81. // }
  82. // }