Files
BuYuHouDuan/AdminSrc/AdminHost/wwwroot/admin/dist/controller/common.js
T
2026-10-12 16:22:04 +08:00

142 lines
4.7 KiB
JavaScript

/** layuiAdmin.pro-v1.4.0 LPPL License By https://www.layui.com/admin/ */;
layui.define(function (exports) {
var $ = layui.$;
var i = (layui.$, layui.layer, layui.laytpl, layui.setter, layui.view, layui.admin);
// Helper: convert PascalCase or any case keys to camelCase recursively
function toCamelKey(s) {
if (!s || typeof s !== 'string') return s;
// handle underscores and PascalCase
var str = s.replace(/_([a-z])/g, function (m, p1) { return p1.toUpperCase(); });
// if contains uppercase letters after first char, convert first char to lower
return str.charAt(0).toLowerCase() + str.slice(1);
}
function convertKeysToCamel(obj) {
if (obj === null || obj === undefined) return obj;
if (Array.isArray(obj)) return obj.map(convertKeysToCamel);
if (typeof obj === 'object') {
var res = {};
for (var k in obj) {
if (!Object.prototype.hasOwnProperty.call(obj, k)) continue;
var nk = toCamelKey(k);
res[nk] = convertKeysToCamel(obj[k]);
}
return res;
}
return obj;
}
// Wrap admin.req so that all responses have their .data keys converted to camelCase
if (i && i.req) {
var _origReq = i.req.bind(i);
i.req = function (options) {
if (!options) return _origReq(options);
var userDone = options.done;
options.done = function (res) {
try {
if (res && res.data) {
res.data = convertKeysToCamel(res.data);
}
} catch (e) {
console && console.error && console.error('convertKeysToCamel error', e);
}
if (typeof userDone === 'function') userDone(res);
};
return _origReq(options);
};
}
// Enum definitions: populate as needed. Keys are field names in camelCase, values are arrays of {value,text}
window.enumDefinitions = window.enumDefinitions || {
fishType: [{ value: 1, text: '普通鱼' }, { value: 2, text: 'Boss鱼' }, { value: 3, text: '黄金鱼' }, { value: 4, text: '特殊鱼' }, { value: 5, text: '特殊Boss' }],
saleType: [{ value: 1, text: '钻石' }, { value: 2, text: '房卡' }, { value: 4, text: '金币' }, { value: 5, text: '道具' }]
};
function replaceInputWithSelect($form, name, options, currentValue) {
var $input = $form.find('[name="' + name + '"]');
if ($input.length === 0) return;
// create select
var $select = $('<select></select>');
$select.attr('name', name);
$options = options || [];
$select.append($('<option value=""></option>'));
$options.forEach(function (opt) {
var $opt = $('<option></option>').attr('value', opt.value).text(opt.text);
if (String(opt.value) === String(currentValue)) $opt.attr('selected', 'selected');
$select.append($opt);
});
$input.replaceWith($select);
}
// Normalize input/select/textarea name attributes to camelCase
function normalizeFormInputNames($container) {
try {
var $root = $($container || document);
$root.find('input[name],select[name],textarea[name]').each(function () {
var $el = $(this);
var name = $el.attr('name');
if (!name) return;
var newName = toCamelKey(name);
if (newName !== name) {
$el.attr('name', newName);
}
});
} catch (e) {
console && console.error && console.error('normalizeFormInputNames error', e);
}
}
window.renderEnumControls = function ($container) {
try {
var $ = layui.$;
var $form = $($container || document);
// normalize names first
normalizeFormInputNames($form);
Object.keys(window.enumDefinitions).forEach(function (field) {
var options = window.enumDefinitions[field];
// find any input/select with this name and replace if it's not already a select
var $els = $form.find('[name="' + field + '"]');
$els.each(function () {
var $el = $(this);
if ($el.is('select')) return; // already select
var currentVal = $el.val();
replaceInputWithSelect($form, field, options, currentVal);
});
});
} catch (e) {
console && console.error && console.error('renderEnumControls error', e);
}
};
// Wrap form.render to invoke enum rendering after original render
if (layui && layui.form && layui.form.render) {
var _origFormRender = layui.form.render.bind(layui.form);
layui.form.render = function () {
var res = _origFormRender.apply(null, arguments);
try {
// arguments may include filter name; find forms with that lay-filter
var filter = arguments && arguments.length ? arguments[0] : null;
if (typeof filter === 'string') {
// find forms with lay-filter
var $forms = layui.$("[lay-filter='" + filter + "']");
$forms.each(function () { window.renderEnumControls(this); });
} else {
window.renderEnumControls(document);
}
} catch (e) { }
return res;
};
}
i.events.logout = function () {
i.req({
url: "./json/user/logout.js",
type: "get",
data: {},
done: function (e) {
i.exit()
}
})
}, exports("common", {})
});