useCommon.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import storageUtil from "@utility/storageUtil.js";
  2. import commonUtil from "@utility/commonUtil.js";
  3. import {onActivated, onDeactivated, onMounted} from "vue";
  4. /**
  5. * @method useLifecycle 组件生命周期
  6. * @param option {Object} 生命周期配置
  7. * @property init {Function} 初始化函数,组件初始化时调用
  8. * @property load {Function} 加载函数,组件初始化和激活时调用
  9. * @property unLoad {Function} 卸载函数,组件卸载时调用
  10. * */
  11. const useLifecycle = (option)=>{
  12. onMounted(() => {
  13. if(commonUtil.isFun(option.init)) option.init();
  14. })
  15. onActivated(()=>{
  16. if(commonUtil.isFun(option.load)) option.load();
  17. });
  18. onDeactivated(()=>{
  19. if(commonUtil.isFun(option.unLoad)) option.unLoad();
  20. });
  21. };
  22. /**
  23. * @method useDebounce 防抖hook,多次频繁操作以最后一次为准
  24. * @param fn {Function} 函数
  25. * @param delay {Number} 延时时间
  26. * @returns {Function}
  27. * */
  28. const useDebounce = (fn, delay = 300) => {
  29. let timer = null
  30. return (...args) => {
  31. clearTimeout(timer)
  32. timer = setTimeout(() => {
  33. fn.call(this, ...args)
  34. }, delay);
  35. }
  36. };
  37. /**
  38. * @method useThrottle 节流hook,多次频繁操作只会执行一次
  39. * @param fn {Function} 函数
  40. * @param delay {Number} 延时时间
  41. * @returns {Function}
  42. * */
  43. const useThrottle = (fn, delay = 300) => {
  44. let timer = null
  45. return (...args) => {
  46. if (!timer) {
  47. timer = setTimeout(() => {
  48. fn.apply(this, args);
  49. clearTimeout(timer)
  50. timer = null;
  51. }, delay);
  52. }
  53. };
  54. };
  55. /**
  56. * @method useGetJsonDeepValue 获取多级json值
  57. * @param obj {Object} json对象
  58. * @param key {String} 获取值的可以,如:key.key1
  59. * @returns {String} 返回获取道的json值
  60. * */
  61. const useGetJsonDeepValue = (obj, key) => {
  62. if (!commonUtil.isJson(obj) || !key) return;
  63. let keyArray = '';
  64. let value = key;
  65. if (commonUtil.isStr(key)) {
  66. keyArray = key.split('.');
  67. value = obj[keyArray[0]];
  68. if (keyArray.length > 1) {
  69. for (let index = 1; index < keyArray.length; index++) {
  70. if (!value) break;
  71. value = value[keyArray[index]];
  72. }
  73. }
  74. }
  75. return value;
  76. }
  77. /**
  78. * @method manualCopying 手动复制对象
  79. * @param value {Object} 待复制对象
  80. * @returns {Object} 复制后的对象
  81. * */
  82. const manualCopying = (value) => {
  83. return JSON.parse(JSON.stringify(value))
  84. }
  85. /**
  86. * @method useEnv 获取env变量
  87. * @returns {Object} 转换后的值
  88. * */
  89. const useEnv = () => {
  90. const env = import.meta.env;
  91. return {
  92. nodeEnv: env.VITE_NODE_ENV,
  93. baseUrl: env.VITE_BASE_URL,
  94. fileUrl: env.VITE_FILE_URL,
  95. dateFormat: env.VITE_DATE_FORMAT
  96. }
  97. };
  98. export {
  99. useDebounce,
  100. useThrottle,
  101. useGetJsonDeepValue,
  102. useEnv,
  103. useLifecycle,
  104. manualCopying
  105. }