tableColumnsMixin.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. const list = this.getStorage(this.cacheKeyUrl + 'Cols');
  32. if (list) {
  33. let requireList = [];
  34. let currentList = this.getColumns()
  35. .filter((item) => item.headerSlot)
  36. .map((item) => item.prop);
  37. list.forEach((item) => {
  38. if (currentList.includes(item.prop) && !item.checked) {
  39. requireList.push(item.label);
  40. item.checked = true;
  41. }
  42. });
  43. if (requireList.length) {
  44. this.setStorage(this.cacheKeyUrl + 'Cols', list);
  45. if ('columns' in this.$options.computed) {
  46. this.columnsVersion++;
  47. } else {
  48. this.columns = [...this.columns];
  49. }
  50. this.$message.warning(
  51. `${requireList.toString()} 为必填项,无法隐藏!`
  52. );
  53. return;
  54. }
  55. }
  56. this.debouncedHandleColumnChange();
  57. },
  58. // 获取table-column配置
  59. async getTabColumns() {
  60. const res = await this.getByTableId(this.cacheKeyUrl);
  61. if (res?.columnConfig?.length > 0) {
  62. //对比接口返回和本地columns
  63. let { nlist, type } = this.columnsContrast(res.columnConfig);
  64. //有更新则更新服务缓存配置
  65. if (type) {
  66. this.saveColumns(nlist);
  67. }
  68. this.setStorage(this.cacheKeyUrl + 'Cols', nlist);
  69. // 更新列
  70. if (this._computedWatchers && this._computedWatchers.columns) {
  71. // console.log('columns 是计算属性');
  72. this.columnsVersion++;
  73. } else {
  74. // console.log('columns 是 data 属性');
  75. this.columns = [...this.columns];
  76. this.newColumns = [...this.newColumns];
  77. }
  78. }
  79. },
  80. getColumns() {
  81. if (typeof this.columns == 'function') {
  82. return this.columns();
  83. } else {
  84. return this.columns;
  85. }
  86. },
  87. //服务器和本地配置columns对比
  88. columnsContrast(list) {
  89. const key = 'label';
  90. var updateType = 0;
  91. let sList = list.filter((d, i, r) => {
  92. return d[key];
  93. });
  94. let devColumns = this.newColumns?.length
  95. ? this.newColumns
  96. : this.getColumns();
  97. let dList = devColumns.filter((d, i, r) => {
  98. return d[key] && d[key] !== '序号';
  99. });
  100. const keysA = new Set(sList.map((item) => item[key]));
  101. const keysB = new Set(dList.map((item) => item[key]));
  102. // 本地 比 缓存服务端 多的对象(新增)
  103. const added = dList.filter((item) => {
  104. return !keysA.has(item[key]) && (item.prop || item.label === '操作');
  105. });
  106. // 本地 比 缓存服务端 少的对象(删除)
  107. const removed = sList.filter((item) => !keysB.has(item[key]));
  108. const removedPropSet = new Set(removed.map((item) => item[key]));
  109. // 删除 缓存中 中被移除的对象
  110. const keptA = list.filter((item) => !removedPropSet.has(item[key]));
  111. added.forEach((item) => {
  112. //新增columns字段prop参数为必填
  113. if (item.prop) {
  114. item.id = item.prop;
  115. } else if (item.columnKey) {
  116. item.id = item.columnKey;
  117. }
  118. item.checked = true;
  119. });
  120. if (added.length > 0 || removed.length > 0) {
  121. updateType = 1;
  122. }
  123. // 更新项:key 存在但内容变化
  124. const dMap = new Map(dList.map((item) => [item[key], item]));
  125. const updated = keptA.map((sItem) => {
  126. const dItem = dMap.get(sItem[key]);
  127. if (dItem && dItem.prop && sItem.prop !== dItem.prop) {
  128. updateType = 1;
  129. // 记录旧值和新值
  130. const oldValue = sItem.prop;
  131. const newValue = dItem.prop;
  132. // 遍历所有属性,动态替换匹配旧值的字段
  133. const updatedItem = { ...sItem };
  134. Object.keys(updatedItem).forEach((k) => {
  135. if (updatedItem[k] === oldValue) {
  136. updatedItem[k] = newValue;
  137. }
  138. });
  139. return updatedItem;
  140. }
  141. return sItem;
  142. });
  143. // 合并保留的对象和新增的对象
  144. return { nlist: [...updated, ...added], type: updateType };
  145. },
  146. // 提交columns配置
  147. async saveColumns(e) {
  148. const data = {
  149. tableId: this.cacheKeyUrl,
  150. columnConfig: e
  151. };
  152. const msg = await this.saveTableConfig(data);
  153. // console.log('列配置保存成功:', msg);
  154. return msg;
  155. },
  156. //获取localstorage缓存
  157. setStorage(key, value) {
  158. try {
  159. localStorage.setItem(key, JSON.stringify(value));
  160. } catch (e) {
  161. console.log('LocalStorage 存储错误:', e);
  162. if (e.name === 'QuotaExceededError') {
  163. this.clearCacheByPrefix(); //缓存不足,清除
  164. localStorage.setItem(key, JSON.stringify(value));
  165. }
  166. }
  167. },
  168. //缓存不足清除缓存
  169. clearCacheByPrefix() {
  170. const prefix = 'Cols'; // 标识后缀
  171. Object.keys(localStorage).forEach((key) => {
  172. if (key.endsWith(prefix)) {
  173. localStorage.removeItem(key);
  174. // console.log(`已清除缓存: ${key}`);
  175. }
  176. });
  177. // console.log('缓存清除完成');
  178. },
  179. //设置localstorage缓存
  180. getStorage(key) {
  181. try {
  182. const value = localStorage.getItem(key);
  183. return value ? JSON.parse(value) : null;
  184. } catch (e) {
  185. console.error('LocalStorage 解析错误:', e);
  186. return null;
  187. }
  188. },
  189. //防抖函数
  190. debounce(fn, delay) {
  191. let timer = null;
  192. return (...args) => {
  193. clearTimeout(timer);
  194. timer = setTimeout(() => {
  195. fn.apply(this, args);
  196. }, delay);
  197. };
  198. },
  199. //获取column记录接口
  200. async getByTableId(key) {
  201. try {
  202. const res = await request.get(
  203. `/sys/table-config/getByTableId/${key}`,
  204. {}
  205. );
  206. if (res.data.code == 0) {
  207. return res.data.data;
  208. }
  209. } catch (error) {
  210. console.error('获取列配置失败:', error);
  211. }
  212. },
  213. // 添加column记录接口
  214. async saveTableConfig(data) {
  215. try {
  216. const res = await request({
  217. url: '/sys/table-config/save',
  218. method: 'post',
  219. data
  220. });
  221. if (res.data.code == 0) {
  222. return res.data.data;
  223. }
  224. } catch (error) {
  225. console.error('保存列配置失败:', error);
  226. }
  227. }
  228. }
  229. };