index.vue 9.17 KB
<template>
  <div class="simple-page">
    <el-card shadow="never">
      <el-tabs v-model="activeType" @tab-click="handleTypeChange">
        <el-tab-pane label="产品合同" name="product" />
        <el-tab-pane label="商品合同" name="goods" />
        <el-tab-pane label="套餐合同" name="package" />
      </el-tabs>
      <el-form size="small"
               :inline="true"
               :model="query">
        <el-form-item label="合同名称">
          <el-input v-model.trim="query.templateName" clearable />
        </el-form-item>
        <el-form-item label="商品/套餐编码">
          <el-input v-model.trim="query.packageCode" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">
            查询
          </el-button>
          <el-button @click="handleReset">
            重置
          </el-button>
        </el-form-item>
      </el-form>
      <div class="toolbar">
        <el-button size="small"
                   type="primary"
                   :disabled="!selection.length"
                   @click="batchInitialize">
          批量初始化
        </el-button>
        <el-button size="small"
                   :disabled="!selection.length"
                   @click="openReplaceDialog()">
          批量更换模板
        </el-button>
        <el-button size="small"
                   :loading="exporting"
                   @click="handleExport">
          导出
        </el-button>
      </div>
      <el-table v-loading="loading"
                empty-text="暂无合同"
                border
                :data="rows"
                @selection-change="selection = $event">
        <el-table-column type="selection" width="45" />
        <el-table-column label="商品/套餐编码"
                         min-width="150"
                         prop="packageCode" />
        <el-table-column label="商品/套餐名称"
                         min-width="180"
                         prop="packageName"
                         show-overflow-tooltip />
        <el-table-column label="合同模板"
                         min-width="180"
                         prop="templateName"
                         show-overflow-tooltip />
        <el-table-column label="更新时间"
                         min-width="160"
                         prop="updateTime" />
        <el-table-column label="操作" width="240">
          <template #default="scope">
            <el-button type="text" @click="openEditor(scope.row)">
              编辑
            </el-button>
            <el-button type="text" @click="openEditor(scope.row, true)">
              详情
            </el-button>
            <el-button type="text" @click="preview(scope.row)">
              预览/打印
            </el-button>
            <el-button type="text" @click="openReplaceDialog(scope.row)">
              更换
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination class="pagination"
                     layout="total, sizes, prev, pager, next, jumper"
                     :current-page="pageNum"
                     :page-size="pageSize"
                     :total="total"
                     @current-change="handlePageChange"
                     @size-change="handleSizeChange" />
    </el-card>
    <el-dialog title="更换合同模板"
               width="520px"
               :visible.sync="replaceVisible">
      <el-form label-width="100px">
        <el-form-item label="基础模板">
          <el-select v-model="replaceTemplateId" filterable>
            <el-option v-for="item in baseTemplates"
                       :key="item.id"
                       :label="item.templateName || item.name"
                       :value="item.id" />
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="replaceVisible = false">取消</el-button><el-button type="primary"
                                                                            :loading="replacing"
                                                                            @click="confirmReplace">
          确认更换
        </el-button>
      </span>
    </el-dialog>
    <el-dialog title="合同预览"
               top="4vh"
               width="900px"
               :visible.sync="previewVisible">
      <iframe v-if="previewUrl"
              class="preview-frame"
              title="合同预览"
              :src="previewUrl" />
      <el-empty v-else description="暂无预览内容" />
    </el-dialog>
  </div>
</template>

<script>
import {
	batchInitializeTemplates,
	batchReplaceTemplates,
	exportContractTemplates,
	exportPackageContracts,
	exportProductContracts,
	getBaseTemplateList,
	getContractTemplateList,
	getPackageContractList,
	getProductContractPage,
	initializePackageTemplate,
	printContractTemplate,
	replaceContractTemplate,
} from "@/api/contractManager";
import { normalizePage, openDownload, unwrapData } from "../migrationSupport";

