menuModal.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <template>
  2. <div>
  3. <a-modal
  4. centered
  5. class="modalBox"
  6. :title="roleName"
  7. v-model="isshow"
  8. @cancle="cancel"
  9. :maskClosable="false">
  10. <a-spin :spinning="spinning" tip="Loading...">
  11. <a-form class="form-box" style="max-height: 600px;" :form="form" @submit="handleSubmit">
  12. <a-tree
  13. checkable
  14. @check="onCheck"
  15. @expand="onExpand"
  16. :expandedKeys="expandedKeys"
  17. :autoExpandParent="autoExpandParent"
  18. v-model="checkedKeys"
  19. :treeData="treeData"
  20. />
  21. <a-form-item :wrapper-col="{ offset: 15 }">
  22. <a-button type="primary" @click="handleSubmit()" id="menuModal-handleSubmit">
  23. 保存
  24. </a-button>
  25. <a-button :style="{ marginLeft: '8px' }" @click="cancel" id="menuModal-handleCancel">
  26. 取消
  27. </a-button>
  28. </a-form-item>
  29. </a-form>
  30. </a-spin>
  31. </a-modal>
  32. </div>
  33. </template>
  34. <script>
  35. import { STable, VSelect } from '@/components'
  36. import { addMenuPower } from '@/api/power-role.js'
  37. export default {
  38. name: 'RoleModal',
  39. components: {
  40. STable, VSelect
  41. },
  42. props: {
  43. visible: {
  44. type: Boolean,
  45. default: false
  46. },
  47. data: {
  48. type: Object,
  49. default: function () {
  50. return {}
  51. }
  52. }
  53. },
  54. data () {
  55. return {
  56. spinning: false,
  57. treeData: [],
  58. roleName: '',
  59. expandedKeys: [],
  60. autoExpandParent: true,
  61. checkedKeys: [],
  62. checkedData: [],
  63. isshow: this.visible,
  64. formLayout: 'horizontal',
  65. form: this.$form.createForm(this, { name: 'menuModal' }),
  66. id: '' // 角色id
  67. }
  68. },
  69. methods: {
  70. onExpand (expandedKeys) {
  71. this.expandedKeys = expandedKeys
  72. this.autoExpandParent = false
  73. },
  74. onCheck (checkedKeys, info) {
  75. this.checkedData = [...checkedKeys, ...info.halfCheckedKeys]
  76. this.checkedKeys = checkedKeys
  77. },
  78. cancel (e) {
  79. this.clear()
  80. this.$emit('close')
  81. },
  82. // 保存提交
  83. handleSubmit () {
  84. const _this = this
  85. this.form.validateFields((err, values) => {
  86. if (!err) {
  87. console.log(this.checkedData)
  88. if (this.checkedData.length == 0) {
  89. return this.$message.warning('请先选择菜单')
  90. }
  91. const arr = this.checkedData.join(',')
  92. console.log({ id: this.id, menuIds: arr })
  93. _this.spinning = true
  94. addMenuPower({ id: this.id, menuIds: arr }).then(res => {
  95. console.log(res, 'res--save')
  96. if (res.status + '' === '200') {
  97. this.$message.success(res.message ? res.message : '保存成功')
  98. this.$emit('refresh')
  99. setTimeout(function () {
  100. _this.cancel()
  101. _this.spinning = false
  102. }, 300)
  103. } else {
  104. _this.spinning = false
  105. // this.$message.error(res.message)
  106. }
  107. })
  108. }
  109. })
  110. },
  111. clear () {
  112. this.form.resetFields()
  113. this.roleName = ''
  114. this.id = ''
  115. this.checkedKeys = []
  116. this.expandedKeys = []
  117. },
  118. // 查找叶子节点
  119. findLeaf (data, arr) {
  120. for (let i = 0; i < data.length; i++) {
  121. const node = data[i]
  122. if (node.children) {
  123. this.findLeaf(node.children, arr)
  124. } else {
  125. const hasNode = arr.find(item => {
  126. return item == node.id
  127. })
  128. if (hasNode) {
  129. this.expandedKeys.push(node.id)
  130. this.checkedKeys.push(node.id)
  131. }
  132. }
  133. }
  134. }
  135. },
  136. beforeCreate () {
  137. this.form = this.$form.createForm(this, { name: 'menuModal' })
  138. },
  139. watch: {
  140. visible (newValue, oldValue) {
  141. this.isshow = newValue
  142. },
  143. isshow (newValue, oldValue) {
  144. if (newValue) {
  145. if (this.data) { // 编辑
  146. this.treeData = this.data.allMenuList
  147. this.id = this.data.role.id
  148. this.roleName = '分配权限' + '(' + this.data.role.name + ')'
  149. if (this.data.role.menuIds) {
  150. const arr = this.data.role.menuIds.split(',')
  151. this.checkedData = arr
  152. // 找出叶子节点
  153. this.findLeaf(this.treeData, arr)
  154. }
  155. }
  156. } else {
  157. this.cancel()
  158. }
  159. }
  160. }
  161. }
  162. </script>
  163. <style >
  164. .ant-modal-title{
  165. padding-right: 30px;
  166. }
  167. .ant-modal-footer {
  168. display: none;
  169. }
  170. .form-box {
  171. max-height: 600px !important;
  172. overflow: auto;
  173. }
  174. .form-box::-webkit-scrollbar{
  175. width: 6px;
  176. height: 6px ;
  177. }
  178. .form-box::-webkit-scrollbar-thumb{
  179. width: 6px;
  180. height: 6px ;
  181. border-radius: 4px;
  182. -webkit-box-shadow: inset 0 0 2px #d1d1d1;
  183. background: #e4e4e4;
  184. }
  185. </style>