cover2Descriptions.js 5.15 KB
import dayjs from "dayjs";
import customParseFormat from "dayjs/plugin/customParseFormat";
import isSameOrAfter from "dayjs/plugin/isSameOrAfter";
import isSameOrBefore from "dayjs/plugin/isSameOrBefore";
import DescriptionImageList from "@/components/descriptions/components/imageList.vue";
import { statusOptions } from "@/views/residentManage/taskCenter/constant";
import "dayjs/locale/zh-cn";

if (typeof dayjs === "undefined") {
	console.error("[cover2Descriptions] dayjs 未加载,请检查 CDN 顺序");
}
dayjs.extend(isSameOrBefore);
dayjs.extend(isSameOrAfter);
dayjs.extend(customParseFormat);

function parseDatetime(value) {
	if (!value) {
		return "-";
	}

	const formats = [
		"YYYY-MM-DDTHH:mm:ss",
		"YYYY-MM-DD HH:mm:ss",
		"YYYY-MM-DDTHH:mm",
		"YYYY-MM-DD",
	];

	for (const format of formats) {
		const parsed = dayjs(value, format, true);
		if (parsed.isValid()) {
			return parsed.format("YYYY-MM-DD HH:mm:ss");
		}
	}

	return "-";
}

// tooltip: true,
// 			parentProps: {
// 				style: {
// 					display: "inline-block",
// 					height: "32px",
// 					width: "auto",
// 					maxWidth: "100%",
// 					verticalAlign: "top",
// 				},
// 			},
// 			// tooltipProps: el-tooltip 的传参
// 			render: (h, { data, schemaItem, schema }) => {
// 				return h("div", { class: "text-hide" }, data.description || "-");
// 			},

function renderText() {
	return {
		render: (h, { data, schemaItem, schema }) => {
			return h("div", {
				style: {
					whiteSpace: "normal",
					wordBreak: "break-all",
					wordWrap: "break-word",
					overflowWrap: "break-word",
					textAlign: "left",
				},
			}, schemaItem.meta.fieldValue || "-");
		},
	};
}

function renderEnum() {
	return {
		render: (h, { data, schemaItem, schema }) => {
			const { fieldValue } = schemaItem.meta;

			if (!fieldValue) {
				return h("span", "-");
			}

			// 统一处理:字符串转数组
			const values = Array.isArray(fieldValue) ? fieldValue : [fieldValue];

			// 循环渲染为 el-tag
			const tagChildren = values.map((item, index) => {
				return h("el-tag", {
					attrs: {
						type: "info",
					},
					style: {
						marginRight: index < values.length - 1 ? "8px" : "0",
						marginBottom: "4px",
					},
				}, item);
			});

			return h("div", {
				style: {
					display: "flex",
					flexWrap: "wrap",
					alignItems: "center",
				},
			}, tagChildren);
		},
	};
}

function renderDatetime() {
	return {
		render: (h, { data, schemaItem, schema }) => {
			const formatted = parseDatetime(schemaItem.meta.fieldValue);
			return h("span", formatted);
		},
	};
}
function renderLink() {
	return {
		tooltip: true,
		tooltipProps: {
			render: (h, { data, schemaItem, schema }) => {
				return h("div", schemaItem.meta.fieldValue || "-");
			},
		},
		parentProps: {
			style: {
				display: "inline-block",
				width: "auto",
				maxWidth: "100%",
				verticalAlign: "top",
			},
		},
		render: (h, { data, schemaItem, schema }) => {
			const { fieldValue, linkAlias } = schemaItem.meta;
			if (!fieldValue) {
				return h("span", "-");
			}

			const linkProps = {
				class: "link-type",
				style: {
					width: "100%",
					whiteSpace: "normal",
					wordBreak: "break-all",
					wordWrap: "break-word",
					overflowWrap: "break-word",
				},
				attrs: {
					href: fieldValue,
					target: "_blank",
					type: "primary",
				},
				on: {
					click: (e) => {
						e.preventDefault();
						window.open(fieldValue, "_blank");
					},
				},
			};
			return h("el-link", linkProps, linkAlias || fieldValue);
		},
	};
}

function renderImage() {
	return {
		render: (h, { data, schemaItem, schema }) => {
			const { fieldValue } = schemaItem.meta;
			if (!fieldValue || !Array.isArray(fieldValue) || fieldValue.length === 0) {
				return h("span", "-");
			}
			return h(DescriptionImageList, { props: { list: fieldValue, size: 120 } });
		},
	};
}

function renderAttachment() {
	return {

		render: (h, { data, schemaItem, schema }) => {
			const { fieldValue } = schemaItem.meta;
			if (!fieldValue || !Array.isArray(fieldValue) || fieldValue.length === 0) {
				return h("span", "-");
			}

			const containerChildren = fieldValue.map((item, index) => {
				const fileName = item.name || item.fileName || item.url || `附件${index + 1}`;
				const fileUrl = item.value || item.url || "";

				const rowChildren = [
					h("span", {
						attrs: {
							class: "file-name text-hide",
							title: fileName,
							style: "max-width: 50%",
						},
					}, fileName),
				];

				rowChildren.push(
					h("el-link", {
						attrs: { href: fileUrl, target: "_blank", title: fileUrl },
						props: { type: "primary", underline: false },
						style: { marginLeft: "8px", lineHeight: 1 },
						on: {
							click: (e) => {
								if (!fileUrl) {
									return;
								}
								e.preventDefault();
								window.open(fileUrl, "_blank");
							},
						},
					}, "下载"),
				);

				return h("div", { attrs: { class: "attachment-item" } }, rowChildren);
			});

			return h("div", { attrs: { class: "attachment-list" } }, containerChildren);
		},

	};
}

export default {
	TEXT: renderText,
	NUMBER: renderText,
	ENUM: renderEnum,
	DATETIME: renderDatetime,
	LINK: renderLink,
	IMAGE: renderImage,
	ATTACHMENT: renderAttachment,
};