export default {
	name: "ContractManager",
	data() {
		return {
			activeType: "product",
			query: { templateName: "", packageCode: "" },
			rows: [],
			selection: [],
			baseTemplates: [],
			pageNum: 1,
			pageSize: 20,
			total: 0,
			loading: false,
			exporting: false,
			replacing: false,
			replaceVisible: false,
			replaceTemplateId: "",
			replaceRow: null,
			previewVisible: false,
			previewUrl: "",
		};
	},
	created() {
		this.loadList();
	},
	methods: {
		getListApi() {
			if (this.activeType === "package") {
				return getPackageContractList;
			}
			if (this.activeType === "goods") {
				return getContractTemplateList;
			}
			return getProductContractPage;
		},
		async loadList() {
			this.loading = true;
			try {
				const result = normalizePage(await this.getListApi()({ ...this.query, pageNum: this.pageNum, pageSize: this.pageSize }));
				this.rows = result.rows;
				this.total = result.total;
			} catch (error) {
				this.rows = [];
				this.total = 0;
				this.$message.error("合同列表加载失败,请稍后重试");
			} finally {
				this.loading = false;
			}
		},
		handleTypeChange() {
			this.pageNum = 1;
			this.selection = [];
			return this.loadList();
		},
		handleSearch() {
			this.pageNum = 1;
			return this.loadList();
		},
		handleReset() {
			this.query = { templateName: "", packageCode: "" };
			return this.handleSearch();
		},
		handlePageChange(pageNum) {
			this.pageNum = pageNum;
			this.loadList();
		},
		handleSizeChange(pageSize) {
			this.pageSize = pageSize;
			this.handleSearch();
		},
		openEditor(row, readonly = false) {
			this.$router.push({
				name: "contractTemplateEdit",
				query: { id: row.id, type: this.activeType, readonly: readonly ? "1" : "0" },
			});
		},
		async batchInitialize() {
			try {
				await this.$confirm("确认初始化选中的合同模板吗?", "初始化确认", { type: "warning" });
				const ids = this.selection.map(item => item.id);
				const requestApi = this.activeType === "package" ? initializePackageTemplate : batchInitializeTemplates;
				await requestApi({ ids });
				this.$message.success("合同模板初始化成功");
				await this.loadList();
			} catch (error) {
				if (error !== "cancel" && error !== "close") {
					this.$message.error("合同模板初始化失败");
				}
			}
		},
		async openReplaceDialog(row = null) {
			this.replaceRow = row;
			this.replaceTemplateId = "";
			try {
				const result = unwrapData(await getBaseTemplateList({ contractType: this.activeType }));
				this.baseTemplates = result.list || result || [];
				this.replaceVisible = true;
			} catch (error) {
				this.$message.error("基础合同模板加载失败");
			}
		},
		async confirmReplace() {
			if (!this.replaceTemplateId) {
				return this.$message.error("请选择基础模板");
			}
			this.replacing = true;
			try {
				const rows = this.replaceRow ? [this.replaceRow] : this.selection;
				const params = { templateId: this.replaceTemplateId, ids: rows.map(item => item.id) };
				const requestApi = this.replaceRow ? replaceContractTemplate : batchReplaceTemplates;
				await requestApi(params);
				this.$message.success("合同模板更换成功");
				this.replaceVisible = false;
				await this.loadList();
			} catch (error) {
				this.$message.error("合同模板更换失败");
			} finally {
				this.replacing = false;
			}
		},
		async preview(row) {
			try {
				this.previewUrl = openDownload(await printContractTemplate({ id: row.id, packageCode: row.packageCode }));
				this.previewVisible = true;
			} catch (error) {
				this.$message.error("合同预览失败");
			}
		},
		async handleExport() {
			this.exporting = true;
			try {
				const requestApi = this.activeType === "package"
					? exportPackageContracts
					: this.activeType === "goods" ? exportContractTemplates : exportProductContracts;
				openDownload(await requestApi({ ...this.query }));
			} catch (error) {
				this.$message.error("合同导出失败");
			} finally {
				this.exporting = false;
			}
		},
	},
};
</script>

<style scoped>
.simple-page {
	padding: 16px;
}
.toolbar {
	margin-bottom: 16px;
}
.pagination {
	margin-top: 16px;
	text-align: right;
}
.preview-frame {
	width: 100%;
	height: 72vh;
	border: 0;
}
</style>