UserDepotModal.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <template>
  2. <div ref="container">
  3. <a-modal
  4. :title="title"
  5. :width="800"
  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: 'UserDepotModal',
  60. mixins: [mixinDevice],
  61. data() {
  62. return {
  63. title: '操作',
  64. visible: false,
  65. model: {},
  66. roleId: 0,
  67. iExpandedKeys: [],
  68. roleFunctionTree: [],
  69. checkedKeys: [],
  70. selectedKeys: [],
  71. checkStrictly: false,
  72. hiding: true,
  73. labelCol: {
  74. xs: { span: 24 },
  75. sm: { span: 5 },
  76. },
  77. wrapperCol: {
  78. xs: { span: 24 },
  79. sm: { span: 16 },
  80. },
  81. confirmLoading: false,
  82. form: this.$form.createForm(this),
  83. }
  84. },
  85. created() {},
  86. methods: {
  87. edit(record) {
  88. this.form.resetFields()
  89. this.model = Object.assign({}, {})
  90. this.visible = true
  91. this.$nextTick(() => {
  92. this.form.setFieldsValue(pick(this.model, 'name', 'type', 'description'))
  93. })
  94. this.roleId = record.id
  95. this.checkedKeys = []
  96. this.loadTree(record.id)
  97. },
  98. close() {
  99. this.$emit('close')
  100. this.visible = false
  101. },
  102. handleOk() {
  103. const that = this
  104. // 触发表单验证
  105. this.form.validateFields((err, values) => {
  106. if (!err) {
  107. that.confirmLoading = true
  108. let formData = Object.assign(this.model, values)
  109. formData.type = 'UserDepot'
  110. formData.keyId = this.roleId
  111. formData.value = this.checkedKeys.map((item) => Number(item))
  112. let obj
  113. checkUserBusiness({ type: 'UserDepot', keyId: this.roleId }).then((res) => {
  114. if (res.data && res.data.id) {
  115. formData.id = res.data.id
  116. obj = editUserBusiness(formData)
  117. } else {
  118. obj = addUserBusiness(formData)
  119. }
  120. obj
  121. .then((res) => {
  122. if (res.code === 200) {
  123. that.$emit('ok')
  124. } else {
  125. that.$message.warning(res.data.message)
  126. }
  127. })
  128. .finally(() => {
  129. that.confirmLoading = false
  130. that.close()
  131. })
  132. })
  133. }
  134. })
  135. },
  136. handleCancel() {
  137. this.close()
  138. },
  139. loadTree(id) {
  140. let that = this
  141. that.treeData = []
  142. that.roleFunctionTree = []
  143. let params = {}
  144. params.id = ''
  145. getAction('/depot/findUserDepot?UBType=UserDepot&UBKeyId=' + id).then((res) => {
  146. if (res) {
  147. //机构全选后,再添加机构,选中数量增多
  148. this.allTreeKeys = []
  149. for (let i = 0; i < res.length; i++) {
  150. let temp = res[i]
  151. that.treeData.push(temp)
  152. that.roleFunctionTree.push(temp)
  153. that.setThisExpandedKeys(temp)
  154. that.getAllKeys(temp)
  155. }
  156. console.log(JSON.stringify(this.checkedKeys))
  157. this.loading = false
  158. }
  159. })
  160. },
  161. onCheck(checkedKeys, info) {
  162. console.log('onCheck', checkedKeys, info)
  163. this.hiding = false
  164. if (this.checkStrictly) {
  165. this.checkedKeys = checkedKeys.checked
  166. } else {
  167. this.checkedKeys = checkedKeys
  168. }
  169. },
  170. setThisExpandedKeys(node) {
  171. if (node.checked == true) {
  172. this.checkedKeys.push(node.key)
  173. }
  174. if (node.children && node.children.length > 0) {
  175. this.iExpandedKeys.push(node.key)
  176. for (let a = 0; a < node.children.length; a++) {
  177. this.setThisExpandedKeys(node.children[a])
  178. }
  179. }
  180. },
  181. getAllKeys(node) {
  182. // console.log('node',node);
  183. this.allTreeKeys.push(node.key)
  184. if (node.children && node.children.length > 0) {
  185. for (let a = 0; a < node.children.length; a++) {
  186. this.getAllKeys(node.children[a])
  187. }
  188. }
  189. },
  190. expandAll() {
  191. this.iExpandedKeys = this.allTreeKeys
  192. },
  193. closeAll() {
  194. this.iExpandedKeys = []
  195. },
  196. checkALL() {
  197. this.checkStriccheckStrictlytly = false
  198. this.checkedKeys = this.allTreeKeys
  199. },
  200. cancelCheckALL() {
  201. this.checkedKeys = []
  202. },
  203. switchCheckStrictly(v) {
  204. if (v == 1) {
  205. this.checkStrictly = false
  206. } else if (v == 2) {
  207. this.checkStrictly = true
  208. }
  209. },
  210. onExpand(expandedKeys) {
  211. console.log('onExpand', expandedKeys)
  212. this.iExpandedKeys = expandedKeys
  213. },
  214. },
  215. }
  216. </script>
  217. <style scoped></style>