userSelectorPlus.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. <template>
  2. <a-modal
  3. v-model:open="visible"
  4. title="用户选择"
  5. :width="1000"
  6. :mask-closable="false"
  7. :destroy-on-close="true"
  8. @ok="handleOk"
  9. @cancel="handleClose"
  10. >
  11. <a-row :gutter="10">
  12. <a-col :span="7">
  13. <a-card size="small" :loading="cardLoading" class="selectorTreeDiv">
  14. <a-tree
  15. v-if="treeData"
  16. v-model:expandedKeys="defaultExpandedKeys"
  17. :tree-data="treeData"
  18. :field-names="treeFieldNames"
  19. @select="treeSelect"
  20. >
  21. </a-tree>
  22. </a-card>
  23. </a-col>
  24. <a-col :span="11">
  25. <div class="table-operator xn-mb10">
  26. <a-form ref="searchFormRef" name="advanced_search" class="ant-advanced-search-form" :model="searchFormState">
  27. <a-row :gutter="24">
  28. <a-col :span="12">
  29. <a-form-item name="searchKey">
  30. <a-input v-model:value="searchFormState.searchKey" placeholder="请输入用户名" />
  31. </a-form-item>
  32. </a-col>
  33. <a-col :span="12">
  34. <a-button type="primary" class="primarySele" @click="loadData()"> 查询 </a-button>
  35. <a-button class="snowy-button-left" @click="reset()"> 重置 </a-button>
  36. </a-col>
  37. </a-row>
  38. </a-form>
  39. </div>
  40. <div class="user-table">
  41. <a-table
  42. ref="tableRef"
  43. size="small"
  44. :columns="commons"
  45. :data-source="tableData"
  46. :expand-row-by-click="true"
  47. :loading="pageLoading"
  48. bordered
  49. :pagination="false"
  50. >
  51. <template #title>
  52. <span>待选择列表 {{ tableRecordNum }} 条</span>
  53. <div v-if="!radioModel" class="xn-fdr">
  54. <a-button type="dashed" size="small" @click="addAllPageRecord">添加当前数据</a-button>
  55. </div>
  56. </template>
  57. <template #bodyCell="{ column, record }">
  58. <template v-if="column.dataIndex === 'avatar'">
  59. <a-avatar :src="record.avatar" style="margin-bottom: -5px; margin-top: -5px" />
  60. </template>
  61. <template v-if="column.dataIndex === 'action'">
  62. <a-button type="dashed" size="small" @click="addRecord(record)"><PlusOutlined /></a-button>
  63. </template>
  64. <template v-if="column.dataIndex === 'category'">
  65. {{ $TOOL.dictTypeData('ROLE_CATEGORY', record.category) }}
  66. </template>
  67. </template>
  68. </a-table>
  69. <div class="mt-2">
  70. <a-pagination
  71. v-if="!isEmpty(tableData)"
  72. v-model:current="current"
  73. v-model:page-size="pageSize"
  74. :total="total"
  75. size="small"
  76. showSizeChanger
  77. @change="paginationChange"
  78. />
  79. </div>
  80. </div>
  81. </a-col>
  82. <a-col :span="6">
  83. <div class="user-table">
  84. <a-table
  85. ref="selectedTable"
  86. size="small"
  87. :columns="selectedCommons"
  88. :data-source="selectedData"
  89. :expand-row-by-click="true"
  90. :loading="selectedTableListLoading"
  91. bordered
  92. >
  93. <template #title>
  94. <span>已选择: {{ selectedData.length }}</span>
  95. <div v-if="!radioModel" class="xn-fdr">
  96. <a-button type="dashed" danger size="small" @click="delAllRecord">全部移除</a-button>
  97. </div>
  98. </template>
  99. <template #bodyCell="{ column, record }">
  100. <template v-if="column.dataIndex === 'action'">
  101. <a-button type="dashed" danger size="small" @click="delRecord(record)"><MinusOutlined /></a-button>
  102. </template>
  103. </template>
  104. </a-table>
  105. </div>
  106. </a-col>
  107. </a-row>
  108. </a-modal>
  109. </template>
  110. <script setup name="userSelectorPlus">
  111. import { message } from 'ant-design-vue'
  112. import { remove, isEmpty } from 'lodash-es'
  113. // 弹窗是否打开
  114. const visible = ref(false)
  115. // 主表格common
  116. const commons = [
  117. {
  118. title: '操作',
  119. dataIndex: 'action',
  120. align: 'center',
  121. width: 50
  122. },
  123. {
  124. title: '头像',
  125. dataIndex: 'avatar',
  126. width: 50
  127. },
  128. {
  129. title: '用户名',
  130. dataIndex: 'name',
  131. ellipsis: true
  132. },
  133. {
  134. title: '账号',
  135. dataIndex: 'account'
  136. }
  137. ]
  138. // 选中表格的表格common
  139. const selectedCommons = [
  140. {
  141. title: '操作',
  142. dataIndex: 'action',
  143. align: 'center',
  144. width: 50
  145. },
  146. {
  147. title: '用户名',
  148. dataIndex: 'name',
  149. ellipsis: true
  150. }
  151. ]
  152. // 主表格的ref 名称
  153. const tableRef = ref()
  154. // 选中表格的ref 名称
  155. const selectedTable = ref()
  156. const tableRecordNum = ref()
  157. const searchFormState = ref({})
  158. const searchFormRef = ref()
  159. const cardLoading = ref(true)
  160. const pageLoading = ref(false)
  161. const selectedTableListLoading = ref(false)
  162. // 替换treeNode 中 title,key,children
  163. const treeFieldNames = { children: 'children', title: 'name', key: 'id' }
  164. // 获取机构树数据
  165. const treeData = ref()
  166. // 默认展开二级树的节点id
  167. const defaultExpandedKeys = ref([])
  168. const emit = defineEmits({ onBack: null })
  169. const tableData = ref([])
  170. const selectedData = ref([])
  171. const recordIds = ref()
  172. const props = defineProps(['radioModel', 'dataIsConverterFlw', 'orgTreeApi', 'userPageApi', 'checkedUserListApi'])
  173. // 是否是单选
  174. const radioModel = props.radioModel || false
  175. // 数据是否转换成工作流格式
  176. const dataIsConverterFlw = props.dataIsConverterFlw || false
  177. // 分页相关
  178. const current = ref(0) // 当前页数
  179. const pageSize = ref(20) // 每页条数
  180. const total = ref(0) // 数据总数
  181. // 打开弹框
  182. const showUserPlusModal = (ids = []) => {
  183. visible.value = true
  184. if (dataIsConverterFlw) {
  185. ids = goDataConverter(ids)
  186. }
  187. recordIds.value = ids
  188. // 加载机构树
  189. if (props.orgTreeApi) {
  190. // 获取机构树
  191. props.orgTreeApi().then((data) => {
  192. cardLoading.value = false
  193. if (data !== null) {
  194. treeData.value = data
  195. // 默认展开2级
  196. treeData.value.forEach((item) => {
  197. // 因为0的顶级
  198. if (item.parentId === '0') {
  199. defaultExpandedKeys.value.push(item.id)
  200. // 取到下级ID
  201. if (item.children) {
  202. item.children.forEach((items) => {
  203. defaultExpandedKeys.value.push(items.id)
  204. })
  205. }
  206. }
  207. })
  208. }
  209. })
  210. }
  211. searchFormState.value.size = pageSize.value
  212. loadData()
  213. if (props.checkedUserListApi) {
  214. if (isEmpty(recordIds.value)) {
  215. return
  216. }
  217. const param = {
  218. idList: recordIds.value
  219. }
  220. selectedTableListLoading.value = true
  221. props
  222. .checkedUserListApi(param)
  223. .then((data) => {
  224. selectedData.value = data
  225. })
  226. .finally(() => {
  227. selectedTableListLoading.value = false
  228. })
  229. }
  230. }
  231. // 查询主表格数据
  232. const loadData = () => {
  233. pageLoading.value = true
  234. props
  235. .userPageApi(searchFormState.value)
  236. .then((data) => {
  237. current.value = data.current
  238. // pageSize.value = data.size
  239. total.value = data.total
  240. // 重置、赋值
  241. tableData.value = []
  242. tableRecordNum.value = 0
  243. tableData.value = data.records
  244. if (data.records) {
  245. tableRecordNum.value = data.records.length
  246. } else {
  247. tableRecordNum.value = 0
  248. }
  249. })
  250. .finally(() => {
  251. pageLoading.value = false
  252. })
  253. }
  254. // pageSize改变回调分页事件
  255. const paginationChange = (page, pageSize) => {
  256. searchFormState.value.current = page
  257. searchFormState.value.size = pageSize
  258. loadData()
  259. }
  260. const judge = () => {
  261. return !(radioModel && selectedData.value.length > 0)
  262. }
  263. // 添加记录
  264. const addRecord = (record) => {
  265. if (!judge()) {
  266. message.warning('只可选择一条')
  267. return
  268. }
  269. const selectedRecord = selectedData.value.filter((item) => item.id === record.id)
  270. if (selectedRecord.length === 0) {
  271. selectedData.value.push(record)
  272. } else {
  273. message.warning('该记录已存在')
  274. }
  275. }
  276. // 添加全部
  277. const addAllPageRecord = () => {
  278. let newArray = selectedData.value.concat(tableData.value)
  279. let list = []
  280. for (let item1 of newArray) {
  281. let flag = true
  282. for (let item2 of list) {
  283. if (item1.id === item2.id) {
  284. flag = false
  285. }
  286. }
  287. if (flag) {
  288. list.push(item1)
  289. }
  290. }
  291. selectedData.value = list
  292. }
  293. // 删减记录
  294. const delRecord = (record) => {
  295. remove(selectedData.value, (item) => item.id === record.id)
  296. }
  297. // 删减记录
  298. const delAllRecord = () => {
  299. selectedData.value = []
  300. }
  301. // 点击树查询
  302. const treeSelect = (selectedKeys) => {
  303. searchFormState.value.current = 0
  304. if (selectedKeys.length > 0) {
  305. searchFormState.value.orgId = selectedKeys.toString()
  306. } else {
  307. delete searchFormState.value.orgId
  308. }
  309. loadData()
  310. }
  311. // 确定
  312. const handleOk = () => {
  313. const value = []
  314. selectedData.value.forEach((item) => {
  315. const obj = {
  316. id: item.id,
  317. name: item.name,
  318. account: item.account
  319. }
  320. value.push(obj)
  321. })
  322. // 判断是否做数据的转换为工作流需要的
  323. if (dataIsConverterFlw) {
  324. emit('onBack', outDataConverter(value))
  325. } else {
  326. emit('onBack', value)
  327. }
  328. handleClose()
  329. }
  330. // 重置
  331. const reset = () => {
  332. delete searchFormState.value.searchKey
  333. loadData()
  334. }
  335. const handleClose = () => {
  336. searchFormState.value = {}
  337. tableRecordNum.value = 0
  338. tableData.value = []
  339. current.value = 0
  340. pageSize.value = 20
  341. total.value = 0
  342. selectedData.value = []
  343. visible.value = false
  344. }
  345. // 数据进入后转换
  346. const goDataConverter = (data) => {
  347. const resultData = []
  348. if (data.length > 0) {
  349. const values = data[0].value.split(',')
  350. if (JSON.stringify(values) !== '[""]') {
  351. for (let i = 0; i < values.length; i++) {
  352. resultData.push(values[i])
  353. }
  354. }
  355. }
  356. return resultData
  357. }
  358. // 数据出口转换器
  359. const outDataConverter = (data) => {
  360. const obj = {}
  361. let label = ''
  362. let value = ''
  363. for (let i = 0; i < data.length; i++) {
  364. if (data.length === i + 1) {
  365. label = label + data[i].name
  366. value = value + data[i].id
  367. } else {
  368. label = label + data[i].name + ','
  369. value = value + data[i].id + ','
  370. }
  371. }
  372. obj.key = 'USER'
  373. obj.label = label
  374. obj.value = value
  375. obj.extJson = ''
  376. return obj
  377. }
  378. defineExpose({
  379. showUserPlusModal
  380. })
  381. </script>
  382. <style lang="less" scoped>
  383. .selectorTreeDiv {
  384. max-height: 500px;
  385. overflow: auto;
  386. }
  387. .cardTag {
  388. margin-left: 10px;
  389. }
  390. .primarySele {
  391. margin-right: 10px;
  392. }
  393. .ant-form-item {
  394. margin-bottom: 0 !important;
  395. }
  396. .user-table {
  397. overflow: auto;
  398. max-height: 450px;
  399. }
  400. </style>