selectSectionStaff.js 7.9 KB

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