EmployeeSelector.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. <template>
  2. <div>
  3. <div class="employee_selector_box">
  4. <el-row :gutter="10">
  5. <el-col :xs="11" :sm="11" :md="11" :lg="11" :xl="11" class="scroller-box one">
  6. <div class="search-box">
  7. <div class="search flex-box" style="margin-bottom: 7px;"><el-input v-model="keyword" placeholder="请输入姓名" clearable></el-input></div>
  8. <div class="search flex-box" v-if="can_select_dept">
  9. <el-cascader
  10. v-model="dept_id"
  11. ref="dept"
  12. :options="dept_list"
  13. :props="{ checkStrictly: true, value: 'id', label: 'name', children: '_child' }"
  14. clearable
  15. filterable
  16. placeholder="全公司"
  17. ></el-cascader>
  18. </div>
  19. </div>
  20. <div class="flex-box-end" style="padding: 10px;border-bottom: 1px solid #f1f1f1;height: 40px;box-sizing: border-box;">
  21. <el-checkbox v-if="isChecKedAll && multi" v-model="checked" @change="checkedChange" :indeterminate="indeterminate">全选</el-checkbox>
  22. </div>
  23. <el-scrollbar :style="{ height: can_select_dept ? '310px' : '350px' }" :native="false" v-loading="table_loading">
  24. <div class="choose_left">
  25. <div class="persons_box" style="padding-top:4px;" v-if="list.length > 0">
  26. <a class="employee_cell_a" href="javascript:void(0)" v-for="(item, index) in list" :key="index">
  27. <div
  28. class="employee_cell flex-box flex-v-ce"
  29. v-if="can_select_employee && item.name.indexOf(keyword) >= 0"
  30. @click.prevent.stop="select_employee(item)"
  31. >
  32. <div class="employee_checkbox"><el-checkbox v-model="item.checked"></el-checkbox></div>
  33. <userImage :user_name="item.name" :img_url="item.img_url" width="30px" height="30px"></userImage>
  34. <div class="employee_name">{{ item.name }}</div>
  35. <div class="employee_cell_clear"></div>
  36. </div>
  37. </a>
  38. </div>
  39. <div v-else class="fontColorF" style="text-align: center;margin-top: 50px;">暂无可选人员</div>
  40. </div>
  41. </el-scrollbar>
  42. </el-col>
  43. <el-col :xs="2" :sm="2" :md="2" :lg="2" :xl="2" class="col-line"><span style="opacity: 0;">空</span></el-col>
  44. <el-col :xs="11" :sm="11" :md="11" :lg="11" :xl="11" class="scroller-box">
  45. <div class="option-box flex-box flex-v-ce">
  46. <div class="fontColorF flex-1" v-show="multi">已选择{{ employee_selected_list.length }}个员工</div>
  47. <el-button type="type" @click="clear_data()">清空</el-button>
  48. </div>
  49. <el-scrollbar wrap-class="column-wrapper scrollable-items-container" :native="false" style="height: 390px;">
  50. <div class="choose_right">
  51. <div class="employee_cell flex-box flex-v-ce" v-for="(item, index) in dept_selected_list" :key="index">
  52. <div class="employee_name">{{ item.dept_name }}</div>
  53. <div class="employee_delete"><el-button type="default" size="mini" @click="dept_cancel(item)" icon="el-icon-close" circle></el-button></div>
  54. <div class="employee_cell_clear"></div>
  55. </div>
  56. <div class="employee_cell" v-for="(item, index) in employee_selected_list" :key="index">
  57. <userImage :user_name="item.name" :img_url="item.img_url" width="30px" height="30px"></userImage>
  58. <div class="employee_name">{{ item.name }}</div>
  59. <div class="employee_delete">
  60. <el-button type="default" size="mini" @click="employee_cancel(item, true)" icon="el-icon-close" circle></el-button>
  61. </div>
  62. <div class="employee_cell_clear"></div>
  63. </div>
  64. </div>
  65. </el-scrollbar>
  66. </el-col>
  67. </el-row>
  68. </div>
  69. </div>
  70. </template>
  71. <script>
  72. export default {
  73. props: {
  74. can_select_dept: {
  75. //指定是否能选择部门
  76. type: Boolean,
  77. default: true
  78. },
  79. can_select_employee: {
  80. //指定是否能选择员工
  81. type: Boolean,
  82. default: true
  83. },
  84. selected: {
  85. //已经选择的员工和部门
  86. type: Object,
  87. default: () => {
  88. return {
  89. employee: [],
  90. dept: []
  91. };
  92. }
  93. },
  94. multi: {
  95. //指定员工是否能多选
  96. type: Boolean,
  97. default: true
  98. },
  99. dept_multi: {
  100. //指定部门是否能多选
  101. type: Boolean,
  102. default: true
  103. },
  104. employee_list: {
  105. //指定显示的员工列表,注意:传值后组件的员工数据将不是统一向服务器获取的,而是指定的员工列表,格式是:[{id: 1, name: “张三”, img_url: “”}]
  106. type: Array,
  107. default: () => {
  108. return [];
  109. }
  110. },
  111. close_clear_data: {
  112. //关闭时是否清空选择的数据
  113. type: Boolean,
  114. default: true
  115. },
  116. max: {
  117. //当multi为true时,用来限制选择人数,0不生效
  118. type: Number,
  119. default: 0
  120. },
  121. user_no_select: {
  122. //是否能选择创始人
  123. type: Boolean,
  124. default: true
  125. },
  126. isChecKedAll: {
  127. //是否能全人员
  128. type: Boolean,
  129. default: true
  130. },
  131. my_no_select: {
  132. //当前设置人的id,过滤某位人员,一般为自己
  133. type: Number,
  134. default: 0
  135. },
  136. isCreatorSelect: {
  137. //当employee_list为空时,依然显示空值而不是显示人员列表
  138. type: Boolean,
  139. default: false
  140. },
  141. use_Administrator_list: {
  142. //未指定员工并请求人员接口时,保留管理员,过滤员工
  143. type: Boolean,
  144. default: false
  145. }
  146. },
  147. name: 'EmployeeSelector',
  148. data() {
  149. let selected = JSON.parse(JSON.stringify(this.selected));
  150. let user_no_select = JSON.parse(JSON.stringify(this.user_no_select));
  151. return {
  152. table_loading: false,
  153. employee_selected_list: selected.employee,
  154. dept_selected_list: selected.dept,
  155. employee_selected: [],
  156. dept_selected: [],
  157. com_height: '100%',
  158. list: [], //人员列表
  159. dept_name: '', //部门名称
  160. dept_id: 0, //部门ID
  161. pid: 0, //部门ID实传
  162. dept_list: [], //部门列表
  163. dept_list_arr: [], //收集的部门
  164. keyword: '', //搜索值
  165. checked: false, //是否全选
  166. indeterminate: false,
  167. not_user: user_no_select,
  168. all_list: []
  169. };
  170. },
  171. watch: {
  172. dept_id(val) {
  173. if (val.length > 1) {
  174. this.pid = val[val.length - 1];
  175. } else {
  176. this.pid = val[0];
  177. }
  178. this.$nextTick(() => {
  179. this.$refs.dept.dropDownVisible = false;
  180. this.get_user_list();
  181. this.checked = false;
  182. });
  183. },
  184. selected(val) {
  185. let selected = JSON.parse(JSON.stringify(val));
  186. this.employee_selected = [];
  187. this.dept_selected = [];
  188. this.employee_selected_list = selected.employee;
  189. this.dept_selected_list = selected.dept;
  190. for (let i in selected.employee) {
  191. this.employee_selected.push(selected.employee[i].id);
  192. }
  193. for (let i in selected.dept) {
  194. this.dept_selected.push(selected.dept[i].dept_id);
  195. }
  196. },
  197. //搜索
  198. keyword(val) {
  199. this.checked = false;
  200. }
  201. },
  202. methods: {
  203. //全选择
  204. checkedChange(val) {
  205. if (val) {
  206. for (let i in this.list) {
  207. this.$set(this.list[i], 'checked', true);
  208. }
  209. this.$nextTick(() => {
  210. let employeeSelectedList = JSON.parse(JSON.stringify(this.employee_selected_list));
  211. let employeeSelectedIds = [],
  212. total = 0;
  213. employeeSelectedList.forEach(element => {
  214. employeeSelectedIds.push(element.id);
  215. });
  216. for (const i in this.list) {
  217. if (employeeSelectedIds.includes(this.list[i].id)) {
  218. total += 1;
  219. } else {
  220. this.employee_selected_list.push(this.list[i]);
  221. }
  222. }
  223. this.list.length == total ? (this.indeterminate = true) : (this.indeterminate = false);
  224. });
  225. } else {
  226. this.clear_data();
  227. }
  228. },
  229. //添加或移除已添加的员工
  230. employee_cancel(item) {
  231. this.employee_selected_list.map((arr, index) => {
  232. if (arr.id == item.id) {
  233. this.employee_selected_list.splice(index, 1);
  234. }
  235. });
  236. this.list.map((arr, index) => {
  237. if (arr.id == item.id) {
  238. this.$set(arr, 'checked', false);
  239. }
  240. });
  241. },
  242. //获取部门
  243. get_dept_list() {
  244. this.$axios('get', '/api/department/tree').then(res => {
  245. this.dept_list = this.getTreeData(res.data.data.list);
  246. });
  247. },
  248. // 递归判断列表,把最后的children设为undefined
  249. getTreeData(data) {
  250. for (var i = 0; i < data.length; i++) {
  251. if (data[i]._child.length < 1) {
  252. // children若为空数组,则将children设为undefined
  253. data[i]._child = undefined;
  254. } else {
  255. // children若不为空数组,则继续 递归调用 本方法
  256. this.getTreeData(data[i]._child);
  257. }
  258. }
  259. return data;
  260. },
  261. //选择部门
  262. dept_cancel(item) {
  263. this.dept_selected.splice(this.dept_selected.indexOf(item.id), 1);
  264. let delete_index = -1;
  265. for (let i in this.dept_selected_list) {
  266. if (this.dept_selected.indexOf(this.dept_selected_list[i].id) < 0) {
  267. delete_index = i;
  268. }
  269. }
  270. if (delete_index >= 0) {
  271. this.dept_selected_list.splice(delete_index, 1);
  272. for (let i in this.dept_list) {
  273. if (this.dept_list[i].dept_id == item.id) {
  274. this.$set(this.dept_list[i], 'checked', false);
  275. }
  276. }
  277. }
  278. },
  279. //选择员工
  280. select_employee(item) {
  281. if (item.checked) {
  282. item.checked = false;
  283. this.employee_cancel(item);
  284. } else {
  285. if (!this.multi) {
  286. this.employee_selected = [];
  287. this.employee_selected_list = [];
  288. for (let i in this.list) {
  289. this.$set(this.list[i], 'checked', false);
  290. }
  291. }
  292. if (this.max > 0 && this.employee_selected_list.length == this.max && this.multi) {
  293. this.$message.error('最多只能选择' + this.max + '人');
  294. return false;
  295. }
  296. this.employee_selected.push(item.id);
  297. this.employee_selected_list.push({
  298. id: item.id,
  299. name: item.name,
  300. img_url: item.img_url
  301. });
  302. item.checked = true;
  303. }
  304. },
  305. clear_data() {
  306. this.employee_selected = [];
  307. this.employee_selected_list = [];
  308. this.dept_selected = [];
  309. this.dept_selected_list = [];
  310. this.checked = false;
  311. for (let i in this.list) {
  312. this.$set(this.list[i], 'checked', false);
  313. }
  314. for (let i in this.dept_list) {
  315. this.$set(this.dept_list[i], 'checked', false);
  316. }
  317. },
  318. close() {
  319. if (this.close_clear_data) {
  320. this.employee_selected = [];
  321. this.employee_selected_list = [];
  322. this.dept_selected = [];
  323. this.dept_selected_list = [];
  324. for (let i in this.list) {
  325. this.$set(this.list[i], 'checked', false);
  326. }
  327. for (let i in this.dept_list) {
  328. this.$set(this.dept_list[i], 'checked', false);
  329. }
  330. }
  331. },
  332. confirm() {
  333. this.$emit('confirm', {
  334. employee: this.employee_selected_list,
  335. dept: this.dept_selected_list
  336. });
  337. this.close();
  338. },
  339. filtration() {
  340. //获取员工列表,限制已禁用的员工
  341. this.$axios('get', '/api/employee/index', { dept_id: this.pid, page: 0, page_size: 3000,is_official:1 })
  342. .then(res => {
  343. if (res.data.code == 1) {
  344. let list = res.data.data.list;
  345. let data = [];
  346. // list.forEach((item, index) => {
  347. // if (item.is_official == 1) {
  348. // data.push(item);
  349. // }
  350. // });
  351. this.userdatars(list);
  352. } else {
  353. this.userdatars();
  354. }
  355. })
  356. .catch(() => {
  357. this.userdatars();
  358. });
  359. },
  360. userdatars(userlist) {
  361. var employee_selected_list = this.employee_selected_list;
  362. let list = this.employee_list;
  363. if (userlist) {
  364. list = [...userlist].filter(x => [...list].some(y => y.id === x.id));
  365. }
  366. list.map(item => {
  367. item['checked'] = false;
  368. for (var i in employee_selected_list) {
  369. //判断传进来的员工是否是员工集合里的,是就设为已点击状态
  370. if (employee_selected_list[i].id == item.id) {
  371. item['checked'] = true;
  372. }
  373. }
  374. });
  375. this.list = list;
  376. this.table_loading = false;
  377. },
  378. //获取员工详情
  379. get_user_list() {
  380. this.table_loading = true;
  381. var that = this;
  382. var employee_selected_list = this.employee_selected_list;
  383. if (this.employee_list.length > 0 || this.isCreatorSelect) {
  384. this.filtration();
  385. return false;
  386. }
  387. this.list = [];
  388. this.$axios('get', '/api/employee/index', { dept_id: this.pid, keywords: this.keyword, page: 0, page_size: 3000,is_official:1 })
  389. .then(res => {
  390. var employee = res.data.data.list || [];
  391. if (this.use_Administrator_list) {
  392. //使用管理员列表
  393. employee = this.useAdministratorList(employee);
  394. }
  395. var userAll = [];
  396. employee.forEach((item, i) => {
  397. if (!this.not_user && item.is_creator == 1) {
  398. //过滤创始人
  399. return null;
  400. } else if (this.my_no_select == item.id) {
  401. //过滤掉当前设置人
  402. return null;
  403. } else if (item.id) {
  404. userAll.push(item);
  405. }
  406. // 前端先隐藏过滤未开启的人员的功能
  407. // } else if (item.is_official == 1) {
  408. // userAll.push(item);
  409. // }
  410. });
  411. userAll.map(item => {
  412. item['checked'] = false;
  413. for (var i in employee_selected_list) {
  414. //判断传进来的员工是否是员工集合里的,是就设为已点击状态
  415. if (employee_selected_list[i].id == item.id) {
  416. item['checked'] = true;
  417. }
  418. }
  419. });
  420. that.list = userAll;
  421. })
  422. .finally(() => {
  423. that.table_loading = false;
  424. });
  425. },
  426. useAdministratorList(list) {
  427. //过滤掉普通员工,返回管理员
  428. let arr = [];
  429. list.forEach((item, i) => {
  430. if (
  431. item.employee_detail.role_list.findIndex(
  432. value => value.name == 'dept_manager' || value.name == 'creator' || value.name == 'point_manager' || value.name == 'admin'
  433. ) >= 0
  434. ) {
  435. arr.push(item);
  436. }
  437. });
  438. return arr;
  439. }
  440. },
  441. created() {
  442. var that = this;
  443. that.checked = false;
  444. setTimeout(() => {
  445. that.get_user_list();
  446. if (that.can_select_dept) {
  447. that.get_dept_list();
  448. }
  449. }, 200);
  450. }
  451. };
  452. </script>
  453. <style scoped>
  454. a {
  455. cursor: pointer;
  456. color: inherit;
  457. text-decoration: none;
  458. }
  459. .employee_selector_box {
  460. width: 600px;
  461. padding: 20px;
  462. box-sizing: border-box;
  463. margin: 0 auto;
  464. }
  465. /* 搜索框 */
  466. .employee_selector_box .search {
  467. margin: 0 auto;
  468. width: 80%;
  469. }
  470. /* 左边框 */
  471. .employee_selector_box .choose_left {
  472. padding: 8px 10px 8px 10px;
  473. }
  474. .employee_selector_box .choose_left .employee_cell {
  475. border-bottom: 1px #f8f8f8 solid;
  476. }
  477. .employee_selector_box .choose_left .employee_cell_a .employee_cell {
  478. border-bottom: none;
  479. }
  480. .employee_selector_box .choose_left .employee_checkbox {
  481. padding-left: 8px;
  482. }
  483. .employee_selector_box .choose_left .employee_img_url {
  484. padding-left: 8px;
  485. }
  486. .employee_selector_box .choose_left .employee_name {
  487. padding-left: 8px;
  488. }
  489. /* .employee_selector_box ::v-deep .el-scrollbar__thumb {
  490. width: 2px;
  491. margin: 15px 0 0 6px;
  492. background: #409eff;
  493. } */
  494. /* 右边距 */
  495. .employee_selector_box .option-box {
  496. padding: 14px;
  497. }
  498. .employee_selector_box .choose_right {
  499. padding: 8px 10px;
  500. }
  501. .employee_selector_box .choose_right .employee_delete .el-button {
  502. transform: scale(0.5);
  503. background: #c0c4cc;
  504. }
  505. .employee_selector_box .choose_right .employee_delete .el-button ::v-deep .el-icon-close {
  506. color: #fff;
  507. }
  508. .employee_selector_box .choose_right .employee_delete .el-button:active {
  509. background: #409eff;
  510. }
  511. .employee_selector_box .choose_right .employee_delete .el-button:active ::v-deep .el-icon-close {
  512. color: #fff;
  513. }
  514. .employee_selector_box .choose_right .employee_cell .employee_name {
  515. padding-left: 8px;
  516. }
  517. .employee_selector_box .choose_right .employee_cell .employee_img_url {
  518. padding-left: 8px;
  519. }
  520. /* 右边距 */
  521. .employee_selector_box ::v-deep .employee_cell {
  522. padding: 8px 0;
  523. }
  524. .employee_selector_box ::v-deep .employee_cell:hover {
  525. background: #ecf5ff;
  526. }
  527. .employee_cell {
  528. display: -webkit-inline-box;
  529. cursor: pointer;
  530. width: 100%;
  531. color: #606266;
  532. padding: 8px 0;
  533. background-color: transparent;
  534. border-bottom: 1px #f1f1f1 solid;
  535. }
  536. .employee_cell .employee_cell_clear {
  537. clear: both;
  538. }
  539. .employee_cell .employee_img_url {
  540. display: table-cell;
  541. vertical-align: middle;
  542. width: 30px;
  543. padding-left: 0px;
  544. }
  545. .employee_cell .employee_img_url img {
  546. width: 24px;
  547. height: 24px;
  548. border-radius: 50%;
  549. }
  550. .employee_cell .employee_checkbox {
  551. display: table-cell;
  552. vertical-align: middle;
  553. width: 30px;
  554. text-align: center;
  555. }
  556. .employee_cell .employee_name {
  557. display: table-cell;
  558. vertical-align: middle;
  559. color: #606266;
  560. }
  561. .employee_delete {
  562. display: table-cell;
  563. text-align: right;
  564. padding-right: 0px;
  565. }
  566. .employee_cell_a {
  567. display: block;
  568. }
  569. .employee_cell_a:hover {
  570. background-color: #ecf5ff;
  571. border-radius: 4px;
  572. }
  573. .dept_child_cell {
  574. display: table-cell;
  575. vertical-align: middle;
  576. }
  577. .dept_child_cell span {
  578. float: right;
  579. padding-right: 8px;
  580. }
  581. .dept_child_cell span:hover {
  582. color: #1c1c1c;
  583. }
  584. .dept_child_cell span:active {
  585. color: #1c1c1c;
  586. }
  587. .scroller-box {
  588. height: 440px;
  589. padding: 0px !important;
  590. background-color: #fdfdfd;
  591. border: 1px solid #eee;
  592. border-radius: 4px;
  593. overflow: hidden;
  594. }
  595. /* .scroller-box.one{
  596. height: 440px;
  597. } */
  598. .col-line {
  599. position: relative;
  600. height: 400px;
  601. }
  602. .col-line:before {
  603. content: ' ';
  604. width: 1px;
  605. height: 400px;
  606. position: absolute;
  607. top: 0;
  608. bottom: 0;
  609. left: 50%;
  610. background-color: #eee;
  611. }
  612. .search-box {
  613. background-color: #fff;
  614. padding: 10px 5px;
  615. border-bottom: 1px solid #eee;
  616. }
  617. .option-box {
  618. padding: 5px;
  619. background-color: #fff;
  620. border-bottom: 1px solid #eee;
  621. margin: 0 -5px;
  622. }
  623. .child_btn {
  624. color: #409eff;
  625. }
  626. .dept_path {
  627. margin: 10px 0;
  628. display: block;
  629. white-space: nowrap;
  630. overflow: auto;
  631. }
  632. /*滚动条的宽度*/
  633. .dept_path::-webkit-scrollbar {
  634. width: 5px;
  635. height: 5px;
  636. }
  637. /*外层轨道。可以用display:none让其不显示,也可以添加背景图片,颜色改变显示效果*/
  638. .dept_path::-webkit-scrollbar-track {
  639. width: 6px;
  640. background-color: #fff0;
  641. -webkit-border-radius: 2em;
  642. -moz-border-radius: 2em;
  643. border-radius: 2em;
  644. }
  645. /*滚动条的设置*/
  646. .dept_path::-webkit-scrollbar-thumb {
  647. background-color: #fff0;
  648. background-clip: padding-box;
  649. min-height: 28px;
  650. -webkit-border-radius: 2em;
  651. -moz-border-radius: 2em;
  652. border-radius: 2em;
  653. }
  654. /*滚动条移上去的背景*/
  655. .dept_path:hover::-webkit-scrollbar-thumb {
  656. background-color: rgba(144, 147, 153, 0.3);
  657. }
  658. </style>