userAndDept.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. <template>
  2. <view class="uni-app">
  3. <view class="status-bar" />
  4. <view class="main-container">
  5. <wk-nav-bar title="选择员工与部门" />
  6. <view class="container">
  7. <view class="header">
  8. <wk-search-box @search="handleToSearch" />
  9. <view class="nav-tree-wrapper">
  10. <view class="nav-tree">
  11. <view
  12. v-for="(item, index) in navTreeList"
  13. :key="item.id"
  14. class="nav-tree-item">
  15. <text
  16. :class="{special: index < navLen - 1 }"
  17. class="text"
  18. @click="handleNavgiateToDept(index)">
  19. {{ item.name }}
  20. </text>
  21. <text
  22. v-if="index < navLen - 1"
  23. class="wk wk-arrow-right icon" />
  24. </view>
  25. </view>
  26. </view>
  27. </view>
  28. <view class="content">
  29. <!-- <view class="choose-all" @click="toggleCheckedAll">
  30. <view :class="{active: checkedAll }" class="checkbox">
  31. <text v-if="checkedAll" class="wk wk-check icon"></text>
  32. </view>
  33. 全选
  34. </view> -->
  35. <view v-if="!$isEmpty(dataList.deptList)" class="dept-list">
  36. <view class="box" />
  37. <view
  38. v-for="item in dataList.deptList"
  39. :key="item.deptId"
  40. class="dept-list-item"
  41. @click="handleToChild(item)">
  42. <view
  43. :class="{active: item.checked }"
  44. class="checkbox"
  45. @click.stop="handleChoose(item, 'deptList')">
  46. <text v-if="item.checked" class="wk wk-check icon" />
  47. </view>
  48. <image :src="$static('images/icon/structure.png')" class="dept-icon" />
  49. <view class="text">
  50. {{ item.name }}<text v-if="item.allNum > 0">
  51. ({{ item.allNum }}人)
  52. </text>
  53. </view>
  54. <text
  55. :style="{visibility: item.currentNum > 0 ? 'visible' : 'hidden' }"
  56. class="wk wk-arrow-right more-icon" />
  57. </view>
  58. </view>
  59. <template v-if="!$isEmpty(dataList.userList)">
  60. <view class="user-tips">
  61. {{ userTips }}
  62. </view>
  63. <view class="user-list">
  64. <view
  65. v-for="item in dataList.userList"
  66. :key="item.userId"
  67. class="user-list-item"
  68. @click="handleChoose(item, 'userList')">
  69. <view :class="{active: item.checked }" class="checkbox">
  70. <text v-if="item.checked" class="wk wk-check icon" />
  71. </view>
  72. <wk-avatar
  73. :avatar="item.img"
  74. :name="item.realname"
  75. :size="14"
  76. :preview="false"
  77. class="avatar" />
  78. <view class="text">
  79. {{ item.realname }}
  80. </view>
  81. </view>
  82. </view>
  83. </template>
  84. </view>
  85. <view class="footer">
  86. <view class="left" @click="showPopup">
  87. <view class="total-box">
  88. <text class="total">
  89. {{ totalText }}
  90. </text>
  91. <text class="wk wk-arrow-right icon" />
  92. </view>
  93. <view class="desc">
  94. (不包含子部门)
  95. </view>
  96. </view>
  97. <view class="confirm-btn" @click="handleConfirm">
  98. 确定
  99. </view>
  100. </view>
  101. </view>
  102. </view>
  103. <uni-popup
  104. ref="popup"
  105. radius="20rpx 20rpx 0 0"
  106. type="bottom">
  107. <view class="popup">
  108. <view class="popup-header">
  109. <text class="text">
  110. {{ totalText }}
  111. </text>
  112. <text class="confirm-btn" @click="handleConfirm">
  113. 确定
  114. </text>
  115. </view>
  116. <view class="popup-content">
  117. <view class="dept-list">
  118. <view
  119. v-for="(item, index) in selectedList.deptList"
  120. :key="item.deptId"
  121. class="dept-list-item">
  122. <image :src="$static('images/icon/structure.png')" class="dept-icon" />
  123. <view class="text">
  124. {{ item.name }}
  125. </view>
  126. <view class="remove-btn" @click="handleRemove(index, 'deptList')">
  127. 移除
  128. </view>
  129. </view>
  130. </view>
  131. <template v-if="!$isEmpty(selectedList.userList)">
  132. <view class="box" />
  133. <view class="user-list">
  134. <view
  135. v-for="(item, index) in selectedList.userList"
  136. :key="item.userId"
  137. class="user-list-item">
  138. <wk-avatar
  139. :avatar="item.img"
  140. :name="item.realname"
  141. :size="14"
  142. :preview="false"
  143. class="avatar" />
  144. <view class="text">
  145. {{ item.realname }}
  146. </view>
  147. <view class="remove-btn" @click="handleRemove(index, 'userList')">
  148. 移除
  149. </view>
  150. </view>
  151. </view>
  152. </template>
  153. </view>
  154. </view>
  155. </uni-popup>
  156. </view>
  157. </template>
  158. <script>
  159. import { AdminQueryUserListByDept } from 'API/admin.js'
  160. import { mapGetters } from 'vuex'
  161. import { deepCopy } from '@/utils/lib.js'
  162. import { isObject } from '@/utils/types.js'
  163. export default {
  164. name: 'UserAndDept',
  165. data() {
  166. return {
  167. navTreeList: [],
  168. dataList: {
  169. deptList: [],
  170. userList: []
  171. },
  172. copyList: null,
  173. selectedList: {
  174. deptList: [],
  175. userList: []
  176. },
  177. checkedAll: false,
  178. bridge: {},
  179. defaultVal: null
  180. }
  181. },
  182. computed: {
  183. ...mapGetters({
  184. companyInfo: 'user/expirationInfo'
  185. }),
  186. navLen() {
  187. // 处理动态增减导航项时小程序无法渲染样式问题
  188. return this.navTreeList.length
  189. },
  190. userTips() {
  191. if (this.$isEmpty(this.navTreeList)) return ''
  192. return this.navTreeList[this.navTreeList.length - 1].name + '员工'
  193. },
  194. totalText() {
  195. const userLen = this.selectedList.userList.length || 0
  196. const deptLen = this.selectedList.deptList.length || 0
  197. return `已选择:${userLen}人,${deptLen}个部门`
  198. }
  199. },
  200. watch: {
  201. companyInfo: {
  202. handler(val) {
  203. if (!this.$isEmpty(val)) {
  204. this.navTreeList = [{
  205. deptId: 0,
  206. name: val.companyName
  207. }]
  208. this.getUserList()
  209. }
  210. },
  211. deep: true,
  212. immediate: true
  213. }
  214. },
  215. onLoad() {
  216. this.bridge = getApp().globalData.selectedValBridge.userAndDept || {}
  217. const obj = this.bridge.defaultVal || {}
  218. this.selectedList.deptList = this.$isEmpty(obj.deptList) ? [] : obj.deptList
  219. this.selectedList.userList = this.$isEmpty(obj.userList) ? [] : obj.userList
  220. },
  221. onUnload() {
  222. getApp().globalData.selectedValBridge = {}
  223. },
  224. methods: {
  225. getUserList(deptId = 0) {
  226. AdminQueryUserListByDept({
  227. deptId: deptId
  228. }).then(res => {
  229. console.log('rrrrr', res)
  230. this.dataList = res
  231. this.getDefaultChecked()
  232. this.copyList = null
  233. }).catch(() => {
  234. })
  235. },
  236. /**
  237. * 默认勾选
  238. */
  239. getDefaultChecked() {
  240. const deptIds = (this.selectedList.deptList || []).map(o => o.deptId)
  241. const userIds = (this.selectedList.userList || []).map(o => o.userId)
  242. this.dataList.deptList.forEach(item => {
  243. if (deptIds.includes(item.deptId)) {
  244. this.$set(item, 'checked', true)
  245. } else {
  246. this.$set(item, 'checked', false)
  247. }
  248. })
  249. this.dataList.userList.forEach(item => {
  250. if (userIds.includes(item.userId)) {
  251. this.$set(item, 'checked', true)
  252. } else {
  253. this.$set(item, 'checked', false)
  254. }
  255. })
  256. },
  257. /**
  258. * 搜索
  259. * @param {Object} keyworad
  260. */
  261. handleToSearch(keyworad) {
  262. console.log('data: ', keyworad)
  263. if (!this.copyList) {
  264. this.copyList = { ...this.dataList }
  265. }
  266. if (this.$isEmpty(keyworad)) {
  267. this.dataList = { ...this.copyList }
  268. this.copyList = null
  269. return
  270. }
  271. this.dataList = {
  272. userList: this.copyList.userList.filter(o => o.realname.includes(keyworad)),
  273. deptList: this.copyList.deptList.filter(o => o.name.includes(keyworad)),
  274. }
  275. },
  276. /**
  277. * 切换全选状态
  278. */
  279. // toggleCheckedAll() {
  280. // this.checkedAll = !this.checkedAll
  281. // this.dataList.userList.forEach(o => {
  282. // this.$set(o, 'checked', this.checkedAll)
  283. // })
  284. // this.dataList.deptList.forEach(o => {
  285. // this.$set(o, 'checked', this.checkedAll)
  286. // })
  287. // if (this.checkedAll) {
  288. // this.$set(this.selectedList, 'userList', [...this.dataList.userList])
  289. // this.$set(this.selectedList, 'deptList', [...this.dataList.deptList])
  290. // } else {
  291. // this.$set(this.selectedList, 'userList', [])
  292. // this.$set(this.selectedList, 'deptList', [])
  293. // }
  294. // },
  295. /**
  296. * 跳转到子部门
  297. * @param {Object} item
  298. */
  299. handleToChild(item) {
  300. console.log('to child', item)
  301. if (item.currentNum > 0) {
  302. this.getUserList(item.deptId)
  303. this.navTreeList.push({
  304. deptId: item.deptId,
  305. name: item.name
  306. })
  307. } else {
  308. this.handleChoose(item, 'deptList')
  309. }
  310. },
  311. /**
  312. * 导航切换部门
  313. * @param {Object} index
  314. */
  315. handleNavgiateToDept(index) {
  316. if (index >= this.navTreeList.length - 1) return
  317. const item = this.navTreeList[index]
  318. this.getUserList(item.deptId)
  319. this.navTreeList.splice(index + 1)
  320. },
  321. /**
  322. * 切换选中
  323. * @param {Object} item
  324. * @param {Object} type
  325. */
  326. handleChoose(item, type) {
  327. this.$set(item, 'checked', !item.checked)
  328. if (item.checked) {
  329. // 勾选
  330. if (type === 'userList') {
  331. this.selectedList.userList.push(item)
  332. } else if (type === 'deptList') {
  333. this.selectedList.deptList.push(item)
  334. }
  335. } else {
  336. // 取消勾选
  337. let findIndex = -1
  338. if (type === 'userList') {
  339. findIndex = this.selectedList.userList.findIndex(o => o.userId === item.userId)
  340. if (findIndex !== -1) {
  341. this.checkedAll = false
  342. this.selectedList.userList.splice(findIndex, 1)
  343. }
  344. } else if (type === 'deptList') {
  345. findIndex = this.selectedList.deptList.findIndex(o => o.deptId === item.deptId)
  346. if (findIndex !== -1) {
  347. this.checkedAll = false
  348. this.selectedList.deptList.splice(findIndex, 1)
  349. }
  350. }
  351. }
  352. },
  353. /**
  354. * 移除
  355. * @param {Object} index
  356. * @param {Object} type
  357. */
  358. handleRemove(index, type) {
  359. this.checkedAll = false
  360. const delItem = this.selectedList[type].splice(index, 1)[0]
  361. this.$set(delItem, 'checked', false)
  362. // console.log('remove---', this.selectedList)
  363. },
  364. showPopup() {
  365. this.$refs.popup.open()
  366. },
  367. /**
  368. * 确认选择,通知上一级页面,员工部门已经选择
  369. */
  370. handleConfirm() {
  371. this.$refs.popup.close()
  372. const data = {
  373. guid: this.bridge.guid,
  374. bridge: this.bridge,
  375. data: this.selectedList
  376. }
  377. if (this.bridge.config && this.bridge.config.checkFn) {
  378. const res = this.bridge.config.checkFn(data)
  379. if (!res) return
  380. }
  381. uni.$emit('selected-user-and-dept', data)
  382. this.$Router.navigateBack()
  383. }
  384. }
  385. }
  386. </script>
  387. <style scoped lang="scss">
  388. $active-color: #1C69FF;
  389. .checkbox {
  390. width: 40rpx;
  391. height: 40rpx;
  392. text-align: center;
  393. border: 4rpx solid $light;
  394. border-radius: 50%;
  395. display: inline-flex;
  396. align-items: center;
  397. justify-content: center;
  398. .icon {
  399. font-size: $wk-font-sm;
  400. line-height: 1;
  401. color: white;
  402. }
  403. &.active {
  404. border-color: $active-color;
  405. background-color: $active-color;
  406. }
  407. }
  408. .container {
  409. width: 100%;
  410. height: 100%;
  411. display: flex;
  412. flex-direction: column;
  413. overflow: hidden;
  414. .header {
  415. width: 100%;
  416. background-color: white;
  417. ::v-deep .wk-search-box {
  418. padding-left: 4%;
  419. padding-right: 4%;
  420. .search-box__body {
  421. background-color: #EFEFEF;
  422. }
  423. }
  424. .nav-tree-wrapper {
  425. padding: 15rpx 4% 30rpx;
  426. .nav-tree {
  427. width: 100%;
  428. flex-wrap: nowrap;
  429. overflow: auto;
  430. @include left;
  431. .nav-tree-item {
  432. flex-shrink: 0;
  433. font-size: $wk-font-base;
  434. color: $gray;
  435. .special {
  436. color: $active-color;
  437. }
  438. .icon {
  439. font-size: $wk-font-mini;
  440. color: $light;
  441. margin: 0 10rpx;
  442. }
  443. }
  444. }
  445. }
  446. }
  447. .content {
  448. flex: 1;
  449. overflow: auto;
  450. // .choose-all {
  451. // width: 100%;
  452. // font-size: $wk-font-medium;
  453. // background-color: white;
  454. // padding: 25rpx 4%;
  455. // margin: 16rpx 0;
  456. // @include left;
  457. // .checkbox {
  458. // margin-right: 36rpx;
  459. // }
  460. // }
  461. .dept-list {
  462. .box {
  463. width: 100%;
  464. height: 16rpx;
  465. }
  466. .dept-list-item {
  467. position: relative;
  468. width: 100%;
  469. font-size: $wk-font-medium;
  470. padding: 20rpx 4%;
  471. background-color: white;
  472. @include left;
  473. .checkbox {
  474. margin-right: 26rpx;
  475. }
  476. .dept-icon {
  477. width: 74rpx;
  478. height: 74rpx;
  479. }
  480. .text {
  481. flex: 1;
  482. margin-left: 36rpx;
  483. @include ellipsis
  484. }
  485. .more-icon {
  486. color: #919191;
  487. margin-right: 15rpx;
  488. }
  489. &::after {
  490. position: absolute;
  491. bottom: 1rpx;
  492. right: 0;
  493. content: '';
  494. width: calc(100% - 190rpx);
  495. height: 1rpx;
  496. background-color: #EBEEF1;
  497. display: block;
  498. }
  499. &:last-child::after {
  500. display: none;
  501. }
  502. }
  503. }
  504. .user-tips {
  505. width: 100%;
  506. height: 60rpx;
  507. line-height: 60rpx;
  508. color: $gray;
  509. font-size: $wk-font-base;
  510. padding: 0 4%;
  511. }
  512. .user-list {
  513. .user-list-item {
  514. position: relative;
  515. width: 100%;
  516. font-size: $wk-font-medium;
  517. padding: 20rpx 4%;
  518. background-color: white;
  519. @include left;
  520. .checkbox {
  521. margin-right: 26rpx;
  522. }
  523. .text {
  524. flex: 1;
  525. margin-left: 36rpx;
  526. @include ellipsis
  527. }
  528. .avatar {
  529. width: 74rpx;
  530. height: 74rpx;
  531. }
  532. &::after {
  533. position: absolute;
  534. bottom: 1rpx;
  535. right: 0;
  536. content: '';
  537. width: calc(100% - 190rpx);
  538. height: 1rpx;
  539. background-color: #EBEEF1;
  540. display: block;
  541. }
  542. &:last-child::after {
  543. display: none;
  544. }
  545. }
  546. }
  547. }
  548. .footer {
  549. position: relative;
  550. z-index: 1;
  551. width: 100%;
  552. height: 104rpx;
  553. box-shadow: 0 -4rpx 14rpx rgba(220,220,220,0.7);
  554. background-color: white;
  555. padding: 0 4%;
  556. @include left;
  557. .left {
  558. flex: 1;
  559. overflow: hidden;
  560. margin-right: 30rpx;
  561. .total-box {
  562. width: 100%;
  563. font-size: $wk-font-base;
  564. color: $active-color;
  565. .total {
  566. margin-right: 15rpx;
  567. }
  568. .icon {
  569. transform: rotate(-90deg);
  570. display: inline-block;
  571. }
  572. }
  573. .desc {
  574. font-size: $wk-font-mini;
  575. color: $light;
  576. }
  577. }
  578. .confirm-btn {
  579. width: 170rpx;
  580. height: 74rpx;
  581. font-size: $wk-font-base;
  582. color: white;
  583. border-radius: 12rpx;
  584. background-color: $active-color;
  585. flex-shrink: 0;
  586. @include center;
  587. }
  588. }
  589. }
  590. .popup {
  591. padding-bottom: 20rpx;
  592. .popup-header {
  593. position: relative;
  594. padding: 15rpx 38rpx;
  595. border-bottom: 1rpx solid $border-color;
  596. @include left;
  597. .text {
  598. flex: 1;
  599. color: $dark;
  600. font-size: $wk-font-medium;
  601. }
  602. .confirm-btn {
  603. color: $active-color;
  604. font-size: $wk-font-base;
  605. padding: 20rpx 0 20rpx 20rpx;
  606. }
  607. }
  608. .popup-content {
  609. min-height: 50vh;
  610. max-height: 80vh;
  611. overflow: auto;
  612. .remove-btn {
  613. width: 104rpx;
  614. height: 56rpx;
  615. font-size: $wk-font-base;
  616. border: 1rpx solid #D2D2D2;
  617. border-radius: 8rpx;
  618. @include center;
  619. }
  620. .dept-list {
  621. .dept-list-item {
  622. width: 100%;
  623. font-size: $wk-font-medium;
  624. padding: 20rpx 38rpx;
  625. background-color: white;
  626. border-bottom: 1rpx solid $border-color;
  627. @include left;
  628. .dept-icon {
  629. width: 74rpx;
  630. height: 74rpx;
  631. }
  632. .text {
  633. flex: 1;
  634. margin-left: 36rpx;
  635. @include ellipsis
  636. }
  637. }
  638. }
  639. .box {
  640. width: 100%;
  641. height: 15rpx;
  642. background-color: #F5F5F5;
  643. }
  644. .user-list {
  645. .user-list-item {
  646. position: relative;
  647. width: 100%;
  648. font-size: $wk-font-medium;
  649. padding: 20rpx 4%;
  650. background-color: white;
  651. @include left;
  652. .text {
  653. flex: 1;
  654. margin-left: 36rpx;
  655. @include ellipsis
  656. }
  657. .avatar {
  658. width: 74rpx;
  659. height: 74rpx;
  660. }
  661. &::after {
  662. position: absolute;
  663. bottom: 1rpx;
  664. right: 0;
  665. content: '';
  666. width: calc(100% - 130rpx);
  667. height: 1rpx;
  668. background-color: #EBEEF1;
  669. display: block;
  670. }
  671. &:last-child::after {
  672. display: none;
  673. }
  674. }
  675. }
  676. }
  677. }
  678. </style>