From 9057e04efad1b7d61c77a72e5c37a504d0aee935 Mon Sep 17 00:00:00 2001 From: doum <doum> Date: 星期五, 26 九月 2025 09:24:03 +0800 Subject: [PATCH] H5静态化 --- admin/src/components/base/BaseTable.vue | 266 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 266 insertions(+), 0 deletions(-) diff --git a/admin/src/components/base/BaseTable.vue b/admin/src/components/base/BaseTable.vue new file mode 100644 index 0000000..515df9e --- /dev/null +++ b/admin/src/components/base/BaseTable.vue @@ -0,0 +1,266 @@ +<script> +import BasePage from './BasePage' +import { isFunction } from 'element-ui' + +export default { + name: 'BaseTable', + extends: BasePage, + data () { + return { + // 鎺ュ彛 + api: null, + // 妯″潡鍚嶇О + module: '鏁版嵁', + // 閰嶇疆鏁版嵁 + configData: { + // id瀛楁 + 'field.id': 'id', + // 涓诲瓧娈� + 'field.main': 'name' + }, + // 鏄惁姝e湪鎵ц + isWorking: { + // 鎼滅储涓� + search: false, + // 鍒犻櫎涓� + delete: false, + // 瀵煎嚭涓� + export: false + }, + // 琛ㄦ牸鏁版嵁 + tableData: { + // 宸查�変腑鐨勬暟鎹� + selectedRows: [], + // 鎺掑簭鐨勫瓧娈� + sorts: [], + // 褰撳墠椤垫暟鎹� + list: [], + // 鍒嗛〉 + pagination: { + pageIndex: 1, + pageSize: 10, + total: 0 + } + } + } + }, + methods: { + /** + * 閰嶇疆 + * + * @param extParams 閰嶇疆鍙傛暟 + */ + config (extParams) { + if (extParams == null) { + throw new Error('Parameter can not be null of method \'config\' .') + } + if (extParams.api == null) { + throw new Error('Missing config option \'api\'.') + } + this.api = require('@/api' + extParams.api) + extParams.module && (this.module = extParams.module) + extParams['field.id'] && (this.configData['field.id'] = extParams['field.id']) + extParams['field.main'] && (this.configData['field.main'] = extParams['field.main']) + this.tableData.sorts = extParams.sorts + }, + /** + * 鎼滅储锛堢偣鍑绘悳绱㈡寜閽椂瑙﹀彂锛� + */ + search () { + this.handlePageChange(1) + }, + /** + * 瀵煎嚭Excel锛堢偣鍑诲鍑烘寜閽椂瑙﹀彂锛� + */ + exportExcel () { + this.__checkApi() + this.$dialog.exportConfirm('纭瀵煎嚭鍚楋紵') + .then(() => { + this.isWorking.export = true + this.api.exportExcel({ + page: this.tableData.pagination.pageIndex, + capacity: 1000000, + model: this.searchForm, + sorts: this.tableData.sorts + }) + .then(response => { + this.download(response) + }) + .catch(e => { + this.$tip.apiFailed(e) + }) + .finally(() => { + this.isWorking.export = false + }) + }) + .catch(() => {}) + }, + /** + * 閲嶇疆鎼滅储鏉′欢锛堢偣鍑婚噸缃寜閽椂瑙﹀彂锛� + */ + reset () { + this.$refs.searchForm.resetFields() + this.search() + }, + refresh () { + window.location.reload() + }, + /** + * 椤靛閲忓彉鏇村鐞嗭紙鍒囨崲椤靛閲忔椂瑙﹀彂锛� + * + * @param pageSize 椤靛閲� + */ + handleSizeChange (pageSize) { + this.tableData.pagination.pageSize = pageSize + this.search() + }, + /** + * 琛岄�変腑澶勭悊锛堢偣鍑婚�変腑鍒楁椂瑙﹀彂锛� + * + * @param selectedRows 宸查�変腑鐨勮鏁扮粍 + */ + handleSelectionChange (selectedRows) { + this.tableData.selectedRows = selectedRows + }, + /** + * 鎺掑簭锛堢偣鍑诲垪澶存帓搴忔椂瑙﹀彂锛� + * + * @param sortData 鎺掑簭鍙傛暟 + */ + handleSortChange (sortData) { + this.tableData.sorts = [] + if (sortData.order != null) { + this.tableData.sorts.push({ + property: sortData.column.sortBy, + direction: sortData.order === 'descending' ? 'DESC' : 'ASC' + }) + } + this.handlePageChange() + }, + /** + * 椤电爜鍙樻洿澶勭悊锛堝垎椤垫椂瑙﹀彂锛� + * + * @param pageIndex 鏂伴〉鐮� + */ + handlePageChange (pageIndex) { + this.__checkApi() + this.tableData.pagination.pageIndex = pageIndex || this.tableData.pagination.pageIndex + this.isWorking.search = true + this.api.fetchList({ + page: this.tableData.pagination.pageIndex, + capacity: this.tableData.pagination.pageSize, + model: this.searchForm, + sorts: this.tableData.sorts + }) + .then(data => { + this.tableData.list = data.records + this.tableData.pagination.total = data.total + }) + .catch(e => { + this.$tip.apiFailed(e) + }) + .finally(() => { + this.isWorking.search = false + }) + }, + /** + * 鍒犻櫎锛堢偣鍑昏鎿嶄綔/鍒犻櫎鏃惰Е鍙戯級 + * + * @param row 琛屽璞� + * @param childConfirm 鍒犻櫎瀛愯妭鐐规椂鏄惁杩涜浜屾纭 + */ + deleteById (row, childConfirm = true, call) { + this.__checkApi() + let message = `纭鍒犻櫎${this.module}鍚�?` + if (childConfirm && row.children != null && row.children.length > 0) { + message = `纭鍒犻櫎${this.module}銆�${row[this.configData['field.main']]}銆戝強鍏跺瓙${this.module}鍚�?` + } + this.$dialog.deleteConfirm(message) + .then(() => { + this.isWorking.delete = true + this.api.deleteById(row[this.configData['field.id']]) + .then(() => { + this.__afterDelete() + if (call) { + call() + } + }) + .catch(e => { + this.$tip.apiFailed(e) + }) + .finally(() => { + this.isWorking.delete = false + }) + }) + .catch(() => {}) + }, + /** + * 鎵归噺鍒犻櫎锛堢偣鍑绘壒閲忓垹闄ゆ椂瑙﹀彂锛� + * + * @param childConfirm 鍒犻櫎瀛愯妭鐐规椂鏄惁杩涜浜屾纭 + */ + deleteByIdInBatch (childConfirm = true, call) { + this.__checkApi() + if (this.tableData.selectedRows.length === 0) { + this.$tip.warning('璇疯嚦灏戦�夋嫨涓�鏉℃暟鎹�') + return + } + let message = `纭鍒犻櫎宸查�変腑鐨� ${this.tableData.selectedRows.length} 鏉�${this.module}璁板綍鍚�?` + if (childConfirm) { + const containChildrenRows = [] + for (const row of this.tableData.selectedRows) { + if (row.children != null && row.children.length > 0) { + containChildrenRows.push(row[this.configData['field.main']]) + } + } + if (containChildrenRows.length > 0) { + message = `鏈灏嗗垹闄�${this.module}銆�${containChildrenRows.join('銆�')}銆戝強鍏跺瓙${this.module}璁板綍锛岀‘璁ゅ垹闄ゅ悧锛焋 + } + } + this.$dialog.deleteConfirm(message) + .then(() => { + this.isWorking.delete = true + this.api.deleteByIdInBatch(this.tableData.selectedRows.map(row => row[this.configData['field.id']]).join(',')) + .then(() => { + this.__afterDelete(this.tableData.selectedRows.length) + if (call) { + call() + } + }) + .catch(e => { + this.$tip.apiFailed(e) + }) + .finally(() => { + this.isWorking.delete = false + }) + }) + .catch(() => {}) + }, + /** + * 鍒犻櫎鍚庡鐞嗭紝鍦ㄥ崟琛屽垹闄ゆ垨澶氳鍒犻櫎鍚庤皟鐢� + * + * @param deleteCount 鍒犻櫎鏁伴噺 + * @private + */ + __afterDelete (deleteCount = 1) { + this.$tip.apiSuccess('鍒犻櫎鎴愬姛') + // 鍒犻櫎褰撳墠椤垫渶鍚庝竴鏉¤褰曟椂鏌ヨ涓婁竴椤垫暟鎹� + if (this.tableData.list.length - deleteCount === 0) { + this.handlePageChange(this.tableData.pagination.pageIndex - 1 === 0 ? 1 : this.tableData.pagination.pageIndex - 1) + } else { + this.handlePageChange(this.tableData.pagination.pageIndex) + } + }, + /** + * 妫�鏌ユ帴鍙f槸鍚﹂厤缃紝鍦ㄨ皟鐢ㄦ帴鍙f椂璋冪敤 + * + * @private + */ + __checkApi () { + if (this.api == null) { + throw new Error('The page is not initialized, you can use method \'this.config\' to initialize this page.') + } + } + } +} +</script> -- Gitblit v1.9.3