iam-audit-modaltree.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. :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 auditModaltreeService from './audit-modaltree-service'
  73. import components from './_import-components/audit-tree-import'
  74. export default {
  75. name: 'IamAuditModaltree',
  76. metaInfo: {
  77. title: 'IamAuditModaltree',
  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: true,
  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. refreshKey: {
  158. type: Number,
  159. default: 0,
  160. },
  161. },
  162. data() {
  163. return {
  164. defaultTreeExpandedKeys: ['0'],
  165. defaultSelectedKeys: ['0'],
  166. depvalue: [], // 选择的组织数据
  167. checkedKeys: [], // 选中的节点数据
  168. treeData: [],
  169. dataList: [], // 数组dataList,搜索要用
  170. spinning: true,
  171. empty: false,
  172. icontype: 'left',
  173. fold: false,
  174. replaceFields: {
  175. title: 'text',
  176. key: 'id',
  177. },
  178. expandedKeys: ['0'],
  179. backupsExpandedKeys: [],
  180. autoExpandParent: false,
  181. searchValue: '',
  182. searchStr: '',
  183. defaultTopNodeText: '根节点',
  184. defaultTopNodeId: '0',
  185. key: 0,
  186. }
  187. },
  188. mounted() {
  189. // 部门下拉框列表值赋值
  190. this.initTreeData()
  191. },
  192. methods: {
  193. onDragEnter(info) {
  194. // expandedKeys 需要受控时设置
  195. this.expandedKeys = info.expandedKeys
  196. },
  197. // 拖拽节点方法
  198. onDrop(info) {
  199. // 被插入节点信息
  200. const dropKey = info.node.eventKey
  201. // 拖拽节点信息
  202. const dragKey = info.dragNode.eventKey
  203. const dropPos = info.node.pos.split('-')
  204. // -1表示之前 0表示合并 1表示之后
  205. const dropPosition = info.dropPosition - Number(dropPos[dropPos.length - 1])
  206. const loop = (data, key, callback) => {
  207. data.forEach((item, index, arr) => {
  208. if (item.id === key) {
  209. return callback(item, index, arr)
  210. }
  211. if (item.children) {
  212. return loop(item.children, key, callback)
  213. }
  214. })
  215. }
  216. const data = this.treeData
  217. // Find dragObject
  218. let dragObj
  219. loop(data, dragKey, (item, index, arr) => {
  220. arr.splice(index, 1)
  221. dragObj = item
  222. })
  223. if (!info.dropToGap) {
  224. // Drop on the content
  225. loop(data, dropKey, (item) => {
  226. item.children = item.children || []
  227. // where to insert 示例添加到尾部,可以是随意位置
  228. item.children.push(dragObj)
  229. })
  230. } else if (
  231. (info.node.children || []).length > 0 && // Has children
  232. info.node.expanded && // Is expanded
  233. dropPosition === 1 // On the bottom gap
  234. ) {
  235. loop(data, dropKey, (item) => {
  236. item.children = item.children || []
  237. // where to insert 示例添加到尾部,可以是随意位置
  238. item.children.unshift(dragObj)
  239. })
  240. } else {
  241. let ar
  242. let i
  243. loop(data, dropKey, (item, index, arr) => {
  244. ar = arr
  245. i = index
  246. })
  247. if (dropPosition === -1) {
  248. ar.splice(i, 0, dragObj)
  249. } else {
  250. ar.splice(i + 1, 0, dragObj)
  251. }
  252. }
  253. this.treeData = data
  254. // 拖拽完成,调用接口
  255. const dragParams = {
  256. configId: this.treeparams.configId,
  257. id: dragKey,
  258. targetId: dropKey,
  259. position: dropPosition,
  260. }
  261. auditModaltreeService.dragNode(dragParams).then((res) => {
  262. console.log(res)
  263. })
  264. },
  265. // 点击地址树展开时调用
  266. onLoadData(treeNode) {
  267. return auditModaltreeService
  268. .getCategoryTree(treeNode.dataRef.id, this.treeparams)
  269. .then((res) => {
  270. treeNode.dataRef.children = res.data
  271. this.treeData = this.transformData([...this.treeData])
  272. this.generateList(this.treeData)
  273. })
  274. },
  275. // 初始化地址树
  276. initTreeData(depId) {
  277. const topDepId = !depId ? 0 : depId
  278. auditModaltreeService.getCategoryTree(topDepId, this.treeparams).then((res) => {
  279. this.spinning = false
  280. if (res.data.length) {
  281. const treeNode = [
  282. {
  283. id: this.defaultTopNodeId,
  284. text: this.defaultTopNodeText,
  285. leaf: false,
  286. props: {},
  287. children: res.data,
  288. key: this.defaultTopNodeId,
  289. },
  290. ]
  291. this.treeData = this.transformData(treeNode)
  292. this.expandedKeys = this.defaultTreeExpandedKeys
  293. this.generateList(this.treeData)
  294. this.empty = false
  295. } else {
  296. this.empty = true
  297. }
  298. })
  299. },
  300. },
  301. }
  302. </script>
  303. <style module lang="scss">
  304. @use '@/common/design' as *;
  305. .spin {
  306. width: 100%;
  307. line-height: 30;
  308. }
  309. .ywlxtree {
  310. :global(.ant-tree-title) {
  311. display: inline-block;
  312. width: 100%;
  313. overflow: hidden;
  314. text-overflow: ellipsis;
  315. white-space: nowrap;
  316. }
  317. :global(.ant-input-search) {
  318. margin: 8px 0;
  319. overflow: hidden;
  320. }
  321. .active {
  322. color: $primary-color;
  323. }
  324. :global(.ant-select) {
  325. width: 100%;
  326. overflow: hidden;
  327. }
  328. }
  329. .treewrap {
  330. position: relative;
  331. display: flex;
  332. flex-direction: column;
  333. width: 20%;
  334. min-height: 100%;
  335. margin-right: $padding-lg;
  336. transition: width 0.2s;
  337. .fold {
  338. position: absolute;
  339. top: calc(50% - 30px);
  340. right: -15px;
  341. z-index: 2;
  342. width: 15px;
  343. height: 75px;
  344. padding: 0;
  345. border-radius: 0 10px 10px 0;
  346. }
  347. :global(.ant-tree) {
  348. overflow: hidden;
  349. text-overflow: ellipsis;
  350. white-space: nowrap;
  351. }
  352. :global(.ant-card-body) {
  353. background: $white;
  354. }
  355. }
  356. .collapse {
  357. width: 0;
  358. :global(.ant-card-body) {
  359. background: transparent;
  360. :global(.ant-empty) {
  361. display: none;
  362. }
  363. }
  364. }
  365. // 单选样式
  366. .single {
  367. :global .ant-tree-checkbox {
  368. .ant-tree-checkbox-inner {
  369. border-radius: 100px;
  370. &::after {
  371. position: absolute;
  372. top: 3px;
  373. left: 3px;
  374. display: table;
  375. width: 8px;
  376. height: 8px;
  377. content: ' ';
  378. background-color: $primary-color;
  379. border: 0;
  380. border-radius: 8px;
  381. opacity: 0;
  382. transition: all 0.3s cubic-bezier(0.78, 0.14, 0.15, 0.86);
  383. transform: scale(0);
  384. }
  385. }
  386. &.ant-tree-checkbox-checked {
  387. &::after {
  388. position: absolute;
  389. top: 16.67%;
  390. left: 0;
  391. width: 100%;
  392. height: 66.67%;
  393. content: '';
  394. border: 1px solid #1890ff;
  395. border-radius: 50%;
  396. animation: antRadioEffect 0.36s ease-in-out;
  397. animation-fill-mode: both;
  398. }
  399. .ant-tree-checkbox-inner {
  400. background-color: $white;
  401. &::after {
  402. opacity: 1;
  403. transition: all 0.3s cubic-bezier(0.78, 0.14, 0.15, 0.86);
  404. transform: scale(1);
  405. }
  406. }
  407. }
  408. }
  409. }
  410. </style>