selectSectionStaff.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. var app = getApp()
  2. var that;
  3. Component({
  4. mixins: [],
  5. data: {
  6. userList: [],//人员集合
  7. all_user_list: [],//人员集合
  8. isShow: false,
  9. showId: 0,//显示的部门ID下员工
  10. terr: [
  11. { name: '全部', dept_id: 0 }
  12. ],
  13. all_rule_list: [],
  14. rule_tree: [],
  15. selectUserId: [],
  16. isAllChecked: false,
  17. isAll: true,
  18. userData: [],
  19. isCreator: false,
  20. keyVal: '',
  21. },
  22. props: {
  23. onClose: (data) => console.log(data),
  24. onConfirm: (data) => console.log(data),
  25. selectUser: [],
  26. isAllSelect: true,//是否多选
  27. isLeadership: false,//数据源是否是自己的管理范围
  28. isBoss: false,//是否去除创始人
  29. isUser: false,//是否去除自己
  30. },
  31. didMount() {
  32. that = this;
  33. that.getData(0);
  34. that.getBmData();
  35. that.setData({
  36. isCreator: app.globalData.isCreator,
  37. selectUserId: that.props.selectUser[0] ? that.props.selectUser : [],
  38. isAll: that.props.isAllSelect,
  39. // userData: app.globalData.userData.employee_detail.manage_scope,
  40. terr: [{ name: '全部', dept_id: 0 }]
  41. })
  42. },
  43. didUpdate() { },
  44. didUnmount() {
  45. },
  46. methods: {
  47. onReset() {
  48. this.setData({
  49. isVal: false,
  50. keyVal: ''
  51. })
  52. },
  53. onConfirm() {
  54. if (this.data.selectUserId.length == 0) {
  55. app.globalData.showToast("请选择一名员工")
  56. return
  57. }
  58. this.props.onConfirm(this.data.selectUserId);
  59. this.onClose();
  60. },
  61. //搜索
  62. bindKeyInput(e) {
  63. this.setData({
  64. keyVal: e.detail.value,
  65. isVal: e.detail.value ? true : false
  66. })
  67. },
  68. changeSection(e) {
  69. var item = e.target.dataset.item;
  70. var terr = this.data.terr;
  71. terr.push(item);
  72. if (item._child.length > 0) {
  73. that.setData({
  74. rule_tree: item._child,
  75. terr: terr,
  76. isAllChecked: false
  77. })
  78. } else {
  79. that.setData({
  80. rule_tree: [],
  81. terr: terr,
  82. isAllChecked: false
  83. })
  84. }
  85. this.getData(item.dept_id);
  86. },
  87. //点击导航栏
  88. activeItem(e) {
  89. var item = e.target.dataset.item;
  90. var index = e.target.dataset.index;
  91. var terr = this.data.terr;
  92. if ((index + 1) == terr.length) { return false };
  93. this.setData({ rule_tree: [] });
  94. if (index == 0) {
  95. this.setData({
  96. terr: [{ name: '全部', dept_id: 0 }],
  97. rule_tree: this.data.all_rule_list,
  98. isAllChecked: false
  99. })
  100. } else {
  101. var arr = terr.slice(0, index + 1);
  102. this.setData({
  103. rule_tree: item._child,
  104. terr: arr,
  105. isAllChecked: false
  106. })
  107. }
  108. this.getData(item.dept_id);
  109. },
  110. // 点击下一级
  111. openDown(e) {
  112. this.setData({ rule_tree: [] });
  113. var item = e.target.dataset.item;
  114. var terr = this.data.terr;
  115. terr.push(item);
  116. if (item._child.length > 0) {
  117. that.setData({
  118. rule_tree: item._child,
  119. terr: terr,
  120. isAllChecked: false
  121. })
  122. }
  123. this.getData(item.dept_id);
  124. },
  125. getArrDifference(arr1, arr2) {
  126. var obj = {};
  127. arr1.concat(arr2).forEach(function (v, i, arr) {
  128. obj[v.id] = v;
  129. });
  130. return obj;
  131. },
  132. //全选择
  133. ruleActiveAll(e) {
  134. var userList = this.data.userList;
  135. var selectUserId = this.data.selectUserId;
  136. if (e.detail.value) {
  137. userList.map(item => {
  138. item.checked = true;
  139. })
  140. if (selectUserId.length > 0) {
  141. var obj = this.getArrDifference(selectUserId, userList);
  142. this.setData({
  143. selectUserId: Object.values(obj),
  144. userList: userList,
  145. isAllChecked: true
  146. })
  147. } else {
  148. this.setData({
  149. selectUserId: userList,
  150. userList: userList,
  151. isAllChecked: true
  152. })
  153. }
  154. } else {
  155. userList.map(item => {
  156. item.checked = false;
  157. })
  158. this.setData({
  159. userList: userList,
  160. selectUserId: [],
  161. isAllChecked: false
  162. })
  163. }
  164. },
  165. //选择
  166. radioChange: function (e) {
  167. var selectUserId = this.data.selectUserId;
  168. var isExist = false;
  169. var userList = this.data.userList;
  170. if (!this.data.isAll) {//单选择
  171. let item = e.target.dataset.item;
  172. let index = e.target.dataset.index;
  173. let checked = `userList[${index}].checked`;//获取当前控制选中的字段
  174. let elm = this.data.userList[index].checked;//当前的状态
  175. //将所有的选中状态改成未中
  176. var userList = this.data.userList;
  177. userList.map((item, item_index) => {
  178. if (item.checked) {
  179. var item_checked = `userList[${item_index}].checked`;
  180. this.setData({
  181. [item_checked]: false
  182. })
  183. }
  184. })
  185. if (!elm) {
  186. selectUserId.forEach((element, _this) => {
  187. if (element.id == item.id) {
  188. isExist = true;
  189. }
  190. });
  191. if (!isExist) {
  192. selectUserId = [];
  193. selectUserId.push(item);
  194. }
  195. } else {//取消选中
  196. selectUserId = [];
  197. }
  198. this.setData({
  199. [checked]: !elm,
  200. })
  201. } else {//多选择
  202. let item = e.target.dataset.item;
  203. let index = e.target.dataset.index;
  204. let checked = `userList[${index}].checked`;//获取当前控制选中的字段
  205. let elm = this.data.userList[index].checked;//当前的状态
  206. this.setData({
  207. [checked]: !elm,
  208. })
  209. if (e.detail.value) {//当有值时选中
  210. selectUserId.forEach((element, _this) => {
  211. if (element.id == item.id) {
  212. isExist = true;
  213. }
  214. });
  215. if (!isExist) {
  216. selectUserId.push(item);
  217. }
  218. } else {//取消选中
  219. selectUserId.forEach((element, index, _this) => {
  220. if (element.id == item.id) {
  221. _this.splice(index, 1);
  222. }
  223. });
  224. }
  225. }
  226. this.setData({
  227. selectUserId: selectUserId
  228. })
  229. },
  230. //关闭
  231. onClose(e) {
  232. this.props.onClose();
  233. },
  234. //获取人员列表
  235. getData(dept_id) {
  236. this.setData({
  237. userList: [],
  238. all_user_list: []
  239. })
  240. app.$get("api/per/user/employee_list", { dept_id: dept_id,page:0,pageSize:0 }).then((res) => {
  241. var list = res.data.data.list || [];
  242. var selectUserId = this.data.selectUserId;
  243. var arr = [];
  244. var userList = [];
  245. list.forEach(item => {
  246. if (this.props.isBoss && item.is_creator == 1) {
  247. return null
  248. } else if (this.props.isUser && item.id == app.globalData.userData.id) {
  249. return null
  250. } else {
  251. userList.push(item)
  252. }
  253. });
  254. userList.map((item) => { //设置列表的选中状态
  255. item.checked = false;
  256. if (selectUserId.length > 0) {
  257. selectUserId.forEach(item2 => {
  258. if (item.id == item2.id) {
  259. item.checked = true;
  260. }
  261. })
  262. }
  263. })
  264. // that.data.userData.map((item) => {//列表数据是否是自己的管理范围
  265. // userList.map((item2) => {
  266. // if (item.id == item2.id) {
  267. // arr.push(item2);
  268. // }
  269. // })
  270. // })
  271. this.setData({
  272. userList: that.props.isLeadership ? arr : userList,
  273. all_user_list: that.props.isLeadership ? arr : userList,
  274. })
  275. })
  276. },
  277. //获取部门列表
  278. getBmData() {
  279. app.$get("api/per/user/department", {}).then((res) => {
  280. var list = res.data.data.dept_tree;
  281. this.setData({
  282. rule_tree: list,
  283. all_rule_list: list
  284. })
  285. })
  286. },
  287. },
  288. });