tableColumnsMixin.js 5.2 KB

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