123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494 |
- <template>
- <a-card :bordered="false">
- <a-row :gutter="24">
- <a-button style="margin-right: 10px;" type="primary" icon="plus" @click="addChild">添加子菜单</a-button>
- <a-button style="margin-right: 10px;background-color: #19be6b;color: #fff;" type="" icon="plus" @click="addTitle">添加顶部菜单</a-button>
- <a-button style="margin-right: 10px;background-color: red;color: #fff;" type="" icon="delete" @click="delect()">删除</a-button>
- </a-row>
- <a-row :gutter="24">
- <!-- 左侧菜单树列表 -->
- <a-col :span="8">
- <a-row :gutter="24" class="textCount">
- <a-icon type="info-circle" style="color: #1890FF;margin-right: 10px;" />
- <span>当前选择:<span style="color: #40a9ff;font-weight: 600;">{{ selectedMenu }}</span><span v-if="selectedKeys.length" class="imgSize" @click="cancelSel">取消选择</span></span>
- </a-row>
- <a-row :gutter="24" >
- <a-input-search placeholder="输入搜索菜单名搜索" style="width: 100%" @change="onChange" />
- <a-tree
- @expand="onExpand"
- :treeData="getMenus"
- :expandedKeys="expandedKeys"
- :autoExpandParent="autoExpandParent"
- @select="onSelect"
- :selectedKeys="selectedKeys"
- showIcon
- >
- <template slot="title" slot-scope="{title}">
- <span v-if="title.indexOf(searchValue) > -1">
- {{ title.substr(0, title.indexOf(searchValue)) }}
- <span style="color: #f50">{{ searchValue }}</span>
- {{ title.substr(title.indexOf(searchValue) + searchValue.length) }}
- </span>
- <span v-else>{{ title }}</span>
- </template>
- <a-icon slot="file" type="file" />
- <a-icon slot="pushpin" type="pushpin" />
- </a-tree>
- </a-row>
- </a-col>
- <!-- 右侧 -->
- <a-col :span="16" >
- <a-form :form="form" @submit="handleSubmit">
- <!-- 类型 -->
- <a-form-item label="类型" :label-col="{ span: 5 }" :wrapper-col="{ span: 14 }">
- <div v-if="!formData.type||formData.type=='0'">
- <a-icon type="file" style="margin-right: 10px;" /><span >页面菜单</span>
- </div>
- <div v-if="formData.type=='1'">
- <a-icon type="pushpin" style="margin-right: 10px;" /><span >操作按钮</span>
- </div>
- </a-form-item>
- <!-- 名称 -->
- <a-form-item label="名称" :label-col="{ span: 5 }" :wrapper-col="{ span: 14 }">
- <a-input
- placeholder="请输入名称"
- v-decorator="['formData.name', {
- initialValue: formData.name,
- rules: [{ required: true, message: '请输入名称!' },{pattern:'^[^<|>]{1,20}$',message:'请输入不含<或>的字符,最多20个字符'}] }]"
- />
- </a-form-item>
- <!-- 路由英文名 -->
- <!-- <a-form-item v-if="!formData.type||formData.type=='0'" label="路由英文名" :label-col="{ span: 5 }" :wrapper-col="{ span: 14 }">
- <a-input
- placeholder="请输入路由英文名"
- v-decorator="['formData.routeName', {
- initialValue: formData.routeName,
- rules: [{ required: true, message: '请输入路由英文名!' }] }]"
- />
- </a-form-item> -->
- <!-- 路径 -->
- <!-- <a-form-item v-if="!formData.type||formData.type=='0'" label="路径" :label-col="{ span: 5 }" :wrapper-col="{ span: 14 }">
- <a-input
- placeholder="请输入路径"
- v-decorator="['formData.routePath', {
- initialValue: formData.routePath,
- rules: [{ required: true, message: '请输入路径!' }] }]"
- />
- </a-form-item> -->
- <!-- 请求路径 -->
- <!-- <a-form-item v-if="formData.type==1" label="请求路径" :label-col="{ span: 5 }" :wrapper-col="{ span: 14 }">
- <a-input
- placeholder="请输入请求路径"
- v-decorator="['formData.urlPath', {
- initialValue: formData.urlPath,
- rules: [{ required: true, message: '请输入请求路径!' }] }]"
- />
- </a-form-item> -->
- <!-- 前端权限编码 -->
- <a-form-item label="前端权限编码" :label-col="{ span: 5 }" :wrapper-col="{ span: 14 }">
- <a-input
- placeholder="请输入前端权限编码"
- v-decorator="[
- 'formData.code',
- {
- initialValue: formData.code,
- rules: [{ required: true, message: '请输入前端权限编码!' }] },
- ]"
- />
- </a-form-item>
- <!-- 后台权限编码 -->
- <a-form-item label="后台权限编码" :label-col="{ span: 5 }" :wrapper-col="{ span: 14 }">
- <a-input
- placeholder="请输入后台权限编码"
- v-decorator="[
- 'formData.permission',
- {
- initialValue: formData.permission,
- rules: [{ required: formData.isLeaf, message: '请输入后台权限编码!' }] },
- ]"
- />
- </a-form-item>
- <!-- 图标 -->
- <!-- <a-form-item v-if="!formData.type ||formData.type=='0' " label="图标" :label-col="{ span: 5 }" :wrapper-col="{ span: 14 }">
- <a-input
- placeholder="请输入图标名称"
- v-decorator="['formData.icon', {
- initialValue: formData.icon,
- rules: [] }]"
- />
- </a-form-item> -->
- <!-- 前端组件 -->
- <!-- <a-form-item v-if="!formData.type ||formData.type=='0'" label="前端组件" :label-col="{ span: 5 }" :wrapper-col="{ span: 14 }">
- <a-input
- placeholder="请输入前端组件路径"
- v-decorator="['formData.componentPath', {
- initialValue: formData.componentPath,
- rules: [{ required: true, message: '请输入前端组件路径!' }] }]"
- />
- </a-form-item> -->
- <!-- 第三方链接 -->
- <!-- <a-form-item v-if="!formData.type ||formData.type=='0'" label="第三方链接" :label-col="{ span: 5 }" :wrapper-col="{ span: 14 }">
- <a-input
- placeholder="请输入第三方链接地址"
- v-decorator="['formData.urlPath', {
- initialValue: formData.urlPath,
- rules: [] }]"
- />
- </a-form-item> -->
- <!-- 排序值 -->
- <a-form-item label="排序值" :label-col="{ span: 5 }" :wrapper-col="{ span: 14 }">
- <a-input
- type="number"
- placeholder="请输入排序值"
- v-decorator="['formData.sort', {
- initialValue: formData.sort,
- rules: [] }]"
- />
- </a-form-item>
- <!-- 连锁可用 -->
- <!-- <a-form-item label="连锁可用" :label-col="{ span: 5 }" :wrapper-col="{ span: 14 }">
- <a-radio-group
- name="radioGroup"
- v-decorator="[
- 'formData.chainOrgUse',
- {
- initialValue: formData.chainOrgUse,
- rules: [{ required: true, message: '请选择!' }] },
- ]"
- >
- <a-radio :value="1">是</a-radio>
- <a-radio :value="0">否</a-radio>
- </a-radio-group>
- </a-form-item> -->
- <!-- 单店可用 -->
- <!-- <a-form-item label="单店可用" :label-col="{ span: 5 }" :wrapper-col="{ span: 14 }">
- <a-radio-group
- name="radioGroup"
- v-decorator="[
- 'formData.singleOrgUse',
- {
- initialValue: formData.singleOrgUse,
- rules: [{ required: true, message: '请选择!' }] },
- ]"
- >
- <a-radio :value="1">是</a-radio>
- <a-radio :value="0">否</a-radio>
- </a-radio-group>
- </a-form-item> -->
- <!-- 状态 -->
- <a-form-item label="启用" :label-col="{ span: 5 }" :wrapper-col="{ span: 14 }">
- <a-radio-group
- name="radioGroup"
- v-decorator="[
- 'formData.status',
- {
- initialValue: formData.status,
- rules: [{ required: true, message: '请选择状态!' }] },
- ]"
- >
- <a-radio :value="1">是</a-radio>
- <a-radio :value="0">否</a-radio>
- </a-radio-group>
- </a-form-item>
- <a-form-item :wrapper-col="{ span: 12, offset: 5 }">
- <a-button type="primary" @click="handleSubmit()">
- 保存
- </a-button>
- <a-button :style="{ marginLeft: '8px' }" @click="clear">
- 重置
- </a-button>
- </a-form-item>
- </a-form>
- </a-col>
- </a-row>
- <childModal parentType="store" :visible="showChildModal" @refresh="getMdMenuList" :data="parentData" @close="showChildModal = false"></childModal>
- </a-card>
- </template>
- <script>
- import { STable, VSelect } from '@/components'
- import childModal from './childModal.vue'
- import { getMdMenuList, saveMdMenu, deleteMdMenu } from '@/api/menu.js'
- export default {
- name: 'Menu',
- components: {
- STable, VSelect, childModal
- },
- props: {},
- data () {
- return {
- treeData: [],
- dataList: [],
- searchValue: '',
- expandedKeys: [],
- autoExpandParent: true,
- selectedKeys: [],
- selectedMenu: '', // 选择的树节点名
- formLayout: 'horizontal',
- form: this.$form.createForm(this, { name: 'menu' }),
- showChildModal: false,
- parentData: {}, // 父级菜单
- formData: {
- id: '',
- name: '',
- routePath: '',
- icon: '',
- routeName: '',
- componentPath: '',
- urlPath: '',
- code: '',
- permission: '',
- sort: '',
- type: '0',
- isLeaf: true,
- chainOrgUse: '1',
- singleOrgUse: '0',
- status: '' // 活动状态
- }
- }
- },
- computed: {
- getMenus () {
- this.generateList(this.treeData)
- return this.treeData
- }
- },
- methods: {
- onExpand (expandedKeys) {
- // console.log('onExpand', expandedKeys)
- this.expandedKeys = expandedKeys
- this.autoExpandParent = false
- },
- onSelect (selectedKeys, info) {
- console.log('onSelect', info)
- this.selectedKeys = selectedKeys
- if (info.selected) {
- const data = info.selectedNodes[0].data.props
- const isLeaf = data.dataRef.children
- // this.formData = Object.assign({}, this.formData, info.selectedNodes[0].data.props)
- this.formData = {
- id: data.id,
- name: data.name,
- routePath: data.routePath,
- icon: data.icon,
- routeName: data.routeName,
- componentPath: data.componentPath,
- urlPath: data.urlPath,
- code: data.code,
- permission: data.permission,
- sort: data.sort,
- type: data.type,
- chainOrgUse: data.chainOrgUse,
- singleOrgUse: data.singleOrgUse,
- status: data.status,
- isLeaf: !(isLeaf && isLeaf.length)
- }
- this.selectedMenu = this.formData.name
- this.formData.status = Number(this.formData.status)
- this.formData.chainOrgUse = Number(this.formData.chainOrgUse)
- this.formData.singleOrgUse = Number(this.formData.singleOrgUse)
- } else {
- this.selectedMenu = ''
- this.clear()
- }
- },
- generateList (data) {
- for (let i = 0; i < data.length; i++) {
- const node = data[i]
- const key = node.title
- this.dataList.push({ key, title: key })
- node.scopedSlots = { title: 'title' }
- node.slots = { icon: node.type == '0' ? 'file' : 'pushpin' }
- if (node.children) {
- this.generateList(node.children)
- }
- }
- },
- getParentKey (key, tree) {
- let parentKey
- for (let i = 0; i < tree.length; i++) {
- const node = tree[i]
- if (node.children) {
- if (node.children.some(item => item.title == key)) {
- parentKey = node.key
- } else if (this.getParentKey(key, node.children)) {
- parentKey = this.getParentKey(key, node.children)
- }
- }
- }
- return parentKey
- },
- onChange (e) {
- const value = e.target.value
- const expandedKeys = this.dataList
- .map(item => {
- if (item.title.indexOf(value) > -1) {
- return this.getParentKey(item.title, this.treeData)
- }
- return null
- })
- .filter((item, i, self) => item && self.indexOf(item) === i)
- Object.assign(this, {
- expandedKeys,
- searchValue: value,
- autoExpandParent: true
- })
- },
- // 获取菜单树列表
- getMdMenuList () {
- getMdMenuList().then(res => {
- console.log(res, 'res1111111')
- if (res.status == 200) {
- this.treeData = res.data
- console.log(this.treeData, 'this.treeData')
- }
- })
- },
- // 添加子节点
- addChild () {
- if (!this.selectedMenu) {
- return this.$message.warning('请先选择一个父级菜单')
- } else if (this.formData.type == '1') {
- return this.$message.warning('请选择一个页面级菜单')
- }
- this.showChildModal = true
- this.parentData = this.formData
- },
- // 添加顶部菜单
- addTitle () {
- this.showChildModal = true
- this.parentData = {}
- },
- // 取消选择
- cancelSel () {
- this.selectedKeys = []
- this.selectedMenu = ''
- this.clear()
- },
- // 清空列表
- clear () {
- this.form.resetFields()
- this.formData.id = ''
- this.formData.name = ''
- this.formData.routePath = ''
- this.formData.icon = ''
- this.formData.routeName = ''
- this.formData.componentPath = ''
- this.formData.urlPath = ''
- this.formData.sort = ''
- this.formData.type = ''
- this.formData.chainOrgUse = ''
- this.formData.singleOrgUse = ''
- this.formData.status = ''
- this.formData.code = ''
- this.formData.permission = ''
- this.formData.isLeaf = true
- },
- // 删除数据
- delect () {
- if (!this.selectedKeys.length) {
- return this.$message.warning('您还未选择要删除的数据')
- }
- const _this = this
- console.log(_this.formData.id, '_this.formData.id')
- this.$confirm({
- title: '警告',
- content: `确定要删除您选择的 ${_this.formData.name} 的数据吗?`,
- okText: '确定',
- cancelText: '取消',
- onOk () {
- deleteMdMenu({
- id: _this.formData.id
- }).then(res => {
- if (res.status == 200) {
- _this.$message.success('删除成功')
- _this.getMdMenuList()
- }
- }).catch(() => {
- _this.$message.success('删除失败, 请稍后重试')
- })
- }
- })
- },
- // 保存提交
- handleSubmit () {
- const _this = this
- this.form.validateFields((err, values) => {
- if (!err) {
- console.log(values.formData, ' formData.type222222222')
- saveMdMenu(Object.assign({ id: this.formData.id }, values.formData)).then(res => {
- console.log(res, 'res--save')
- if (res.status + '' === '200') {
- this.$message.success(res.message ? res.message : '保存成功')
- _this.getMdMenuList()
- _this.clear()
- } else {
- // this.$message.warning(res.message)
- }
- })
- }
- })
- },
- handleCancel () {
- this.clear()
- }
- },
- mounted () {
- },
- beforeCreate () {
- this.form = this.$form.createForm(this, { name: 'menu' })
- },
- beforeRouteEnter (to, from, next) {
- next(vm => {
- vm.getMdMenuList()
- })
- },
- watch: {
- }
- }
- </script>
- <style>
- .menu-box::-webkit-scrollbar{
- width: 6px;
- height: 6px ;
- }
- .menu-box::-webkit-scrollbar-thumb{
- width: 6px;
- height: 6px ;
- border-radius: 4px;
- -webkit-box-shadow: inset 0 0 2px #d1d1d1;
- background: #e4e4e4;
- }
- .textCount{
- border: 1px solid #91d5ff;
- background-color: #e6f7ff;
- border-radius: 5px;
- padding: 5px 10px 5px 20px;
- margin: 10px 0;
- color: rgba(0, 0, 0, 0.85);
- }
- .imgSize{
- color: #2d8cf0;
- margin-left: 10px;
- font-size: 14px;
- }
- </style>
|