tableColumnsMixin.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import request from '@/utils/request';
  2. export default {
  3. data() {
  4. return {
  5. newColumns: []
  6. };
  7. },
  8. mounted() {
  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. //对比接口返回和本地columns
  38. let { nlist, type } = this.columnsContrast(res.columnConfig);
  39. //有更新则更新服务缓存配置
  40. if (type) {
  41. this.saveColumns(nlist);
  42. }
  43. this.setStorage(this.cacheKeyUrl + 'Cols', nlist);
  44. // 更新列
  45. if (this._computedWatchers && this._computedWatchers.columns) {
  46. // console.log('columns 是计算属性');
  47. this.columnsVersion++;
  48. } else {
  49. // console.log('columns 是 data 属性');
  50. this.columns = [...this.columns];
  51. this.newColumns = [...this.newColumns];
  52. }
  53. }
  54. },
  55. //服务器和本地配置columns对比
  56. columnsContrast(list) {
  57. const key = 'label';
  58. var updateType = 0;
  59. let sList = list.filter((d, i, r) => {
  60. return d[key];
  61. });
  62. let devColumns =
  63. this.newColumns?.length > 0 ? this.newColumns : this.columns;
  64. let dList = devColumns.filter((d, i, r) => {
  65. return d[key] && d[key] !== '序号';
  66. });
  67. const keysA = new Set(sList.map((item) => item[key]));
  68. const keysB = new Set(dList.map((item) => item[key]));
  69. // 本地 比 缓存服务端 多的对象(新增)
  70. const added = dList.filter((item) => !keysA.has(item[key]));
  71. // 本地 比 缓存服务端 少的对象(删除)
  72. const removed = sList.filter((item) => !keysB.has(item[key]));
  73. const removedPropSet = new Set(removed.map((item) => item[key]));
  74. // 删除 缓存中 中被移除的对象
  75. const keptA = list.filter((item) => !removedPropSet.has(item[key]));
  76. added.forEach((item) => {
  77. //新增columns字段prop参数为必填
  78. if (item.prop) {
  79. item.id = item.prop;
  80. } else if (item.columnKey) {
  81. item.id = item.columnKey;
  82. } else {
  83. item.prop = item.label;
  84. item.id = item.label;
  85. }
  86. item.checked = true;
  87. });
  88. if (added.length > 0 || removed.length > 0) {
  89. updateType = 1;
  90. }
  91. // 合并保留的对象和新增的对象
  92. return { nlist: [...keptA, ...added], type: updateType };
  93. },
  94. // 提交columns配置
  95. async saveColumns(e) {
  96. const data = {
  97. tableId: this.cacheKeyUrl,
  98. columnConfig: e
  99. };
  100. const msg = await this.saveTableConfig(data);
  101. // console.log('列配置保存成功:', msg);
  102. return msg;
  103. },
  104. //获取localstorage缓存
  105. setStorage(key, value) {
  106. try {
  107. localStorage.setItem(key, JSON.stringify(value));
  108. } catch (e) {
  109. console.log('LocalStorage 存储错误:', e);
  110. if (e.name === 'QuotaExceededError') {
  111. this.clearCacheByPrefix(); //缓存不足,清除
  112. localStorage.setItem(key, JSON.stringify(value));
  113. }
  114. }
  115. },
  116. //缓存不足清除缓存
  117. clearCacheByPrefix() {
  118. const prefix = 'Cols'; // 标识后缀
  119. Object.keys(localStorage).forEach((key) => {
  120. if (key.endsWith(prefix)) {
  121. localStorage.removeItem(key);
  122. // console.log(`已清除缓存: ${key}`);
  123. }
  124. });
  125. // console.log('缓存清除完成');
  126. },
  127. //设置localstorage缓存
  128. getStorage(key) {
  129. try {
  130. const value = localStorage.getItem(key);
  131. return value ? JSON.parse(value) : null;
  132. } catch (e) {
  133. console.error('LocalStorage 解析错误:', e);
  134. return null;
  135. }
  136. },
  137. //防抖函数
  138. debounce(fn, delay) {
  139. let timer = null;
  140. return (...args) => {
  141. clearTimeout(timer);
  142. timer = setTimeout(() => {
  143. fn.apply(this, args);
  144. }, delay);
  145. };
  146. },
  147. //获取column记录接口
  148. async getByTableId(key) {
  149. try {
  150. const res = await request.get(
  151. `/sys/table-config/getByTableId/${key}`,
  152. {}
  153. );
  154. if (res.data.code == 0) {
  155. return res.data.data;
  156. }
  157. } catch (error) {
  158. console.error('获取列配置失败:', error);
  159. }
  160. },
  161. // 添加column记录接口
  162. async saveTableConfig(data) {
  163. try {
  164. const res = await request({
  165. url: '/sys/table-config/save',
  166. method: 'post',
  167. data
  168. });
  169. if (res.data.code == 0) {
  170. return res.data.data;
  171. }
  172. } catch (error) {
  173. console.error('保存列配置失败:', error);
  174. }
  175. }
  176. }
  177. };