SelectPopup.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. <template>
  2. <u-popup class="jnpf-tree-select-popup" mode="right" v-model="showPopup" width="100%" @close="close">
  3. <view class="jnpf-tree-select-body">
  4. <view class="jnpf-tree-select-title">
  5. <text class="icon-ym icon-ym-report-icon-preview-pagePre backIcon" @tap="close()"></text>
  6. <view class="title">选择用户</view>
  7. </view>
  8. <view class="jnpf-tree-select-search">
  9. <u-search :placeholder="$t('app.apply.pleaseKeyword')" v-model="keyword" height="72"
  10. :show-action="false" @change="handleSearch" bg-color="#f0f2f6" shape="square">
  11. </u-search>
  12. </view>
  13. <view class="jnpf-tree-selected">
  14. <view class="jnpf-tree-selected-head">
  15. <view>{{$t('component.jnpf.common.selected')}}({{selectedList.length||0}})</view>
  16. <view v-if="multiple" class="clear-btn" @click="setCheckAll">
  17. {{$t('component.jnpf.common.clearAll')}}
  18. </view>
  19. </view>
  20. <view class="jnpf-tree-selected-box">
  21. <scroll-view scroll-y="true" class="select-list">
  22. <u-tag closeable @close="delSelect(index)" v-for="(item,index) in selectedList" :key="index"
  23. :text="item.fullName" class="u-selectTag" />
  24. </scroll-view>
  25. </view>
  26. </view>
  27. <view class="jnpf-tree-selected-line"></view>
  28. <view class="jnpf-tree-selected-tabs">
  29. <view class="tab-item" :class="{'tab-item-active':activeKey==='user'}" @click="toggloActive('user')">
  30. 用户
  31. </view>
  32. <block v-if="selectType === 'all'">
  33. <view class="tab-item" :class="{'tab-item-active':activeKey==='position'}"
  34. @click="toggloActive('position')">
  35. 组织岗位
  36. </view>
  37. <view class="tab-item" :class="{'tab-item-active':activeKey==='role'}"
  38. @click="toggloActive('role')">
  39. 角色
  40. </view>
  41. <view class="tab-item" :class="{'tab-item-active':activeKey==='current'}"
  42. @click="toggloActive('current')">
  43. 当前用户
  44. </view>
  45. </block>
  46. </view>
  47. <view class="jnpf-tree-selected-breadcrumb" v-if="selectType === 'all' && activeKey!=='current'">
  48. <view class="breadcrumb-item" :class="{'breadcrumb-item-active':!currStep}" @click="handleToFirst()"
  49. v-if="activeKey==='user'">
  50. 用户组
  51. </view>
  52. <view class="breadcrumb-item" :class="{'breadcrumb-item-active':!currStep}" @click="handleToFirst()"
  53. v-if="activeKey==='position'">
  54. 组织岗位
  55. </view>
  56. <view class="breadcrumb-item" :class="{'breadcrumb-item-active':!currStep}" @click="handleToFirst()"
  57. v-if="activeKey==='role'">
  58. 角色
  59. </view>
  60. <view class="icon-ym icon-ym-caret-right breadcrumb-item" v-if="currStep"></view>
  61. <view class="breadcrumb-item" :class="{'breadcrumb-item-active':currStep}" v-if="currStep">
  62. 用户列表
  63. </view>
  64. </view>
  65. <view class="jnpf-tree-select-tree">
  66. <scroll-view :scroll-y="true" style="height: 100%" :scroll-top="scrollTop" @scroll="handleScroll"
  67. v-show="(!hasPage && !currStep) && selectType === 'all' && activeKey!=='current'">
  68. <ly-tree ref="tree" :props="realProps" :node-key="realProps.value" :load="loadNode" lazy
  69. :tree-data="lazyOptions" show-node-icon :defaultExpandAll='false'
  70. @node-click="handleTreeNodeClick" :expandOnClickNode="false" :checkOnClickNode="false"
  71. v-if="activeKey==='position'" />
  72. <block v-else>
  73. <view class="jnpf-selcet-list" v-if="list.length">
  74. <view class="jnpf-selcet-cell" v-for="item in list" :key="item.id"
  75. @click.stop="handleNodeClick(item)">
  76. <view class="jnpf-selcet-cell-icon" :class='item.icon'></view>
  77. <view class="jnpf-selcet-cell-name">
  78. {{item.fullName}}
  79. </view>
  80. </view>
  81. </view>
  82. <Empty class="h-full" v-else />
  83. </block>
  84. </scroll-view>
  85. <scroll-view :scroll-y="true" style="height: 100%" :refresher-enabled="false" :refresher-threshold="100"
  86. :scroll-with-animation='true' :refresher-triggered="triggered" @scrolltolower="handleScrollToLower"
  87. v-if="hasPage||currStep||activeKey==='current'">
  88. <view class="jnpf-selcet-list" v-if="userList.length">
  89. <view class="jnpf-selcet-cell" v-for="item in userList" :key="item.id"
  90. @click.stop="handleUserNodeClick(item)">
  91. <view class="jnpf-selcet-cell-action">
  92. <lyCheckbox :type="multiple ? 'checkbox' : 'radio'"
  93. :checked="selectedIds.includes(item.id)" />
  94. </view>
  95. <u-avatar class="jnpf-selcet-cell-avatar" :src="baseURL+item.headIcon" mode="circle"
  96. size="44"></u-avatar>
  97. <view class="jnpf-selcet-cell-name">
  98. {{item.fullName}}
  99. </view>
  100. </view>
  101. </view>
  102. <Empty class="h-full" v-else />
  103. </scroll-view>
  104. </view>
  105. <!-- 底部按钮 -->
  106. <view class="jnpf-tree-select-actions">
  107. <u-button class="buttom-btn" @click="close()">{{$t('common.cancelText')}}</u-button>
  108. <u-button class="buttom-btn" type="primary"
  109. @click.stop="handleConfirm()">{{$t('common.okText')}}</u-button>
  110. </view>
  111. </view>
  112. </u-popup>
  113. </template>
  114. <script>
  115. import {
  116. getOrgAndPosSelector,
  117. getUserList,
  118. getSelectedUserList
  119. } from '@/api/common'
  120. import lyCheckbox from '@/components/ly-tree/components/ly-checkbox.vue';
  121. import {
  122. useBaseStore
  123. } from '@/store/modules/base'
  124. import Empty from '../Empty/index.vue'
  125. const baseStore = useBaseStore()
  126. const defaultProps = {
  127. label: 'fullName',
  128. value: 'id',
  129. icon: 'icon',
  130. children: 'children'
  131. }
  132. const defaultUserQuery = {
  133. enabledMark: 1,
  134. groupId: '',
  135. organizeId: '',
  136. positionId: '',
  137. roleId: '',
  138. };
  139. export default {
  140. props: {
  141. selectedData: {
  142. type: Array,
  143. default: () => []
  144. },
  145. ableIds: {
  146. type: Array,
  147. default: () => []
  148. },
  149. selectType: {
  150. type: String,
  151. default: 'all'
  152. },
  153. // 通过双向绑定控制组件的弹出与收起
  154. modelValue: {
  155. type: Boolean,
  156. default: false
  157. },
  158. // 弹出的z-index值
  159. zIndex: {
  160. type: [String, Number],
  161. default: 0
  162. },
  163. props: {
  164. type: Object,
  165. default: () => ({
  166. label: 'fullName',
  167. value: 'id',
  168. icon: 'icon',
  169. children: 'children',
  170. isLeaf: 'isLeaf'
  171. })
  172. },
  173. multiple: {
  174. type: Boolean,
  175. default: false
  176. }
  177. },
  178. components: {
  179. lyCheckbox,
  180. Empty
  181. },
  182. data() {
  183. return {
  184. moving: false,
  185. selectedList: [],
  186. selectedIds: [],
  187. keyword: '',
  188. showPopup: false,
  189. lazyOptions: [],
  190. activeKey: 'user',
  191. hasPage: false,
  192. pagination: {
  193. hasPage: 1,
  194. currentPage: 1,
  195. pageSize: 20
  196. },
  197. triggered: false,
  198. finish: false,
  199. list: [],
  200. userList: [],
  201. scrollTop: 0,
  202. currStep: 0,
  203. userQuery: {},
  204. };
  205. },
  206. watch: {
  207. // 在select弹起的时候,重新初始化所有数据
  208. modelValue: {
  209. handler(val) {
  210. this.showPopup = val
  211. if (val) setTimeout(() => this.init(), 10);
  212. },
  213. immediate: true
  214. },
  215. selectedList: {
  216. handler(val) {
  217. this.selectedIds = val.map((o) => o.id);
  218. this.$refs.tree && this.$refs.tree.setCheckedKeys(this.selectedIds)
  219. },
  220. deep: true
  221. },
  222. },
  223. computed: {
  224. baseURL() {
  225. return this.define.baseURL
  226. },
  227. uZIndex() {
  228. // 如果用户有传递z-index值,优先使用
  229. return this.zIndex ? this.zIndex : this.$u.zIndex.popup;
  230. },
  231. realProps() {
  232. return {
  233. ...defaultProps,
  234. ...this.props
  235. }
  236. },
  237. getCurrList() {
  238. const userInfo = uni.getStorageSync('userInfo') || {}
  239. const current = {
  240. fullName: `${userInfo.userName}/${userInfo.userAccount}`,
  241. headIcon: userInfo.headIcon,
  242. id: userInfo.userId,
  243. realName: userInfo.userName,
  244. };
  245. return [current];
  246. }
  247. },
  248. methods: {
  249. init() {
  250. this.keyword = ""
  251. this.hasPage = 0
  252. this.currStep = 0
  253. this.activeKey = 'user'
  254. this.resetQuery()
  255. this.$nextTick(() => {
  256. this.triggered = true
  257. })
  258. this.selectedList = JSON.parse(JSON.stringify(this.selectedData))
  259. if (this.selectType === 'all') {
  260. this.getGroupList()
  261. } else {
  262. this.hasPage = 1
  263. this.getConditionOptions()
  264. }
  265. },
  266. handleToFirst() {
  267. if (!this.currStep) return
  268. this.currStep = 0
  269. this.hasPage = false
  270. this.keyword = ''
  271. this.resetQuery()
  272. },
  273. resetQuery() {
  274. this.userList = []
  275. this.finish = false
  276. this.pagination.currentPage = 1
  277. },
  278. getConditionOptions() {
  279. if (!this.ableIds.length) return
  280. const query = {
  281. keyword: this.keyword,
  282. ids: this.ableIds,
  283. ...this.pagination
  284. }
  285. getSelectedUserList(query).then(res => {
  286. const list = res.data.list || []
  287. if (list.length < this.pagination.pageSize) this.finish = true;
  288. this.userList = this.userList.concat(list);
  289. this.pagination.currentPage++
  290. })
  291. },
  292. loadNode(node, resolve) {
  293. const id = node.key || '0'
  294. const type = node?.data?.type || 'organize'
  295. const data = {
  296. id,
  297. type
  298. }
  299. getOrgAndPosSelector(data).then(res => {
  300. const list = res.data?.list || []
  301. resolve(list)
  302. })
  303. },
  304. handleScroll(e) {
  305. this.scrollTop = e.detail.scrollTop
  306. },
  307. goTop() {
  308. this.scrollTop = 0
  309. },
  310. handleScrollToLower() {
  311. if (this.finish || this.activeKey === 'current') return
  312. this.getUserData()
  313. },
  314. getUserData() {
  315. this.selectType == 'all' ? this.getUserList() : this.getConditionOptions()
  316. },
  317. getUserList() {
  318. let data = {
  319. keyword: this.keyword,
  320. ...this.pagination,
  321. ...this.userQuery
  322. }
  323. getUserList(data).then(res => {
  324. const list = res.data.list || []
  325. if (list.length < this.pagination.pageSize) this.finish = true;
  326. this.userList = this.userList.concat(list);
  327. this.pagination.currentPage++
  328. })
  329. },
  330. handleTreeNodeClick(item) {
  331. const data = item.data
  332. this.handleNodeClick(data)
  333. },
  334. handleNodeClick(data) {
  335. let key = `${this.activeKey==='position'?(data.type||'position'):this.activeKey}Id`;
  336. if (this.activeKey === 'user') key = 'groupId'
  337. this.userQuery = {
  338. ...defaultUserQuery,
  339. [key]: data.id
  340. };
  341. this.currStep = 1
  342. this.keyword = ''
  343. this.resetQuery()
  344. this.getUserList()
  345. },
  346. handleUserNodeClick(data) {
  347. const index = this.selectedList.findIndex((o) => o.id === data.id);
  348. if (index !== -1) return this.selectedList.splice(index, 1);
  349. this.multiple ? this.selectedList.push(data) : (this.selectedList = [data]);
  350. },
  351. delSelect(index) {
  352. this.selectedList.splice(index, 1);
  353. },
  354. setCheckAll() {
  355. this.selectedIds = []
  356. this.selectedList = []
  357. },
  358. handleConfirm() {
  359. this.$emit('confirm', this.selectedList, this.selectedIds);
  360. this.close();
  361. },
  362. close() {
  363. this.$emit('close', false);
  364. },
  365. toggloActive(key) {
  366. if (this.activeKey === key) return
  367. this.currStep = 0
  368. this.keyword = ''
  369. this.resetQuery()
  370. this.$nextTick(async () => {
  371. this.activeKey = key
  372. this.goTop()
  373. if (this.activeKey === 'user') return this.getGroupList()
  374. if (this.activeKey === 'role') return (this.list = await baseStore.getRoleList())
  375. if (this.activeKey === 'current') this.userList = this.getCurrList
  376. })
  377. },
  378. async getGroupList() {
  379. const list = await baseStore.getGroupList()
  380. this.list = [{
  381. fullName: '全部用户',
  382. icon: 'icon-ym icon-ym-generator-group1',
  383. id: ''
  384. }, ...list]
  385. },
  386. handleSearch(val) {
  387. this.keyword = val
  388. this.hasPage = this.selectType !== 'all' || !!val
  389. this.currStep = val ? 1 : 0
  390. this.goTop()
  391. if (this.activeKey != 'user') this.activeKey = 'user'
  392. this.$nextTick(() => {
  393. if (this.keyword || this.selectType !== 'all') {
  394. this.userQuery = {
  395. ...defaultUserQuery
  396. };
  397. this.getGroupList()
  398. this.resetQuery()
  399. this.$u.debounce(this.getUserData, 300)
  400. }
  401. })
  402. },
  403. }
  404. };
  405. </script>