2021-06-07 17:22:07 +08:00
|
|
|
<template>
|
|
|
|
|
<div class="mock-data-dialog">
|
|
|
|
|
<el-dialog
|
|
|
|
|
:title="title"
|
2021-09-08 17:55:57 +08:00
|
|
|
v-model="dialogVisible"
|
2021-06-07 17:22:07 +08:00
|
|
|
:close-on-click-modal="false"
|
|
|
|
|
:before-close="cancel"
|
|
|
|
|
:show-close="true"
|
|
|
|
|
:destroy-on-close="true"
|
2022-08-15 20:14:02 +08:00
|
|
|
width="900px"
|
2021-06-07 17:22:07 +08:00
|
|
|
>
|
2022-08-15 20:14:02 +08:00
|
|
|
<el-form :model="form" ref="scriptForm" label-width="70px" size="small">
|
2021-06-07 17:22:07 +08:00
|
|
|
<el-form-item prop="method" label="名称">
|
|
|
|
|
<el-input v-model.trim="form.name" placeholder="请输入名称"></el-input>
|
|
|
|
|
</el-form-item>
|
|
|
|
|
|
|
|
|
|
<el-form-item prop="description" label="描述">
|
|
|
|
|
<el-input v-model.trim="form.description" placeholder="请输入描述"></el-input>
|
|
|
|
|
</el-form-item>
|
|
|
|
|
|
|
|
|
|
<el-form-item prop="type" label="类型">
|
|
|
|
|
<el-select v-model="form.type" default-first-option style="width: 100%" placeholder="请选择类型">
|
|
|
|
|
<el-option v-for="item in enums.scriptTypeEnum" :key="item.value" :label="item.label" :value="item.value"></el-option>
|
|
|
|
|
</el-select>
|
|
|
|
|
</el-form-item>
|
|
|
|
|
|
2022-07-26 18:32:45 +08:00
|
|
|
<el-row style="margin-left: 30px; margin-bottom: 5px">
|
|
|
|
|
<el-button @click="onAddParam" size="small" type="success">新增占位符参数</el-button>
|
|
|
|
|
</el-row>
|
|
|
|
|
<el-form-item :key="param" v-for="(param, index) in params" prop="params" :label="`参数${index + 1}`">
|
|
|
|
|
<el-row>
|
2022-08-15 20:14:02 +08:00
|
|
|
<el-col :span="5"><el-input v-model="param.model" placeholder="内容中用{{.model}}替换"></el-input></el-col>
|
2022-07-26 18:32:45 +08:00
|
|
|
<el-divider :span="1" direction="vertical" border-style="dashed" />
|
2022-08-15 20:14:02 +08:00
|
|
|
<el-col :span="4"><el-input v-model="param.name" placeholder="字段名"></el-input></el-col>
|
2022-07-26 18:32:45 +08:00
|
|
|
<el-divider :span="1" direction="vertical" border-style="dashed" />
|
2022-08-15 20:14:02 +08:00
|
|
|
<el-col :span="4"><el-input v-model="param.placeholder" placeholder="字段说明"></el-input></el-col>
|
2022-07-26 18:32:45 +08:00
|
|
|
<el-divider :span="1" direction="vertical" border-style="dashed" />
|
2022-08-15 20:14:02 +08:00
|
|
|
<el-col :span="4">
|
|
|
|
|
<el-input v-model="param.options" placeholder="可选值 ,分割"></el-input>
|
|
|
|
|
</el-col>
|
|
|
|
|
<el-divider :span="1" direction="vertical" border-style="dashed" />
|
|
|
|
|
<el-col :span="2"><el-button @click="onDeleteParam(index)" size="small" type="danger">删除</el-button></el-col>
|
2022-07-26 18:32:45 +08:00
|
|
|
</el-row>
|
2021-07-28 18:03:19 +08:00
|
|
|
</el-form-item>
|
|
|
|
|
|
2021-06-07 17:22:07 +08:00
|
|
|
<el-form-item prop="script" label="内容" id="content">
|
2022-01-28 11:30:11 +08:00
|
|
|
<codemirror ref="cmEditor" v-model="form.script" language="shell" width="700px" />
|
2021-06-07 17:22:07 +08:00
|
|
|
</el-form-item>
|
|
|
|
|
</el-form>
|
|
|
|
|
|
|
|
|
|
<template #footer>
|
|
|
|
|
<div class="dialog-footer">
|
2022-08-15 20:14:02 +08:00
|
|
|
<el-button @click="cancel()" :disabled="submitDisabled">关 闭</el-button>
|
2021-07-28 18:03:19 +08:00
|
|
|
<el-button
|
|
|
|
|
v-auth="'machine:script:save'"
|
|
|
|
|
type="primary"
|
|
|
|
|
:loading="btnLoading"
|
|
|
|
|
@click="btnOk"
|
|
|
|
|
:disabled="submitDisabled"
|
|
|
|
|
>保 存</el-button
|
|
|
|
|
>
|
2021-06-07 17:22:07 +08:00
|
|
|
</div>
|
|
|
|
|
</template>
|
|
|
|
|
</el-dialog>
|
|
|
|
|
</div>
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<script lang="ts">
|
|
|
|
|
import { ref, toRefs, reactive, watch, defineComponent } from 'vue';
|
|
|
|
|
import { ElMessage } from 'element-plus';
|
|
|
|
|
import { machineApi } from './api';
|
|
|
|
|
import enums from './enums';
|
|
|
|
|
import { notEmpty } from '@/common/assert';
|
|
|
|
|
|
|
|
|
|
import { codemirror } from '@/components/codemirror';
|
|
|
|
|
|
|
|
|
|
export default defineComponent({
|
|
|
|
|
name: 'ScriptEdit',
|
|
|
|
|
components: {
|
|
|
|
|
codemirror,
|
|
|
|
|
},
|
|
|
|
|
props: {
|
|
|
|
|
visible: {
|
|
|
|
|
type: Boolean,
|
|
|
|
|
},
|
|
|
|
|
data: {
|
|
|
|
|
type: Object,
|
|
|
|
|
},
|
|
|
|
|
title: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
|
|
|
|
machineId: {
|
|
|
|
|
type: Number,
|
|
|
|
|
},
|
|
|
|
|
isCommon: {
|
|
|
|
|
type: Boolean,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
setup(props: any, { emit }) {
|
|
|
|
|
const { isCommon, machineId } = toRefs(props);
|
2022-07-26 18:32:45 +08:00
|
|
|
const scriptForm: any = ref(null);
|
2021-06-07 17:22:07 +08:00
|
|
|
|
|
|
|
|
const state = reactive({
|
2021-09-08 17:55:57 +08:00
|
|
|
dialogVisible: false,
|
2021-06-07 17:22:07 +08:00
|
|
|
submitDisabled: false,
|
2022-07-26 18:32:45 +08:00
|
|
|
params: [] as any,
|
2021-06-07 17:22:07 +08:00
|
|
|
form: {
|
|
|
|
|
id: null,
|
|
|
|
|
name: '',
|
|
|
|
|
machineId: 0,
|
|
|
|
|
description: '',
|
|
|
|
|
script: '',
|
2022-07-26 18:32:45 +08:00
|
|
|
params: '',
|
2021-06-07 17:22:07 +08:00
|
|
|
type: null,
|
|
|
|
|
},
|
|
|
|
|
btnLoading: false,
|
|
|
|
|
});
|
|
|
|
|
|
2021-09-08 17:55:57 +08:00
|
|
|
watch(props, (newValue) => {
|
2022-07-26 18:32:45 +08:00
|
|
|
state.dialogVisible = newValue.visible;
|
|
|
|
|
if (!newValue.visible) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-06-07 17:22:07 +08:00
|
|
|
if (newValue.data) {
|
|
|
|
|
state.form = { ...newValue.data };
|
2022-07-26 18:32:45 +08:00
|
|
|
if (state.form.params) {
|
|
|
|
|
state.params = JSON.parse(state.form.params);
|
|
|
|
|
}
|
2021-06-07 17:22:07 +08:00
|
|
|
} else {
|
|
|
|
|
state.form = {} as any;
|
|
|
|
|
state.form.script = '';
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2022-07-26 18:32:45 +08:00
|
|
|
const onAddParam = () => {
|
|
|
|
|
state.params.push({ name: '', model: '', placeholder: '' });
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onDeleteParam = (idx: number) => {
|
|
|
|
|
state.params.splice(idx, 1);
|
|
|
|
|
};
|
|
|
|
|
|
2021-06-07 17:22:07 +08:00
|
|
|
const btnOk = () => {
|
|
|
|
|
state.form.machineId = isCommon.value ? 9999999 : (machineId.value as any);
|
|
|
|
|
console.log('machineid:', machineId);
|
2022-07-26 18:32:45 +08:00
|
|
|
scriptForm.value.validate((valid: any) => {
|
2021-06-07 17:22:07 +08:00
|
|
|
if (valid) {
|
|
|
|
|
notEmpty(state.form.name, '名称不能为空');
|
|
|
|
|
notEmpty(state.form.description, '描述不能为空');
|
|
|
|
|
notEmpty(state.form.script, '内容不能为空');
|
2022-07-26 18:32:45 +08:00
|
|
|
if (state.params) {
|
|
|
|
|
state.form.params = JSON.stringify(state.params);
|
|
|
|
|
}
|
2021-06-07 17:22:07 +08:00
|
|
|
machineApi.saveScript.request(state.form).then(
|
2021-09-08 17:55:57 +08:00
|
|
|
() => {
|
2021-06-07 17:22:07 +08:00
|
|
|
ElMessage.success('保存成功');
|
|
|
|
|
emit('submitSuccess');
|
|
|
|
|
state.submitDisabled = false;
|
|
|
|
|
cancel();
|
|
|
|
|
},
|
2021-09-08 17:55:57 +08:00
|
|
|
() => {
|
2021-06-07 17:22:07 +08:00
|
|
|
state.submitDisabled = false;
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const cancel = () => {
|
|
|
|
|
emit('update:visible', false);
|
|
|
|
|
emit('cancel');
|
2022-07-26 18:32:45 +08:00
|
|
|
state.params = [];
|
2021-06-07 17:22:07 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
...toRefs(state),
|
|
|
|
|
enums,
|
2022-07-26 18:32:45 +08:00
|
|
|
onAddParam,
|
|
|
|
|
onDeleteParam,
|
|
|
|
|
scriptForm,
|
2021-06-07 17:22:07 +08:00
|
|
|
btnOk,
|
|
|
|
|
cancel,
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
</script>
|
|
|
|
|
<style lang="scss">
|
|
|
|
|
// .m-dialog {
|
|
|
|
|
// .el-cascader {
|
|
|
|
|
// width: 100%;
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
#content {
|
|
|
|
|
.CodeMirror {
|
|
|
|
|
height: 300px !important;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
</style>
|