selecUser.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. <template>
  2. <div>
  3. <el-dialog
  4. title="选择人员"
  5. :visible.sync="userVisible"
  6. width="70%"
  7. :show-close="false"
  8. >
  9. <el-row :gutter="20">
  10. <!--部门数据-->
  11. <el-col :span="6" :xs="24">
  12. <div class="head-container">
  13. <el-input
  14. v-model="deptName"
  15. placeholder="请输入部门名称"
  16. clearable
  17. size="small"
  18. prefix-icon="el-icon-search"
  19. style="margin-bottom: 20px"
  20. />
  21. </div>
  22. <div class="head-container">
  23. <el-tree
  24. :data="deptOptions"
  25. :props="defaultProps"
  26. :expand-on-click-node="false"
  27. :filter-node-method="filterNode"
  28. ref="tree"
  29. node-key="id"
  30. :default-expand-all="true"
  31. highlight-current
  32. @node-click="handleNodeClick"
  33. />
  34. </div>
  35. </el-col>
  36. <!--用户数据-->
  37. <el-col :span="18" :xs="24">
  38. <el-form
  39. :model="queryParams"
  40. ref="queryForm"
  41. size="small"
  42. :inline="true"
  43. v-show="showSearch"
  44. label-width="68px"
  45. >
  46. <el-form-item label="用户姓名" prop="nickName">
  47. <el-input
  48. v-model.trim="queryParams.nickName"
  49. placeholder="请输入用户姓名"
  50. clearable
  51. style="width: 150px"
  52. @keyup.enter.native="handleQuery"
  53. />
  54. </el-form-item>
  55. <el-form-item label="手机号码" prop="phonenumber">
  56. <el-input
  57. v-model.trim="queryParams.phonenumber"
  58. placeholder="请输入手机号码"
  59. clearable
  60. style="width: 150px"
  61. @keyup.enter.native="handleQuery"
  62. />
  63. </el-form-item>
  64. <el-form-item>
  65. <el-button
  66. type="primary"
  67. icon="el-icon-search"
  68. size="mini"
  69. @click="handleQuery"
  70. >搜索</el-button
  71. >
  72. <el-button icon="el-icon-refresh" size="mini" type="primary" @click="resetQuery"
  73. >重置</el-button
  74. >
  75. </el-form-item>
  76. </el-form>
  77. <el-table
  78. @row-click="clickRow"
  79. v-show="checkType === 'multiple'"
  80. ref="dataTable"
  81. v-loading="loading"
  82. :row-key="getRowKey"
  83. :data="userList"
  84. @select="handleSelect"
  85. @selection-change="handleMultipleUserSelect"
  86. >
  87. <el-table-column
  88. type="selection"
  89. :reserve-selection="true"
  90. width="50"
  91. align="center"
  92. />
  93. <el-table-column
  94. label="用户编号"
  95. align="center"
  96. key="userId"
  97. prop="userId"
  98. v-if="columns[0].visible"
  99. />
  100. <!-- <el-table-column
  101. label="用户昵称"
  102. align="center"
  103. key="nickName"
  104. prop="nickName"
  105. v-if="columns[1].visible"
  106. :show-overflow-tooltip="true"
  107. /> -->
  108. <el-table-column
  109. label="用户姓名"
  110. align="center"
  111. key="nickName"
  112. prop="nickName"
  113. v-if="columns[2].visible"
  114. :show-overflow-tooltip="true"
  115. />
  116. <el-table-column
  117. label="部门"
  118. align="center"
  119. key="deptName"
  120. prop="dept.deptName"
  121. v-if="columns[3].visible"
  122. :show-overflow-tooltip="true"
  123. />
  124. <el-table-column
  125. label="手机号码"
  126. align="center"
  127. key="phonenumber"
  128. prop="phonenumber"
  129. v-if="columns[4].visible"
  130. width="120"
  131. />
  132. </el-table>
  133. <el-table
  134. v-show="checkType === 'single'"
  135. v-loading="loading"
  136. :data="userList"
  137. @current-change="handleSingleUserSelect"
  138. >
  139. <el-table-column width="55" align="center">
  140. <template slot-scope="scope">
  141. <el-radio v-model="radioSelected" :label="scope.row.userId">{{
  142. ""
  143. }}</el-radio>
  144. </template>
  145. </el-table-column>
  146. <el-table-column
  147. label="用户编号"
  148. align="center"
  149. key="userId"
  150. prop="userId"
  151. v-if="columns[0].visible"
  152. />
  153. <el-table-column
  154. label="登录账号"
  155. align="center"
  156. prop="nickName"
  157. v-if="columns[1].visible"
  158. :show-overflow-tooltip="true"
  159. />
  160. <el-table-column
  161. label="用户姓名"
  162. align="center"
  163. key="nickName"
  164. prop="nickName"
  165. v-if="columns[2].visible"
  166. :show-overflow-tooltip="true"
  167. />
  168. <el-table-column
  169. label="部门"
  170. align="center"
  171. key="deptName"
  172. prop="dept.deptName"
  173. v-if="columns[3].visible"
  174. :show-overflow-tooltip="true"
  175. />
  176. <el-table-column
  177. label="手机号码"
  178. align="center"
  179. key="phonenumber"
  180. prop="phonenumber"
  181. v-if="columns[4].visible"
  182. width="120"
  183. />
  184. </el-table>
  185. <pagination
  186. v-show="total > 0"
  187. :total="total"
  188. :page-sizes="[5, 10]"
  189. :page.sync="queryParams.pageNum"
  190. :limit.sync="queryParams.pageSize"
  191. @pagination="getList"
  192. />
  193. </el-col>
  194. </el-row>
  195. <span slot="footer" class="dialog-footer">
  196. <el-button @click="handleUserVisible(false)">取 消</el-button>
  197. <el-button type="primary" @click="checkUserComplete">确 定</el-button>
  198. </span>
  199. </el-dialog>
  200. </div>
  201. </template>
  202. <script>
  203. import { listUserNew, deptTreeSelect } from "@/api/system/user";
  204. import Treeselect from "@riophae/vue-treeselect";
  205. import "@riophae/vue-treeselect/dist/vue-treeselect.css";
  206. import { StrUtil } from "@/utils/StrUtil";
  207. export default {
  208. name: "FlowUser",
  209. dicts: ["sys_normal_disable", "sys_user_sex"],
  210. components: { Treeselect },
  211. // 接受父组件的值
  212. props: {
  213. // 回显数据传值
  214. selectValues: {
  215. type: Number | String | Array,
  216. default: null,
  217. required: false,
  218. },
  219. selectNameValues: {
  220. type: Number | String | Array,
  221. default: null,
  222. required: false,
  223. },
  224. // 表格类型
  225. checkType: {
  226. type: String,
  227. default: "multiple",
  228. required: true,
  229. },
  230. },
  231. data() {
  232. return {
  233. userVisible: false,
  234. // 遮罩层
  235. loading: true,
  236. // 选中数组
  237. ids: [],
  238. // 非单个禁用
  239. single: true,
  240. // 非多个禁用
  241. multiple: true,
  242. // 显示搜索条件
  243. showSearch: true,
  244. // 总条数
  245. total: 0,
  246. // 用户表格数据
  247. userList: [],
  248. // 弹出层标题
  249. title: "",
  250. // 部门树选项
  251. deptOptions: undefined,
  252. // 是否显示弹出层
  253. open: false,
  254. // 部门名称
  255. deptName: undefined,
  256. // 表单参数
  257. form: {},
  258. defaultProps: {
  259. children: "children",
  260. label: "label",
  261. },
  262. // 查询参数
  263. queryParams: {
  264. pageNum: 1,
  265. pageSize: 10,
  266. nickName: undefined,
  267. phonenumber: undefined,
  268. status: undefined,
  269. deptId: undefined,
  270. },
  271. // 列信息
  272. columns: [
  273. { key: 0, label: `用户编号`, visible: true },
  274. { key: 1, label: `用户名称`, visible: true },
  275. { key: 2, label: `用户昵称`, visible: true },
  276. { key: 3, label: `部门`, visible: true },
  277. { key: 4, label: `手机号码`, visible: true },
  278. { key: 5, label: `状态`, visible: true },
  279. { key: 6, label: `创建时间`, visible: true },
  280. ],
  281. radioSelected: null, // 单选框传值
  282. selectUserList: [], // 回显数据传值
  283. checkList: [],
  284. };
  285. },
  286. watch: {
  287. // 根据名称筛选部门树
  288. deptName(val) {
  289. this.$refs.tree.filter(val);
  290. },
  291. // selectValues: {
  292. // handler(newVal) {
  293. // if (StrUtil.isNotBlank(newVal)) {
  294. // if (newVal instanceof Number) {
  295. // this.radioSelected = newVal;
  296. // } else {
  297. // this.selectUserList = newVal;
  298. // }
  299. // }
  300. // },
  301. // immediate: true,
  302. // },
  303. // selectNameValues: {
  304. // handler(newVal) {
  305. // if (StrUtil.isNotBlank(newVal)) {
  306. // this.selectNameValues = newVal;
  307. // // console.log("选中的userId=", this.selectUserList);
  308. // // console.log("选中的昵称=", newVal);
  309. // let userIdList = this.selectUserList.split(",");
  310. // let nickNameList = newVal.split(",");
  311. // // console.log(userIdList,nickNameList)
  312. // for (let i in userIdList) {
  313. // this.handleCheckList(userIdList[i], nickNameList[i]);
  314. // console.log("2222");
  315. // }
  316. // }
  317. // },
  318. // immediate: true,
  319. // },
  320. userList: {
  321. handler(newVal) {
  322. if (StrUtil.isNotBlank(newVal) && newVal.length > 0) {
  323. if (this.selectUserList && this.selectUserList.length > 0) {
  324. this.$nextTick(() => {
  325. this.$refs.dataTable.clearSelection();
  326. // console.log("this.selectUserList=", this.selectUserList);
  327. this.selectUserList?.split(",").forEach((key) => {
  328. let item = newVal.find((item) => key == item.userId);
  329. if (item) {
  330. this.$refs.dataTable.toggleRowSelection(item, true);
  331. this.handleCheckList(item.userId, item.nickName);
  332. }
  333. });
  334. });
  335. } else {
  336. this.$refs.dataTable.clearSelection();
  337. }
  338. }
  339. },
  340. immediate: true, // 立即生效
  341. deep: true, //监听对象或数组的时候,要用到深度监听
  342. },
  343. userVisible(newVal, oldVal) {
  344. if (newVal === true) {
  345. this.initialize();
  346. }
  347. },
  348. },
  349. // beforeDestroy() {
  350. // console.log("销毁组件1");
  351. // },
  352. // destroyed() {
  353. // console.log("销毁组件2");
  354. // },
  355. created() {
  356. // this.getList();
  357. // this.getDeptTree();
  358. },
  359. methods: {
  360. initialize() {
  361. this.checkList = [];
  362. // 遮罩层
  363. this.loading = true;
  364. // 选中数组
  365. // this.ids = [];
  366. // 非单个禁用
  367. this.single = true;
  368. // 非多个禁用
  369. this.multiple = true;
  370. // 显示搜索条件
  371. this.showSearch = true;
  372. // 总条数
  373. this.total = 0;
  374. // 用户表格数据
  375. // this.userList = [];
  376. // 弹出层标题
  377. this.title = "";
  378. // 部门树选项
  379. this.deptOptions = undefined;
  380. // 是否显示弹出层
  381. this.open = false;
  382. // 部门名称
  383. this.deptName = undefined;
  384. // 表单参数
  385. this.form = {};
  386. this.defaultProps = {
  387. children: "children",
  388. label: "label",
  389. };
  390. // 查询参数
  391. this.queryParams = {
  392. pageNum: 1,
  393. pageSize: 10,
  394. nickName: undefined,
  395. phonenumber: undefined,
  396. status: undefined,
  397. deptId: undefined,
  398. };
  399. this.getList();
  400. this.getDeptTree();
  401. if (this.selectValues) {
  402. this.selectUserList = this.selectValues;
  403. // console.log("选中的userId=", this.selectUserList);
  404. // console.log("选中的昵称=", newVal);
  405. let userIdList = this.selectUserList.split(",");
  406. let nickNameList = this.selectNameValues.split(",");
  407. // console.log("接收的数据", userIdList, nickNameList);
  408. for (let i in userIdList) {
  409. this.handleCheckList(userIdList[i], nickNameList[i]);
  410. }
  411. } else {
  412. this.selectUserList = null;
  413. this.checkList = [];
  414. if (this.$refs.dataTable) {
  415. this.$refs.dataTable.clearSelection();
  416. }
  417. }
  418. },
  419. clickRow(row) {
  420. this.$refs.dataTable.toggleRowSelection(row);
  421. },
  422. /** 查询用户列表 */
  423. getList() {
  424. this.loading = true;
  425. listUserNew(this.queryParams).then((response) => {
  426. this.userList = response.rows;
  427. this.total = response.total;
  428. this.loading = false;
  429. });
  430. },
  431. /** 查询部门下拉树结构 */
  432. getDeptTree() {
  433. deptTreeSelect().then((response) => {
  434. this.deptOptions = response.data;
  435. });
  436. },
  437. handleCheckList(userId, nickName) {
  438. this.checkList.push({
  439. userId: userId.toString(),
  440. nickName: nickName,
  441. });
  442. // console.log("========未去重数组", this.checkList);
  443. let obj = {};
  444. let nArr = this.checkList.reduce((cur, next) => {
  445. obj[next.userId] ? "" : (obj[next.userId] = true && cur.push(next));
  446. return cur;
  447. }, []);
  448. this.checkList = nArr;
  449. // console.log("现在的checkList=", this.checkList);
  450. },
  451. handleUserVisible(userVisible) {
  452. this.userVisible = userVisible;
  453. },
  454. /*用户选中赋值*/
  455. checkUserComplete() {
  456. this.userVisible = false;
  457. // this.checkType = "";
  458. },
  459. // 保存选中的数据id,row-key就是要指定一个key标识这一行的数据
  460. getRowKey(row) {
  461. return row.id;
  462. },
  463. // 筛选节点
  464. filterNode(value, data) {
  465. if (!value) return true;
  466. return data.label.indexOf(value) !== -1;
  467. },
  468. // 节点单击事件
  469. handleNodeClick(data) {
  470. this.queryParams.deptId = data.id;
  471. this.handleQuery();
  472. },
  473. handleSelect(selection, row) {
  474. let posIndex;
  475. let flag = this.checkList.some((item, index) => {
  476. if (item.userId === row.userId.toString()) {
  477. posIndex = index;
  478. return true;
  479. }
  480. });
  481. if (flag) {
  482. this.checkList.splice(posIndex, 1);
  483. }
  484. },
  485. // 多选框选中数据
  486. handleMultipleUserSelect(selection) {
  487. for (let i in selection) {
  488. this.handleCheckList(selection[i].userId, selection[i].nickName);
  489. }
  490. this.$emit("handleUserSelect", this.checkList);
  491. },
  492. // 单选框选中数据
  493. handleSingleUserSelect(selection) {
  494. this.radioSelected = selection.userId; //点击当前行时,radio同样有选中效果
  495. this.$emit("handleUserSelect", selection);
  496. },
  497. /** 搜索按钮操作 */
  498. handleQuery() {
  499. this.queryParams.pageNum = 1;
  500. this.getList();
  501. },
  502. /** 重置按钮操作 */
  503. resetQuery() {
  504. this.dateRange = [];
  505. this.resetForm("queryForm");
  506. this.queryParams.deptId = undefined;
  507. this.$refs.tree.setCurrentKey(null);
  508. this.handleQuery();
  509. },
  510. },
  511. };
  512. </script>
  513. <style>
  514. /*隐藏radio展示的label及本身自带的样式*/
  515. /*.el-radio__label{*/
  516. /* display:none;*/
  517. /*}*/
  518. </style>