| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- // 劳动合同(/hr/contracts)—— 供「原合同信息」带出使用,对齐 PC kd-aiot-frontend-hr
- import { get } from "@/utils/request";
- import Vue from "vue";
- function ok(res) {
- return res && Number(res.code) === 0;
- }
- function fmtDate(value) {
- if (!value) return "";
- return String(value).slice(0, 10);
- }
- /** 合同后端 DTO → 前端展示对象(仅保留原合同信息所需字段) */
- export function adaptContract(item = {}) {
- const startDate = fmtDate(item.startDate);
- const endDate = fmtDate(item.endDate);
- const contractTerm =
- item.contractTerm ||
- (item.contractTermMonths != null && item.contractTermMonths !== ""
- ? `${item.contractTermMonths}个月`
- : "");
- return {
- id: item.id != null ? item.id : item.contractId,
- contractNo: item.contractNo || item.contractCode || "",
- contractTerm,
- startDate,
- endDate,
- periodText: [startDate, endDate].filter(Boolean).join(" 至 "),
- positionLevel: item.positionLevel || item.positionLevelName || "",
- statusCode: String(item.contractStatus || "").toUpperCase(),
- };
- }
- /** 员工合同状态 GET /hr/contracts/employee/{employeeId}/status */
- export async function getEmployeeContractStatus(employeeId, loding = false) {
- if (employeeId == null || employeeId === "") return null;
- const res = await get(
- Vue.prototype.apiUrl + `/hr/contracts/employee/${employeeId}/status`,
- {},
- loding
- );
- if (!ok(res)) return null;
- return res.data || null;
- }
- /** 合同详情 GET /hr/contracts/{id} */
- export async function getContractById(id, loding = false) {
- if (id == null || id === "") return null;
- const res = await get(Vue.prototype.apiUrl + `/hr/contracts/${id}`, {}, loding);
- if (!ok(res)) return null;
- return adaptContract(res.data || {});
- }
- /**
- * 带出员工「原合同信息」:
- * 1) 合同状态接口取该员工合同主键(优先生效中合同,否则最新一条)
- * 2) 合同详情取签订年限、合同期限与岗位层级快照
- * 任一环节失败都返回空值,不阻断主流程。
- */
- export async function loadEmployeeContractInfo(employeeId, loding = false) {
- const empty = { contractTerm: "", periodText: "", positionLevel: "", contractNo: "" };
- if (employeeId == null || employeeId === "") return empty;
- try {
- const status = await getEmployeeContractStatus(employeeId, loding);
- const contracts = Array.isArray(status && status.contracts)
- ? status.contracts
- : status && (status.contractId != null || status.id != null)
- ? [status]
- : [];
- if (!contracts.length) return empty;
- const current =
- contracts.find(
- (item) => String(item.contractStatus || "").toUpperCase() === "EFFECTIVE"
- ) || contracts[0];
- const contractId = current && (current.contractId || current.id);
- if (contractId == null || contractId === "") return empty;
- const detail = await getContractById(contractId, loding);
- if (!detail) return empty;
- return {
- contractTerm: detail.contractTerm || "",
- periodText: detail.periodText || "",
- positionLevel: detail.positionLevel || "",
- contractNo: detail.contractNo || "",
- };
- } catch (error) {
- return empty;
- }
- }
|