onlineDocument.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // HR 线上 Word 模板 —— 由 kd-aiot-frontend-hr-H5/src/api/onlineDocument.ts 迁移,去掉演示模式分支
  2. import { get, postJ, putJ } from "@/utils/request";
  3. import Vue from "vue";
  4. export const TEMPLATE_STATUS_LABELS = {
  5. DRAFT: "草稿",
  6. ENABLED: "已启用",
  7. DISABLED: "已停用",
  8. };
  9. export const TEMPLATE_STATUS_FILTERS = [
  10. { value: "all", label: "全部" },
  11. { value: "ENABLED", label: "已启用" },
  12. { value: "DRAFT", label: "草稿" },
  13. { value: "DISABLED", label: "已停用" },
  14. ];
  15. function ok(res) {
  16. return res && Number(res.code) === 0;
  17. }
  18. function compact(object = {}) {
  19. return Object.keys(object).reduce((result, key) => {
  20. const value = object[key];
  21. if (value !== "" && value !== null && value !== undefined) result[key] = value;
  22. return result;
  23. }, {});
  24. }
  25. function keepId(value) {
  26. if (value === undefined || value === null || value === "") return undefined;
  27. return String(value).trim();
  28. }
  29. function extractId(result) {
  30. if (result == null) return null;
  31. if (typeof result === "object") {
  32. return result.id != null ? result.id : result.templateId != null ? result.templateId : null;
  33. }
  34. return result;
  35. }
  36. function asList(data) {
  37. if (Array.isArray(data)) return data;
  38. if (data && Array.isArray(data.list)) return data.list;
  39. if (data && Array.isArray(data.records)) return data.records;
  40. if (data && typeof data === "object" && data.id != null) return [data];
  41. return [];
  42. }
  43. /** 后端「HR线上Word模板信息」→ 页面形状 */
  44. export function adaptOnlineTemplate(raw = {}) {
  45. if (!raw || typeof raw !== "object") return null;
  46. const status = String(raw.templateStatus || "DRAFT").toUpperCase();
  47. const enabled =
  48. raw.status === undefined || raw.status === null ? status === "ENABLED" : Number(raw.status) === 1;
  49. return Object.assign({}, raw, {
  50. id: raw.id != null ? raw.id : raw.templateId != null ? raw.templateId : "",
  51. templateName: raw.templateName || "",
  52. templateCode: raw.templateCode || "",
  53. categoryId: raw.categoryId === undefined || raw.categoryId === null ? "" : String(raw.categoryId),
  54. templateStatus: status,
  55. templateStatusLabel: TEMPLATE_STATUS_LABELS[status] || status,
  56. versionStatus: raw.versionStatus || "",
  57. currentVersionNo: raw.currentVersionNo != null ? raw.currentVersionNo : 1,
  58. documentContent: raw.documentContent || "",
  59. variablesJson: raw.variablesJson || "",
  60. changeRemark: raw.changeRemark || "",
  61. remark: raw.remark || "",
  62. createTime: raw.createTime || "",
  63. status: raw.status,
  64. enabled,
  65. });
  66. }
  67. export function adaptOnlineTemplateVersion(raw = {}, index = 0) {
  68. return {
  69. id: raw.id != null ? raw.id : raw.versionId != null ? raw.versionId : `v-${index}`,
  70. versionNo: raw.currentVersionNo != null ? raw.currentVersionNo : raw.versionNo != null ? raw.versionNo : index + 1,
  71. versionStatus: raw.versionStatus || "",
  72. changeRemark: raw.changeRemark || "",
  73. createTime: raw.createTime || "",
  74. };
  75. }
  76. /** 模板分页 GET /hr/online-documents/templates */
  77. export async function getOnlineTemplatePage(params = {}, loding = true) {
  78. const res = await get(
  79. Vue.prototype.apiUrl + "/hr/online-documents/templates",
  80. compact({
  81. pageNum: params.pageNum || 1,
  82. size: params.size || 200,
  83. templateName: params.templateName,
  84. templateStatus: params.templateStatus,
  85. }),
  86. loding
  87. );
  88. const data = (res && res.data) || {};
  89. return {
  90. list: asList(data)
  91. .map(adaptOnlineTemplate)
  92. .filter(Boolean),
  93. count: Number(data.count != null ? data.count : data.total != null ? data.total : 0),
  94. };
  95. }
  96. /** 模板详情 GET /hr/online-documents/templates/{id} */
  97. export async function getOnlineTemplateById(id, loding = true) {
  98. const res = await get(Vue.prototype.apiUrl + `/hr/online-documents/templates/${id}`, {}, loding);
  99. if (!ok(res)) return null;
  100. return adaptOnlineTemplate(res.data || {});
  101. }
  102. /** 新增模板 POST /hr/online-documents/templates */
  103. export async function createOnlineTemplate(payload, loding = true) {
  104. const res = await postJ(Vue.prototype.apiUrl + "/hr/online-documents/templates", payload, loding);
  105. if (!ok(res)) throw new Error(res.message || "新增模板失败");
  106. return extractId(res.data);
  107. }
  108. /** 修改模板草稿 PUT /hr/online-documents/templates/{id} */
  109. export async function updateOnlineTemplate(id, payload, loding = true) {
  110. const res = await putJ(Vue.prototype.apiUrl + `/hr/online-documents/templates/${id}`, payload, loding);
  111. if (!ok(res)) throw new Error(res.message || "保存模板失败");
  112. return id;
  113. }
  114. export async function enableOnlineTemplate(id, loding = true) {
  115. const res = await postJ(Vue.prototype.apiUrl + `/hr/online-documents/templates/${id}/enable`, {}, loding);
  116. if (!ok(res)) throw new Error(res.message || "启用模板失败");
  117. return id;
  118. }
  119. export async function disableOnlineTemplate(id, loding = true) {
  120. const res = await postJ(Vue.prototype.apiUrl + `/hr/online-documents/templates/${id}/disable`, {}, loding);
  121. if (!ok(res)) throw new Error(res.message || "停用模板失败");
  122. return id;
  123. }
  124. /** 版本历史 GET /hr/online-documents/templates/{id}/versions */
  125. export async function getOnlineTemplateVersions(id, loding = true) {
  126. const res = await get(Vue.prototype.apiUrl + `/hr/online-documents/templates/${id}/versions`, {}, loding);
  127. if (!ok(res)) return [];
  128. return asList(res.data).map((raw, idx) => adaptOnlineTemplateVersion(raw, idx));
  129. }
  130. /** 创建新版本 POST /hr/online-documents/templates/{id}/versions */
  131. export async function createOnlineTemplateVersion(id, payload = {}, loding = true) {
  132. const res = await postJ(
  133. Vue.prototype.apiUrl + `/hr/online-documents/templates/${id}/versions`,
  134. compact({
  135. documentContent: payload.documentContent,
  136. variablesJson: payload.variablesJson,
  137. changeRemark: payload.changeRemark,
  138. }),
  139. loding
  140. );
  141. if (!ok(res)) throw new Error(res.message || "创建版本失败");
  142. return extractId(res.data);
  143. }
  144. /** 表单 → 后端「HR线上Word模板保存参数」 */
  145. export function buildOnlineTemplatePayload(form = {}) {
  146. return compact({
  147. id: keepId(form.id),
  148. templateName: form.templateName,
  149. templateCode: form.templateCode,
  150. categoryId: keepId(form.categoryId),
  151. deptId: keepId(form.deptId),
  152. remark: form.remark,
  153. documentContent: form.documentContent,
  154. variablesJson: form.variablesJson,
  155. });
  156. }
  157. /** 模板列表 */
  158. export async function loadOnlineTemplateList(params = {}, loding = true) {
  159. const { templateName = "", templateStatus = "" } = params;
  160. const page = await getOnlineTemplatePage(
  161. {
  162. templateName: templateName || undefined,
  163. templateStatus: templateStatus && templateStatus !== "all" ? templateStatus : undefined,
  164. },
  165. loding
  166. );
  167. const list = page.list
  168. .filter((row) => !templateStatus || templateStatus === "all" || row.templateStatus === templateStatus)
  169. .filter((row) => {
  170. if (!templateName) return true;
  171. const text = `${row.templateName}${row.templateCode}`.toLowerCase();
  172. return text.includes(String(templateName).toLowerCase());
  173. })
  174. .sort((a, b) => String(b.createTime).localeCompare(String(a.createTime)));
  175. return { list, count: page.count, source: "api" };
  176. }
  177. /** 模板详情;传列表行时优先用行内数据兜底 */
  178. export async function loadOnlineTemplateDetail(id, fallbackRow = null, loding = true) {
  179. if (!id) return fallbackRow || null;
  180. const row = await getOnlineTemplateById(id, loding);
  181. return row ? Object.assign({}, fallbackRow || {}, row) : fallbackRow || null;
  182. }
  183. /** 保存模板:有 id 走 PUT,无 id 走 POST */
  184. export async function saveOnlineTemplate({ form, existingId = null } = {}) {
  185. if (!form) throw new Error("模板参数为空");
  186. const id = existingId || form.id || null;
  187. const payload = buildOnlineTemplatePayload(Object.assign({}, form, { id }));
  188. if (id) {
  189. await updateOnlineTemplate(id, payload);
  190. return id;
  191. }
  192. return createOnlineTemplate(payload);
  193. }
  194. /** 启用 / 停用切换 */
  195. export async function toggleOnlineTemplate(row = {}) {
  196. if (!row.id) throw new Error("缺少模板主键");
  197. const nextEnabled = !row.enabled;
  198. if (nextEnabled) await enableOnlineTemplate(row.id);
  199. else await disableOnlineTemplate(row.id);
  200. return nextEnabled;
  201. }
  202. export async function loadOnlineTemplateVersions(id, loding = true) {
  203. if (!id) return [];
  204. return getOnlineTemplateVersions(id, loding);
  205. }
  206. export async function publishOnlineTemplateVersion(id, payload = {}) {
  207. if (!id) throw new Error("缺少模板主键");
  208. return createOnlineTemplateVersion(id, payload);
  209. }
  210. /**
  211. * 模板分类选项:后端暂无独立分类接口,退化为「已有模板中出现过的分类」。
  212. */
  213. export async function loadOnlineTemplateCategories() {
  214. try {
  215. const page = await getOnlineTemplatePage({ size: 200 }, false);
  216. const ids = [
  217. ...new Set(
  218. page.list
  219. .map((row) => row.categoryId)
  220. .filter(Boolean)
  221. .map(String)
  222. ),
  223. ];
  224. return ids.map((id) => ({ id, categoryName: `分类 ${id}` }));
  225. } catch (error) {
  226. return [];
  227. }
  228. }