iam-audit-listtree.vue 9.9 KB

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