menu.vue 15 KB

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