selectUserDialog.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. <template>
  2. <u-popup
  3. :show="visible"
  4. mode="bottom"
  5. :round="10"
  6. :closeOnClickOverlay="false"
  7. @close="close"
  8. class="select-user-popup"
  9. >
  10. <view class="popup-content">
  11. <!-- 头部 -->
  12. <view class="popup-header">
  13. <text class="popup-title">选择人员</text>
  14. <view class="close-btn" @click="close">×</view>
  15. </view>
  16. <!-- 搜索行 -->
  17. <view class="search-row">
  18. <input
  19. v-model="searchVal"
  20. placeholder="请输入名称/工号/账号"
  21. class="search-input"
  22. @confirm="doSearch"
  23. />
  24. <view class="search-btn" @click="doSearch">
  25. <text>🔍</text>
  26. <text>搜索</text>
  27. </view>
  28. </view>
  29. <!-- 部门选择行 -->
  30. <view class="dept-row">
  31. <view class="dept-btn" @click="showTreePicker">
  32. <text>🏢</text>
  33. <text>{{ deptName || "全部部门" }}</text>
  34. <text class="arrow">▾</text>
  35. </view>
  36. <!-- 清空部门筛选 -->
  37. <view class="clear-dept" v-if="categoryLevelId" @click="clearDept">
  38. <text>✕</text>
  39. </view>
  40. </view>
  41. <!-- 列表 -->
  42. <scroll-view class="popup-body" scroll-y @scrolltolower="scrolltolower">
  43. <view
  44. v-for="(item, index) in listData"
  45. :key="item.id"
  46. class="list-item"
  47. :class="{ active: isSelected(item.id) }"
  48. @click="toggleItem(item, index)"
  49. >
  50. <!-- 自定义选中图标 -->
  51. <view class="item-check">
  52. <view class="check-box" :class="{ checked: isSelected(item.id) }">
  53. <text v-if="isSelected(item.id)" class="check-mark">✓</text>
  54. </view>
  55. </view>
  56. <view class="item-info">
  57. <view class="item-top">
  58. <text class="item-name">{{ item.name }}</text>
  59. <text class="item-code">{{ item.loginName }}</text>
  60. </view>
  61. <view class="item-bottom">
  62. <text>工号:{{ item.jobNumber || "-" }}</text>
  63. <text>部门:{{ item.groupName || "-" }}</text>
  64. </view>
  65. </view>
  66. </view>
  67. <view style="height: 20rpx"></view>
  68. <view v-if="loading" class="load-more">加载中...</view>
  69. <view v-if="isEnd && listData.length > 0" class="load-more"
  70. >没有更多了</view
  71. >
  72. <u-empty
  73. class="no-data"
  74. v-if="!loading && listData.length === 0"
  75. text="暂无数据"
  76. />
  77. </scroll-view>
  78. <!-- 底部 -->
  79. <view class="footer">
  80. <view class="bottom-left" v-if="isAll == 1" @click="toggleSelectAll">
  81. <!-- 自定义全选复选框 -->
  82. <view class="check-box" :class="{ checked: seletedAll }">
  83. <text v-if="seletedAll" class="check-mark">✓</text>
  84. </view>
  85. <text class="select-all-text">{{
  86. seletedAll ? "取消全选" : "全选"
  87. }}</text>
  88. </view>
  89. <view
  90. class="confirm-btn"
  91. :class="{ disabled: selectedIds.length === 0 }"
  92. @click="confirmSelect"
  93. >
  94. 确定 ({{ selectedIds.length }})
  95. </view>
  96. </view>
  97. </view>
  98. <!-- 部门树选择器 -->
  99. <ba-tree-picker
  100. ref="treePicker"
  101. key="verify"
  102. :multiple="false"
  103. @select-change="onDeptConfirm"
  104. title="选择部门"
  105. :localdata="classificationList"
  106. valueKey="id"
  107. textKey="name"
  108. />
  109. <u-toast ref="uToast" />
  110. </u-popup>
  111. </template>
  112. <script>
  113. import { listOrganizations, getUserPage } from "@/api/myTicket/index.js";
  114. import baTreePicker from "@/components/ba-tree-picker/ba-tree-picker.vue";
  115. export default {
  116. components: {
  117. baTreePicker,
  118. },
  119. data() {
  120. return {
  121. visible: false,
  122. // 列表数据
  123. listData: [],
  124. selectedIds: [], // 选中项的 id 数组
  125. page: 1,
  126. size: 20,
  127. isEnd: true,
  128. loading: false,
  129. searchVal: "",
  130. // 部门筛选
  131. classificationList: [],
  132. categoryLevelId: null,
  133. deptName: "全部部门",
  134. // 模式:1多选,2单选
  135. isAll: "1",
  136. seletedAll: false,
  137. };
  138. },
  139. watch: {
  140. selectedIds: {
  141. handler(val) {
  142. // 更新全选状态
  143. if (this.listData.length > 0) {
  144. this.seletedAll =
  145. val.length === this.listData.length && val.length > 0;
  146. }
  147. },
  148. deep: true,
  149. immediate: true,
  150. },
  151. },
  152. methods: {
  153. // ============ 外部调用 ============
  154. open(isAll) {
  155. this.isAll = isAll || "1";
  156. this.visible = true;
  157. // 重置状态
  158. this.listData = [];
  159. this.selectedIds = [];
  160. this.searchVal = "";
  161. this.categoryLevelId = null;
  162. this.isEnd = false;
  163. this.seletedAll = false;
  164. // 加载数据
  165. this.getClassify();
  166. },
  167. close() {
  168. this.visible = false;
  169. },
  170. // ============ 数据加载 ============
  171. async getClassify() {
  172. try {
  173. const res = await listOrganizations(1);
  174. this.classificationList = res || [];
  175. // 不再默认锁定第一个部门:默认锁定会把列表过滤成「第一个部门下的人」,
  176. // 若该部门没有直属人员就显示「暂无数据」,用户以为选人弹窗坏了。
  177. // 现在默认「全部部门」,需要缩小范围时再手动选。
  178. this.categoryLevelId = null;
  179. this.deptName = "全部部门";
  180. this.page = 1;
  181. this.getList();
  182. } catch (e) {
  183. console.error("获取部门列表失败", e);
  184. // 部门树接口失败也要有人可选的兜底:清空筛选后再拉一次人员列表
  185. this.classificationList = [];
  186. this.categoryLevelId = null;
  187. this.deptName = "全部部门";
  188. this.page = 1;
  189. this.getList();
  190. }
  191. },
  192. async getList() {
  193. if (this.loading || this.isEnd) return;
  194. this.loading = true;
  195. try {
  196. const params = {
  197. pageNum: this.page,
  198. size: this.size,
  199. name: this.searchVal || undefined,
  200. groupId: this.categoryLevelId || undefined,
  201. };
  202. const res = await getUserPage(params);
  203. const list = res.list || [];
  204. if (this.page === 1) {
  205. this.listData = list;
  206. // 如果全选状态开启,自动选中所有
  207. if (this.seletedAll) {
  208. this.selectedIds = this.listData.map((item) => item.id);
  209. }
  210. } else {
  211. this.listData = this.listData.concat(list);
  212. // 追加数据时,如果全选开启,新数据自动选中
  213. if (this.seletedAll) {
  214. const newIds = list.map((item) => item.id);
  215. this.selectedIds = [...this.selectedIds, ...newIds];
  216. }
  217. }
  218. this.isEnd =
  219. list.length < this.size || this.listData.length >= res.count;
  220. this.page += 1;
  221. } catch (e) {
  222. console.error("获取人员列表失败", e);
  223. this.$refs.uToast.show({ type: "error", message: "加载失败,请重试" });
  224. } finally {
  225. this.loading = false;
  226. }
  227. },
  228. // ============ 搜索 ============
  229. doSearch() {
  230. this.page = 1;
  231. this.isEnd = false;
  232. this.selectedIds = [];
  233. this.getList();
  234. },
  235. // ============ 滚动加载更多 ============
  236. scrolltolower() {
  237. if (!this.isEnd && !this.loading) {
  238. this.getList();
  239. }
  240. },
  241. // ============ 部门树选择 ============
  242. showTreePicker() {
  243. this.$refs.treePicker._show();
  244. },
  245. onDeptConfirm(data) {
  246. const id = data[0];
  247. this.categoryLevelId = id;
  248. // 查找部门名称
  249. const findDept = (list) => {
  250. for (const item of list) {
  251. if (item.id === id) {
  252. return item.name;
  253. }
  254. if (item.children && item.children.length > 0) {
  255. const found = findDept(item.children);
  256. if (found) return found;
  257. }
  258. }
  259. return null;
  260. };
  261. this.deptName = findDept(this.classificationList) || "全部部门";
  262. this.page = 1;
  263. this.isEnd = false;
  264. this.selectedIds = [];
  265. this.getList();
  266. },
  267. // ============ 清空部门筛选 ============
  268. clearDept() {
  269. this.categoryLevelId = null;
  270. this.deptName = "全部部门";
  271. this.page = 1;
  272. this.isEnd = false;
  273. this.selectedIds = [];
  274. this.getList();
  275. },
  276. // ============ 判断是否选中 ============
  277. isSelected(id) {
  278. return this.selectedIds.includes(id);
  279. },
  280. // ============ 点击切换选中 ============
  281. toggleItem(item, index) {
  282. console.log(this.isAll,'this.isAll')
  283. if (item.disabled) return;
  284. // 单选模式:只能选一个
  285. if (this.isAll != "1") {
  286. if (this.selectedIds.includes(item.id)) {
  287. this.selectedIds = [];
  288. } else {
  289. this.selectedIds = [item.id];
  290. }
  291. return;
  292. }
  293. // 多选模式:切换
  294. const idx = this.selectedIds.indexOf(item.id);
  295. console.log(idx,'idx')
  296. if (idx > -1) {
  297. this.selectedIds.splice(idx, 1);
  298. } else {
  299. this.selectedIds.push(item.id);
  300. }
  301. // 触发响应式更新
  302. this.selectedIds = [...this.selectedIds];
  303. },
  304. // ============ 全选 ============
  305. toggleSelectAll() {
  306. if (this.seletedAll) {
  307. this.selectedIds = [];
  308. } else {
  309. this.selectedIds = this.listData.map((item) => item.id);
  310. }
  311. this.seletedAll = !this.seletedAll;
  312. },
  313. // ============ 确认选择 ============
  314. confirmSelect() {
  315. if (this.selectedIds.length === 0) {
  316. this.$refs.uToast.show({ type: "warning", message: "请至少选择一人" });
  317. return;
  318. }
  319. const selected = this.listData.filter((item) =>
  320. this.selectedIds.includes(item.id),
  321. );
  322. this.$emit("confirm", selected);
  323. this.close();
  324. },
  325. },
  326. };
  327. </script>
  328. <style lang="scss" scoped>
  329. .select-user-popup {
  330. /deep/ .u-popup__content {
  331. border-radius: 32rpx 32rpx 0 0;
  332. overflow: hidden;
  333. }
  334. .popup-content {
  335. height: 75vh;
  336. display: flex;
  337. flex-direction: column;
  338. background: #f5f8f6;
  339. }
  340. // ===== 头部 =====
  341. .popup-header {
  342. display: flex;
  343. justify-content: space-between;
  344. align-items: center;
  345. padding: 30rpx 32rpx;
  346. background: #fff;
  347. border-bottom: 2rpx solid #eef2f6;
  348. flex-shrink: 0;
  349. .popup-title {
  350. font-size: 36rpx;
  351. font-weight: bold;
  352. color: #1f2b3c;
  353. }
  354. .close-btn {
  355. width: 60rpx;
  356. height: 60rpx;
  357. display: flex;
  358. align-items: center;
  359. justify-content: center;
  360. font-size: 52rpx;
  361. color: #8e9aae;
  362. line-height: 1;
  363. }
  364. }
  365. // ===== 搜索行 =====
  366. .search-row {
  367. display: flex;
  368. align-items: center;
  369. padding: 16rpx 24rpx 8rpx;
  370. background: #fff;
  371. flex-shrink: 0;
  372. gap: 16rpx;
  373. .search-input {
  374. flex: 1;
  375. height: 70rpx;
  376. background: #f5f8f6;
  377. border-radius: 48rpx;
  378. padding: 0 24rpx;
  379. font-size: 28rpx;
  380. border: 2rpx solid transparent;
  381. &:focus {
  382. border-color: #157a2c;
  383. }
  384. }
  385. .search-btn {
  386. display: flex;
  387. align-items: center;
  388. justify-content: center;
  389. height: 70rpx;
  390. padding: 0 28rpx;
  391. background: #157a2c;
  392. border-radius: 48rpx;
  393. color: #fff;
  394. font-size: 28rpx;
  395. gap: 8rpx;
  396. flex-shrink: 0;
  397. .text {
  398. font-size: 26rpx;
  399. }
  400. }
  401. }
  402. // ===== 部门选择行 =====
  403. .dept-row {
  404. display: flex;
  405. align-items: center;
  406. padding: 8rpx 24rpx 16rpx;
  407. background: #fff;
  408. border-bottom: 2rpx solid #eef2f6;
  409. flex-shrink: 0;
  410. gap: 16rpx;
  411. .dept-btn {
  412. display: inline-flex;
  413. align-items: center;
  414. height: 60rpx;
  415. padding: 0 24rpx;
  416. background: #e9f3ec;
  417. border-radius: 48rpx;
  418. color: #157a2c;
  419. font-size: 26rpx;
  420. gap: 8rpx;
  421. .arrow {
  422. font-size: 20rpx;
  423. margin-left: 4rpx;
  424. }
  425. }
  426. .clear-dept {
  427. display: flex;
  428. align-items: center;
  429. justify-content: center;
  430. width: 44rpx;
  431. height: 44rpx;
  432. border-radius: 50%;
  433. background: #f0f0f0;
  434. color: #999;
  435. font-size: 24rpx;
  436. }
  437. }
  438. // ===== 列表 =====
  439. .popup-body {
  440. flex: 1;
  441. overflow-y: auto;
  442. padding: 0 24rpx;
  443. background: #f5f8f6;
  444. .list-item {
  445. display: flex;
  446. align-items: center;
  447. padding: 24rpx 20rpx;
  448. background: #fff;
  449. border-radius: 24rpx;
  450. margin-top: 20rpx;
  451. box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.04);
  452. transition: all 0.2s;
  453. border: 3rpx solid transparent;
  454. &.active {
  455. border-color: #157a2c;
  456. background: #eef6f0;
  457. }
  458. &:active {
  459. transform: scale(0.99);
  460. }
  461. .item-check {
  462. margin-right: 20rpx;
  463. flex-shrink: 0;
  464. }
  465. // 自定义复选框
  466. .check-box {
  467. width: 40rpx;
  468. height: 40rpx;
  469. border-radius: 50%;
  470. border: 3rpx solid #ccc;
  471. display: flex;
  472. align-items: center;
  473. justify-content: center;
  474. transition: all 0.2s;
  475. background: #fff;
  476. &.checked {
  477. background: #157a2c;
  478. border-color: #157a2c;
  479. }
  480. .check-mark {
  481. color: #fff;
  482. font-size: 24rpx;
  483. font-weight: bold;
  484. }
  485. }
  486. .item-info {
  487. flex: 1;
  488. min-width: 0;
  489. .item-top {
  490. display: flex;
  491. justify-content: space-between;
  492. align-items: center;
  493. margin-bottom: 8rpx;
  494. .item-name {
  495. font-size: 30rpx;
  496. font-weight: 600;
  497. color: #1f2b3c;
  498. }
  499. .item-code {
  500. font-size: 24rpx;
  501. color: #8e9aae;
  502. }
  503. }
  504. .item-bottom {
  505. display: flex;
  506. justify-content: space-between;
  507. font-size: 24rpx;
  508. color: #8e9aae;
  509. text {
  510. flex: 1;
  511. overflow: hidden;
  512. text-overflow: ellipsis;
  513. white-space: nowrap;
  514. }
  515. }
  516. }
  517. }
  518. .no-data {
  519. margin-top: 40vh;
  520. }
  521. .load-more {
  522. text-align: center;
  523. font-size: 26rpx;
  524. color: #aaa;
  525. padding: 30rpx 0 60rpx;
  526. }
  527. }
  528. // ===== 底部 =====
  529. .footer {
  530. display: flex;
  531. justify-content: space-between;
  532. align-items: center;
  533. padding: 16rpx 24rpx;
  534. background: #fff;
  535. border-top: 2rpx solid #eef2f6;
  536. flex-shrink: 0;
  537. min-height: 90rpx;
  538. .bottom-left {
  539. display: flex;
  540. align-items: center;
  541. gap: 12rpx;
  542. .check-box {
  543. width: 40rpx;
  544. height: 40rpx;
  545. border-radius: 4rpx;
  546. border: 3rpx solid #ccc;
  547. display: flex;
  548. align-items: center;
  549. justify-content: center;
  550. transition: all 0.2s;
  551. background: #fff;
  552. &.checked {
  553. background: #157a2c;
  554. border-color: #157a2c;
  555. }
  556. .check-mark {
  557. color: #fff;
  558. font-size: 28rpx;
  559. font-weight: bold;
  560. }
  561. }
  562. .select-all-text {
  563. font-size: 28rpx;
  564. color: #333;
  565. }
  566. }
  567. .confirm-btn {
  568. height: 72rpx;
  569. padding: 0 40rpx;
  570. border-radius: 48rpx;
  571. background: #157a2c;
  572. color: #fff;
  573. font-size: 28rpx;
  574. display: flex;
  575. align-items: center;
  576. justify-content: center;
  577. /* 单选模式没有左侧「全选」块,靠这个保证确定按钮仍然靠右 */
  578. margin-left: auto;
  579. flex-shrink: 0;
  580. &.disabled {
  581. background: #ccc;
  582. color: #999;
  583. }
  584. }
  585. }
  586. }
  587. </style>