| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- // HR 线上 Word 模板 —— 由 kd-aiot-frontend-hr-H5/src/api/onlineDocument.ts 迁移,去掉演示模式分支
- import { get, postJ, putJ } from "@/utils/request";
- import Vue from "vue";
- export const TEMPLATE_STATUS_LABELS = {
- DRAFT: "草稿",
- ENABLED: "已启用",
- DISABLED: "已停用",
- };
- export const TEMPLATE_STATUS_FILTERS = [
- { value: "all", label: "全部" },
- { value: "ENABLED", label: "已启用" },
- { value: "DRAFT", label: "草稿" },
- { value: "DISABLED", label: "已停用" },
- ];
- function ok(res) {
- return res && Number(res.code) === 0;
- }
- function compact(object = {}) {
- return Object.keys(object).reduce((result, key) => {
- const value = object[key];
- if (value !== "" && value !== null && value !== undefined) result[key] = value;
- return result;
- }, {});
- }
- function keepId(value) {
- if (value === undefined || value === null || value === "") return undefined;
- return String(value).trim();
- }
- function extractId(result) {
- if (result == null) return null;
- if (typeof result === "object") {
- return result.id != null ? result.id : result.templateId != null ? result.templateId : null;
- }
- return result;
- }
- function asList(data) {
- if (Array.isArray(data)) return data;
- if (data && Array.isArray(data.list)) return data.list;
- if (data && Array.isArray(data.records)) return data.records;
- if (data && typeof data === "object" && data.id != null) return [data];
- return [];
- }
- /** 后端「HR线上Word模板信息」→ 页面形状 */
- export function adaptOnlineTemplate(raw = {}) {
- if (!raw || typeof raw !== "object") return null;
- const status = String(raw.templateStatus || "DRAFT").toUpperCase();
- const enabled =
- raw.status === undefined || raw.status === null ? status === "ENABLED" : Number(raw.status) === 1;
- return Object.assign({}, raw, {
- id: raw.id != null ? raw.id : raw.templateId != null ? raw.templateId : "",
- templateName: raw.templateName || "",
- templateCode: raw.templateCode || "",
- categoryId: raw.categoryId === undefined || raw.categoryId === null ? "" : String(raw.categoryId),
- templateStatus: status,
- templateStatusLabel: TEMPLATE_STATUS_LABELS[status] || status,
- versionStatus: raw.versionStatus || "",
- currentVersionNo: raw.currentVersionNo != null ? raw.currentVersionNo : 1,
- documentContent: raw.documentContent || "",
- variablesJson: raw.variablesJson || "",
- changeRemark: raw.changeRemark || "",
- remark: raw.remark || "",
- createTime: raw.createTime || "",
- status: raw.status,
- enabled,
- });
- }
- export function adaptOnlineTemplateVersion(raw = {}, index = 0) {
- return {
- id: raw.id != null ? raw.id : raw.versionId != null ? raw.versionId : `v-${index}`,
- versionNo: raw.currentVersionNo != null ? raw.currentVersionNo : raw.versionNo != null ? raw.versionNo : index + 1,
- versionStatus: raw.versionStatus || "",
- changeRemark: raw.changeRemark || "",
- createTime: raw.createTime || "",
- };
- }
- /** 模板分页 GET /hr/online-documents/templates */
- export async function getOnlineTemplatePage(params = {}, loding = true) {
- const res = await get(
- Vue.prototype.apiUrl + "/hr/online-documents/templates",
- compact({
- pageNum: params.pageNum || 1,
- size: params.size || 200,
- templateName: params.templateName,
- templateStatus: params.templateStatus,
- }),
- loding
- );
- const data = (res && res.data) || {};
- return {
- list: asList(data)
- .map(adaptOnlineTemplate)
- .filter(Boolean),
- count: Number(data.count != null ? data.count : data.total != null ? data.total : 0),
- };
- }
- /** 模板详情 GET /hr/online-documents/templates/{id} */
- export async function getOnlineTemplateById(id, loding = true) {
- const res = await get(Vue.prototype.apiUrl + `/hr/online-documents/templates/${id}`, {}, loding);
- if (!ok(res)) return null;
- return adaptOnlineTemplate(res.data || {});
- }
- /** 新增模板 POST /hr/online-documents/templates */
- export async function createOnlineTemplate(payload, loding = true) {
- const res = await postJ(Vue.prototype.apiUrl + "/hr/online-documents/templates", payload, loding);
- if (!ok(res)) throw new Error(res.message || "新增模板失败");
- return extractId(res.data);
- }
- /** 修改模板草稿 PUT /hr/online-documents/templates/{id} */
- export async function updateOnlineTemplate(id, payload, loding = true) {
- const res = await putJ(Vue.prototype.apiUrl + `/hr/online-documents/templates/${id}`, payload, loding);
- if (!ok(res)) throw new Error(res.message || "保存模板失败");
- return id;
- }
- export async function enableOnlineTemplate(id, loding = true) {
- const res = await postJ(Vue.prototype.apiUrl + `/hr/online-documents/templates/${id}/enable`, {}, loding);
- if (!ok(res)) throw new Error(res.message || "启用模板失败");
- return id;
- }
- export async function disableOnlineTemplate(id, loding = true) {
- const res = await postJ(Vue.prototype.apiUrl + `/hr/online-documents/templates/${id}/disable`, {}, loding);
- if (!ok(res)) throw new Error(res.message || "停用模板失败");
- return id;
- }
- /** 版本历史 GET /hr/online-documents/templates/{id}/versions */
- export async function getOnlineTemplateVersions(id, loding = true) {
- const res = await get(Vue.prototype.apiUrl + `/hr/online-documents/templates/${id}/versions`, {}, loding);
- if (!ok(res)) return [];
- return asList(res.data).map((raw, idx) => adaptOnlineTemplateVersion(raw, idx));
- }
- /** 创建新版本 POST /hr/online-documents/templates/{id}/versions */
- export async function createOnlineTemplateVersion(id, payload = {}, loding = true) {
- const res = await postJ(
- Vue.prototype.apiUrl + `/hr/online-documents/templates/${id}/versions`,
- compact({
- documentContent: payload.documentContent,
- variablesJson: payload.variablesJson,
- changeRemark: payload.changeRemark,
- }),
- loding
- );
- if (!ok(res)) throw new Error(res.message || "创建版本失败");
- return extractId(res.data);
- }
- /** 表单 → 后端「HR线上Word模板保存参数」 */
- export function buildOnlineTemplatePayload(form = {}) {
- return compact({
- id: keepId(form.id),
- templateName: form.templateName,
- templateCode: form.templateCode,
- categoryId: keepId(form.categoryId),
- deptId: keepId(form.deptId),
- remark: form.remark,
- documentContent: form.documentContent,
- variablesJson: form.variablesJson,
- });
- }
- /** 模板列表 */
- export async function loadOnlineTemplateList(params = {}, loding = true) {
- const { templateName = "", templateStatus = "" } = params;
- const page = await getOnlineTemplatePage(
- {
- templateName: templateName || undefined,
- templateStatus: templateStatus && templateStatus !== "all" ? templateStatus : undefined,
- },
- loding
- );
- const list = page.list
- .filter((row) => !templateStatus || templateStatus === "all" || row.templateStatus === templateStatus)
- .filter((row) => {
- if (!templateName) return true;
- const text = `${row.templateName}${row.templateCode}`.toLowerCase();
- return text.includes(String(templateName).toLowerCase());
- })
- .sort((a, b) => String(b.createTime).localeCompare(String(a.createTime)));
- return { list, count: page.count, source: "api" };
- }
- /** 模板详情;传列表行时优先用行内数据兜底 */
- export async function loadOnlineTemplateDetail(id, fallbackRow = null, loding = true) {
- if (!id) return fallbackRow || null;
- const row = await getOnlineTemplateById(id, loding);
- return row ? Object.assign({}, fallbackRow || {}, row) : fallbackRow || null;
- }
- /** 保存模板:有 id 走 PUT,无 id 走 POST */
- export async function saveOnlineTemplate({ form, existingId = null } = {}) {
- if (!form) throw new Error("模板参数为空");
- const id = existingId || form.id || null;
- const payload = buildOnlineTemplatePayload(Object.assign({}, form, { id }));
- if (id) {
- await updateOnlineTemplate(id, payload);
- return id;
- }
- return createOnlineTemplate(payload);
- }
- /** 启用 / 停用切换 */
- export async function toggleOnlineTemplate(row = {}) {
- if (!row.id) throw new Error("缺少模板主键");
- const nextEnabled = !row.enabled;
- if (nextEnabled) await enableOnlineTemplate(row.id);
- else await disableOnlineTemplate(row.id);
- return nextEnabled;
- }
- export async function loadOnlineTemplateVersions(id, loding = true) {
- if (!id) return [];
- return getOnlineTemplateVersions(id, loding);
- }
- export async function publishOnlineTemplateVersion(id, payload = {}) {
- if (!id) throw new Error("缺少模板主键");
- return createOnlineTemplateVersion(id, payload);
- }
- /**
- * 模板分类选项:后端暂无独立分类接口,退化为「已有模板中出现过的分类」。
- */
- export async function loadOnlineTemplateCategories() {
- try {
- const page = await getOnlineTemplatePage({ size: 200 }, false);
- const ids = [
- ...new Set(
- page.list
- .map((row) => row.categoryId)
- .filter(Boolean)
- .map(String)
- ),
- ];
- return ids.map((id) => ({ id, categoryName: `分类 ${id}` }));
- } catch (error) {
- return [];
- }
- }
|