angular.module('push') .controller('HomeCtrl', function ($scope, UserService, HomeService, $ionicTabsDelegate, $rootScope, $timeout, $ionicSlideBoxDelegate, $ionicModal, UtilService, LoginService, ConfigService, $ionicScrollDelegate, AreaService, LocationService) { $scope.$on("$ionicView.beforeEnter", function () { if (angular.isDefined(AreaService.cityname) && AreaService.cityname.length > 0) { $scope.city = AreaService.cityname; AreaService.cityname = ""; } else { $scope.city = ConfigService.location.city; if (!UtilService.isDefined($scope.city)) { LocationService.getCurrentLocation().then(function () { console.log(ConfigService.location); }, function () { $scope.city = "全国"; }); } } if($scope.app){ $scope.setStatusBar(1); } }); $scope.tabs = [];//默认频道 $scope.categoryListOtherData = [];//推荐频道 var chooseIndex = 0;//当前频道索引 $scope.index = chooseIndex; /*当有本地频道时,与本地进行比较,存储对应索引*/ var initRecommedChannel = function (templist, channellist) { var indexlist = []; angular.forEach(templist, function (channel, index) { angular.forEach(channellist, function (data, ind) { if (data.appModuleName == channel.appModuleName) { indexlist.unshift(index); } }); }); //根据索引剔除已存在的推荐频道 angular.forEach(indexlist, function (value, key) { templist.splice(value, 1); }); $scope.categoryListOtherData = templist; }; /*获取所有频道列表*/ var initChannel = function (chatype, channellist) { HomeService.getChannelList(82).then(function (response) { if (chatype == 0) { $scope.tabs = channellist; initRecommedChannel(response.channelList, channellist); } if (chatype == 1) { $scope.tabs = channellist; initRecommedChannel(response.channelList, channellist); } if (chatype == 2) { angular.forEach(response.channelList, function (value, key) { if (value.fixType == 0) { $scope.tabs.push(value); } }); angular.forEach(response.channelList, function (value, key) { if (value.fixType == 1) { $scope.categoryListOtherData.push(value); } }); } $timeout(function () { $(".Chanel_scroll_a").eq(0).addClass("scroll_a_on").siblings().removeClass("scroll_a_on"); }, 50); $scope.doRefresh(); }, function () { $scope.tabs = channellist; }); }; if (ConfigService.islogin && UtilService.isDefined(ConfigService.mychannellist) && ConfigService.mychannellist.length > 0) { //登录后且有我的频道信息 initChannel(0, ConfigService.mychannellist); } else { //未登录或没有我的频道,取本地频道信息 var localchannel = JSON.parse(localStorage.getItem("localchannel")); if (UtilService.isDefined(localchannel) && localchannel.length > 0) { initChannel(1, localchannel); } else { //初始化频道信息 initChannel(2, []); } } $scope.editaction = "编辑"; //长按显示已选频道的删选框; $scope.onHold = function () { $scope.myChannel = true; $scope.editaction = "完成"; }; //频道编辑完成删除框隐藏; $scope.editaAction = function () { if ($scope.editaction == "编辑") { $scope.myChannel = true; $scope.editaction = "完成"; } else { $scope.myChannel = false; $scope.editaction = "编辑"; } }; var editSlide = function (index) { var tablength = $scope.tabs.length;//标题个数 var middlewidth = $(window).width() / 2;//获取当前手机屏幕宽度一般 var OffLeft = $(".Chanel_scroll_a").eq(index).offset().left;//获取当前点击标题距离父容器左边框的距离 var SelfWidth = $(".Chanel_scroll_a").eq(index).width();//获取当前点击标题宽度 var startTranform = $("#p_Chanel .scroll").offset().left; var movewidth = OffLeft - middlewidth + SelfWidth / 2; startTranform -= movewidth; if (startTranform <= 0) { if (index < tablength - 2) { $('#p_Chanel .scroll').css({ 'transform': 'translate3d(' + startTranform + 'px,0,0)' }); } } else if (index < 4) { $('#p_Chanel .scroll').css({ 'transform': 'translate3d(0,0,0)' }); } $(".Chanel_scroll_a").eq(index).addClass("scroll_a_on").siblings().removeClass("scroll_a_on"); }; $scope.clickTab = function (index) { if (index == chooseIndex) { return; } // chooseIndex = index; // $scope.index = chooseIndex; // editSlide(index); // $scope.doRefresh(); $ionicSlideBoxDelegate.slide(index); }; //滑动频道 $scope.slideChanged = function (index) { chooseIndex = index; editSlide(index); $(".Chanel_scroll_a").eq(index).addClass("scroll_a_on").siblings().removeClass("scroll_a_on"); $scope.index = chooseIndex; $scope.doRefresh(); }; $scope.doRefresh = function () { $scope.tabs[chooseIndex].isload = false; //加载动画Loading if ($scope.updataversion != "block") { $scope.showLoadingToast(); } HomeService.getClassify($scope.tabs[chooseIndex], "", $scope.city).then(function (response) { // console.log(response.tngou); //第一次刷新完成,更新当前页数、数据 $scope.tabs[chooseIndex].currentPage = 2; $scope.tabs[chooseIndex].items = response.tngou; var trHtml = "
更新了" + $scope.tabs[chooseIndex].items.length + "条内容
"; $timeout(function () { $(".home_content .list").prepend(trHtml); $(".refresh_tip_div").animate({width: 100 + "%"}, 150); }, 500); $timeout(function () { $(".refresh_tip_div").animate({height: 0}, 500); }, 1200); //判断是否有下一页 $timeout(function () { $timeout(function () { if ($scope.tabs[chooseIndex].currentPage >= response.totalPage) { $scope.tabs[chooseIndex].isload = false; } else { $scope.tabs[chooseIndex].isload = true; } }, 800); $scope.hideLoadingToast(); $ionicScrollDelegate.$getByHandle("homeContent").scrollTo(0, 0); $ionicSlideBoxDelegate.update(); }, 50); }, function () { $scope.hideLoadingToast(); }).finally(function () { $rootScope.homeBadge = 0; $scope.$broadcast('scroll.refreshComplete'); }); }; $scope.loadMore = function () { $scope.tabs[chooseIndex].isload = false; //加载动画Loading /* $scope.showLoadingToast();*/ $scope.loadmore = true; HomeService.loadMoreClassify($scope.tabs[chooseIndex], "", $scope.city).then(function (response) { //加载数据,更新当前页数 $timeout(function () { if (response.tngou.length > 0) { $scope.tabs[chooseIndex].currentPage++; $scope.tabs[chooseIndex].items = $scope.tabs[chooseIndex].items.concat(response.tngou); } $scope.loadmore = false; }, 2000); // $scope.hideLoadingToast(); //判断是否有下一页 $timeout(function () { if ($scope.tabs[chooseIndex].currentPage >= response.totalPage) { $scope.tabs[chooseIndex].isload = false; } else { $scope.tabs[chooseIndex].isload = true; } }, 2000); }, function () { // $scope.hideLoadingToast(); $scope.loadmore = false; }).finally(function () { $scope.$broadcast('scroll.infiniteScrollComplete'); }); }; //进入详情 $scope.goDetails = function (item, type) { var module = item.moduleId; var id = ""; var url = ""; if (module == 9 || module == 10 || module == 100 || module > 50) { url = item.siteUrl; id = item.id; } else { id = item.infoId; } $scope.go('detail', { url: url, creatorId: item.creator, id: id, title: item.title, type: type, siteName: item.siteName, time: item.time, content: item.content, moduleId: item.moduleId, favourCount: item.favourCount }); }; //上传我的频道 var uploadMyChannel = function () { var templist = angular.copy($scope.tabs); var moduleIdStr = ""; angular.forEach(templist, function (value, key) { moduleIdStr = moduleIdStr + value.id + ","; }); moduleIdStr = moduleIdStr.substr(0, moduleIdStr.length - 1); HomeService.uploadMyChannel(moduleIdStr).then(function (response) { changedflg = false; }, function () { }); }; $ionicModal.fromTemplateUrl("templates/modal.html", { scope: $scope, animation: 'slide-in-left' }).then(function (modal) { $scope.categoryModal = modal; }); $scope.openCategoryModal = function () { if($scope.app){ $scope.setStatusBar(0); } $scope.categoryModal.show(); }; $scope.closeCategoryModal = function () { if($scope.app){ $scope.setStatusBar(1); } var templist = angular.copy($scope.tabs); var channellist = []; angular.forEach(templist, function (value, key) { value.items = []; value.isload = false; value.currentPage = 1; channellist.push(value); }); var str = JSON.stringify(channellist); localStorage.setItem("localchannel", str); $ionicSlideBoxDelegate.update(); $scope.categoryModal.hide(); }; $scope.deltab = function (appModuleName) { delorAdditem($scope.tabs, appModuleName, $scope.categoryListOtherData) }; $scope.addtab = function (appModuleName) { delorAdditem($scope.categoryListOtherData, appModuleName, $scope.tabs) }; var changedflg = false; var delorAdditem = function (objarry, appModuleName, targetarry) { angular.forEach(objarry, function (data, index) { if (data.appModuleName == appModuleName) { targetarry.push(data); objarry.splice(index, 1); } }); changedflg = true; }; //离开页面时,当且仅当已登录/频道修改时,上传我的频道信息 $scope.$on("$ionicView.beforeLeave", function () { if (ConfigService.islogin && changedflg) { uploadMyChannel(); } }); $scope.scrollTop = function () { $ionicScrollDelegate.scrollTop(); }; $scope.toptask = false; $scope.getScrollPosition = function () { var domScroll = $(".home_refresh").eq(chooseIndex).offset().top;//距离页面顶部的距离 if (domScroll < -250) { $scope.toptask = true; } else { $scope.toptask = false; } $scope.$apply();//触发用以更新view }; $scope.goProvince = function () { ConfigService.cityflg = 1; $scope.go('chooseProvince'); } }) ;