writeAttr.js 8.6 KB
Newer Older
Skyeye云's avatar
Skyeye云 已提交
1 2 3 4 5 6

layui.config({
	base: basePath,
	version: skyeyeVersion
}).extend({
    window: 'js/winui.window'
7
}).define(['window', 'jquery', 'winui', 'form', 'textool', 'eleTree'], function (exports) {
Skyeye云's avatar
Skyeye云 已提交
8 9 10 11 12
	winui.renderColor();
	var index = parent.layer.getFrameIndex(window.name);
	var $ = layui.$,
		form = layui.form,
		textool = layui.textool;
Skyeye云's avatar
Skyeye云 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
	var selOption = getFileContent('tpl/template/select-option.tpl');

	// 不同的数据来源对应不同的html
	var dataTypeObject = {
		"1": `<div class="layui-form-item layui-col-xs12">
                <label class="layui-form-label">默认数据<i class="red">*</i></label>
                <div class="layui-input-block">
                    <textarea id="defaultData" name="defaultData" win-verify="required" class="layui-textarea"></textarea>
                    <div class="layui-form-mid layui-word-aux">数据样式为:[{"id":"1","name":"男",...},{"id":"2","name":"女",...}]</div>
                </div>
            </div>`,
		"2": `<div class="layui-form-item layui-col-xs12">
                <label class="layui-form-label">枚举数据<i class="red">*</i></label>
                <div class="layui-input-block">
                    <select id="objectId" name="objectId" lay-search="" win-verify="required" lay-filter="objectId"></select>
                </div>
            </div>`,
		"3": `<div class="layui-form-item layui-col-xs12">
                <label class="layui-form-label">数据字典<i class="red">*</i></label>
                <div class="layui-input-block">
                    <select id="objectId" name="objectId" lay-search="" win-verify="required" lay-filter="objectId"></select>
                </div>
            </div>`
	};

	// 表单控件集合
	var componentList = [];
Skyeye云's avatar
Skyeye云 已提交
40 41 42 43 44 45 46 47

	var className = GetUrlParam('className');
	var attrKey = GetUrlParam('attrKey');

	AjaxPostUtil.request({url: reqBasePath + "queryAttrDefinitionCustom", params: {className: className, attrKey: attrKey}, type: 'json', method: "GET", callback: function (json) {
		$("#showForm").html(getDataUseHandlebars($("#beanTemplate").html(), json));
		textool.init({eleId: 'remark', maxlength: 200});

48 49 50 51
		// 加载组件信息
		treeSelectUtil.init({
			eleTree: layui.eleTree,
			elem: 'componentId',
Skyeye云's avatar
Skyeye云 已提交
52
			url: reqBasePath + "queryAllDsFormComponentList?serviceClassName=" + className,
Skyeye云's avatar
Skyeye云 已提交
53 54 55 56 57 58 59
			defaultId: json.bean.componentId,
			ajaxCallback: function (data) {
				$.each(data.rows, function (i, item) {
					if (!isNull(item.children)) {
						componentList = componentList.concat(item.children);
					}
				});
Skyeye云's avatar
Skyeye云 已提交
60
				var value;
61
				if (json.bean.dataType == 4) {
Skyeye云's avatar
Skyeye云 已提交
62 63
					value = json.bean.businessApi;
				} else {
64
					value = json.bean.dataType == 1 ? json.bean.defaultData : json.bean.objectId;
Skyeye云's avatar
Skyeye云 已提交
65
				}
Skyeye云's avatar
Skyeye云 已提交
66 67 68 69 70
				loadLinkData(json.bean.componentId, json.bean.dataType, value);
			},
			clickCallback: function (chooseId) {
				loadLinkData(chooseId, '', '');
			}
71 72 73
		});

		// 如果不是入参属性,则不能设置特定的一些值
Skyeye云's avatar
Skyeye云 已提交
74
		if (!json.bean.whetherInputParams) {
75
			$('.inputParams').hide();
Skyeye云's avatar
Skyeye云 已提交
76 77
		} else {
			$('.propertyParams').hide();
78 79
		}

Skyeye云's avatar
Skyeye云 已提交
80 81 82 83 84 85 86 87 88
		var id = isNull(json.bean.id) ? '' : json.bean.id;
		matchingLanguage();
		form.render();
		form.on('submit(formEditBean)', function (data) {
			if (winui.verifyForm(data.elem)) {
				var params = {
					className: className,
					attrKey: attrKey,
					name: $("#name").val(),
89 90 91
					componentId: isNull($("#componentId").val()) ? "" : $("#componentId").attr("componentId"),
					minLength: $("#minLength").val(),
					maxLength: $("#maxLength").val(),
Skyeye云's avatar
Skyeye云 已提交
92
					remark: $("#remark").val(),
Skyeye云's avatar
Skyeye云 已提交
93
					dataType: '',
Skyeye云's avatar
Skyeye云 已提交
94 95
					id: id
				};
Skyeye云's avatar
Skyeye云 已提交
96 97 98 99 100

				var dsFormComponent = getInPoingArr(componentList, 'id', params.componentId);
				if (!isNull(dsFormComponent) && dsFormComponent.linkedData == 1) {
					params.dataType = $("#dataType").val();
					if (params.dataType == 1) {
Skyeye云's avatar
Skyeye云 已提交
101
						// 自定义json
Skyeye云's avatar
Skyeye云 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
						var defaultDataStr = $("#defaultData").val();
						if (isNull(defaultDataStr)) {
							winui.window.msg("请填写Json串!", {icon: 2, time: 2000});
							return false;
						} else {
							if (isJSON(defaultDataStr)) {
								var defaultKey = getOutKey(defaultDataStr);//取出json串的键
								// 获取数据展示模板
								var tplContentVal = strMatchAllByTwo(dsFormComponent.htmlDataFrom, '{{', '}}');//取出数据模板中用{{}}包裹的词
								removeByValue(tplContentVal, "#each this");
								removeByValue(tplContentVal, "/each");
								if (subset(tplContentVal, defaultKey)) {
									params.defaultData = defaultDataStr;
								} else {
									winui.window.msg('json串内容有误,请重新填写!', {icon: 2, time: 2000});
									return false;
								}
							} else {
								winui.window.msg('json串格式不正确,请重新填写!', {icon: 2, time: 2000});
								return false;
							}
						}
Skyeye云's avatar
Skyeye云 已提交
124 125 126 127 128 129 130 131 132 133 134 135 136 137
					} else if (params.dataType == 4) {
						// 自定义接口
						var dataList = initTableChooseUtil.getDataList('apiParams').dataList;
						var apiParams = {};
						$.each(dataList, function (i, item) {
							apiParams[item.key] = item.value;
						});
						var businessApi = {
							serviceStr: $("#serviceStr").val(),
							api: $("#api").val(),
							method: $("#method").val(),
							params: apiParams
						};
						params.businessApi = JSON.stringify(businessApi);
Skyeye云's avatar
Skyeye云 已提交
138 139 140 141
					} else {
						params.objectId = $("#objectId").val();
					}
				}
Skyeye云's avatar
Skyeye云 已提交
142 143 144 145 146 147 148 149 150
				AjaxPostUtil.request({url: reqBasePath + "saveAttrDefinitionCustom", params: params, type: 'json', method: "POST", callback: function (json) {
					parent.layer.close(index);
					parent.refreshCode = '0';
				}});
			}
			return false;
		});
	}});

Skyeye云's avatar
Skyeye云 已提交
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
	function loadLinkData(componentId, dataType, value) {
		var dsFormComponent = getInPoingArr(componentList, 'id', componentId);
		if (!isNull(dsFormComponent) && dsFormComponent.linkedData == 1) {
			// 允许关联数据
			$("#linkDataBox").removeClass("layui-hide");
			// 数据来源类型
			skyeyeClassEnumUtil.showEnumDataListByClassName("attrKeyDataType", 'select', "dataType", dataType, form);
			form.on('select(dataType)', function(data) {
				loadDataMation($('#dataType').val(), '');
			});
			loadDataMation(dataType, value);
		} else {
			$("#linkDataBox").addClass("layui-hide");
		}
	}

	function loadDataMation(dataType, value) {
		$("#dataTypeObjectBox").html(dataTypeObject[dataType]);
		if (dataType == 1) {
			// 自定义
			$("#defaultData").val(value);
		} else if (dataType == 2) {
			// 枚举
			initEnumData();
			$("#objectId").val(value);
		} else if (dataType == 3) {
			// 数据字典
			initDictData();
			$("#objectId").val(value);
Skyeye云's avatar
Skyeye云 已提交
180
		} else if (dataType == 4) {
181
			// 自定义接口
Skyeye云's avatar
Skyeye云 已提交
182 183 184 185 186 187
			$("#dataTypeObjectBox").html(commonHtml['businessApi']);
			$("#serviceStr").html(getDataUseHandlebars(selOption, {rows: serviceMap}));
			loadParamsTable('apiParams');
			if (!isNull(value)) {
				$("#serviceStr").val(value.serviceStr);
				$("#api").val(value.api);
188
				skyeyeClassEnumUtil.showEnumDataListByClassName("httpMethodEnum", 'select', "method", value.method, form);
Skyeye云's avatar
Skyeye云 已提交
189 190

				initTableChooseUtil.deleteAllRow('apiParams');
191
				$.each(value.params, function(key, value) {
Skyeye云's avatar
Skyeye云 已提交
192 193 194 195 196 197 198 199 200
					var params = {
						"key": key,
						"value": value
					};
					initTableChooseUtil.resetData('apiParams', params);
				});
			} else {
				skyeyeClassEnumUtil.showEnumDataListByClassName("httpMethodEnum", 'select', "method", '', form);
			}
Skyeye云's avatar
Skyeye云 已提交
201 202 203
		} else {
			$("#dataTypeObjectBox").html(dataTypeObject["2"]);
			$("#objectId").html("<option value=''>请选择</option>");
Skyeye云's avatar
Skyeye云 已提交
204 205 206 207
		}
		form.render('select');
	}

Skyeye云's avatar
Skyeye云 已提交
208 209 210 211 212
	function loadParamsTable(id) {
		initTableChooseUtil.initTable({
			id: id,
			cols: [
				{id: 'key', title: '入参Key', formType: 'input', width: '150', verify: 'required'},
213
				{id: 'value', title: '值(目前仅支持objectId和objectKey的传值)', formType: 'input', width: '150', verify: 'required'}
Skyeye云's avatar
Skyeye云 已提交
214 215 216 217 218 219 220 221 222
			],
			deleteRowCallback: function (trcusid) {
			},
			addRowCallback: function (trcusid) {
			},
			form: form
		});
	}

Skyeye云's avatar
Skyeye云 已提交
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
	/**
	 * 加载枚举类可选列表
	 */
	function initEnumData() {
		var arr = [];
		$.each(skyeyeClassEnum, function (key, value) {
			arr.push({
				id: key,
				name: value.name
			})
		});
		$("#objectId").html(getDataUseHandlebars(selOption, {rows: arr}));
		form.render('select');
	}

	/**
	 * 加载数据字典可选列表
	 */
	function initDictData() {
		AjaxPostUtil.request({url: reqBasePath + "queryDictTypeListByEnabled", params: {enabled: 1}, type: 'json', method: 'GET', callback: function (json) {
Skyeye云's avatar
Skyeye云 已提交
243
			$("#objectId").html(getDataUseHandlebars(`<option value="">请选择</option>{{#each rows}}<option value="{{dictCode}}">{{name}}</option>{{/each}}`, json));
Skyeye云's avatar
Skyeye云 已提交
244 245 246
		}, async: false});
	}

Skyeye云's avatar
Skyeye云 已提交
247 248 249
	$("body").on("click", "#cancle", function() {
		parent.layer.close(index);
	});
Skyeye云's avatar
Skyeye云 已提交
250

Skyeye云's avatar
Skyeye云 已提交
251
});