SUserSelect2.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. const e = `<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" v-if="multiple">
  41. <a-button type="link" @click="handSelectAll">
  42. {{ !isAll ? $l('全选') : $l('反选') }}
  43. </a-button>
  44. </div>
  45. </div>
  46. <div class="user-list" @scroll="handScroll">
  47. <div
  48. class="item"
  49. v-for="item in userList"
  50. :key="item.id"
  51. @click="handItemChange(item)"
  52. >
  53. <div class="icon s-theme-icon-bg">
  54. <img
  55. v-if="item.avatar || item.cover"
  56. class="avatar-img"
  57. :src="avatarUrl(item.avatar || item.cover)"
  58. alt=""
  59. @error="item.cover = ''"
  60. />
  61. <template v-else>{{ item.name && item.name.slice(0, 1) }}</template>
  62. </div>
  63. <div class="cont">
  64. <div class="name">
  65. {{
  66. item.name +
  67. (item.userid || item.loginName
  68. ? '(' + (item.userid || item.loginName) + ')'
  69. : '')
  70. }}
  71. </div>
  72. <div class="job">{{ item.mainJobName }}</div>
  73. </div>
  74. </div>
  75. <div class="not-more" v-if="pageNo >= total">{{ $l('没有更多了') }}</div>
  76. </div>
  77. </div>
  78. <div class="item right-wrap">
  79. <div class="head">
  80. <div class="sub-title">{{ $l('本次已选') }}</div>
  81. <a-button type="link" @click="clearAll">{{ $l('清除') }}</a-button>
  82. </div>
  83. <div class="user-list select-list" @scroll="handRightScroll">
  84. <div class="item" v-for="(item, idx) in selectList" :key="item.id">
  85. <div class="icon s-theme-icon-bg">
  86. <img
  87. v-if="item.avatar || item.cover"
  88. class="avatar-img"
  89. :src="avatarUrl(item.avatar || item.cover)"
  90. alt=""
  91. @error="item.cover = ''"
  92. />
  93. <template v-else>{{ item.name && item.name.slice(0, 1) }}</template>
  94. </div>
  95. <div class="cont">
  96. <div class="name">{{ item.name || item.title }}</div>
  97. <div class="job">{{ item.mainJobName }}</div>
  98. </div>
  99. <div class="icon no-bg" @click="handItemClear(item, idx)">
  100. <CloseCircleOutlined />
  101. </div>
  102. </div>
  103. </div>
  104. </div>
  105. </div>
  106. </div>
  107. </div>
  108. </s-modal>
  109. </div>
  110. </template>
  111. <script setup>
  112. import { ref, watch, onMounted } from 'vue'
  113. import { levelOneChildren, levelTwoChildren, authorized, getUserByIds } from '../../api/common'
  114. const emits = defineEmits(['change', 'update:value'])
  115. const props = defineProps({
  116. // 选中值(v-model:value)
  117. value: {
  118. type: [String, Array],
  119. default() {
  120. return ''
  121. }
  122. },
  123. // 占位文案
  124. placeholder: {
  125. type: String,
  126. default() {
  127. return window.$l('请选择')
  128. }
  129. },
  130. // 弹窗标题
  131. title: {
  132. type: String,
  133. default() {
  134. return window.$l('人员选择')
  135. }
  136. },
  137. // 是否多选,传 false 为单选(只保留最后选中的一个)
  138. multiple: {
  139. type: Boolean,
  140. default: true
  141. }
  142. })
  143. // 头像地址:avatar/cover 为完整 http(s) 地址直接用;否则视为文件 id,走 web-config 配置的文件服务(file/preview)
  144. const FILE_BASE = (window.$utils?.env?.VITE_APP_API_FILE_URL || '/') + ''
  145. const avatarUrl = (a) => (/^https?:\\/\\//.test(a) ? a : \`\${FILE_BASE}file/preview?id=\${a}\`)
  146. const userValue = ref('')
  147. const treeValue = ref('')
  148. const searchValue = ref('')
  149. const loading = ref(false)
  150. const visible = ref(false)
  151. const treeNode = ref({})
  152. const userList = ref([])
  153. const userIds = ref([])
  154. const pageNo = ref(1)
  155. const total = ref(0)
  156. const isAll = ref(false)
  157. let timer = null
  158. const unitId = ref('')
  159. const selectList = ref([])
  160. const pageNum = ref(1)
  161. const pageCount = ref(20)
  162. const prevNum = ref(0)
  163. // 人员列表请求序号,丢弃切换部门后的过期响应
  164. let userReqSeq = 0
  165. // 最近一次请求签名,避免同一(部门+页码)在途时重复发起请求
  166. let lastReqKey = ''
  167. let isMounted = false
  168. const syncIds = (val) => {
  169. if (val && val.length) {
  170. const arr = Array.isArray(val) ? val : [val]
  171. userIds.value = props.multiple ? arr : arr.slice(0, 1)
  172. } else {
  173. userIds.value = []
  174. }
  175. }
  176. watch(
  177. () => props.value,
  178. (val) => {
  179. syncIds(val)
  180. // 挂载后 value 变化(编辑回显)时刷新输入框已选人员姓名
  181. if (isMounted) initSelected()
  182. },
  183. { immediate: true }
  184. )
  185. // 打开选择弹窗(供外部按钮等调用)
  186. const open = () => {
  187. visible.value = true
  188. initSelected()
  189. }
  190. const onSearch = () => {
  191. open()
  192. }
  193. const handleCancel = () => {
  194. visible.value = false
  195. // 取消时回退到外部传入的已选值,避免回显与 v-model 不一致
  196. syncIds(props.value)
  197. initSelected()
  198. }
  199. const getSelectUsers = async (pageNo = 1, prevNo = 0) => {
  200. if (loading.value) {
  201. loading.value = false
  202. return
  203. }
  204. if (pageNo === 1) {
  205. selectList.value = []
  206. }
  207. let ids = []
  208. if (userIds.value.length < 100) {
  209. pageNum.value = 1
  210. prevNum.value = 0
  211. ids = userIds.value
  212. } else {
  213. ids = userIds.value.slice(prevNo * (pageCount.value || 20), pageNo * (pageCount.value || 20))
  214. }
  215. if (
  216. !ids.length ||
  217. (selectList.value.length &&
  218. ids.length >= userIds.value.length &&
  219. ids.length >= selectList.value.length)
  220. )
  221. return
  222. loading.value = true
  223. const res = await getUserByIds(ids)
  224. // 去重后合并,避免重复渲染
  225. const merged = selectList.value.concat(res || [])
  226. selectList.value = [...new Map(merged.map((i) => [i.id, i])).values()]
  227. timer = setTimeout(() => {
  228. loading.value = false
  229. clearTimeout(timer)
  230. }, 200)
  231. }
  232. const handSearch = () => {
  233. pageNo.value = 1
  234. total.value = 0
  235. userList.value = []
  236. loadUsers()
  237. }
  238. const setUsers = () => {
  239. const list = selectList.value.filter((i) => userIds.value.includes(i.id))
  240. const nameList = list.map((i) => i.name)
  241. const len = nameList.length - 1
  242. userValue.value = nameList.slice(0, 1).join(',') + (len > 0 ? ' +' + len : '')
  243. }
  244. // 回显:根据已选 ids 拉取人员信息,展示到输入框和「本次已选」列表
  245. const initSelected = async () => {
  246. if (!userIds.value.length) {
  247. userValue.value = ''
  248. selectList.value = []
  249. return
  250. }
  251. const res = await getUserByIds(userIds.value)
  252. selectList.value = res || []
  253. setUsers()
  254. }
  255. onMounted(() => {
  256. isMounted = true
  257. initSelected()
  258. })
  259. const clearCurrListSelect = () => {
  260. userList.value.forEach((i) => {
  261. if (userIds.value.includes(i.id)) {
  262. const index = userIds.value.indexOf(i.id)
  263. userIds.value.splice(index, 1)
  264. }
  265. })
  266. getSelectUsers()
  267. }
  268. const handSelectAll = () => {
  269. isAll.value = !isAll.value
  270. if (isAll.value) {
  271. selectAll()
  272. } else {
  273. clearCurrListSelect()
  274. }
  275. }
  276. const selectAll = () => {
  277. userList.value.forEach((i) => {
  278. if (!userIds.value.includes(i.id)) {
  279. userIds.value.push(i.id)
  280. }
  281. })
  282. getSelectUsers()
  283. }
  284. const clearAll = () => {
  285. isAll.value = false
  286. userIds.value = []
  287. selectList.value = []
  288. }
  289. const handleOk = () => {
  290. visible.value = false
  291. const list = selectList.value.filter((i) => userIds.value.includes(i.id))
  292. const nameList = list.map((i) => i.name)
  293. const len = nameList.length - 1
  294. const tags = nameList.slice(0, 1).join(',') + (len > 0 ? ' +' + len : '')
  295. const idList = list.map((i) => i.id)
  296. // v-model:value —— 单选回传字符串,多选回传数组
  297. emits('update:value', props.multiple ? idList : idList[0] || '')
  298. emits('change', tags, nameList, idList, list)
  299. // 点确认后才回填输入框显示
  300. setUsers()
  301. }
  302. // 点树加载人员:与平台生产请求对齐(unitId 驼峰 + unitType 树节点类型 + containsChild + isEnabled)
  303. // 后端 selectUserList SQL:unitType=1 → e.campus_id=#{unitId} OR o.parent_ids LIKE '%unitId%'(containsChild)
  304. // unitType=2 → e.campus_id='-1';unitType=3 → e.unit_id=#{unitId}
  305. const handTreeSelect = (e, n) => {
  306. // e = 选中 key 数组(antd directory-tree @select: selectedKeys);n.node 为 antd TreeNode,原始数据在 dataRef
  307. const keys = Array.isArray(e) ? e : e ? [e] : []
  308. if (!keys.length) return
  309. treeNode.value = n?.node?.dataRef || n?.node || {}
  310. unitId.value = keys
  311. pageNo.value = 1
  312. total.value = 0
  313. userList.value = []
  314. loadUsers()
  315. }
  316. const loadUsers = async () => {
  317. const seq = ++userReqSeq
  318. const node = treeNode.value || {}
  319. const key = \`\${(unitId.value || [])[0]}_\${pageNo.value}_\${searchValue.value}\`
  320. // 同一(部门+页码+关键字)请求在途时不再重复发起
  321. if (key === lastReqKey && loading.value) return
  322. lastReqKey = key
  323. const payload = {
  324. pageNum: pageNo.value,
  325. pageSize: 20,
  326. searchValue: searchValue.value,
  327. unitId: node.id,
  328. unitType: node.type,
  329. containsChild: true,
  330. isEnabled: true
  331. }
  332. loading.value = true
  333. const res = await authorized(payload)
  334. // 已切换到其它部门/条件,丢弃过期响应
  335. if (seq !== userReqSeq) return
  336. // 按 id 去重合并,避免翻页/重复请求导致同一人渲染多次
  337. const merged = userList.value.concat(res.list || [])
  338. userList.value = [...new Map(merged.map((i) => [i.id, i])).values()]
  339. total.value = res.pages
  340. timer = setTimeout(() => {
  341. loading.value = false
  342. clearTimeout(timer)
  343. }, 200)
  344. }
  345. const handItemChange = (item) => {
  346. if (!props.multiple) {
  347. // 单选:只替换暂存列表,点确认后才回填(不即时 setUsers)
  348. userIds.value = [item.id]
  349. selectList.value = [item]
  350. return
  351. }
  352. if (!userIds.value.includes(item.id)) {
  353. userIds.value.push(item.id)
  354. }
  355. getSelectUsers()
  356. }
  357. const handItemClear = (item, idx) => {
  358. selectList.value.splice(idx, 1)
  359. userIds.value.splice(userIds.value.indexOf(item.id), 1)
  360. getSelectUsers()
  361. }
  362. const handScroll = ({ target: { offsetHeight, scrollHeight, scrollTop } }) => {
  363. if (loading.value) return
  364. if (offsetHeight + scrollTop >= scrollHeight - 20) {
  365. // pages 为总页数,已到末页则停止;翻页沿用当前选中的部门
  366. if (pageNo.value < total.value) {
  367. pageNo.value++
  368. loadUsers()
  369. }
  370. }
  371. }
  372. const handRightScroll = ({ target: { offsetHeight, scrollHeight, scrollTop } }) => {
  373. if (loading.value) return
  374. if (offsetHeight + scrollTop >= scrollHeight - 20) {
  375. prevNum.value = pageNum.value
  376. pageNum.value++
  377. getSelectUsers(pageNum.value, prevNum.value)
  378. }
  379. }
  380. const getLevelOneChildren = (id) => {
  381. return levelOneChildren(id)
  382. }
  383. const getLevelTwoChildren = (id) => {
  384. return levelTwoChildren(id)
  385. }
  386. defineExpose({ open })
  387. <\/script>
  388. <style lang="less">
  389. .s-modal {
  390. .ant-modal-content {
  391. }
  392. .modal-container {
  393. display: flex;
  394. justify-content: space-between;
  395. align-items: flex-start;
  396. border: 1px solid var(--s-border-color-split);
  397. .side {
  398. height: 500px;
  399. width: 300px;
  400. border-right: 1px solid var(--s-border-color-split);
  401. overflow: auto;
  402. }
  403. .main {
  404. width: 700px;
  405. }
  406. .filter {
  407. width: 100%;
  408. height: 60px;
  409. border-bottom: 1px solid var(--s-border-color-split);
  410. display: flex;
  411. justify-content: flex-start;
  412. align-items: center;
  413. padding-left: 10px;
  414. box-sizing: border-box;
  415. }
  416. .list-wrap {
  417. height: 440px;
  418. display: flex;
  419. justify-content: space-between;
  420. align-items: center;
  421. }
  422. .left-wrap,
  423. .right-wrap {
  424. max-width: 350px;
  425. min-width: 350px;
  426. width: 350px;
  427. height: 440px;
  428. }
  429. .left-wrap {
  430. border-right: 1px solid var(--s-border-color-split);
  431. }
  432. .right-wrap {
  433. .head {
  434. padding-right: 0;
  435. }
  436. }
  437. .head {
  438. display: flex;
  439. justify-content: space-between;
  440. align-items: center;
  441. height: 40px;
  442. padding: 0 20px;
  443. box-sizing: border-box;
  444. border-bottom: 1px solid var(--s-border-color-split);
  445. // user-select: none;
  446. .sub-title {
  447. width: 80%;
  448. text-overflow: ellipsis;
  449. overflow: hidden;
  450. white-space: nowrap;
  451. }
  452. }
  453. .user-list {
  454. height: 380px;
  455. overflow: hidden auto;
  456. .item {
  457. padding: 5px 20px;
  458. box-sizing: border-box;
  459. display: flex;
  460. justify-content: flex-start;
  461. align-items: center;
  462. margin-bottom: 1px;
  463. cursor: pointer;
  464. // user-select: none;
  465. }
  466. .item:first-child {
  467. margin-bottom: 2px;
  468. }
  469. .icon {
  470. min-width: 40px;
  471. width: 40px;
  472. height: 40px;
  473. border-radius: 40px;
  474. display: flex;
  475. justify-content: center;
  476. align-items: center;
  477. color: var(--s-text-color-regular);
  478. font-size: 18px;
  479. margin-right: 20px;
  480. overflow: hidden;
  481. .avatar-img {
  482. width: 100%;
  483. height: 100%;
  484. border-radius: 50%;
  485. object-fit: cover;
  486. }
  487. }
  488. .cont {
  489. width: 230px;
  490. }
  491. .name {
  492. color: var(--s-text-color);
  493. }
  494. .job {
  495. color: var(--s-text-color-secondary);
  496. }
  497. .name,
  498. .job {
  499. width: 100%;
  500. text-overflow: ellipsis;
  501. overflow: hidden;
  502. white-space: nowrap;
  503. }
  504. }
  505. .select-list {
  506. .cont {
  507. width: 180px;
  508. }
  509. .no-bg {
  510. background-color: var(--s-bg-color-container);
  511. justify-content: flex-end;
  512. opacity: 0;
  513. font-size: 20px;
  514. color: var(--s-color-error);
  515. }
  516. .item:hover {
  517. .no-bg {
  518. opacity: 1;
  519. }
  520. }
  521. }
  522. .not-more {
  523. color: var(--s-text-color-secondary);
  524. font-size: 13px;
  525. text-align: center;
  526. margin: 10px 0;
  527. }
  528. }
  529. .right {
  530. display: flex;
  531. justify-content: flex-end;
  532. align-items: center;
  533. .mt-4 {
  534. transform: translateY(1px);
  535. }
  536. }
  537. }
  538. </style>
  539. `;
  540. export {
  541. e as default
  542. };