SearchResult.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. angular.module('push')
  2. .controller('SearchResultCtrl', function ($scope, $state, $timeout, $ionicTabsDelegate, $stateParams, SearchResultService, $http, UtilService) {
  3. $scope.key = $stateParams.key;
  4. $scope.showbaidu = false;
  5. //综合、用户
  6. $scope.act = $stateParams.actype;
  7. $scope.changeAct = function () {
  8. $scope.act = !$scope.act;
  9. $scope.searchResultByKey($scope.key);
  10. };
  11. //取本地存储已选择的频道信息
  12. var channellist = JSON.parse(localStorage.getItem("mychannel"));
  13. //搜索结果频道与本地已选择频道匹配
  14. var checkIsAddChannel = function (channel) {
  15. $scope.channellist = [];
  16. angular.forEach(channel, function (data, index) {
  17. angular.forEach(channellist, function (value, key) {
  18. if (value.title == data.title) {
  19. channel[index].isadd = true;
  20. }
  21. });
  22. });
  23. $scope.channellist = channel;
  24. };
  25. //根据关键字搜索综合、用户信息
  26. $scope.searchResultByKey = function (key) {
  27. if(!UtilService.isDefined(key)){
  28. return;
  29. }
  30. if ($scope.act) {
  31. searchKey(key);
  32. } else {
  33. searchUser(key);
  34. }
  35. };
  36. var searchKey = function (key) {
  37. $scope.showbaidu = false;
  38. //加载动画Loading
  39. $scope.showLoadingToast();
  40. SearchResultService.searchResult(key).then(function (response) {
  41. if (response.tngou.length == 0) {
  42. UtilService.showMess("未搜索到相关信息");
  43. }
  44. $scope.items = response.tngou;
  45. //返回结果中有频道信息,则与本地匹配
  46. if (angular.isDefined(response.channel)) {
  47. checkIsAddChannel(response.channel)
  48. }
  49. $scope.hideLoadingToast();
  50. }, function () {
  51. $scope.hideLoadingToast();
  52. })
  53. };
  54. if ($scope.act) {
  55. $scope.searchResultByKey($scope.key);
  56. }
  57. var searchUser = function (key) {
  58. $scope.showbaidu = false;
  59. //加载动画Loading
  60. $scope.showLoadingToast();
  61. SearchResultService.searchUser(key).then(function (response) {
  62. // console.log(response);
  63. if (response.length == 0) {
  64. UtilService.showMess("未搜索到相关用户");
  65. }
  66. $scope.usersList = response;
  67. $scope.hideLoadingToast();
  68. }, function () {
  69. $scope.hideLoadingToast();
  70. })
  71. };
  72. //添加频道至本地
  73. $scope.addSearchChannel = function (item, index) {
  74. channellist.push(item);
  75. $scope.channellist[index].isadd = true;
  76. localStorage.setItem("mychannel", JSON.stringify(channellist));
  77. };
  78. //关注
  79. $scope.doFocus = function (user, index) {
  80. SearchResultService.doFocus(user.isFocus, user.userId).then(function (response) {
  81. if (response.status) {
  82. $scope.usersList[index].isFocus = user.isFocus == 1 ? 2 : 1;
  83. }
  84. }, function () {
  85. })
  86. };
  87. //进入详情
  88. $scope.goDetails = function (item) {
  89. var module = item.moduleId;
  90. var id = "";
  91. var url = "";
  92. if (module == 9 || module == 10 || module == 100) {
  93. url = item.siteUrl;
  94. id = item.id;
  95. } else {
  96. id = item.infoId;
  97. }
  98. $scope.go('detail', {
  99. url: url,
  100. creatorId: item.creator,
  101. id: id,
  102. title: item.title,
  103. siteName: item.siteName,
  104. time: item.time,
  105. content: item.content,
  106. moduleId: item.moduleId,
  107. favourCount: item.favourCount
  108. });
  109. };
  110. //清空搜索内容
  111. $scope.clearSearch = function () {
  112. $scope.key = "";
  113. };
  114. $scope.showFunc = function () {
  115. $scope.showbaidu = true;
  116. };
  117. //监测key变化,即时匹配词库
  118. $scope.fixKey = function () {
  119. if (angular.isDefined($scope.key) && $scope.key.length > 0) {
  120. $scope.searchKey($scope.key);
  121. }
  122. };
  123. $scope.data = [];
  124. //百度词库
  125. $scope.searchKey = function (searchName) {
  126. var search_history = angular.element(document.getElementById("his_search"));
  127. if (search_history.val().length != 0) {
  128. var baidu = angular.element(document.getElementById("baidu_search"));
  129. baidu.css("display", "block");
  130. }
  131. $http({
  132. method: 'JSONP',
  133. url: 'https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=' + searchName + '&cb=JSON_CALLBACK'
  134. }).success(function (data) {
  135. $scope.data = data.s;
  136. });
  137. };
  138. });