userSearchCtrl.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. angular.module('push')
  2. .controller('userSearchCtrl', function ($scope, $timeout, $stateParams, $http, UtilService, SearchResultService, UserService, AuthorityModel, AuthorityService, AuthorityModelService) {
  3. $scope.searchtype = $stateParams.searchtype;//0:搜索用户关注、1:搜索用户添加
  4. var isset = $stateParams.isset;//0:创建、1:设置
  5. var authorityid = $stateParams.authorityid;
  6. var authoritycomefrom = $stateParams.authoritycomefrom;
  7. var modeltype = $stateParams.modeltype;//0:新建权限(默认),1:新建组
  8. $scope.key = "";
  9. $scope.userid = UserService.id;
  10. $scope.showbaidu = false;
  11. //已选择用户id拼接
  12. var useridlist = "";
  13. var getUserIdList = function () {
  14. useridlist = "";
  15. if (modeltype == 0) {
  16. angular.forEach(AuthorityModel.getMemberList(), function (value, index) {
  17. var tempid = "'" + value.userId + "'";
  18. useridlist = useridlist + tempid + ",";
  19. });
  20. } else {
  21. angular.forEach(AuthorityModelService.getMemberList(), function (value, index) {
  22. var tempid = "'" + value.userId + "'";
  23. useridlist = useridlist + tempid + ",";
  24. });
  25. }
  26. // console.log(useridlist);
  27. };
  28. getUserIdList();
  29. //关注用户刷选
  30. var toSelectUserList = function (userList) {
  31. $scope.userList = angular.copy(userList);
  32. angular.forEach(userList, function (data, index) {
  33. var tempid = "'" + data.userId + "'";
  34. if (useridlist.indexOf(tempid) != -1) {
  35. $scope.userList[index].isExisits = 1;
  36. } else {
  37. $scope.userList[index].isExisits = 0;
  38. }
  39. })
  40. };
  41. //根据关键字搜索综合、用户信息
  42. $scope.searchResultByKey = function (key) {
  43. if (!UtilService.isDefined(key)) {
  44. return;
  45. }
  46. searchUser(key);
  47. };
  48. //搜索用户
  49. var searchUser = function (key) {
  50. $scope.showbaidu = false;
  51. //加载动画Loading
  52. $scope.showLoadingToast();
  53. SearchResultService.searchUser(key).then(function (response) {
  54. // console.log(response);
  55. if (response.length == 0) {
  56. UtilService.showMess("未搜索到相关用户");
  57. }
  58. $scope.userList = response;
  59. toSelectUserList(response);
  60. }, function () {
  61. UtilService.showMess("网络不给力,请重试");
  62. }).finally(function () {
  63. $scope.hideLoadingToast();
  64. });
  65. };
  66. //关注
  67. $scope.doFocus = function (user, index) {
  68. SearchResultService.doFocus(user.isFocus, user.userId).then(function (response) {
  69. if (response.status) {
  70. $scope.userList[index].isFocus = user.isFocus == 1 ? 2 : 1;
  71. }
  72. }, function () {
  73. })
  74. };
  75. var adduseridlist = [];
  76. var addUser = function () {
  77. var useridstr = adduseridlist.join(',');
  78. AuthorityService.editAuthority(2, authorityid, authoritycomefrom, "", useridstr).then(function (response) {
  79. // console.log(response);
  80. AuthorityModel.editmemberflg = true;
  81. }, function () {
  82. UtilService.showMess("网络不给力,请稍后重试");
  83. });
  84. };
  85. //关注用户添加
  86. $scope.addMember = function (user, index) {
  87. if (isset == 0) {
  88. $scope.userList[index].isExisits = 1;
  89. var tempobj = {
  90. userId: user.userId,
  91. userName: user.userName,
  92. photo: user.photo,
  93. iscreator: 0,
  94. comefrom: user.comefrom
  95. };
  96. if (modeltype == 0) {
  97. AuthorityModel.addMemberList(tempobj);
  98. } else {
  99. AuthorityModelService.addMemberList(tempobj);
  100. }
  101. } else {
  102. adduseridlist = [];
  103. $scope.userList[index].isExisits = 1;
  104. adduseridlist.push(user.userId);
  105. addUser();
  106. }
  107. };
  108. $scope.gobackback = function () {
  109. $timeout(function () {
  110. $scope.goback();
  111. }, 350)
  112. };
  113. //清空搜索内容
  114. $scope.clearSearch = function () {
  115. $scope.key = "";
  116. };
  117. $scope.showFunc = function () {
  118. $scope.showbaidu = true;
  119. };
  120. //监测key变化,即时匹配词库
  121. $scope.fixKey = function () {
  122. if (angular.isDefined($scope.key) && $scope.key.length > 0) {
  123. $scope.searchKey($scope.key);
  124. }
  125. };
  126. $scope.data = [];
  127. //百度词库
  128. $scope.searchKey = function (searchName) {
  129. var search_history = angular.element(document.getElementById("his_search"));
  130. if (search_history.val().length != 0) {
  131. var baidu = angular.element(document.getElementById("baidu_search"));
  132. baidu.css("display", "block");
  133. }
  134. $http({
  135. method: 'JSONP',
  136. url: 'https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=' + searchName + '&cb=JSON_CALLBACK'
  137. }).success(function (data) {
  138. $scope.data = data.s;
  139. });
  140. };
  141. });