dept.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. <template>
  2. <view class="uni-app">
  3. <view class="status-bar" />
  4. <view class="main-container">
  5. <wk-nav-bar :title="navTitle">
  6. <!-- #ifndef MP-WEIXIN -->
  7. <button v-if="isRadio" class="button white-btn" @click="handleConfirm">
  8. 确定
  9. </button>
  10. <!-- #endif -->
  11. </wk-nav-bar>
  12. <view class="container">
  13. <view class="header">
  14. <wk-search-box @search="handleToSearch" />
  15. <view class="nav-tree-wrapper">
  16. <view class="nav-tree">
  17. <view v-for="(item, index) in navTreeList" :key="item.id" class="nav-tree-item">
  18. <text
  19. :class="{special: index < navLen - 1 }" class="text"
  20. @click="handleNavgiateToDept(index)">
  21. {{ item.name }}
  22. </text>
  23. <text v-if="index < navLen - 1" 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(listData)" class="dept-list">
  36. <view class="box" />
  37. <view
  38. v-for="item in listData"
  39. :key="item[typeObj.valueField]"
  40. class="dept-list-item"
  41. @click="handleToChild(item)">
  42. <view
  43. :class="{active: item.checked }"
  44. class="checkbox"
  45. @click.stop="handleChoose(item)">
  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[typeObj.labelField] }}<text v-if="item.currentNum && item.currentNum > 0">
  51. ({{ item.currentNum }}人)
  52. </text>
  53. </view>
  54. <text
  55. :style="{visibility: !$isEmpty(item.children) ? 'visible' : 'hidden' }"
  56. class="wk wk-arrow-right more-icon" />
  57. </view>
  58. </view>
  59. </view>
  60. <!-- #ifdef MP-WEIXIN -->
  61. <view v-if="isRadio" class="footer-btn-group">
  62. <button class="button" @click="handleConfirm">
  63. 确定
  64. </button>
  65. </view>
  66. <!-- #endif -->
  67. <view
  68. v-if="!isRadio"
  69. class="footer">
  70. <view class="left" @click="showPopup">
  71. <view class="total-box">
  72. <text class="total">
  73. 已选择:{{ selectedList.length }}个部门
  74. </text>
  75. <text class="wk wk-arrow-right icon" />
  76. </view>
  77. <view class="desc">
  78. (不包含子部门)
  79. </view>
  80. </view>
  81. <view class="confirm-btn" @click="handleConfirm">
  82. 确定
  83. </view>
  84. </view>
  85. </view>
  86. </view>
  87. <uni-popup ref="popup" radius="20rpx 20rpx 0 0" type="bottom">
  88. <view class="popup">
  89. <view class="popup-header">
  90. <text class="text">
  91. 已选择:{{ selectedList.length }}个部门
  92. </text>
  93. <text class="confirm-btn" @click="handleConfirm">
  94. 确定
  95. </text>
  96. </view>
  97. <view class="popup-content">
  98. <view class="dept-list">
  99. <view
  100. v-for="(item, index) in selectedList"
  101. :key="item.deptId"
  102. class="dept-list-item">
  103. <image :src="$static('images/icon/structure.png')" class="dept-icon" />
  104. <view class="text">
  105. {{ item.name }}
  106. </view>
  107. <view class="remove-btn" @click="handleRemove(index)">
  108. 移除
  109. </view>
  110. </view>
  111. </view>
  112. </view>
  113. </view>
  114. </uni-popup>
  115. </view>
  116. </template>
  117. <script>
  118. import {
  119. AdminQueryDeptTree
  120. } from 'API/admin'
  121. import { mapGetters } from 'vuex'
  122. import { deepCopy } from '@/utils/lib.js'
  123. import { isObject } from '@/utils/types.js'
  124. export default {
  125. name: 'DeptChoose',
  126. data() {
  127. return {
  128. navTreeList: [],
  129. typeObj: {
  130. labelField: 'name',
  131. valueField: 'id'
  132. },
  133. selectedList: [],
  134. listData: [],
  135. deptData: [],
  136. copyList: null,
  137. checkedAll: false,
  138. defaultVal: null,
  139. routerQuery: {},
  140. bridge: {}
  141. }
  142. },
  143. computed: {
  144. ...mapGetters({
  145. companyInfo: 'user/expirationInfo'
  146. }),
  147. navTitle() {
  148. return this.bridge.title || '选择部门'
  149. },
  150. isRadio() {
  151. return this.bridge.maxlength === 1
  152. },
  153. navLen() {
  154. // 处理动态增减导航项时小程序无法渲染样式问题
  155. return this.navTreeList.length
  156. }
  157. },
  158. watch: {
  159. companyInfo: {
  160. handler(val) {
  161. if (!this.$isEmpty(val)) {
  162. this.navTreeList = [{
  163. deptId: 0,
  164. name: val.companyName
  165. }]
  166. this.getList()
  167. }
  168. },
  169. deep: true,
  170. immediate: true
  171. }
  172. },
  173. onLoad(options) {
  174. this.routerQuery = options || {}
  175. this.bridge = getApp().globalData.selectedValBridge.dept || {}
  176. this.selectedList = this.bridge.defaultVal || []
  177. },
  178. onUnload() {
  179. getApp().globalData.selectedValBridge = {}
  180. },
  181. methods: {
  182. /**
  183. * 获取选项列表
  184. */
  185. getList(deptId = null) {
  186. if (!this.$isEmpty(this.bridge.list)) {
  187. this.listData = this.bridge.list
  188. return
  189. }
  190. const otherParams = this.bridge.params || {} // 附加参数
  191. const params = {
  192. search: '',
  193. type: 'tree',
  194. ...otherParams
  195. }
  196. if (deptId) {
  197. params.id = deptId
  198. }
  199. const request = this.bridge.request || AdminQueryDeptTree
  200. if (!request) return
  201. request(params).then(res => {
  202. this.listData = res
  203. this.getDefaultChecked()
  204. this.copyList = null
  205. }).catch()
  206. },
  207. getDefaultChecked() {
  208. const key = this.typeObj.valueField
  209. const deptIds = (this.selectedList || []).map(o => o[key])
  210. this.listData.forEach(item => {
  211. if (deptIds.includes(item[key])) {
  212. this.$set(item, 'checked', true)
  213. } else {
  214. this.$set(item, 'checked', false)
  215. }
  216. })
  217. console.log('deptIds: ', deptIds, this.listData)
  218. },
  219. handleToSearch(keyworad) {
  220. if (!this.copyList) {
  221. this.copyList = [...this.listData]
  222. }
  223. if (this.$isEmpty(keyworad)) {
  224. this.listData = [...this.copyList]
  225. this.copyList = null
  226. return
  227. }
  228. this.listData = this.copyList.filter(o => o[this.typeObj.labelField].includes(keyworad))
  229. },
  230. /**
  231. * 跳转到子部门
  232. * @param {Object} item
  233. */
  234. handleToChild(item) {
  235. console.log('to child', item)
  236. if (!this.$isEmpty(item.children)) {
  237. this.listData = item.children
  238. this.copyList = null
  239. this.navTreeList.push({
  240. deptId: item[this.typeObj.valueField],
  241. name: item[this.typeObj.labelField]
  242. })
  243. this.getDefaultChecked()
  244. } else {
  245. this.handleChoose(item)
  246. }
  247. },
  248. /**
  249. * 导航切换部门
  250. * @param {Object} index
  251. */
  252. handleNavgiateToDept(index) {
  253. if (index >= this.navTreeList.length - 1) return
  254. const item = this.navTreeList[index]
  255. this.getList(item.deptId)
  256. this.navTreeList.splice(index + 1)
  257. },
  258. /**
  259. * 切换选中
  260. * @param {Object} item
  261. * @param {Object} type
  262. */
  263. handleChoose(item) {
  264. this.$set(item, 'checked', !item.checked)
  265. const key = this.typeObj.valueField
  266. if (item.checked) {
  267. // 勾选
  268. const maxlength = this.bridge.maxLength
  269. if (this.selectedList.length >= maxlength) {
  270. this.selectedList.splice(maxlength - 1)
  271. }
  272. this.selectedList.push(item)
  273. this.getDefaultChecked()
  274. } else {
  275. // 取消勾选
  276. const findIndex = this.selectedList.findIndex(o => o[key] === item[key])
  277. if (findIndex !== -1) {
  278. this.checkedAll = false
  279. this.selectedList.splice(findIndex, 1)
  280. }
  281. }
  282. },
  283. showPopup() {
  284. this.$refs.popup.open()
  285. },
  286. handleRemove(index) {
  287. this.selectedList.splice(index, 1)
  288. this.getDefaultChecked()
  289. },
  290. handleConfirm() {
  291. this.$refs.popup.close()
  292. const data = {
  293. guid: this.bridge.guid,
  294. bridge: this.bridge,
  295. data: this.selectedList
  296. }
  297. console.log('data: ', data)
  298. if (this.bridge.config && this.bridge.config.checkFn) {
  299. const res = this.bridge.config.checkFn(data)
  300. if (!res) return
  301. }
  302. uni.$emit('selected-dept', data)
  303. this.$Router.navigateBack()
  304. }
  305. }
  306. }
  307. </script>
  308. <style scoped lang="scss">
  309. $active-color: #1C69FF;
  310. .checkbox {
  311. width: 40rpx;
  312. height: 40rpx;
  313. text-align: center;
  314. border: 4rpx solid $light;
  315. border-radius: 50%;
  316. display: inline-flex;
  317. align-items: center;
  318. justify-content: center;
  319. .icon {
  320. font-size: $wk-font-sm;
  321. line-height: 1;
  322. color: white;
  323. }
  324. &.active {
  325. border-color: $active-color;
  326. background-color: $active-color;
  327. }
  328. }
  329. .container {
  330. width: 100%;
  331. height: 100%;
  332. display: flex;
  333. flex-direction: column;
  334. overflow: hidden;
  335. .header {
  336. width: 100%;
  337. background-color: white;
  338. ::v-deep .wk-search-box {
  339. padding-left: 4%;
  340. padding-right: 4%;
  341. .search-box__body {
  342. background-color: #EFEFEF;
  343. }
  344. }
  345. .nav-tree-wrapper {
  346. padding: 15rpx 4% 30rpx;
  347. .nav-tree {
  348. width: 100%;
  349. flex-wrap: nowrap;
  350. overflow: auto;
  351. @include left;
  352. .nav-tree-item {
  353. flex-shrink: 0;
  354. font-size: $wk-font-base;
  355. color: $gray;
  356. .special {
  357. color: $active-color;
  358. }
  359. .icon {
  360. font-size: $wk-font-mini;
  361. color: $light;
  362. margin: 0 10rpx;
  363. }
  364. }
  365. }
  366. }
  367. }
  368. .content {
  369. flex: 1;
  370. overflow: auto;
  371. // .choose-all {
  372. // width: 100%;
  373. // font-size: $wk-font-medium;
  374. // background-color: white;
  375. // padding: 25rpx 4%;
  376. // margin: 16rpx 0;
  377. // @include left;
  378. // .checkbox {
  379. // margin-right: 36rpx;
  380. // }
  381. // }
  382. .dept-list {
  383. .box {
  384. width: 100%;
  385. height: 16rpx;
  386. }
  387. .dept-list-item {
  388. position: relative;
  389. width: 100%;
  390. font-size: $wk-font-medium;
  391. padding: 20rpx 4%;
  392. background-color: white;
  393. @include left;
  394. .checkbox {
  395. margin-right: 26rpx;
  396. }
  397. .dept-icon {
  398. width: 74rpx;
  399. height: 74rpx;
  400. }
  401. .text {
  402. flex: 1;
  403. margin-left: 36rpx;
  404. @include ellipsis
  405. }
  406. .more-icon {
  407. color: #919191;
  408. margin-right: 15rpx;
  409. }
  410. &::after {
  411. position: absolute;
  412. bottom: 1rpx;
  413. right: 0;
  414. content: '';
  415. width: calc(100% - 190rpx);
  416. height: 1rpx;
  417. background-color: #EBEEF1;
  418. display: block;
  419. }
  420. &:last-child::after {
  421. display: none;
  422. }
  423. }
  424. }
  425. }
  426. .footer {
  427. position: relative;
  428. z-index: 1;
  429. width: 100%;
  430. height: 104rpx;
  431. box-shadow: 0 -4rpx 14rpx rgba(220, 220, 220, 0.7);
  432. background-color: white;
  433. padding: 0 4%;
  434. @include left;
  435. .left {
  436. flex: 1;
  437. overflow: hidden;
  438. margin-right: 30rpx;
  439. .total-box {
  440. width: 100%;
  441. font-size: $wk-font-base;
  442. color: $active-color;
  443. .total {
  444. margin-right: 15rpx;
  445. }
  446. .icon {
  447. transform: rotate(-90deg);
  448. display: inline-block;
  449. }
  450. }
  451. .desc {
  452. font-size: $wk-font-mini;
  453. color: $light;
  454. }
  455. }
  456. .confirm-btn {
  457. width: 170rpx;
  458. height: 74rpx;
  459. font-size: $wk-font-base;
  460. color: white;
  461. border-radius: 12rpx;
  462. background-color: $active-color;
  463. flex-shrink: 0;
  464. @include center;
  465. }
  466. }
  467. }
  468. .popup {
  469. padding-bottom: 20rpx;
  470. .popup-header {
  471. position: relative;
  472. padding: 15rpx 38rpx;
  473. border-bottom: 1rpx solid $border-color;
  474. @include left;
  475. .text {
  476. flex: 1;
  477. color: $dark;
  478. font-size: $wk-font-medium;
  479. }
  480. .confirm-btn {
  481. color: $active-color;
  482. font-size: $wk-font-base;
  483. padding: 20rpx 0 20rpx 20rpx;
  484. }
  485. }
  486. .popup-content {
  487. min-height: 50vh;
  488. max-height: 80vh;
  489. overflow: auto;
  490. .remove-btn {
  491. width: 104rpx;
  492. height: 56rpx;
  493. font-size: $wk-font-base;
  494. border: 1rpx solid #D2D2D2;
  495. border-radius: 8rpx;
  496. @include center;
  497. }
  498. .dept-list {
  499. .dept-list-item {
  500. width: 100%;
  501. font-size: $wk-font-medium;
  502. padding: 20rpx 38rpx;
  503. background-color: white;
  504. border-bottom: 1rpx solid $border-color;
  505. @include left;
  506. .dept-icon {
  507. width: 74rpx;
  508. height: 74rpx;
  509. }
  510. .text {
  511. flex: 1;
  512. margin-left: 36rpx;
  513. @include ellipsis
  514. }
  515. }
  516. }
  517. }
  518. }
  519. </style>