RoleFunctionModal.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <template>
  2. <div ref="container">
  3. <a-modal
  4. :title="title"
  5. :width="width"
  6. :visible="visible"
  7. :confirmLoading="confirmLoading"
  8. :getContainer="() => $refs.container"
  9. :maskStyle="{ top: '93px', left: '154px' }"
  10. :wrapClassName="wrapClassNameInfo()"
  11. :mask="isDesktop()"
  12. :maskClosable="false"
  13. @ok="handleOk"
  14. @cancel="handleCancel"
  15. cancelText="取消"
  16. okText="保存"
  17. style="top: 5%; height: 95%"
  18. >
  19. <a-spin :spinning="confirmLoading">
  20. <div class="drawer-bootom-button">
  21. <a-dropdown :trigger="['click']" placement="topCenter">
  22. <a-menu slot="overlay">
  23. <a-menu-item key="1" @click="switchCheckStrictly(1)">父子关联</a-menu-item>
  24. <a-menu-item key="2" @click="switchCheckStrictly(2)">取消关联</a-menu-item>
  25. <a-menu-item key="3" @click="checkALL">全部勾选</a-menu-item>
  26. <a-menu-item key="4" @click="cancelCheckALL">取消全选</a-menu-item>
  27. <a-menu-item key="5" @click="expandAll">展开所有</a-menu-item>
  28. <a-menu-item key="6" @click="closeAll">合并所有</a-menu-item>
  29. </a-menu>
  30. <a-button> 树操作 <a-icon type="up" /> </a-button>
  31. </a-dropdown>
  32. </div>
  33. <a-col :md="10" :sm="24">
  34. <template>
  35. <a-tree
  36. checkable
  37. multiple
  38. @check="onCheck"
  39. :selectedKeys="selectedKeys"
  40. :checkedKeys="checkedKeys"
  41. :treeData="roleFunctionTree"
  42. :checkStrictly="checkStrictly"
  43. :expandedKeys="iExpandedKeys"
  44. :autoExpandParent="true"
  45. @expand="onExpand"
  46. />
  47. </template>
  48. </a-col>
  49. </a-spin>
  50. </a-modal>
  51. </div>
  52. </template>
  53. <script>
  54. import pick from 'lodash.pick'
  55. import { mixinDevice } from '@/utils/mixin'
  56. import { addUserBusiness, editUserBusiness, checkUserBusiness } from '@/api/api'
  57. import { getAction } from '../../../api/manage'
  58. export default {
  59. name: 'RoleFunctionModal',
  60. mixins: [mixinDevice],
  61. data() {
  62. return {
  63. title: '操作',
  64. width: '800px',
  65. visible: false,
  66. model: {},
  67. roleId: 0,
  68. iExpandedKeys: [],
  69. roleFunctionTree: [],
  70. checkedKeys: [],
  71. selectedKeys: [],
  72. checkStrictly: false,
  73. hiding: true,
  74. labelCol: {
  75. xs: { span: 24 },
  76. sm: { span: 5 },
  77. },
  78. wrapperCol: {
  79. xs: { span: 24 },
  80. sm: { span: 16 },
  81. },
  82. confirmLoading: false,
  83. form: this.$form.createForm(this),
  84. }
  85. },
  86. created() {},
  87. methods: {
  88. edit(record) {
  89. this.form.resetFields()
  90. this.model = Object.assign({}, {})
  91. this.visible = true
  92. this.$nextTick(() => {
  93. this.form.setFieldsValue(pick(this.model, 'name', 'type', 'description'))
  94. })
  95. this.roleId = record.id
  96. this.checkedKeys = []
  97. this.loadTree(record.id)
  98. },
  99. close() {
  100. this.$emit('close')
  101. this.visible = false
  102. },
  103. handleOk() {
  104. const that = this
  105. // 触发表单验证
  106. this.form.validateFields((err, values) => {
  107. if (!err) {
  108. that.confirmLoading = true
  109. let formData = Object.assign(this.model, values)
  110. formData.type = 'RoleFunctions'
  111. formData.keyId = this.roleId
  112. formData.value = this.checkedKeys.map((item) => Number(item))
  113. let obj
  114. checkUserBusiness({ type: 'RoleFunctions', keyId: this.roleId }).then((res) => {
  115. if (res.data && res.data.id) {
  116. formData.id = res.data.id
  117. obj = editUserBusiness(formData)
  118. } else {
  119. obj = addUserBusiness(formData)
  120. }
  121. obj
  122. .then((res) => {
  123. if (res.code === 200) {
  124. that.$emit('ok', this.roleId)
  125. } else {
  126. that.$message.warning(res.data.message)
  127. }
  128. })
  129. .finally(() => {
  130. that.confirmLoading = false
  131. that.close()
  132. })
  133. })
  134. }
  135. })
  136. },
  137. handleCancel() {
  138. this.close()
  139. },
  140. loadTree(id) {
  141. let that = this
  142. that.treeData = []
  143. that.roleFunctionTree = []
  144. let params = {}
  145. params.id = ''
  146. getAction('/function/findRoleFunction?UBType=RoleFunctions&UBKeyId=' + id).then((res) => {
  147. if (res) {
  148. //机构全选后,再添加机构,选中数量增多
  149. this.allTreeKeys = []
  150. for (let i = 0; i < res.length; i++) {
  151. let temp = res[i]
  152. that.treeData.push(temp)
  153. that.roleFunctionTree.push(temp)
  154. that.setThisExpandedKeys(temp)
  155. that.getAllKeys(temp)
  156. }
  157. console.log(JSON.stringify(this.checkedKeys))
  158. this.loading = false
  159. }
  160. })
  161. },
  162. onCheck(checkedKeys, info) {
  163. console.log('onCheck', checkedKeys, info)
  164. this.hiding = false
  165. if (this.checkStrictly) {
  166. this.checkedKeys = checkedKeys.checked
  167. } else {
  168. this.checkedKeys = checkedKeys
  169. }
  170. },
  171. setThisExpandedKeys(node) {
  172. if (node.checked == true) {
  173. this.checkedKeys.push(Number(node.key))
  174. }
  175. if (node.children && node.children.length > 0) {
  176. this.iExpandedKeys.push(Number(node.key))
  177. for (let a = 0; a < node.children.length; a++) {
  178. this.setThisExpandedKeys(node.children[a])
  179. }
  180. }
  181. },
  182. getAllKeys(node) {
  183. // console.log('node',node);
  184. this.allTreeKeys.push(Number(node.key))
  185. if (node.children && node.children.length > 0) {
  186. for (let a = 0; a < node.children.length; a++) {
  187. this.getAllKeys(node.children[a])
  188. }
  189. }
  190. },
  191. expandAll() {
  192. this.iExpandedKeys = this.allTreeKeys
  193. },
  194. closeAll() {
  195. this.iExpandedKeys = []
  196. },
  197. checkALL() {
  198. this.checkStriccheckStrictlytly = false
  199. this.checkedKeys = this.allTreeKeys
  200. },
  201. cancelCheckALL() {
  202. this.checkedKeys = []
  203. },
  204. switchCheckStrictly(v) {
  205. if (v == 1) {
  206. this.checkStrictly = false
  207. } else if (v == 2) {
  208. this.checkStrictly = true
  209. }
  210. },
  211. onExpand(expandedKeys) {
  212. console.log('onExpand', expandedKeys)
  213. this.iExpandedKeys = expandedKeys
  214. },
  215. },
  216. }
  217. </script>
  218. <style scoped></style>