util.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import common from '../../common/js/common'
  2. // 存储localStorage
  3. export const setStore = (name, content) => {
  4. if (!name) return
  5. localStorage.setItem(name, JSON.stringify(content))
  6. }
  7. // 获取localStorage
  8. export const getStore = name => {
  9. if (!name) return
  10. return common.castEval(localStorage.getItem(name))
  11. }
  12. export function removeSpace(value) {
  13. return value.replace(/\s+/g, '')
  14. }
  15. export function formValidate(val, type) {
  16. const phoneReg = /(^1[3|4|5|7|8]\d{9}$)|(^09\d{8}$)/
  17. const emailReg = /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/
  18. if (val === '') {
  19. return false
  20. } else {
  21. // 非空验证
  22. if (type === 'require') {
  23. return !!removeSpace(val)
  24. }
  25. if (type === 'phone') {
  26. return phoneReg.test(val)
  27. }
  28. if (type === 'email') {
  29. return emailReg.test(val)
  30. }
  31. }
  32. }
  33. // 获取url参数
  34. export function getUrlKey(name) {
  35. return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ''])[1].replace(/\+/g, '%20')) || null
  36. }
  37. // 数组去重
  38. export function dedupe(array) {
  39. return Array.from(new Set(array))
  40. }
  41. // 去除重复对象
  42. // export function dedupeObject(obj) {
  43. // var uniques = []
  44. // var stringify = {}
  45. // for (var i = 0; i < obj.length; i++) {
  46. // var keys = Object.keys(obj[i])
  47. // keys.sort(function(a, b) {
  48. // return (Number(a) - Number(b))
  49. // })
  50. // var str = ''
  51. // for (var j = 0; j < keys.length; j++) {
  52. // str += JSON.stringify(keys[j])
  53. // str += JSON.stringify(obj[i][keys[j]])
  54. // }
  55. // if (!stringify.hasOwnProperty(str)) {
  56. // uniques.push(obj[i])
  57. // stringify[str] = true
  58. // }
  59. // }
  60. // uniques = uniques
  61. // return uniques
  62. // }