storeMenus.vue 17 KB

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