menu.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. <template>
  2. <a-card :bordered="false">
  3. <a-row :gutter="24">
  4. <a-button style="margin-right: 10px;" type="primary" icon="plus" @click="addChild" id="menu-addChild">新增子菜单</a-button>
  5. <a-button style="margin-right: 10px;background-color: #19be6b;color: #fff;" type="" icon="plus" @click="addTitle" id="menu-addTitle">新增顶部菜单</a-button>
  6. <a-button style="margin-right: 10px;background-color: red;color: #fff;" type="" icon="delete" @click="delect()" id="menu-delect">删除</a-button>
  7. </a-row>
  8. <a-row :gutter="24">
  9. <!-- 左侧菜单树列表 -->
  10. <a-col :span="8">
  11. <a-row :gutter="24" class="textCount">
  12. <a-icon type="info-circle" style="color: #1890FF;margin-right: 10px;" />
  13. <span>当前选择:<span style="color: #40a9ff;font-weight: 600;">{{ selectedMenu }}</span><span v-if="selectedKeys.length" class="imgSize" @click="cancelSel">取消选择</span></span>
  14. </a-row>
  15. <a-row :gutter="24" >
  16. <a-input-search placeholder="输入搜索菜单名搜索" style="width: 100%" @change="onChange" id="menu-onChange" />
  17. <a-tree
  18. @expand="onExpand"
  19. :treeData="getMenus"
  20. :expandedKeys="expandedKeys"
  21. :autoExpandParent="autoExpandParent"
  22. @select="onSelect"
  23. :selectedKeys="selectedKeys"
  24. showIcon
  25. >
  26. <template slot="title" slot-scope="{title}">
  27. <span v-if="title.indexOf(searchValue) > -1">
  28. {{ title.substr(0, title.indexOf(searchValue)) }}
  29. <span style="color: #f50">{{ searchValue }}</span>
  30. {{ title.substr(title.indexOf(searchValue) + searchValue.length) }}
  31. </span>
  32. <span v-else>{{ title }}</span>
  33. </template>
  34. <a-icon slot="file" type="file" />
  35. <a-icon slot="pushpin" type="pushpin" />
  36. </a-tree>
  37. </a-row>
  38. </a-col>
  39. <!-- 右侧 -->
  40. <a-col :span="16" >
  41. <a-form :form="form" @submit="handleSubmit">
  42. <!-- 类型 -->
  43. <a-form-item label="类型" :label-col="{ span: 5 }" :wrapper-col="{ span: 14 }">
  44. <div v-if="!formData.type||formData.type=='0'">
  45. <a-icon type="file" style="margin-right: 10px;" /><span >页面菜单</span>
  46. </div>
  47. <div v-if="formData.type=='1'">
  48. <a-icon type="pushpin" style="margin-right: 10px;" /><span >操作按钮</span>
  49. </div>
  50. </a-form-item>
  51. <!-- 名称 -->
  52. <a-form-item label="名称" :label-col="{ span: 5 }" :wrapper-col="{ span: 14 }">
  53. <a-input
  54. placeholder="请输入名称30个字以内"
  55. :maxLength="30"
  56. allowClear
  57. id="menu-name"
  58. v-decorator="['formData.name', {
  59. initialValue: formData.name,getValueFromEvent: $filterEmpty,
  60. rules: [{ required: true, message: '请输入名称30个字以内!' },{pattern:'^[^<|>]{1,30}$',message:'请输入不含<或>的字符,最多30个字符'}] }]"
  61. />
  62. </a-form-item>
  63. <!-- 路由英文名 -->
  64. <!-- <a-form-item v-if="!formData.type||formData.type=='0'" label="路由英文名" :label-col="{ span: 5 }" :wrapper-col="{ span: 14 }">
  65. <a-input
  66. placeholder="请输入路由英文名"
  67. v-decorator="['formData.routeName', {
  68. initialValue: formData.routeName,
  69. rules: [{ required: true, message: '请输入路由英文名!' }] }]"
  70. />
  71. </a-form-item> -->
  72. <!-- 路径 -->
  73. <!-- <a-form-item v-if="!formData.type||formData.type=='0'" label="路径" :label-col="{ span: 5 }" :wrapper-col="{ span: 14 }">
  74. <a-input
  75. placeholder="请输入路径"
  76. v-decorator="['formData.routePath', {
  77. initialValue: formData.routePath,
  78. rules: [{ required: true, message: '请输入路径!' }] }]"
  79. />
  80. </a-form-item> -->
  81. <!-- 请求路径 -->
  82. <!-- <a-form-item v-if="formData.type==1" label="请求路径" :label-col="{ span: 5 }" :wrapper-col="{ span: 14 }">
  83. <a-input
  84. placeholder="请输入请求路径"
  85. v-decorator="['formData.urlPath', {
  86. initialValue: formData.urlPath,
  87. rules: [{ required: true, message: '请输入请求路径!' }] }]"
  88. />
  89. </a-form-item> -->
  90. <!-- 前端权限编码 -->
  91. <a-form-item label="前端权限编码" :label-col="{ span: 5 }" :wrapper-col="{ span: 14 }">
  92. <a-input
  93. placeholder="请输入前端权限编码"
  94. allowClear
  95. id="menu-code"
  96. v-decorator="[
  97. 'formData.code',
  98. {
  99. initialValue: formData.code,getValueFromEvent: $filterEmpty,
  100. rules: [{ required: true, message: '请输入前端权限编码!' }] },
  101. ]"
  102. />
  103. </a-form-item>
  104. <!-- 后台权限编码 -->
  105. <a-form-item label="后台权限编码" :label-col="{ span: 5 }" :wrapper-col="{ span: 14 }">
  106. <a-input
  107. placeholder="请输入后台权限编码"
  108. allowClear
  109. id="menu-permission"
  110. v-decorator="[
  111. 'formData.permission',
  112. {
  113. initialValue: formData.permission,getValueFromEvent: $filterEmpty,
  114. rules: [{ required: formData.isLeaf, message: '请输入后台权限编码!' }] },
  115. ]"
  116. />
  117. </a-form-item>
  118. <!-- 图标 -->
  119. <!-- <a-form-item v-if="!formData.type ||formData.type=='0' " label="图标" :label-col="{ span: 5 }" :wrapper-col="{ span: 14 }">
  120. <a-input
  121. placeholder="请输入图标名称"
  122. v-decorator="['formData.icon', {
  123. initialValue: formData.icon,
  124. rules: [] }]"
  125. />
  126. </a-form-item> -->
  127. <!-- 前端组件 -->
  128. <!-- <a-form-item v-if="!formData.type ||formData.type=='0'" label="前端组件" :label-col="{ span: 5 }" :wrapper-col="{ span: 14 }">
  129. <a-input
  130. placeholder="请输入前端组件路径"
  131. v-decorator="['formData.componentPath', {
  132. initialValue: formData.componentPath,
  133. rules: [{ required: true, message: '请输入前端组件路径!' }] }]"
  134. />
  135. </a-form-item> -->
  136. <!-- 第三方链接 -->
  137. <!-- <a-form-item v-if="!formData.type ||formData.type=='0'" label="第三方链接" :label-col="{ span: 5 }" :wrapper-col="{ span: 14 }">
  138. <a-input
  139. placeholder="请输入第三方链接地址"
  140. v-decorator="['formData.urlPath', {
  141. initialValue: formData.urlPath,
  142. rules: [] }]"
  143. />
  144. </a-form-item> -->
  145. <!-- 排序值 -->
  146. <a-form-item label="排序值" :label-col="{ span: 5 }" :wrapper-col="{ span: 14 }">
  147. <a-input
  148. type="number"
  149. oninput="if(value.length>6)value=value.slice(0,6);if(value<0)value=0"
  150. placeholder="请输入(0~999999)之间的排序值"
  151. allowClear
  152. id="menu-sort"
  153. v-decorator="['formData.sort', {
  154. initialValue: formData.sort,getValueFromEvent: $filterEmpty,
  155. rules: [] }]"
  156. />
  157. </a-form-item>
  158. <!-- 状态 -->
  159. <a-form-item label="启用" :label-col="{ span: 5 }" :wrapper-col="{ span: 14 }">
  160. <a-radio-group
  161. name="radioGroup"
  162. id="menu-status"
  163. v-decorator="[
  164. 'formData.status',
  165. {
  166. initialValue: formData.status,
  167. rules: [{ required: true, message: '请选择状态!' }] },
  168. ]"
  169. >
  170. <a-radio :value="1">是</a-radio>
  171. <a-radio :value="0">否</a-radio>
  172. </a-radio-group>
  173. </a-form-item>
  174. <a-form-item :wrapper-col="{ span: 12, offset: 5 }">
  175. <a-button type="primary" @click="handleSubmit()" id="menu-handleSubmit">
  176. 保存
  177. </a-button>
  178. <a-button :style="{ marginLeft: '8px' }" @click="clear" id="menu-clear">
  179. 重置
  180. </a-button>
  181. </a-form-item>
  182. </a-form>
  183. </a-col>
  184. </a-row>
  185. <childModal :visible="showChildModal" @refresh="getMenuList" :data="parentData" @close="showChildModal = false"></childModal>
  186. </a-card>
  187. </template>
  188. <script>
  189. import { STable, VSelect } from '@/components'
  190. import childModal from './childModal.vue'
  191. import { getMenuList, saveMenu, deleteMenu } from '@/api/menu.js'
  192. export default {
  193. name: 'Menu',
  194. components: {
  195. STable, VSelect, childModal
  196. },
  197. props: {},
  198. data () {
  199. return {
  200. treeData: [],
  201. dataList: [],
  202. searchValue: '',
  203. expandedKeys: [],
  204. autoExpandParent: true,
  205. selectedKeys: [],
  206. selectedMenu: '', // 选择的树节点名
  207. formLayout: 'horizontal',
  208. form: this.$form.createForm(this, { name: 'menu' }),
  209. showChildModal: false,
  210. parentData: {}, // 父级菜单
  211. formData: {
  212. id: '',
  213. name: '',
  214. routePath: '',
  215. icon: '',
  216. routeName: '',
  217. componentPath: '',
  218. urlPath: '',
  219. code: '',
  220. permission: '',
  221. sort: '',
  222. type: '0',
  223. isLeaf: true,
  224. status: '' // 活动状态
  225. }
  226. }
  227. },
  228. computed: {
  229. getMenus () {
  230. this.generateList(this.treeData)
  231. return this.treeData
  232. }
  233. },
  234. methods: {
  235. onExpand (expandedKeys) {
  236. // console.log('onExpand', expandedKeys)
  237. this.expandedKeys = expandedKeys
  238. this.autoExpandParent = false
  239. },
  240. onSelect (selectedKeys, info) {
  241. console.log('onSelect', info)
  242. this.clear()
  243. this.selectedKeys = selectedKeys
  244. if (info.selected) {
  245. const data = info.selectedNodes[0].data.props
  246. const isLeaf = data.dataRef.children
  247. // this.formData = Object.assign({}, this.formData, info.selectedNodes[0].data.props)
  248. this.formData = {
  249. id: data.id,
  250. name: data.name,
  251. routePath: data.routePath,
  252. icon: data.icon,
  253. routeName: data.routeName,
  254. componentPath: data.componentPath,
  255. urlPath: data.urlPath,
  256. code: data.code,
  257. permission: data.permission,
  258. sort: data.sort,
  259. type: data.type,
  260. status: data.status,
  261. isLeaf: !(isLeaf && isLeaf.length)
  262. }
  263. this.selectedMenu = this.formData.name
  264. this.formData.status = Number(this.formData.status)
  265. } else {
  266. this.selectedMenu = ''
  267. this.clear()
  268. }
  269. },
  270. generateList (data) {
  271. for (let i = 0; i < data.length; i++) {
  272. const node = data[i]
  273. const key = node.title
  274. this.dataList.push({ key, title: key })
  275. node.scopedSlots = { title: 'title' }
  276. node.slots = { icon: node.type == '0' ? 'file' : 'pushpin' }
  277. if (node.children) {
  278. this.generateList(node.children)
  279. }
  280. }
  281. },
  282. getParentKey (key, tree) {
  283. let parentKey
  284. for (let i = 0; i < tree.length; i++) {
  285. const node = tree[i]
  286. if (node.children) {
  287. if (node.children.some(item => item.title == key)) {
  288. parentKey = node.key
  289. } else if (this.getParentKey(key, node.children)) {
  290. parentKey = this.getParentKey(key, node.children)
  291. }
  292. }
  293. }
  294. return parentKey
  295. },
  296. onChange (e) {
  297. const value = e.target.value
  298. const expandedKeys = this.dataList
  299. .map(item => {
  300. if (item.title.indexOf(value) > -1) {
  301. return this.getParentKey(item.title, this.treeData)
  302. }
  303. return null
  304. })
  305. .filter((item, i, self) => item && self.indexOf(item) === i)
  306. Object.assign(this, {
  307. expandedKeys,
  308. searchValue: value,
  309. autoExpandParent: true
  310. })
  311. },
  312. // 获取菜单树列表
  313. getMenuList () {
  314. getMenuList().then(res => {
  315. console.log(res, 'res1111111')
  316. if (res.status == 200) {
  317. this.treeData = res.data
  318. console.log(this.treeData, 'this.treeData')
  319. }
  320. })
  321. },
  322. // 添加子节点
  323. addChild () {
  324. if (!this.selectedMenu) {
  325. return this.$message.warning('请先选择一个父级菜单')
  326. } else if (this.formData.type == '1') {
  327. return this.$message.warning('请选择一个页面级菜单')
  328. }
  329. this.showChildModal = true
  330. this.parentData = this.formData
  331. },
  332. // 添加顶部菜单
  333. addTitle () {
  334. this.showChildModal = true
  335. this.parentData = {}
  336. },
  337. // 取消选择
  338. cancelSel () {
  339. this.selectedKeys = []
  340. this.selectedMenu = ''
  341. this.clear()
  342. },
  343. // 清空列表
  344. clear () {
  345. this.form.resetFields()
  346. this.formData.id = ''
  347. this.formData.name = ''
  348. this.formData.routePath = ''
  349. this.formData.icon = ''
  350. this.formData.routeName = ''
  351. this.formData.componentPath = ''
  352. this.formData.urlPath = ''
  353. this.formData.sort = ''
  354. this.formData.type = ''
  355. this.formData.status = ''
  356. this.formData.code = ''
  357. this.formData.permission = ''
  358. this.formData.isLeaf = true
  359. },
  360. // 删除数据
  361. delect () {
  362. if (!this.selectedKeys.length) {
  363. return this.$message.warning('您还未选择要删除的数据')
  364. }
  365. const _this = this
  366. console.log(_this.formData.id, '_this.formData.id')
  367. this.$confirm({
  368. title: '警告',
  369. content: `确定要删除您选择的 ${_this.formData.name} 的数据吗?`,
  370. okText: '确定',
  371. cancelText: '取消',
  372. onOk () {
  373. deleteMenu({
  374. id: _this.formData.id
  375. }).then(res => {
  376. if (res.status == 200) {
  377. _this.$message.success('删除成功')
  378. _this.getMenuList()
  379. }
  380. }).catch(() => {
  381. _this.$message.success('删除失败, 请稍后重试')
  382. })
  383. }
  384. })
  385. },
  386. // 保存提交
  387. handleSubmit () {
  388. const _this = this
  389. this.form.validateFields((err, values) => {
  390. if (!err) {
  391. console.log(values.formData, ' formData.type222222222')
  392. saveMenu(Object.assign({ id: this.formData.id }, values.formData)).then(res => {
  393. console.log(res, 'res--save')
  394. if (res.status + '' === '200') {
  395. this.$message.success(res.message ? res.message : '保存成功')
  396. _this.getMenuList()
  397. // _this.clear()
  398. } else {
  399. // this.$message.warning(res.message)
  400. }
  401. })
  402. }
  403. })
  404. },
  405. handleCancel () {
  406. this.clear()
  407. }
  408. },
  409. mounted () {
  410. },
  411. beforeCreate () {
  412. this.form = this.$form.createForm(this, { name: 'menu' })
  413. },
  414. beforeRouteEnter (to, from, next) {
  415. next(vm => {
  416. vm.getMenuList()
  417. })
  418. },
  419. watch: {
  420. }
  421. }
  422. </script>
  423. <style>
  424. .menu-box::-webkit-scrollbar{
  425. width: 6px;
  426. height: 6px ;
  427. }
  428. .menu-box::-webkit-scrollbar-thumb{
  429. width: 6px;
  430. height: 6px ;
  431. border-radius: 4px;
  432. -webkit-box-shadow: inset 0 0 2px #d1d1d1;
  433. background: #e4e4e4;
  434. }
  435. .textCount{
  436. border: 1px solid #91d5ff;
  437. background-color: #e6f7ff;
  438. border-radius: 5px;
  439. padding: 5px 10px 5px 20px;
  440. margin: 10px 0;
  441. color: rgba(0, 0, 0, 0.85);
  442. }
  443. .imgSize{
  444. color: #2d8cf0;
  445. margin-left: 10px;
  446. font-size: 14px;
  447. }
  448. </style>