storeMenus.vue 18 KB

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