tableColumnsMixin.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import request from '@/utils/request';
  2. export default {
  3. data() {
  4. return {
  5. newColumns: []
  6. };
  7. },
  8. created() {
  9. //从服务器获取缓存列表配置
  10. this.getTabColumns();
  11. // 创建防抖函数并绑定this
  12. this.debouncedHandleColumnChange = this.debounce(
  13. this.handleColumnChangeImpl,
  14. 1000
  15. );
  16. },
  17. methods: {
  18. // 实际的列变更处理逻辑
  19. handleColumnChangeImpl() {
  20. try {
  21. const list = this.getStorage(this.cacheKeyUrl + 'Cols');
  22. if (list) {
  23. this.saveColumns(list);
  24. }
  25. } catch (error) {
  26. console.error('处理列配置出错:', error);
  27. }
  28. },
  29. // 列表变化回调
  30. handleColumnChange() {
  31. this.debouncedHandleColumnChange();
  32. },
  33. // 获取table-column配置
  34. async getTabColumns() {
  35. const res = await this.getByTableId(this.cacheKeyUrl);
  36. if (res?.columnConfig?.length > 0) {
  37. this.setStorage(this.cacheKeyUrl + 'Cols', res.columnConfig);
  38. // 更新列
  39. if (this._computedWatchers && this._computedWatchers.columns) {
  40. // console.log('columns 是计算属性');
  41. this.columnsVersion++;
  42. } else {
  43. // console.log('columns 是 data 属性');
  44. this.columns = [...this.columns];
  45. this.newColumns = [...this.newColumns];
  46. }
  47. }
  48. },
  49. // 提交columns配置
  50. async saveColumns(e) {
  51. const data = {
  52. tableId: this.cacheKeyUrl,
  53. columnConfig: e
  54. };
  55. const msg = await this.saveTableConfig(data);
  56. // console.log('列配置保存成功:', msg);
  57. return msg;
  58. },
  59. //获取localstorage缓存
  60. setStorage(key, value) {
  61. try {
  62. localStorage.setItem(key, JSON.stringify(value));
  63. } catch (e) {
  64. console.log('LocalStorage 存储错误:', e);
  65. if (e.name === 'QuotaExceededError') {
  66. this.clearCacheByPrefix(); //缓存不足,清除
  67. localStorage.setItem(key, JSON.stringify(value));
  68. }
  69. }
  70. },
  71. //缓存不足清除缓存
  72. clearCacheByPrefix() {
  73. const prefix = 'Cols'; // 标识后缀
  74. Object.keys(localStorage).forEach((key) => {
  75. if (key.endsWith(prefix)) {
  76. localStorage.removeItem(key);
  77. // console.log(`已清除缓存: ${key}`);
  78. }
  79. });
  80. // console.log('缓存清除完成');
  81. },
  82. //设置localstorage缓存
  83. getStorage(key) {
  84. try {
  85. const value = localStorage.getItem(key);
  86. return value ? JSON.parse(value) : null;
  87. } catch (e) {
  88. console.error('LocalStorage 解析错误:', e);
  89. return null;
  90. }
  91. },
  92. //防抖函数
  93. debounce(fn, delay) {
  94. let timer = null;
  95. return (...args) => {
  96. clearTimeout(timer);
  97. timer = setTimeout(() => {
  98. fn.apply(this, args);
  99. }, delay);
  100. };
  101. },
  102. //获取column记录接口
  103. async getByTableId(key) {
  104. try {
  105. const res = await request.get(
  106. `/sys/table-config/getByTableId/${key}`,
  107. {}
  108. );
  109. if (res.data.code == 0) {
  110. return res.data.data;
  111. }
  112. } catch (error) {
  113. console.error('获取列配置失败:', error);
  114. }
  115. },
  116. // 添加column记录接口
  117. async saveTableConfig(data) {
  118. try {
  119. const res = await request({
  120. url: '/sys/table-config/save',
  121. method: 'post',
  122. data
  123. });
  124. if (res.data.code == 0) {
  125. return res.data.data;
  126. }
  127. } catch (error) {
  128. console.error('保存列配置失败:', error);
  129. }
  130. }
  131. }
  132. };