angular.module('push') .controller('SearchResultCtrl', function ($scope, $state, $timeout, $ionicTabsDelegate, $stateParams, SearchResultService, $http, UtilService) { $scope.key = $stateParams.key; $scope.showbaidu = false; //综合、用户 $scope.act = $stateParams.actype; $scope.changeAct = function () { $scope.act = !$scope.act; $scope.searchResultByKey($scope.key); }; //取本地存储已选择的频道信息 var channellist = JSON.parse(localStorage.getItem("mychannel")); //搜索结果频道与本地已选择频道匹配 var checkIsAddChannel = function (channel) { $scope.channellist = []; angular.forEach(channel, function (data, index) { angular.forEach(channellist, function (value, key) { if (value.title == data.title) { channel[index].isadd = true; } }); }); $scope.channellist = channel; }; //根据关键字搜索综合、用户信息 $scope.searchResultByKey = function (key) { if(!UtilService.isDefined(key)){ return; } if ($scope.act) { searchKey(key); } else { searchUser(key); } }; var searchKey = function (key) { $scope.showbaidu = false; //加载动画Loading $scope.showLoadingToast(); SearchResultService.searchResult(key).then(function (response) { if (response.tngou.length == 0) { UtilService.showMess("未搜索到相关信息"); } $scope.items = response.tngou; //返回结果中有频道信息,则与本地匹配 if (angular.isDefined(response.channel)) { checkIsAddChannel(response.channel) } $scope.hideLoadingToast(); }, function () { $scope.hideLoadingToast(); }) }; if ($scope.act) { $scope.searchResultByKey($scope.key); } var searchUser = function (key) { $scope.showbaidu = false; //加载动画Loading $scope.showLoadingToast(); SearchResultService.searchUser(key).then(function (response) { // console.log(response); if (response.length == 0) { UtilService.showMess("未搜索到相关用户"); } $scope.usersList = response; $scope.hideLoadingToast(); }, function () { $scope.hideLoadingToast(); }) }; //添加频道至本地 $scope.addSearchChannel = function (item, index) { channellist.push(item); $scope.channellist[index].isadd = true; localStorage.setItem("mychannel", JSON.stringify(channellist)); }; //关注 $scope.doFocus = function (user, index) { SearchResultService.doFocus(user.isFocus, user.userId).then(function (response) { if (response.status) { $scope.usersList[index].isFocus = user.isFocus == 1 ? 2 : 1; } }, function () { }) }; //进入详情 $scope.goDetails = function (item) { var module = item.moduleId; var id = ""; var url = ""; if (module == 9 || module == 10 || module == 100) { url = item.siteUrl; id = item.id; } else { id = item.infoId; } $scope.go('detail', { url: url, creatorId: item.creator, id: id, title: item.title, siteName: item.siteName, time: item.time, content: item.content, moduleId: item.moduleId, favourCount: item.favourCount }); }; //清空搜索内容 $scope.clearSearch = function () { $scope.key = ""; }; $scope.showFunc = function () { $scope.showbaidu = true; }; //监测key变化,即时匹配词库 $scope.fixKey = function () { if (angular.isDefined($scope.key) && $scope.key.length > 0) { $scope.searchKey($scope.key); } }; $scope.data = []; //百度词库 $scope.searchKey = function (searchName) { var search_history = angular.element(document.getElementById("his_search")); if (search_history.val().length != 0) { var baidu = angular.element(document.getElementById("baidu_search")); baidu.css("display", "block"); } $http({ method: 'JSONP', url: 'https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=' + searchName + '&cb=JSON_CALLBACK' }).success(function (data) { $scope.data = data.s; }); }; });