userPicker.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. mbui.define(['layer'], function (exports) {
  2. var layer = mbui.layer;
  3. var userSelector = function () {
  4. this.config = {
  5. type:1,
  6. url: "/api/index/get_employee",
  7. where: {},
  8. callback: function(){}
  9. };
  10. this.loaded = 0;
  11. };
  12. // 初始化
  13. userSelector.prototype.init = function (options) {
  14. var that = this;
  15. $.extend(true, that.config, options);
  16. that.select();
  17. };
  18. userSelector.prototype.select = function () {
  19. var that = this;
  20. var types = that.config.type == 2 ? 'checkbox' : 'radio';
  21. var users = [];
  22. var alphabet = [];
  23. var userGroups = {};
  24. var selector = $('.user-selector');
  25. if (selector.length > 0) {
  26. return false;
  27. }
  28. let loading;
  29. $.ajax({
  30. url: that.config.url,
  31. type: 'get',
  32. data: { did: 0 },
  33. beforeSend:function(){
  34. loading = layer.loading('加载中...');
  35. },
  36. success: function (e) {
  37. if (e.code == 0) {
  38. users = e.data;
  39. bulidSelector();
  40. }
  41. },
  42. complete:function(){
  43. layer.close(loading);
  44. }
  45. })
  46. function bulidSelector() {
  47. // 将用户账号按照首字母分组
  48. for (var i = 0; i < users.length; i++) {
  49. var user = users[i]['username'];
  50. var firstLetter = user.charAt(0).toUpperCase();
  51. if (!userGroups[firstLetter]) {
  52. userGroups[firstLetter] = [];
  53. alphabet.push(firstLetter);
  54. }
  55. userGroups[firstLetter].push(users[i]);
  56. }
  57. alphabet.sort();
  58. var $container = $('<div class="mbui-picker-selector"><header class="mbui-bar"><a class="mbui-bar-item left" href="javascript:;"><i class="mbui-bar-arrow-left"></i>关闭</a><a class="mbui-bar-item right text-blue" href="javascript:;">确认</a><h1 class="mbui-bar-title">选择员工</h1></header></div>');
  59. var $letters = $('<div class="letters"></div>');
  60. var $userList = $('<div class="contacts mbui-' + types + '"></div>');
  61. var item = '';
  62. for (var j = 0; j < alphabet.length; j++) {
  63. var $letterLink = $('<a href="#' + alphabet[j] + '">' + alphabet[j] + '</a>');
  64. $letters.append($letterLink);
  65. item += '<div class="mbui-contacts-title" id="' + alphabet[j] + '">' + alphabet[j] + '</div>';
  66. var userData = userGroups[alphabet[j]];
  67. for (var k = 0; k < userData.length; k++) {
  68. item += '<label class="mbui-picker-item" data-id="' + userData[k]['id'] + '" data-did="' + userData[k]['did'] + '" data-name="' + userData[k]['name'] + '" data-department="' + userData[k]['department'] + '">\
  69. <input class="mbui-input-'+types+'" name="radio_user[]" type="'+types+'" value="' + userData[k]['id'] + '">\
  70. <i class="mbui-picker-avatar"><img src="' + userData[k]['thumb'] + '" width="32" height="32" align="'+userData[k]['name']+'" style="border-radius:4px;"></i>\
  71. <span class="mbui-picker-name">' + userData[k]['name'] + ' <span class="text-gray f14">『' + userData[k]['department'] + '』</span></span>\
  72. </label>';
  73. }
  74. }
  75. $userList.append(item);
  76. $container.append($letters).append($userList);
  77. $('body').append($container);
  78. // 点击字母滚动到对应的字母开头的用户位置
  79. $letters.on('click', 'a', function (e) {
  80. e.preventDefault();
  81. var letter = $(this).attr('href').substring(1);
  82. var $letterSection = $('#' + letter);
  83. var scrollTop = $letterSection.offset().top - $userList.offset().top + $userList.scrollTop();
  84. $userList.animate({ scrollTop: scrollTop }, 500);
  85. });
  86. $container.find('.left').click(function () {
  87. $container.fadeOut(function () {
  88. $container.remove();
  89. });
  90. });
  91. $container.find('.right').click(function () {
  92. let selected = $container.find('input:checked');
  93. if (selected.length == 0) {
  94. layer.msg('请选择员工');
  95. return false;
  96. }
  97. let ids = [], names = [], dids = [],departments=[];
  98. for (var m = 0; m < selected.length; m++) {
  99. let selected_item = $(selected[m]).parent();
  100. ids.push(selected_item.data('id'));
  101. names.push(selected_item.data('name'));
  102. dids.push(selected_item.data('did'));
  103. departments.push(selected_item.data('department'));
  104. }
  105. that.config.callback(ids,names,dids,departments);
  106. $container.fadeOut(function () {
  107. $container.remove();
  108. });
  109. });
  110. }
  111. };
  112. //选择员工弹窗
  113. $('body').on('click','.picker-admin',function () {
  114. let that = $(this);
  115. let type = that.data('type');
  116. let sub = that.data('sub');
  117. if (typeof(type) == "undefined" || type == '') {
  118. type = 1;
  119. }
  120. if (typeof(sub) == "undefined" || sub == '') {
  121. sub = 0;
  122. }
  123. let ids=that.next().val()+'',names = that.val()+'';
  124. let picker = new userSelector();
  125. if(sub ==1){
  126. picker.init({
  127. type:type,
  128. url:'/api/index/get_employee_sub',
  129. callback:function(ids,names,dids,departments){
  130. that.val(names.join(','));
  131. that.next().val(ids.join(','));
  132. that.next().next().val(dids.join(','));
  133. }
  134. });
  135. }
  136. else{
  137. picker.init({
  138. type:type,
  139. callback:function(ids,names,dids,departments){
  140. that.val(names.join(','));
  141. that.next().val(ids.join(','));
  142. that.next().next().val(dids.join(','));
  143. }
  144. });
  145. }
  146. });
  147. // 导出userPicker模块
  148. exports('userPicker', function (options) {
  149. var userPicker = new userSelector();
  150. userPicker.init(options);
  151. });
  152. });