audit-permission-tree.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. <template>
  2. <a-card
  3. :class="[
  4. $style.treewrap,
  5. $style.ywlxtree,
  6. { [$style.collapse]: fold, [$style.single]: single },
  7. ]"
  8. >
  9. <!-- 选择部门 -->
  10. <a-tree-select
  11. v-if="isSelectDep"
  12. v-model="depvalue"
  13. :allow-clear="true"
  14. :tree-data="depOptions"
  15. @change="changedep"
  16. >
  17. </a-tree-select>
  18. <!-- 搜索框 -->
  19. <a-input-search
  20. v-if="isSearch"
  21. v-model="searchStr"
  22. placeholder=""
  23. allow-clear
  24. @change="onSearchChange"
  25. />
  26. <a-spin :spinning="spinning" :class="$style.spin" />
  27. <a-empty v-if="empty" />
  28. <a-tree
  29. v-else
  30. :key="refreshKey"
  31. ref="auditTree"
  32. v-model="checkedKeys"
  33. :block-node="false"
  34. :show-icon="true"
  35. :show-line="showLine"
  36. :check-strictly="true"
  37. :checkable="checkable"
  38. :disabled="disabled"
  39. :draggable="draggable"
  40. :tree-data="treeData"
  41. :replace-fields="replaceFields"
  42. :selected-keys="defaultSelectedKeys"
  43. :expanded-keys="expandedKeys"
  44. :default-expanded-keys="defaultTreeExpandedKeys"
  45. :load-data="onLoadData"
  46. @select="treeSelect"
  47. @check="treeCheck"
  48. @expand="onExpand"
  49. @dragenter="onDragEnter"
  50. @drop="onDrop"
  51. >
  52. <template v-slot:title="{ text, props }">
  53. <span v-if="text.indexOf(searchValue) > -1" :title="text">
  54. {{ text.substr(0, text.indexOf(searchValue)) }}
  55. <span :class="$style.active">{{ searchValue }}</span>
  56. {{ text.substr(text.indexOf(searchValue) + searchValue.length) }}
  57. </span>
  58. <span v-else :title="text">{{ text }}</span>
  59. <!-- 后台返回数量,如果有数量的话就显示 -->
  60. <span v-if="props.count > 0">({{ props.count }})</span>
  61. </template>
  62. <!-- 如果已配置负责人,则展示用户图标 -->
  63. <template slot="hasuser" slot-scope="item">
  64. <a-icon type="team" @click="showUser(item)" />
  65. </template>
  66. </a-tree>
  67. <a-button v-if="showPrimary" type="primary" :class="$style.fold" @click="foldClick">
  68. <a-icon :type="icontype" />
  69. </a-button>
  70. </a-card>
  71. </template>
  72. <script>
  73. import AuditPermissionTreeMixins from './audit-permission-tree-mixins'
  74. import auditPermissionTreeService from './audit-permission-tree-service'
  75. import components from './_import-components/audit-permission-tree-import'
  76. export default {
  77. name: 'AuditPermissionTree',
  78. metaInfo: {
  79. title: 'AuditPermissionTree',
  80. },
  81. components,
  82. mixins: [AuditPermissionTreeMixins],
  83. props: {
  84. // 默认选中节点
  85. // selectedKeys: {
  86. // type: Array,
  87. // default: function() {
  88. // return [1]
  89. // },
  90. // },
  91. // 是否开启搜索
  92. isSearch: {
  93. type: Boolean,
  94. default: false,
  95. },
  96. // 是否显示复选框
  97. checkable: {
  98. type: Boolean,
  99. default: false,
  100. },
  101. // 是否可以拖拽
  102. draggable: {
  103. type: Boolean,
  104. default: false,
  105. },
  106. // 是否显示连线
  107. showLine: {
  108. type: Boolean,
  109. default: false,
  110. },
  111. // 根节点名称
  112. topNodeText: {
  113. type: String,
  114. default: 'XX科技',
  115. },
  116. // 地址树接口数据源
  117. treeparams: {
  118. type: Object,
  119. default: () => {
  120. return {}
  121. },
  122. },
  123. // 默认展开节点id
  124. defaultExpandedKeys: {
  125. type: Array,
  126. default: () => {
  127. return ['0']
  128. },
  129. },
  130. // 是否显示部门选择下拉框
  131. isSelectDep: {
  132. type: Boolean,
  133. default: false,
  134. },
  135. // 部门下拉框列表值
  136. depOptions: {
  137. type: Array,
  138. default: () => {
  139. return []
  140. },
  141. },
  142. // 部门下拉框默认值
  143. defaultDepValue: {
  144. type: Array,
  145. default: () => {
  146. return []
  147. },
  148. },
  149. // 是否显示隐藏按钮
  150. showPrimary: {
  151. type: Boolean,
  152. default: true,
  153. },
  154. // 地址树是否单选
  155. single: {
  156. type: Boolean,
  157. default: false,
  158. },
  159. refreshKey: {
  160. type: Number,
  161. default: 0,
  162. },
  163. // 复选框是否失效
  164. disabled: {
  165. type: Boolean,
  166. default: false,
  167. },
  168. },
  169. data() {
  170. return {
  171. defaultTreeExpandedKeys: ['1'],
  172. defaultSelectedKeys: ['1'],
  173. selectedKeys: ['1'],
  174. depvalue: [], // 选择的组织数据
  175. checkedKeys: [], // 选中的节点数据
  176. treeData: [],
  177. dataList: [], // 数组dataList,搜索要用
  178. spinning: true,
  179. empty: false,
  180. icontype: 'left',
  181. fold: false,
  182. replaceFields: {
  183. title: 'text',
  184. key: 'id',
  185. },
  186. expandedKeys: ['1'],
  187. backupsExpandedKeys: [],
  188. autoExpandParent: false,
  189. searchValue: '',
  190. searchStr: '',
  191. defaultTopNodeText: 'XX科技',
  192. defaultTopNodeId: '1',
  193. key: 1,
  194. }
  195. },
  196. created() {},
  197. methods: {
  198. onDragEnter(info) {
  199. // expandedKeys 需要受控时设置
  200. this.expandedKeys = info.expandedKeys
  201. },
  202. // 拖拽节点方法
  203. onDrop(info) {
  204. // 被插入节点信息
  205. const dropKey = info.node.eventKey
  206. // 拖拽节点信息
  207. const dragKey = info.dragNode.eventKey
  208. const dropPos = info.node.pos.split('-')
  209. // -1表示之前 0表示合并 1表示之后
  210. const dropPosition = info.dropPosition - Number(dropPos[dropPos.length - 1])
  211. const loop = (data, key, callback) => {
  212. data.forEach((item, index, arr) => {
  213. if (item.id === key) {
  214. return callback(item, index, arr)
  215. }
  216. if (item.children) {
  217. return loop(item.children, key, callback)
  218. }
  219. })
  220. }
  221. const data = this.treeData
  222. // Find dragObject
  223. let dragObj
  224. loop(data, dragKey, (item, index, arr) => {
  225. arr.splice(index, 1)
  226. dragObj = item
  227. })
  228. if (!info.dropToGap) {
  229. // Drop on the content
  230. loop(data, dropKey, (item) => {
  231. item.children = item.children || []
  232. // where to insert 示例添加到尾部,可以是随意位置
  233. item.children.push(dragObj)
  234. })
  235. } else if (
  236. (info.node.children || []).length > 0 && // Has children
  237. info.node.expanded && // Is expanded
  238. dropPosition === 1 // On the bottom gap
  239. ) {
  240. loop(data, dropKey, (item) => {
  241. item.children = item.children || []
  242. // where to insert 示例添加到尾部,可以是随意位置
  243. item.children.unshift(dragObj)
  244. })
  245. } else {
  246. let ar
  247. let i
  248. loop(data, dropKey, (item, index, arr) => {
  249. ar = arr
  250. i = index
  251. })
  252. if (dropPosition === -1) {
  253. ar.splice(i, 0, dragObj)
  254. } else {
  255. ar.splice(i + 1, 0, dragObj)
  256. }
  257. }
  258. this.treeData = data
  259. // 拖拽完成,调用接口
  260. const dragParams = {
  261. configId: this.treeparams.configId,
  262. id: dragKey,
  263. targetId: dropKey,
  264. position: dropPosition,
  265. }
  266. auditPermissionTreeService.dragNode(dragParams).then((res) => {
  267. console.log(res)
  268. })
  269. },
  270. // 点击地址树展开时调用
  271. onLoadData(treeNode) {
  272. let moduleId = ''
  273. if (this.treeparams.type === 'SJ') {
  274. moduleId = 'iamOrg'
  275. } else if (this.treeparams.type === 'FK') {
  276. moduleId = 'iamOrg2'
  277. }
  278. const params = {
  279. moduleId: moduleId,
  280. isDisabled: this.treeparams.isDisabled,
  281. }
  282. return auditPermissionTreeService.getCategoryTree(treeNode.dataRef.id, params).then((res) => {
  283. treeNode.dataRef.children = res.data
  284. this.treeData = this.transformData([...this.treeData])
  285. this.generateList(this.treeData)
  286. // 更新宽度
  287. setTimeout(() => {
  288. this.fnchangewidth()
  289. }, 500)
  290. })
  291. },
  292. // 初始化地址树
  293. initTreeData(depId) {
  294. let topDepId = null
  295. let moduleId = ''
  296. if (this.treeparams.type === 'SJ') {
  297. moduleId = 'iamOrg'
  298. } else if (this.treeparams.type === 'FK') {
  299. moduleId = 'iamOrg2'
  300. }
  301. const params = {
  302. moduleId: moduleId,
  303. isDisabled: this.treeparams.isDisabled,
  304. }
  305. auditPermissionTreeService.getParentOrgInfo(params).then((res) => {
  306. if (res.data.length > 0) {
  307. const treeNode = []
  308. this.defaultTopNodeId = res.data[0].id
  309. // if (this.topNodeText) {
  310. // this.defaultTopNodeText = this.topNodeText
  311. // } else {
  312. this.defaultTopNodeText = res.data[0].text
  313. // }
  314. this.defaultTreeExpandedKeys = [res.data[0].id]
  315. this.defaultSelectedKeys = [res.data[0].id]
  316. this.selectedKeys = [res.data[0].id]
  317. // 根节点信息
  318. // const treeNode = [
  319. // {
  320. // id: this.defaultTopNodeId,
  321. // text: this.defaultTopNodeText,
  322. // leaf: false,
  323. // props: {
  324. // isroot: true,
  325. // },
  326. // children: [],
  327. // key: this.defaultTopNodeId,
  328. // },
  329. // ]
  330. // res.data.forEach((i, index) => {
  331. for (var fi = 0; fi < res.data.length; fi++) {
  332. topDepId = !depId ? res.data[fi].id : depId
  333. var i = res.data[fi]
  334. treeNode.push({
  335. id: i.id,
  336. text: i.text,
  337. leaf: false,
  338. props: i.props,
  339. children: [],
  340. key: i.id,
  341. })
  342. }
  343. var fni = 0
  344. treeNode.forEach((i) => {
  345. auditPermissionTreeService.getCategoryTree(i.id, params).then((res1) => {
  346. this.spinning = false
  347. fni = fni + 1
  348. if (res1.data.length) {
  349. i.children = res1.data
  350. }
  351. if (fni === treeNode.length) {
  352. this.treeSelect([treeNode[0].id], {
  353. node: { dataRef: { id: treeNode[0].id, text: treeNode[0].text }, checked: false },
  354. selected: true,
  355. })
  356. this.treeData = this.transformData(treeNode)
  357. this.expandedKeys = this.defaultTreeExpandedKeys
  358. this.generateList(this.treeData)
  359. this.empty = false
  360. }
  361. })
  362. })
  363. } else {
  364. this.empty = true
  365. this.spinning = false
  366. }
  367. })
  368. },
  369. },
  370. }
  371. </script>
  372. <style module lang="scss">
  373. @use '@/common/design' as *;
  374. .spin {
  375. width: 100%;
  376. line-height: 30;
  377. }
  378. .ywlxtree {
  379. :global(.ant-tree-title) {
  380. display: inline-block;
  381. width: 100%;
  382. overflow: hidden;
  383. text-overflow: ellipsis;
  384. white-space: nowrap;
  385. }
  386. :global(.ant-input-search) {
  387. margin: 8px 0;
  388. overflow: hidden;
  389. }
  390. .active {
  391. color: $primary-color;
  392. }
  393. :global(.ant-select) {
  394. width: 100%;
  395. overflow: hidden;
  396. }
  397. }
  398. .treewrap {
  399. position: relative;
  400. display: flex;
  401. flex-direction: column;
  402. width: 20%;
  403. min-height: 600px;
  404. margin-right: $padding-lg;
  405. transition: width 0.2s;
  406. .fold {
  407. position: absolute;
  408. top: calc(50% - 30px);
  409. right: -15px;
  410. z-index: 2;
  411. width: 18px;
  412. height: 75px;
  413. padding: 0;
  414. border-radius: 0 10px 10px 0;
  415. }
  416. :global(.ant-tree) {
  417. width: 100% !important;
  418. overflow: hidden;
  419. text-overflow: ellipsis;
  420. white-space: nowrap;
  421. }
  422. :global(.ant-card-body) {
  423. height: 100%;
  424. background: $white;
  425. // overflow: auto;
  426. }
  427. }
  428. .collapse {
  429. width: 0;
  430. margin-right: 55px;
  431. :global(.ant-card-body) {
  432. background: transparent;
  433. // overflow: auto;
  434. :global(.ant-empty) {
  435. display: none;
  436. }
  437. }
  438. }
  439. // 单选样式
  440. .single {
  441. :global .ant-tree-checkbox {
  442. .ant-tree-checkbox-inner {
  443. border-radius: 100px;
  444. &::after {
  445. position: absolute;
  446. top: 3px;
  447. left: 3px;
  448. display: table;
  449. width: 8px;
  450. height: 8px;
  451. content: ' ';
  452. background-color: $primary-color;
  453. border: 0;
  454. border-radius: 8px;
  455. opacity: 0;
  456. transition: all 0.3s cubic-bezier(0.78, 0.14, 0.15, 0.86);
  457. transform: scale(0);
  458. }
  459. }
  460. &.ant-tree-checkbox-checked {
  461. &::after {
  462. position: absolute;
  463. top: 16.67%;
  464. left: 0;
  465. width: 100%;
  466. height: 66.67%;
  467. content: '';
  468. border: 1px solid #1890ff;
  469. border-radius: 50%;
  470. animation: antRadioEffect 0.36s ease-in-out;
  471. animation-fill-mode: both;
  472. }
  473. .ant-tree-checkbox-inner {
  474. background-color: $white;
  475. &::after {
  476. opacity: 1;
  477. transition: all 0.3s cubic-bezier(0.78, 0.14, 0.15, 0.86);
  478. transform: scale(1);
  479. }
  480. }
  481. }
  482. }
  483. }
  484. </style>