menu.vue 12 KB

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