contract.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // 劳动合同(/hr/contracts)—— 供「原合同信息」带出使用,对齐 PC kd-aiot-frontend-hr
  2. import { get } from "@/utils/request";
  3. import Vue from "vue";
  4. function ok(res) {
  5. return res && Number(res.code) === 0;
  6. }
  7. function fmtDate(value) {
  8. if (!value) return "";
  9. return String(value).slice(0, 10);
  10. }
  11. /** 合同后端 DTO → 前端展示对象(仅保留原合同信息所需字段) */
  12. export function adaptContract(item = {}) {
  13. const startDate = fmtDate(item.startDate);
  14. const endDate = fmtDate(item.endDate);
  15. const contractTerm =
  16. item.contractTerm ||
  17. (item.contractTermMonths != null && item.contractTermMonths !== ""
  18. ? `${item.contractTermMonths}个月`
  19. : "");
  20. return {
  21. id: item.id != null ? item.id : item.contractId,
  22. contractNo: item.contractNo || item.contractCode || "",
  23. contractTerm,
  24. startDate,
  25. endDate,
  26. periodText: [startDate, endDate].filter(Boolean).join(" 至 "),
  27. positionLevel: item.positionLevel || item.positionLevelName || "",
  28. statusCode: String(item.contractStatus || "").toUpperCase(),
  29. };
  30. }
  31. /** 员工合同状态 GET /hr/contracts/employee/{employeeId}/status */
  32. export async function getEmployeeContractStatus(employeeId, loding = false) {
  33. if (employeeId == null || employeeId === "") return null;
  34. const res = await get(
  35. Vue.prototype.apiUrl + `/hr/contracts/employee/${employeeId}/status`,
  36. {},
  37. loding
  38. );
  39. if (!ok(res)) return null;
  40. return res.data || null;
  41. }
  42. /** 合同详情 GET /hr/contracts/{id} */
  43. export async function getContractById(id, loding = false) {
  44. if (id == null || id === "") return null;
  45. const res = await get(Vue.prototype.apiUrl + `/hr/contracts/${id}`, {}, loding);
  46. if (!ok(res)) return null;
  47. return adaptContract(res.data || {});
  48. }
  49. /**
  50. * 带出员工「原合同信息」:
  51. * 1) 合同状态接口取该员工合同主键(优先生效中合同,否则最新一条)
  52. * 2) 合同详情取签订年限、合同期限与岗位层级快照
  53. * 任一环节失败都返回空值,不阻断主流程。
  54. */
  55. export async function loadEmployeeContractInfo(employeeId, loding = false) {
  56. const empty = { contractTerm: "", periodText: "", positionLevel: "", contractNo: "" };
  57. if (employeeId == null || employeeId === "") return empty;
  58. try {
  59. const status = await getEmployeeContractStatus(employeeId, loding);
  60. const contracts = Array.isArray(status && status.contracts)
  61. ? status.contracts
  62. : status && (status.contractId != null || status.id != null)
  63. ? [status]
  64. : [];
  65. if (!contracts.length) return empty;
  66. const current =
  67. contracts.find(
  68. (item) => String(item.contractStatus || "").toUpperCase() === "EFFECTIVE"
  69. ) || contracts[0];
  70. const contractId = current && (current.contractId || current.id);
  71. if (contractId == null || contractId === "") return empty;
  72. const detail = await getContractById(contractId, loding);
  73. if (!detail) return empty;
  74. return {
  75. contractTerm: detail.contractTerm || "",
  76. periodText: detail.periodText || "",
  77. positionLevel: detail.positionLevel || "",
  78. contractNo: detail.contractNo || "",
  79. };
  80. } catch (error) {
  81. return empty;
  82. }
  83. }