SOfficeSelect2.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. const n = `<template>
  2. <div class="s-office-select-common">
  3. <a-input-search
  4. v-model:value="userValue"
  5. :readonly="true"
  6. :placeholder="placeholder"
  7. @click="onSearch"
  8. @search="onSearch"
  9. />
  10. <s-modal v-model:value="visible" :title="title" width="1000px" :showFullScreen="false">
  11. <template #footer>
  12. <a-button key="back" @click="handleCancel">{{ $l('取消') }}</a-button>
  13. <a-button key="submit" type="primary" :loading="loading" @click="handleOk">
  14. {{ $l('确认') }}
  15. </a-button>
  16. </template>
  17. <div class="modal-container">
  18. <div class="side">
  19. <s-tree
  20. :levelOneChildren="getLevelOneChildren"
  21. :levelTwoChildren="getLevelTwoChildren"
  22. @select:modelValue="handTreeSelect"
  23. v-model:select="treeValue"
  24. :height="470"
  25. />
  26. </div>
  27. <div class="main">
  28. <div class="filter">
  29. <a-input-search
  30. v-model:value="searchValue"
  31. :placeholder="$l('请输入')"
  32. style="width: 330px"
  33. @search="handSearch"
  34. />
  35. </div>
  36. <div class="list-wrap">
  37. <div class="item left-wrap">
  38. <div class="head">
  39. <div class="sub-title">{{ treeNode.title || treeNode.name }}</div>
  40. <div class="right">
  41. <a-button type="link" @click="handSelectAll">
  42. {{ !isAll ? $l('全选') : $l('反选') }}
  43. </a-button>
  44. <a-dropdown>
  45. <a type="link" @click.prevent>
  46. <DownOutlined />
  47. </a>
  48. <template #overlay>
  49. <a-menu>
  50. <a-menu-item @click="handChangeNodesAll">
  51. <a href="javascript:;">{{ $l('节点全选') }}</a>
  52. </a-menu-item>
  53. </a-menu>
  54. </template>
  55. </a-dropdown>
  56. </div>
  57. </div>
  58. <div class="user-list" @scroll="handScroll">
  59. <div
  60. class="item"
  61. v-for="item in userList"
  62. :key="item.id"
  63. @click="handItemChange(item)"
  64. >
  65. <div class="icon s-theme-icon-bg">
  66. {{ item.name && item.name.slice(0, 1) }}
  67. </div>
  68. <div class="cont">
  69. <div class="name">{{ item.name }} ({{ item.nameOrCount }})</div>
  70. <div class="job">{{ item.deptName }}</div>
  71. </div>
  72. </div>
  73. <div class="not-more" v-if="pageNo >= total">{{ $l('没有更多了') }}</div>
  74. </div>
  75. </div>
  76. <div class="item right-wrap">
  77. <div class="head">
  78. <div class="sub-title">{{ $l('本次已选') }}</div>
  79. <a-button type="link" @click="clearAll">{{ $l('清除') }}</a-button>
  80. </div>
  81. <div class="user-list select-list" @scroll="handRightScroll">
  82. <div class="item" v-for="(item, idx) in selectList" :key="item.id">
  83. <div class="icon s-theme-icon-bg">
  84. {{ item.name && item.name.slice(0, 1) }}
  85. </div>
  86. <div class="cont">
  87. <div class="name">{{ item.name }} ({{ item.nameOrCount }})</div>
  88. <div class="job">{{ item.deptName }}</div>
  89. </div>
  90. <div class="icon no-bg" @click="handItemClear(item, idx)">
  91. <CloseCircleOutlined />
  92. </div>
  93. </div>
  94. </div>
  95. </div>
  96. </div>
  97. </div>
  98. </div>
  99. </s-modal>
  100. </div>
  101. </template>
  102. <script setup>
  103. import { ref, watch } from 'vue'
  104. import {
  105. levelOneChildren,
  106. levelTwoChildren,
  107. getJobs,
  108. jobIds,
  109. positionGetByIds
  110. } from '../../api/common'
  111. const emits = defineEmits(['change', 'update:value'])
  112. const props = defineProps({
  113. // 选中值(v-model:value)
  114. value: {
  115. type: [String, Array],
  116. default() {
  117. return ''
  118. }
  119. },
  120. // 是否多选,传 false 为单选
  121. multiple: {
  122. type: Boolean,
  123. default: true
  124. },
  125. // 占位文案
  126. placeholder: {
  127. type: String,
  128. default() {
  129. return window.$l('请选择')
  130. }
  131. },
  132. // 弹窗标题
  133. title: {
  134. type: String,
  135. default() {
  136. return window.$l('岗位选择')
  137. }
  138. }
  139. })
  140. const userValue = ref('')
  141. const treeValue = ref('')
  142. const searchValue = ref('')
  143. const loading = ref(false)
  144. const visible = ref(false)
  145. const treeNode = ref({})
  146. const userList = ref([])
  147. const userIds = ref([])
  148. const pageNo = ref(1)
  149. const total = ref(0)
  150. const isAll = ref(false)
  151. let timer = null
  152. const unitId = ref('')
  153. const selectList = ref([])
  154. const pageNum = ref(1)
  155. const pageCount = ref(20)
  156. const prevNum = ref(0)
  157. watch(
  158. () => props.value,
  159. (val) => {
  160. if (val && val.length) {
  161. const arr = Array.isArray(val) ? val : [val]
  162. userIds.value = props.multiple ? arr : arr.slice(0, 1)
  163. } else {
  164. userIds.value = []
  165. }
  166. },
  167. { immediate: true }
  168. )
  169. const onSearch = () => {
  170. visible.value = true
  171. }
  172. const handleCancel = () => {
  173. visible.value = false
  174. }
  175. const getSelectUsers = async (pageNo = 1, prevNo = 0) => {
  176. if (loading.value) {
  177. loading.value = false
  178. return
  179. }
  180. if (pageNo === 1) {
  181. selectList.value = []
  182. }
  183. let ids = []
  184. if (userIds.value.length < 100) {
  185. pageNum.value = 1
  186. prevNum.value = 0
  187. ids = userIds.value
  188. } else {
  189. ids = userIds.value.slice(prevNo * (pageCount.value || 20), pageNo * (pageCount.value || 20))
  190. }
  191. if (
  192. !ids.length ||
  193. (selectList.value.length &&
  194. ids.length >= userIds.value.length &&
  195. ids.length >= selectList.value.length)
  196. )
  197. return
  198. loading.value = true
  199. const res = await positionGetByIds(ids)
  200. selectList.value = selectList.value.concat(res)
  201. setUsers()
  202. timer = setTimeout(() => {
  203. loading.value = false
  204. clearTimeout(timer)
  205. }, 200)
  206. }
  207. const handSearch = () => {
  208. if (!unitId.value) {
  209. window.$message.warning(window.$l('请选择部门'))
  210. return
  211. }
  212. pageNo.value = 1
  213. userList.value = []
  214. getUserData(unitId.value)
  215. }
  216. const setUsers = () => {
  217. const list = selectList.value.filter((i) => userIds.value.includes(i.id))
  218. const nameList = list.map((i) => i.name)
  219. const len = nameList.length - 1
  220. userValue.value = nameList.slice(0, 1).join(',') + (len > 0 ? ' +' + len : '')
  221. }
  222. const clearCurrListSelect = () => {
  223. userList.value.forEach((i) => {
  224. if (userIds.value.includes(i.id)) {
  225. const index = userIds.value.indexOf(i.id)
  226. userIds.value.splice(index, 1)
  227. }
  228. })
  229. getSelectUsers()()
  230. }
  231. const handSelectAll = () => {
  232. isAll.value = !isAll.value
  233. if (isAll.value) {
  234. selectAll()
  235. } else {
  236. clearCurrListSelect()
  237. }
  238. }
  239. const handChangeNodesAll = async () => {
  240. if (!unitId.value) {
  241. window.$message.warning(window.$l('请选择部门'))
  242. return
  243. }
  244. const payload = {
  245. searchValue: searchValue.value,
  246. unitid: unitId.value.join(',')
  247. }
  248. const res = await jobIds(payload)
  249. const ids = userIds.value.concat(res)
  250. userIds.value = Array.from(new Set(ids))
  251. getSelectUsers()
  252. }
  253. const selectAll = () => {
  254. userList.value.forEach((i) => {
  255. if (!userIds.value.includes(i.id)) {
  256. userIds.value.push(i.id)
  257. }
  258. })
  259. getSelectUsers()
  260. }
  261. const clearAll = () => {
  262. isAll.value = false
  263. userValue.value = ''
  264. userIds.value = []
  265. selectList.value = []
  266. }
  267. const handleOk = () => {
  268. visible.value = false
  269. const list = selectList.value.filter((i) => userIds.value.includes(i.id))
  270. const nameList = list.map((i) => i.name)
  271. const len = nameList.length - 1
  272. const tags = nameList.slice(0, 1).join(',') + (len > 0 ? ' +' + len : '')
  273. const idList = list.map((i) => i.id)
  274. // v-model:value —— 单选回传字符串,多选回传数组
  275. emits('update:value', props.multiple ? idList : idList[0] || '')
  276. emits('change', tags, nameList, idList, list)
  277. }
  278. const handTreeSelect = (e, n) => {
  279. treeNode.value = n.node
  280. pageNo.value = 1
  281. total.value = 0
  282. userList.value = []
  283. loading.value = true
  284. unitId.value = e
  285. getUserData(e)
  286. }
  287. const getUserData = async (id) => {
  288. const payload = {
  289. pageNo: pageNo.value,
  290. pageSize: 20,
  291. searchValue: searchValue.value,
  292. unitid: id.join(','),
  293. containsChild: true
  294. }
  295. loading.value = true
  296. const res = await getJobs(payload)
  297. userList.value = userList.value.concat(res.list)
  298. total.value = res.pages
  299. timer = setTimeout(() => {
  300. loading.value = false
  301. clearTimeout(timer)
  302. }, 200)
  303. }
  304. const handItemChange = (item) => {
  305. if (!userIds.value.includes(item.id)) {
  306. userIds.value.push(item.id)
  307. }
  308. getSelectUsers()
  309. }
  310. const handItemClear = (item, idx) => {
  311. selectList.value.splice(idx, 1)
  312. userIds.value.splice(userIds.value.indexOf(item.id), 1)
  313. getSelectUsers()
  314. }
  315. const handScroll = ({ target: { offsetHeight, scrollHeight, scrollTop } }) => {
  316. if (loading.value) return
  317. if (offsetHeight + scrollTop >= scrollHeight - 20) {
  318. if (pageNo.value <= total.value) {
  319. pageNo.value++
  320. getUserData([treeNode.value.id])
  321. }
  322. }
  323. }
  324. const handRightScroll = ({ target: { offsetHeight, scrollHeight, scrollTop } }) => {
  325. if (loading.value) return
  326. if (offsetHeight + scrollTop >= scrollHeight - 20) {
  327. prevNum.value = pageNum.value
  328. pageNum.value++
  329. getSelectUsers(pageNum.value, prevNum.value)
  330. }
  331. }
  332. const getLevelOneChildren = (id) => {
  333. return levelOneChildren(id)
  334. }
  335. const getLevelTwoChildren = (id) => {
  336. return levelTwoChildren(id)
  337. }
  338. <\/script>
  339. <style lang="less">
  340. .s-modal {
  341. .ant-modal-content {
  342. }
  343. .modal-container {
  344. display: flex;
  345. justify-content: space-between;
  346. align-items: flex-start;
  347. border: 1px solid var(--s-border-color-split);
  348. .side {
  349. height: 500px;
  350. width: 300px;
  351. border-right: 1px solid var(--s-border-color-split);
  352. overflow: auto;
  353. }
  354. .main {
  355. width: 700px;
  356. }
  357. .filter {
  358. width: 100%;
  359. height: 60px;
  360. border-bottom: 1px solid var(--s-border-color-split);
  361. display: flex;
  362. justify-content: flex-start;
  363. align-items: center;
  364. padding-left: 10px;
  365. box-sizing: border-box;
  366. }
  367. .list-wrap {
  368. height: 440px;
  369. display: flex;
  370. justify-content: space-between;
  371. align-items: center;
  372. }
  373. .left-wrap,
  374. .right-wrap {
  375. max-width: 350px;
  376. min-width: 350px;
  377. width: 350px;
  378. height: 440px;
  379. }
  380. .left-wrap {
  381. border-right: 1px solid var(--s-border-color-split);
  382. }
  383. .right-wrap {
  384. .head {
  385. padding-right: 0;
  386. }
  387. }
  388. .head {
  389. display: flex;
  390. justify-content: space-between;
  391. align-items: center;
  392. height: 40px;
  393. padding: 0 20px;
  394. box-sizing: border-box;
  395. border-bottom: 1px solid var(--s-border-color-split);
  396. // user-select: none;
  397. .sub-title {
  398. width: 80%;
  399. text-overflow: ellipsis;
  400. overflow: hidden;
  401. white-space: nowrap;
  402. }
  403. }
  404. .user-list {
  405. height: 380px;
  406. overflow: hidden auto;
  407. .item {
  408. padding: 5px 20px;
  409. box-sizing: border-box;
  410. display: flex;
  411. justify-content: flex-start;
  412. align-items: center;
  413. margin-bottom: 1px;
  414. cursor: pointer;
  415. // user-select: none;
  416. }
  417. .item:first-child {
  418. margin-bottom: 2px;
  419. }
  420. .icon {
  421. min-width: 40px;
  422. width: 40px;
  423. height: 40px;
  424. border-radius: 40px;
  425. display: flex;
  426. justify-content: center;
  427. align-items: center;
  428. color: var(--s-text-color-regular);
  429. font-size: 18px;
  430. margin-right: 20px;
  431. }
  432. .cont {
  433. width: 230px;
  434. }
  435. .name {
  436. color: var(--s-text-color);
  437. }
  438. .job {
  439. color: var(--s-text-color-secondary);
  440. }
  441. .name,
  442. .job {
  443. width: 100%;
  444. text-overflow: ellipsis;
  445. overflow: hidden;
  446. white-space: nowrap;
  447. }
  448. }
  449. .select-list {
  450. .cont {
  451. width: 180px;
  452. }
  453. .no-bg {
  454. background-color: var(--s-bg-color-container);
  455. justify-content: flex-end;
  456. opacity: 0;
  457. font-size: 20px;
  458. color: var(--s-color-error);
  459. }
  460. .item:hover {
  461. .no-bg {
  462. opacity: 1;
  463. }
  464. }
  465. }
  466. .not-more {
  467. color: var(--s-text-color-secondary);
  468. font-size: 13px;
  469. text-align: center;
  470. margin: 10px 0;
  471. }
  472. }
  473. .right {
  474. display: flex;
  475. justify-content: flex-end;
  476. align-items: center;
  477. .mt-4 {
  478. transform: translateY(1px);
  479. }
  480. }
  481. }
  482. </style>
  483. `;
  484. export {
  485. n as default
  486. };