You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
604 lines
24 KiB
604 lines
24 KiB
<% function handleFormName(name) {
|
|
if (/-/.test(name)) {
|
|
let arrs = name.split("-")
|
|
return arrs.reduce((pre, cur, index) => pre + (index === 0 ? cur : (cur.charAt(0).toUpperCase() + cur.slice(1))), '')
|
|
} else {
|
|
return name
|
|
}
|
|
} %>
|
|
<template>
|
|
<div>
|
|
<el-card shadow="never" style="margin-top: 20px;">
|
|
<template #header>
|
|
<slot name="header">
|
|
<vxe-toolbar :print="isHasAuth('print')" custom ref="toolbar">
|
|
<template #toolSuffix>
|
|
<vxe-button style="margin-right: 10px;" v-if="isHasAuth('export')" icon="vxe-icon-download" circle @click="exportMethod"></vxe-button>
|
|
</template>
|
|
<template #buttons>
|
|
<el-button
|
|
v-if="isHasAuth('create')"
|
|
icon="el-icon-plus"
|
|
type="primary"
|
|
size="small"
|
|
@click="isShowAdd = true"
|
|
>新增</el-button
|
|
>
|
|
<el-button
|
|
v-if="isHasAuth('search')"
|
|
icon="el-icon-search"
|
|
type="primary"
|
|
plain
|
|
size="small"
|
|
@click="getList"
|
|
>搜索</el-button
|
|
>
|
|
</template>
|
|
</vxe-toolbar>
|
|
</slot>
|
|
</template>
|
|
|
|
<vxe-table
|
|
ref="table"
|
|
stripe
|
|
style="margin-top: 10px"
|
|
:loading="loading"
|
|
:height="tableHeight"
|
|
keep-source
|
|
show-overflow
|
|
:menu-config="{
|
|
className: 'my-menus',
|
|
body: {
|
|
options: [
|
|
[
|
|
{ code: 'copy', name: '复制', prefixConfig: { icon: 'vxe-icon-copy' }, suffixConfig: { content: 'Ctrl+C' } },
|
|
{ code: 'remove', name: '删除', prefixConfig: { icon: 'vxe-icon-delete-fill', className: 'color-red' } }
|
|
],
|
|
]
|
|
}
|
|
}"
|
|
@menu-click="contextMenuClickEvent"
|
|
:row-config="{ isCurrent: true, isHover: true }"
|
|
:column-config="{ resizable: true }"
|
|
:export-config="{}"
|
|
:edit-rules="validRules"
|
|
:edit-config="{
|
|
trigger: 'manual',
|
|
mode: 'row',
|
|
showStatus: true,
|
|
isHover: true,
|
|
autoClear: false,
|
|
}"
|
|
<% if (!!data.options?.isTree) { %>
|
|
:tree-config="{
|
|
|
|
}"
|
|
<% } %>
|
|
:align="allAlign"
|
|
:data="tableData"
|
|
>
|
|
<vxe-column type="checkbox" width="50" align="center" />
|
|
<vxe-column type="seq" width="58" align="center" <% if (!!data.options?.isTree) { %>tree-node<% } %> />
|
|
<% for (let index in data.fields) { %>
|
|
<% let field = data.fields[index] %>
|
|
<% if (!field.hidden) { %>
|
|
<% switch (field.type) {
|
|
case 'input':
|
|
case 'textarea': %>
|
|
<vxe-column
|
|
header-align="center"
|
|
field="<%- field.name %>"
|
|
width="<%- field.width || 160 %>"
|
|
title="<%- field.label %>"
|
|
:edit-render="{ name: 'input', attrs: { type: 'text' } }"
|
|
/>
|
|
<% break
|
|
case 'input-number': %>
|
|
<vxe-column
|
|
header-align="center"
|
|
align="right"
|
|
field="<%- field.name %>"
|
|
width="<%- field.width || 160 %>"
|
|
title="<%- field.label %>"
|
|
:edit-render="{ name: 'input', attrs: { type: 'number' } }"
|
|
/>
|
|
<% break
|
|
case 'richtext': %>
|
|
<vxe-column
|
|
align="center"
|
|
header-align="center"
|
|
field="<%- field.name %>"
|
|
width="<%- field.width || 120 %>"
|
|
title="<%- field.label %>"
|
|
:edit-render="{}"
|
|
>
|
|
<template #default="{ row }">
|
|
<el-button slot="reference" size="small" type="primary" icon="el-icon-search" @click="$refs['RichTextModal'].open({ text: row['<%- field.name %>'], readonly: true, fieldName: '<%- field.name %>', row })">查看</el-button>
|
|
</template>
|
|
<template #edit="{ row }">
|
|
<el-button slot="reference" size="small" type="primary" icon="el-icon-edit" @click="$refs['RichTextModal'].open({ text: row['<%- field.name %>'], readonly: false, fieldName: '<%- field.name %>', row })">编辑</el-button>
|
|
</template>
|
|
</vxe-column>
|
|
<% break
|
|
case 'select': %>
|
|
<vxe-column
|
|
align="center"
|
|
field="<%- field.name %>"
|
|
width="<%- field.width || 180 %>"
|
|
title="<%- field.label %>"
|
|
:edit-render="{ name: 'VxeSelect', options: <%- field.optionsFrom ? handleFormName(field.optionsFrom) : JSON.stringify(field.optionsParams).replaceAll('"','\'') %>, props: { multiple: <%- field.props?.multiple ?? false %> }, optionProps: { value: '<%- field.optionProps?.value ?? 'value' %>', label: '<%- field.optionProps?.label ?? 'label' %>' } }"
|
|
/>
|
|
<% break
|
|
case 'date': %>
|
|
<vxe-column
|
|
align="center"
|
|
field="<%- field.name %>"
|
|
width="<%- field.width || 180 %>"
|
|
title="<%- field.label %>"
|
|
:edit-render="{}"
|
|
>
|
|
<template #edit="{ row }">
|
|
<el-date-picker v-model="row['<%- field.name %>']" style="width: 100%;" size="small" type="<%- field.props.type %>" value-format="<%- field.props.valueFormat ?? 'yyyy-MM-dd' %>"></el-date-picker>
|
|
</template>
|
|
</vxe-column>
|
|
<% break
|
|
case 'switch': %>
|
|
<vxe-column
|
|
align="center"
|
|
field="<%- field.name %>"
|
|
width="<%- field.width || 140 %>"
|
|
title="<%- field.label %>"
|
|
:edit-render="{ name: 'VxeSelect', options: [{ label: '是', value: 1 },{ label: '否', value: 0 }] }"
|
|
/>
|
|
<% break
|
|
case 'tree':
|
|
function treeOptions(myfield) {
|
|
if (myfield.isOwnOptions) {
|
|
if (myfield.optionsHasRoot) {
|
|
return `[{ ${myfield.optionProps?.value ?? 'id'}: 0, ${myfield.optionProps?.label ?? 'label'}: '根结点' ,children: tableData }]`
|
|
} else {
|
|
return 'tableData'
|
|
}
|
|
} else {
|
|
return myfield.optionsFrom ? (myfield.optionsHasRoot ? `[{ ${myfield.optionProps?.value ?? 'id'}: 0, ${myfield.optionProps?.label ?? 'label'}: '根结点' ,children: ${handleFormName(myfield.optionsFrom)} }]` : handleFormName(myfield.optionsFrom)) : myfield.optionsParams
|
|
}
|
|
}
|
|
%>
|
|
<vxe-column
|
|
align="center"
|
|
field="<%- field.name %>"
|
|
width="<%- field.width || 160 %>"
|
|
title="<%- field.label %>"
|
|
:edit-render="{ name: 'VxeTreeSelect', options: <%- treeOptions(field) %>, props: { multiple: <%- field.props?.multiple ?? false %> }, optionProps: { value: '<%- field.optionProps?.value ?? 'id' %>', label: '<%- field.optionProps?.label ?? 'label' %>' } }"
|
|
/>
|
|
<% break
|
|
case 'file': %>
|
|
<vxe-column
|
|
field="<%- field.name %>"
|
|
min-width="180"
|
|
title="<%- field.label %>"
|
|
header-align="center"
|
|
align="left"
|
|
:edit-render="{}"
|
|
>
|
|
<template #default="{ row }">
|
|
<vxe-upload v-model="row['<%- field.relationName ? field.relationName : field.name %>']" name-field="original_name" readonly progress-text="{percent}%" :more-config="{ maxCount: 1, layout: 'horizontal' }" :show-button-text="false" />
|
|
</template>
|
|
<template #edit="{ row }">
|
|
<vxe-upload
|
|
name-field="original_name"
|
|
v-model="row['<%- field.relationName ? field.relationName : field.name %>']"
|
|
progress-text="{percent}%"
|
|
:more-config="{ maxCount: 1, layout: 'horizontal' }"
|
|
:limit-size="uploadSize / 1024 / 1024"
|
|
:show-button-text="false"
|
|
:upload-method="({file}) => uploadMethod(file)" />
|
|
</template>
|
|
</vxe-column>
|
|
<% break
|
|
}
|
|
}
|
|
} %>
|
|
<vxe-column field="operate" header-align="center" title="操作" min-width="220" fixed="right">
|
|
<template #default="{ row }">
|
|
<template v-if="isActiveStatus(row)">
|
|
<el-button size="small" type="primary" @click="saveRowEvent(row)"
|
|
>保存</el-button
|
|
>
|
|
<el-button
|
|
size="small"
|
|
type="primary"
|
|
plain
|
|
@click="cancelRowEvent(row)"
|
|
>取消</el-button
|
|
>
|
|
</template>
|
|
<template v-else>
|
|
<el-button v-if="isHasAuth('detail')" size="small" type="primary" plain @click="detail(row)"
|
|
>查看</el-button
|
|
>
|
|
<el-button v-if="isHasAuth('edit')" size="small" type="warning" @click="editRowEvent(row)"
|
|
>编辑</el-button
|
|
>
|
|
<el-button
|
|
v-if="isHasAuth('delete')"
|
|
size="small"
|
|
type="danger"
|
|
@click="destroyRowEvent(row)"
|
|
>删除</el-button
|
|
>
|
|
</template>
|
|
</template>
|
|
</vxe-column>
|
|
</vxe-table>
|
|
|
|
<% if (!data.options?.isTree) { %>
|
|
<el-pagination
|
|
style="margin-top: 10px;display: flex;justify-content: flex-end;"
|
|
:current-page.sync="select.page"
|
|
:page-sizes="[20, 30, 40, 50]"
|
|
:page-size.sync="select.page_size"
|
|
layout="total, sizes, prev, pager, next, jumper"
|
|
:total="total"
|
|
@size-change="
|
|
(e) => {
|
|
select.page_size = e;
|
|
select.page = 1;
|
|
getList();
|
|
}
|
|
"
|
|
@current-change="
|
|
(e) => {
|
|
select.page = e;
|
|
getList();
|
|
}
|
|
"
|
|
/>
|
|
<% } %>
|
|
</el-card>
|
|
|
|
<Add<%- data.name %>
|
|
ref="Add<%- data.name %>"
|
|
<% for (let index in data.fields) {
|
|
let field = data.fields[index]
|
|
if (field.optionsFrom) { %>
|
|
:<%- field.optionsFrom %>="<%- handleFormName(field.optionsFrom) %>"
|
|
<% }
|
|
if (field.type === 'tree' && !field.optionsFrom) { %>
|
|
:table-data="tableData"
|
|
<% }
|
|
}%>
|
|
:is-show.sync="isShowAdd"
|
|
@refresh="getList"
|
|
/>
|
|
<Show<%- data.name %>
|
|
ref="Show<%- data.name %>"
|
|
<% for (let index in data.fields) {
|
|
let field = data.fields[index]
|
|
if (field.optionsFrom) { %>
|
|
:<%- field.optionsFrom %>="<%- handleFormName(field.optionsFrom) %>"
|
|
<% }
|
|
}%>
|
|
:is-show.sync="isShowDetail"
|
|
/>
|
|
<% if (data.fields.find(field => field.type === 'richtext')) {%>
|
|
<rich-text-modal ref="RichTextModal" @confirm="({ row, fieldName, text }) => row[fieldName] = text"></rich-text-modal>
|
|
<%}%>
|
|
</div>
|
|
</template>
|
|
|
|
<script>
|
|
import VxeUI from "vxe-pc-ui";
|
|
<% if (data.fields.find(field => field.type === 'richtext')) {%>
|
|
import RichTextModal from "@/components/RichTextModal/index.vue";
|
|
<%}%>
|
|
import { authMixin } from "@/mixin/authMixin"
|
|
import { uploadSize } from "@/settings";
|
|
import { deepCopy } from "@/utils";
|
|
import { download } from "@/utils/downloadRequest";
|
|
import { destroy, index, save } from "@/api/<%- data.originalName %>/<%- data.originalName %>";
|
|
import Add<%- data.name %> from "./components/Add<%- data.name %>.vue";
|
|
import Show<%- data.name %> from "./components/Show<%- data.name %>.vue";
|
|
import axios from "axios";
|
|
import { getToken } from "@/utils/auth";
|
|
<% for (let index in data.fields) {
|
|
let field = data.fields[index]
|
|
if (field.optionsFrom) { %>
|
|
<%- `import { index as ${handleFormName(field.optionsFrom)}Index } from '@/api/${field.optionsFrom}/${field.optionsFrom}';` %>
|
|
<% }
|
|
}%>
|
|
export default {
|
|
name: "<%- data.name %>",
|
|
mixins: [authMixin],
|
|
components: {
|
|
<% if (data.fields.find(field => field.type === 'richtext')) {%>
|
|
RichTextModal,
|
|
<%}%>
|
|
Add<%- data.name %>,
|
|
Show<%- data.name %>,
|
|
},
|
|
data() {
|
|
return {
|
|
uploadSize,
|
|
examineKey: 0,
|
|
isShowAdd: false,
|
|
isShowDetail: false,
|
|
|
|
loading: false,
|
|
tableHeight: 400,
|
|
select: {
|
|
page: 1,
|
|
page_size: 20,
|
|
keyword: '',
|
|
show_relation: <%- JSON.stringify(data.fields.filter(i => i.hasOwnProperty('relationName') && i.relationName).map(i => i.relationName)) %>,
|
|
<% if (!!data.options?.isTree) { %>is_tree: 1,<% } %>
|
|
},
|
|
total: 0,
|
|
allAlign: null,
|
|
tableData: [],
|
|
form: {
|
|
id: '',
|
|
<% for (let index in data.fields){ %>
|
|
<%- data.fields[index].name %>: <%- JSON.stringify(data.fields[index].defaultValue) ?? "''" %>,
|
|
<% } %>
|
|
},
|
|
validRules: {
|
|
<% let rulesFields = data.fields.filter(i => i.hasOwnProperty('rules'))
|
|
for (let index in rulesFields){ %>
|
|
<%- rulesFields[index].name %>: [
|
|
<% rulesFields[index].rules.forEach(rule => {
|
|
if (rule.hasOwnProperty('required')) { %>
|
|
{
|
|
required: true,
|
|
message: '<%-rulesFields[index].label%>'+'必填'
|
|
},
|
|
<% } else if (rule.hasOwnProperty('pattern')) { %>
|
|
{
|
|
pattern: <%-rule.pattern%>,
|
|
message: '<%-rulesFields[index].label%>'+'验证失败'
|
|
},
|
|
<% }
|
|
})%>
|
|
],
|
|
<% } %>
|
|
},
|
|
|
|
<% for (let index in data.fields) {
|
|
let field = data.fields[index]
|
|
if (field.optionsFrom) { %>
|
|
<%- handleFormName(field.optionsFrom) + ": []," %>
|
|
<% }
|
|
}%>
|
|
};
|
|
},
|
|
computed: {
|
|
isActiveStatus() {
|
|
return function (row) {
|
|
if (this.$refs["table"]) {
|
|
return this.$refs["table"].isEditByRow(row);
|
|
}
|
|
};
|
|
},
|
|
isHasAuth() {
|
|
return function (auth) {
|
|
return this.auths_auth_mixin.indexOf(auth) !== -1
|
|
}
|
|
}
|
|
},
|
|
created() {
|
|
<% for (let index in data.fields) {
|
|
let field = data.fields[index]
|
|
if (field.optionsFrom) { %>
|
|
<%- `this.get${handleFormName(field.optionsFrom).charAt(0).toUpperCase() + handleFormName(field.optionsFrom).slice(1)}()` %>
|
|
<% }
|
|
}%>
|
|
this.getList();
|
|
},
|
|
mounted() {
|
|
this.bindToolbar()
|
|
this.calcTableHeight()
|
|
},
|
|
methods: {
|
|
exportMethod() {
|
|
this.$confirm("请选择导出方式", "提示", {
|
|
confirmButtonText: "全量导出",
|
|
cancelButtonText: "部分导出",
|
|
distinguishCancelAndClose: true
|
|
}).then(_ => {
|
|
download('/api/admin/school/index', 'get', {
|
|
...this.select,
|
|
page: 1,
|
|
page_size: 9999,
|
|
is_export: 1,
|
|
export_fields: Object.keys(this.form)
|
|
})
|
|
}).catch(action => {
|
|
if (action === 'cancel' && this.$refs['table']) {
|
|
this.$refs['table'].openExport()
|
|
}
|
|
});
|
|
},
|
|
calcTableHeight() {
|
|
let clientHeight = document.documentElement.clientHeight;
|
|
let cardTitle = document.querySelector(".el-card__header")?.getBoundingClientRect()?.height;
|
|
let search = document.querySelector(".vxe-toolbar")?.getBoundingClientRect()?.height;
|
|
let paginationHeight = (document.querySelector(".el-pagination")?.getBoundingClientRect()?.height ?? 0) + 10; //分页的高度
|
|
let topHeight = 50; //页面 头部
|
|
let padding = 80;
|
|
let margin = 20;
|
|
this.tableHeight =
|
|
clientHeight - cardTitle - search - paginationHeight - topHeight - padding - margin;
|
|
},
|
|
contextMenuClickEvent ({ menu, row, column }) {
|
|
switch (menu.code) {
|
|
case 'copy':
|
|
// 示例
|
|
if (row && column) {
|
|
if (VxeUI.clipboard.copy(row[column.field])) {
|
|
this.$message.success('已复制到剪贴板!')
|
|
}
|
|
}
|
|
break
|
|
case 'remove':
|
|
if (row && column) {
|
|
this.destroyRowEvent(row)
|
|
}
|
|
default:
|
|
}
|
|
},
|
|
async detail (row) {
|
|
await this.$refs["Show<%- data.name %>"].getDetail(row.id)
|
|
this.isShowDetail = true
|
|
},
|
|
|
|
<% for (let index in data.fields) {
|
|
let field = data.fields[index]
|
|
if (field.optionsFrom) { %>
|
|
<%- `async get${handleFormName(field.optionsFrom).charAt(0).toUpperCase() + handleFormName(field.optionsFrom).slice(1)}() {
|
|
try {
|
|
const res = await ${handleFormName(field.optionsFrom)}Index({
|
|
page: 1,
|
|
page_size: 999,
|
|
${field.type === 'tree' ? 'is_tree: 1,' : ''}
|
|
}, false)
|
|
this.${handleFormName(field.optionsFrom)} = res${field.type !== 'tree' ? '.data': ''}
|
|
} catch(err) {
|
|
console.error(err)
|
|
}
|
|
},` %>
|
|
<% }
|
|
}%>
|
|
|
|
uploadMethod(file) {
|
|
const formData = new FormData()
|
|
formData.append('file', file)
|
|
window.$_uploading = true
|
|
return axios.post(process.env.VUE_APP_UPLOAD_API, formData, {
|
|
headers: {
|
|
Authorization: `Bearer ${getToken()}`,
|
|
}
|
|
}).then((response) => {
|
|
window.$_uploading = false
|
|
if (response.status === 200 && !response.data.code) {
|
|
return {
|
|
response: response.data,
|
|
name: response.data.original_name,
|
|
url: response.data.url
|
|
}
|
|
} else {
|
|
this.$message.error("上传失败")
|
|
}
|
|
}).catch(_ => {
|
|
window.$_uploading = false
|
|
})
|
|
},
|
|
bindToolbar() {
|
|
this.$nextTick(() => {
|
|
if (this.$refs["table"] && this.$refs["toolbar"]) {
|
|
this.$refs["table"].connect(this.$refs["toolbar"]);
|
|
}
|
|
});
|
|
},
|
|
|
|
editRowEvent(row) {
|
|
if (this.$refs["table"]) {
|
|
this.$refs["table"].setEditRow(row);
|
|
}
|
|
},
|
|
cancelRowEvent(row) {
|
|
if (this.$refs["table"]) {
|
|
this.$refs["table"].clearEdit().then(() => {
|
|
// 还原行数据
|
|
this.$refs["table"].revertData(row);
|
|
});
|
|
}
|
|
},
|
|
|
|
async getList() {
|
|
this.loading = true;
|
|
try {
|
|
const res = await index(this.select, false);
|
|
this.tableData = res<% if (!data.options?.isTree) { %>.data<% } %>;
|
|
this.total = res.total;
|
|
this.loading = false;
|
|
} catch (err) {
|
|
console.error(err);
|
|
this.loading = false;
|
|
}
|
|
},
|
|
|
|
async saveRowEvent(row) {
|
|
if (window.$_uploading) {
|
|
this.$message.warning("文件正在上传中")
|
|
return
|
|
}
|
|
try {
|
|
const errMap = await this.$refs["table"].validate();
|
|
if (errMap) {
|
|
throw new Error(errMap);
|
|
}
|
|
await this.$confirm("确认保存?", "提示", {
|
|
confirmButtonText: "确认",
|
|
cancelButtonText: "取消",
|
|
});
|
|
await this.$refs["table"].clearEdit();
|
|
let form = deepCopy(this.form);
|
|
for (const key in form) {
|
|
form[key] = row[key];
|
|
}
|
|
<% for (let index in data.fields) {
|
|
if (data.fields[index].relationName) {
|
|
if (data.fields[index].props?.multiple) {
|
|
%>
|
|
form['<%- data.fields[index].name %>'] = row['<%- data.fields[index].relationName %>'].map(i => i.response?.id).filter(i => i)
|
|
<% } else { %>
|
|
form['<%- data.fields[index].name %>'] = row['<%- data.fields[index].relationName %>'][0]?.response?.id ?? ''
|
|
<% }
|
|
}
|
|
} %>
|
|
this.loading = true;
|
|
await save(form, false);
|
|
await this.getList();
|
|
this.loading = false;
|
|
} catch (err) {
|
|
this.loading = false;
|
|
}
|
|
},
|
|
async destroyRowEvent(row) {
|
|
try {
|
|
await this.$confirm("确认删除?", "提示", {
|
|
confirmButtonText: "确认",
|
|
cancelButtonText: "取消",
|
|
});
|
|
this.loading = true;
|
|
if (row.id) {
|
|
await destroy({
|
|
id: row.id,
|
|
},false);
|
|
await this.getList();
|
|
} else {
|
|
console.log(row);
|
|
this.tableData.splice(
|
|
this.tableData.findIndex((i) => i._X_ROW_KEY === row._X_ROW_KEY),
|
|
1
|
|
);
|
|
}
|
|
this.loading = false;
|
|
} catch (err) {
|
|
this.loading = false;
|
|
}
|
|
},
|
|
|
|
},
|
|
};
|
|
</script>
|
|
|
|
<style scoped lang="scss">
|
|
::v-deep .el-card__header {
|
|
padding: 6px 20px;
|
|
}
|
|
::v-deep .el-tag + .el-tag {
|
|
margin-left: 4px;
|
|
}
|
|
</style>
|