_<源文件名>.jpg -- 用于不显示图片或非图片时显示源文件名。Lin
picUrl.value = props.dlUrl + "&path=" + path;
emit("update:modelValue", path);
},
});
});
return () => {
/// 增加,支持没有缩略图的上传 -- 没有 class= 时,为没有 缩略图 的上传。Lin
// 没有缩略图:
// 点击整个 $(`#${inputId.value}`).click(),
},
srcBaseName
),
]);
}
///
return h("div", {class: [props.class]}, [
h("input", {
type: "file",
accept: "image/*",
id: inputId.value,
style: {display: "none"},
}),
h("input", {
type: "hidden",
name: props.name,
value: pathVal.value,
}),
h(
"div",
{
style: {
width: "100%",
height: "100%",
},
onClick: () => $(`#${inputId.value}`).click(),
},
[
picUrl.value &&
h("img", {
src: picUrl.value,
style:
"width: 100%; height: 100%;object-fit: inherit;position: relative;z-index: 11;",
}),
]
),
]);
}
},
};
///// 再增加,增加 SsUpload(ss-ul)。抄自上面的 SsImgUpload( ({
width: 360,
height: 360,
aspectRatio: NaN, // : 1,。改,增加支持自由裁剪比例 -- 不锁定宽高比。Lin
}),
},
//上传图片url(未加图片名参数之前的部分)
ulUrl: {
type: String,
required: true,
},
//下载图片url(未加图片名参数之前的部分)
dlUrl: {
type: String,
required: true,
},
modelValue: [String, Number],
},
emits: ["update:modelValue"],
setup(props, { emit }) {
const inputId = Vue.computed(
() => `file_${Vue.getCurrentInstance().uid}`
);
//修改图片初始显示路径
let pathVal = ref(props.modelValue);
let srcBaseName = pathVal.value.split('_')[1]; // 再增加,增加上传的源文件名 0/_<源文件名>.jpg -- 用于不显示图片或非图片时显示源文件名。Lin
let picUrl = ref("");
if (props.modelValue) {
picUrl.value = props.dlUrl + "&path=" + props.modelValue;
}
///// 再增加,增加支持上传成功后的回调 -- 抄自 SsObjp( {
if (!props.onchange || typeof props.onchange !== "string" ||
typeof window === "undefined" || !window[props.onchange] || typeof window[props.onchange] !== "function")
return;
try {
window[props.onchange](newValue); // (value, desc)。Lin
} catch (error) {
console.error(`调用全局函数 ${props.onchange} 时出错:`, error);
}
};
Vue.watch(() => props.modelValue, handleModelValueChange, { immediate: true });
/////
Vue.onMounted(() => {
window.SS.cropper.init({
el: $(`#${inputId.value}`),
photoSize: {
width: props.cropperOpt.width,
height: props.cropperOpt.height,
},
aspectRatio: props.cropperOpt.aspectRatio,
uploadUrl: props.ulUrl,
success: (path) => {
pathVal.value = path;
srcBaseName = path.split('_')[1]; // 再增加,增加上传的源文件名 0/_<源文件名>.jpg -- 用于不显示图片或非图片时显示源文件名。Lin
picUrl.value = props.dlUrl + "&path=" + path;
emit("update:modelValue", path);
},
});
});
return () => {
/// 增加,支持没有缩略图的上传 -- 没有 class= 时,为没有 缩略图 的上传。Lin
// 没有缩略图:
// 点击整个 | $(`#${inputId.value}`).click(),
},
srcBaseName
),
]);
}
///
return h("div", { class: [props.class] }, [
h("input", {
type: "file",
accept: "image/*",
id: inputId.value,
style: { display: "none" },
}),
h("input", {
type: "hidden",
name: props.name,
value: pathVal.value,
}),
h(
"div",
{
style: {
width: "100%",
height: "100%",
},
onClick: () => $(`#${inputId.value}`).click(),
},
[
picUrl.value &&
h("img", {
src: picUrl.value,
style:
"width: 100%; height: 100%;object-fit: inherit;position: relative;z-index: 11;",
}),
]
),
]);
}
},
};
/////
// 初始化函数,负责创建和挂载 Vue 应用
// window.SS = { dom: {} };
/**
* 获取当前窗口的父窗口
* @returns {Window} 父窗口对象
*/
window.SS.topWin = (function (p, c) {
while (p != c) {
c = p;
p = p.parent;
}
return c;
})(window.parent, window);
window.SS.createSsDialogInstance = createSsDialogInstance;
/**
* 创建弹窗
* @param {Object} setting
* @param {Function} callbackEvent
*/
window.SS.openDialog = function (setting, callbackEvent) {
if (setting.params) {
const encodedParams = encodeURIComponent(JSON.stringify(setting.params));
setting.src +=
(setting.src.includes("?") ? "&" : "?") + "params=" + encodedParams;
}
if (window.parent && window.parent !== window) {
window.parent.SS.createSsDialogInstance(setting, callbackEvent);
} else {
createSsDialogInstance(setting, callbackEvent);
}
};
//关闭弹窗
window.SS.closeDialog = function () {
console.log("关闭弹窗");
if (topWindow.dialogInstances.length > 0) {
const instance = topWindow.dialogInstances.pop();
console.log("instance", instance);
console.log("instance.callbackEvent", instance.callbackEvent);
console.log(
"instance.callbackEvent.end",
typeof instance.callbackEvent === "function"
);
if (instance.callbackEvent) {
// 判断是否有end回调并执行
if (typeof instance.callbackEvent === "function") {
instance.callbackEvent();
}
if (typeof instance.callbackEvent.end === "function") {
instance.callbackEvent.end();
}
}
instance.app.unmount(); // 卸载最后一个实例
if (instance.container && instance.container.parentNode) {
instance.container.parentNode.removeChild(instance.container); // 移除容器
}
}
};
/**
* 裁剪插件
*/
window.SS.cropper = {
init: function (setting) {
if (!window.top.SS) window.top.SS = {};
// 重要:确保 cropper 对象的完整初始化
if (!window.top.SS.cropper) {
window.top.SS.cropper = {
settings: new Map(),
_backupSettings: {},
getSetting: this.getSetting,
clearSetting: this.clearSetting,
debug: this.debug,
};
} else if (!window.top.SS.cropper.settings) {
// 如果 cropper 存在但 settings 不存在,重新初始化 settings
window.top.SS.cropper.settings = new Map();
window.top.SS.cropper._backupSettings = {};
}
const uploaderId = `uploader_${Date.now()}_${Math.random()
.toString(36)
.substr(2, 9)}`;
window.top.SS.cropper.settings.set(uploaderId, setting);
window.top.SS.cropper._backupSettings[uploaderId] = setting;
setting.box = setting.box || "1";
var winSetting = {
headerTitle: "图片裁剪",
src: "/js/cropper/cropper.jsp", //原来在"/newUI/page/cropper.jsp" Ben(20251205)
width: "900",
height: "500",
};
$(setting.el).change(function () {
if (!window.SS.cropper.verify(setting)) {
$(setting.el).val(""); // 清空文件选择
return false;
}
var files = this.files;
if (files && files.length) {
if (!window.SS.cropper.verifySize($(setting.el)[0], 5)) {
$(setting.el).val(""); // 清空文件选择
alert("文件大小不能超过5M,请重新选择");
return false;
}
var URL = window.URL || window.webkitURL;
var file = files[0];
setting.file = file;
if (
/^image\/\w+$/.test(file.type) &&
/\.(jpg|jpeg|png|)$/i.test(file.name)
) {
const uploadedImageURL = URL.createObjectURL(file);
/// 再增加,增加支持自由裁剪比例 -- 没有指定 width 和 height 时,不锁定比例。Lin
if (!setting.aspectRatio) { // 没有指定 width 和 height。Lin
const img = new Image();
img.onload = () => {
// URL.revokeObjectURL(uploadedImageURL); // 释放内存。先去掉,不释放。Lin
console.log("图片原始宽、高:" + img.width + "*" + img.height);
setting.photoSize.width = img.width;
setting.photoSize.height = img.height;
};
img.src = uploadedImageURL;
}
///
setting.data = uploadedImageURL;
setting.fileName = file.name;
// console.log()
winSetting.params = {
...setting,
uploaderId,
};
console.log("ss-componets中change之后的winSetting", winSetting);
SS.openDialog(winSetting, {
success: function (win) {
console.log("裁剪插件成功");
// win.cropperSetting = setting;
},
end: function () {
console.log("裁剪插件结束");
$(setting.el).val(""); // 清空文件选择
},
});
} else {
/* 再改,增加非图片时直接上传。Lin
alert("请选择图片文件,支持jpg、jpeg、png格式");
*/
const reader = new FileReader();
reader.onload = (e) => {
const blob = new Blob([e.target.result], {type: 'application/octet-stream'});
/// 抄自 cropper.jsp 里的 getCroppedCanvas: function () { -- "获取上传裁剪图片"。Lin
const formData = new FormData();
formData.append('fileEdit', new File([blob], file.name)); // , setting.fileName))。Lin
formData.append("application", ""); // "如果没有这个,后台会报错"。Lin
// FtpServ.rdFileItemMapByReq() 里取不到 Map."true" -- Key = true 时,Value 为 fieldName = "application"。Lin
$.ajax(setting.uploadUrl, {
method: "POST",
data: formData,
processData: false,
contentType: false,
dataType: 'json',
success(data) {
if (data.fileList.length > 0) {
setting.success(data.fileList[0].path);
window.top.SS.cropper.clearSetting(uploaderId);
} else {
console.log('data.data[0].msg',data.data[0].msg);
}
},
error() {
alert('error');
}
});
///
};
reader.readAsArrayBuffer(file);
}
}
});
return uploaderId;
},
verify: function (setting) {
if (!setting) {
console.error(" cropper setting is not undefined! ");
return false;
}
if (!setting.el) {
console.error(" cropper setting.el is not undefined! ");
return false;
}
if (setting.photoSize) {
if (
(!setting.photoSize.width && setting.photoSize.height) ||
(!setting.photoSize.height && setting.photoSize.width)
) {
console.error(
" cropper setting.photoSize { width, height } is not undefined! "
);
return false;
}
}
if (!setting.box) {
setting.box = "1";
}
if (!setting.aspectRatio) {
setting.aspectRatio = NaN; // = 1 / 1;。改,增加支持自由裁剪比例 -- 不锁定宽高比。Lin
}
return true;
},
verifySize: function (fileEl, maxSize) {
// 判断是否为IE浏览器: /msie/i.test(navigator.userAgent) 为一个简单正则
var isIE = /msie/i.test(navigator.userAgent) && !window.opera;
var fileSize = 0;
if (isIE && !fileEl.files) {
// IE浏览器
var filePath = fileEl.value; // 获得上传文件的绝对路径
var fileSystem = new ActiveXObject("Scripting.FileSystemObject");
var file = fileSystem.GetFile(filePath);
fileSize = file.Size; // 文件大小,单位:b
} else {
// 非IE浏览器
fileSize = fileEl.files[0].size;
}
var size = fileSize / 1024 / 1024;
return !(size > maxSize);
},
// 获取特定上传组件的setting
getSetting: function (uploaderId) {
if (!window.top.SS?.cropper) {
console.warn("顶层窗口中未找到 SS.cropper");
return null;
}
// 优先从 Map 中获取
let setting = window.top.SS.cropper.settings.get(uploaderId);
// 如果 Map 中没有,尝试从备份中获取
if (!setting && window.top.SS.cropper._backupSettings[uploaderId]) {
console.log("从备份中恢复 setting");
setting = window.top.SS.cropper._backupSettings[uploaderId];
// 恢复到 Map 中
window.top.SS.cropper.settings.set(uploaderId, setting);
}
return setting;
},
// 清理特定上传组件的setting
clearSetting: function (uploaderId) {
if (!window.top.SS?.cropper) return;
window.top.SS.cropper.settings.delete(uploaderId);
delete window.top.SS.cropper._backupSettings[uploaderId];
console.log(
"清理设置后的 Map size:",
window.top.SS.cropper.settings.size
);
},
};
/**
* 获取url中的参数
* @returns {Object}
*/
window.SS.getQueryParams = function () {
const params = {};
const queryString = window.location.search.substring(1);
const pairs = queryString.split("&");
for (let i = 0; i < pairs.length; i++) {
const pair = pairs[i].split("=");
params[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);
}
if (params.params) {
try {
params.params = JSON.parse(params.params);
} catch (e) {
console.error("Failed to parse params:", e);
}
}
return params;
};
/**
* 创建vue应用
* @param {Object} config 配置项
* @values {String} config.el 挂载的元素
* @values {Boolean} config.isDialogPage 是否是弹窗页面 决定了是否可以使用顶天立地
* @values {Object} config.vueOptions vue配置项
* @returns {Object} vue实例
*/
window.ss = window.ss || window.SS;
window.ss.dom = window.ss.dom || window.SS.dom;
window.SS.dom._vueAppSeq =
window.SS.dom._vueAppSeq || window.ss.dom._vueAppSeq || 0;
window.SS.dom.vueApps = window.SS.dom.vueApps || window.ss.dom.vueApps || [];
window.ss.dom._vueAppSeq = window.SS.dom._vueAppSeq;
window.ss.dom.vueApps = window.SS.dom.vueApps;
window.SS.dom._registerVueApp = function ({ vm, el, appKey, scope, href }) {
if (!vm) return null;
const registry = window.SS.dom.vueApps || (window.SS.dom.vueApps = []);
const id = `vue-app-${++window.SS.dom._vueAppSeq}`;
const record = {
id,
el: el || "",
appKey: appKey || "",
scope: scope || "current",
href: href || window.location.href,
createdAt: Date.now(),
vm,
win: window,
};
registry.push(record);
window.SS.dom.currentApp = vm;
window.SS.dom.currentAppRecord = record;
if (window.ss && window.ss.dom) {
window.ss.dom.vueApps = registry;
window.ss.dom._vueAppSeq = window.SS.dom._vueAppSeq;
window.ss.dom.currentApp = vm;
window.ss.dom.currentAppRecord = record;
}
return record;
};
window.SS.dom._getVmData = function (vm) {
if (!vm) return null;
return vm.$data || vm;
};
window.SS.dom._hasVmKey = function (vm, key) {
if (!vm || !key) return false;
const data = window.SS.dom._getVmData(vm);
return key in vm || (!!data && key in data);
};
window.SS.dom._hasVmMethod = function (vm, methodName) {
return !!(vm && methodName && typeof vm[methodName] === "function");
};
window.SS.dom._normalizeBridgeOptions = function (
options = {},
defaults = {}
) {
return {
scope: "chain",
...defaults,
...options,
};
};
window.SS.dom._matchVueRecord = function (record, options = {}) {
if (!record || !record.vm) return false;
if (options.appKey && record.appKey !== options.appKey) return false;
if (options.el && record.el !== options.el) return false;
if (options.id && record.id !== options.id) return false;
if (
options.dataKey &&
!window.SS.dom._hasVmKey(record.vm, options.dataKey)
) {
return false;
}
if (
options.methodName &&
!window.SS.dom._hasVmMethod(record.vm, options.methodName)
) {
return false;
}
if (typeof options.predicate === "function") {
try {
if (!options.predicate(record.vm, record)) {
return false;
}
} catch (e) {
return false;
}
}
return true;
};
window.SS.dom._getWindowVueRecord = function (targetWindow, options = {}) {
if (!targetWindow || !targetWindow.SS || !targetWindow.SS.dom) return null;
const registry = targetWindow.SS.dom.vueApps || [];
for (let i = registry.length - 1; i >= 0; i--) {
if (targetWindow.SS.dom._matchVueRecord(registry[i], options)) {
return registry[i];
}
}
const currentRecord = targetWindow.SS.dom.currentAppRecord;
if (targetWindow.SS.dom._matchVueRecord(currentRecord, options)) {
return currentRecord;
}
const currentVm = targetWindow.SS.dom.currentApp;
const fallbackRecord = currentVm
? {
vm: currentVm,
win: targetWindow,
el: "",
appKey: "",
id: "",
}
: null;
if (targetWindow.SS.dom._matchVueRecord(fallbackRecord, options)) {
return fallbackRecord;
}
return null;
};
window.SS.dom.getVueAppRecord = function (options = {}) {
const scope = options.scope || "current";
if (scope === "current") {
return window.SS.dom._getWindowVueRecord(window, options);
}
if (scope === "top") {
try {
return window.SS.dom._getWindowVueRecord(window.top, options);
} catch (e) {
return null;
}
}
if (scope === "parent") {
try {
if (window.parent && window.parent !== window) {
return window.SS.dom._getWindowVueRecord(window.parent, options);
}
} catch (e) {
return null;
}
return null;
}
if (scope === "chain") {
let currentWindow = window;
while (currentWindow) {
try {
const record = window.SS.dom._getWindowVueRecord(
currentWindow,
options
);
if (record) {
return record;
}
} catch (e) {
return null;
}
if (currentWindow === currentWindow.parent) {
break;
}
currentWindow = currentWindow.parent;
}
return null;
}
return window.SS.dom._getWindowVueRecord(window, options);
};
window.SS.dom.getVueApp = function (options = {}) {
const record = window.SS.dom.getVueAppRecord(options);
return record ? record.vm : null;
};
window.SS.dom.getVmValue = function (key, options = {}) {
const vm = window.SS.dom.getVueApp(
window.SS.dom._normalizeBridgeOptions(options, { dataKey: key })
);
if (!vm) return undefined;
return vm[key];
};
window.SS.dom.setVmValue = function (key, value, options = {}) {
const vm = window.SS.dom.getVueApp(
window.SS.dom._normalizeBridgeOptions(options, { dataKey: key })
);
if (!vm) return false;
vm[key] = value;
return true;
};
window.SS.dom.callVmMethod = function (methodName, args = [], options = {}) {
const vm = window.SS.dom.getVueApp(
window.SS.dom._normalizeBridgeOptions(options, { methodName })
);
if (!vm || typeof vm[methodName] !== "function") {
return undefined;
}
return vm[methodName].apply(vm, args);
};
window.SS.dom.get = function (key, options = {}) {
return window.SS.dom.getVmValue(key, options);
};
window.SS.dom.set = function (key, value, options = {}) {
return window.SS.dom.setVmValue(key, value, options);
};
window.SS.dom.call = function (methodName, args = [], options = {}) {
return window.SS.dom.callVmMethod(methodName, args, options);
};
[
"_registerVueApp",
"_getVmData",
"_hasVmKey",
"_hasVmMethod",
"_normalizeBridgeOptions",
"_matchVueRecord",
"_getWindowVueRecord",
"getVueAppRecord",
"getVueApp",
"getVmValue",
"setVmValue",
"callVmMethod",
"get",
"set",
"call",
].forEach((name) => {
window.ss.dom[name] = window.SS.dom[name];
});
window.SS.dom.initializeFormApp = function (config) {
const {
el,
isDialogPage = false,
appKey = "",
scope = "current",
...vueOptions
} = config;
const app = createApp({
...vueOptions,
});
if (isDialogPage) {
function checkScroll() {
const elements = document.querySelectorAll(".fit-height-content");
let hasScrollBar = false;
elements.forEach((el) => {
if (el.scrollHeight > el.clientHeight) {
hasScrollBar = true;
}
});
window.parent.postMessage({ hasScrollBar }, "*");
}
function addScrollListeners() {
const elements = document.querySelectorAll("div");
elements.forEach((el) => {
el.addEventListener("scroll", checkScroll);
});
}
const observer = new MutationObserver(() => {
addScrollListeners();
checkScroll();
});
observer.observe(document.body, {
childList: true,
subtree: true,
});
window.addEventListener("resize", checkScroll);
}
app.component("SsLoginIcon", SsLoginIcon);
app.component("SsMark", SsMark);
app.component("SsFullStyleHeader", SsFullStyleHeader);
app.component("SsDialog", SsDialog);
app.component("SsInp", SsInput);
app.component("SsObjp", SsObjp);
app.component("SsHidden", SsHidden);
app.component("SsCcp", SsCcp);
/* 再改, | |