menu.vue 15 KB

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