homeCtrl.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. angular.module('push')
  2. .controller('HomeCtrl', function ($scope, UserService, HomeService, $ionicTabsDelegate, $rootScope, $timeout, $ionicSlideBoxDelegate, $ionicModal, UtilService, LoginService, ConfigService, $ionicScrollDelegate, AreaService, LocationService) {
  3. $scope.$on("$ionicView.beforeEnter", function () {
  4. if (angular.isDefined(AreaService.cityname) && AreaService.cityname.length > 0) {
  5. $scope.city = AreaService.cityname;
  6. AreaService.cityname = "";
  7. } else {
  8. $scope.city = ConfigService.location.city;
  9. if (!UtilService.isDefined($scope.city)) {
  10. LocationService.getCurrentLocation().then(function () {
  11. console.log(ConfigService.location);
  12. }, function () {
  13. $scope.city = "全国";
  14. });
  15. }
  16. }
  17. if($scope.app){
  18. $scope.setStatusBar(1);
  19. }
  20. });
  21. $scope.tabs = [];//默认频道
  22. $scope.categoryListOtherData = [];//推荐频道
  23. var chooseIndex = 0;//当前频道索引
  24. $scope.index = chooseIndex;
  25. /*当有本地频道时,与本地进行比较,存储对应索引*/
  26. var initRecommedChannel = function (templist, channellist) {
  27. var indexlist = [];
  28. angular.forEach(templist, function (channel, index) {
  29. angular.forEach(channellist, function (data, ind) {
  30. if (data.appModuleName == channel.appModuleName) {
  31. indexlist.unshift(index);
  32. }
  33. });
  34. });
  35. //根据索引剔除已存在的推荐频道
  36. angular.forEach(indexlist, function (value, key) {
  37. templist.splice(value, 1);
  38. });
  39. $scope.categoryListOtherData = templist;
  40. };
  41. /*获取所有频道列表*/
  42. var initChannel = function (chatype, channellist) {
  43. HomeService.getChannelList(82).then(function (response) {
  44. if (chatype == 0) {
  45. $scope.tabs = channellist;
  46. initRecommedChannel(response.channelList, channellist);
  47. }
  48. if (chatype == 1) {
  49. $scope.tabs = channellist;
  50. initRecommedChannel(response.channelList, channellist);
  51. }
  52. if (chatype == 2) {
  53. angular.forEach(response.channelList, function (value, key) {
  54. if (value.fixType == 0) {
  55. $scope.tabs.push(value);
  56. }
  57. });
  58. angular.forEach(response.channelList, function (value, key) {
  59. if (value.fixType == 1) {
  60. $scope.categoryListOtherData.push(value);
  61. }
  62. });
  63. }
  64. $timeout(function () {
  65. $(".Chanel_scroll_a").eq(0).addClass("scroll_a_on").siblings().removeClass("scroll_a_on");
  66. }, 50);
  67. $scope.doRefresh();
  68. }, function () {
  69. $scope.tabs = channellist;
  70. });
  71. };
  72. if (ConfigService.islogin && UtilService.isDefined(ConfigService.mychannellist) && ConfigService.mychannellist.length > 0) {
  73. //登录后且有我的频道信息
  74. initChannel(0, ConfigService.mychannellist);
  75. } else {
  76. //未登录或没有我的频道,取本地频道信息
  77. var localchannel = JSON.parse(localStorage.getItem("localchannel"));
  78. if (UtilService.isDefined(localchannel) && localchannel.length > 0) {
  79. initChannel(1, localchannel);
  80. } else {
  81. //初始化频道信息
  82. initChannel(2, []);
  83. }
  84. }
  85. $scope.editaction = "编辑";
  86. //长按显示已选频道的删选框;
  87. $scope.onHold = function () {
  88. $scope.myChannel = true;
  89. $scope.editaction = "完成";
  90. };
  91. //频道编辑完成删除框隐藏;
  92. $scope.editaAction = function () {
  93. if ($scope.editaction == "编辑") {
  94. $scope.myChannel = true;
  95. $scope.editaction = "完成";
  96. } else {
  97. $scope.myChannel = false;
  98. $scope.editaction = "编辑";
  99. }
  100. };
  101. var editSlide = function (index) {
  102. var tablength = $scope.tabs.length;//标题个数
  103. var middlewidth = $(window).width() / 2;//获取当前手机屏幕宽度一般
  104. var OffLeft = $(".Chanel_scroll_a").eq(index).offset().left;//获取当前点击标题距离父容器左边框的距离
  105. var SelfWidth = $(".Chanel_scroll_a").eq(index).width();//获取当前点击标题宽度
  106. var startTranform = $("#p_Chanel .scroll").offset().left;
  107. var movewidth = OffLeft - middlewidth + SelfWidth / 2;
  108. startTranform -= movewidth;
  109. if (startTranform <= 0) {
  110. if (index < tablength - 2) {
  111. $('#p_Chanel .scroll').css({
  112. 'transform': 'translate3d(' + startTranform + 'px,0,0)'
  113. });
  114. }
  115. } else if (index < 4) {
  116. $('#p_Chanel .scroll').css({
  117. 'transform': 'translate3d(0,0,0)'
  118. });
  119. }
  120. $(".Chanel_scroll_a").eq(index).addClass("scroll_a_on").siblings().removeClass("scroll_a_on");
  121. };
  122. $scope.clickTab = function (index) {
  123. if (index == chooseIndex) {
  124. return;
  125. }
  126. // chooseIndex = index;
  127. // $scope.index = chooseIndex;
  128. // editSlide(index);
  129. // $scope.doRefresh();
  130. $ionicSlideBoxDelegate.slide(index);
  131. };
  132. //滑动频道
  133. $scope.slideChanged = function (index) {
  134. chooseIndex = index;
  135. editSlide(index);
  136. $(".Chanel_scroll_a").eq(index).addClass("scroll_a_on").siblings().removeClass("scroll_a_on");
  137. $scope.index = chooseIndex;
  138. $scope.doRefresh();
  139. };
  140. $scope.doRefresh = function () {
  141. $scope.tabs[chooseIndex].isload = false;
  142. //加载动画Loading
  143. if ($scope.updataversion != "block") {
  144. $scope.showLoadingToast();
  145. }
  146. HomeService.getClassify($scope.tabs[chooseIndex], "", $scope.city).then(function (response) {
  147. // console.log(response.tngou);
  148. //第一次刷新完成,更新当前页数、数据
  149. $scope.tabs[chooseIndex].currentPage = 2;
  150. $scope.tabs[chooseIndex].items = response.tngou;
  151. var trHtml = "<div class='refresh_tip_div'>更新了" + $scope.tabs[chooseIndex].items.length + "条内容</div>";
  152. $timeout(function () {
  153. $(".home_content .list").prepend(trHtml);
  154. $(".refresh_tip_div").animate({width: 100 + "%"}, 150);
  155. }, 500);
  156. $timeout(function () {
  157. $(".refresh_tip_div").animate({height: 0}, 500);
  158. }, 1200);
  159. //判断是否有下一页
  160. $timeout(function () {
  161. $timeout(function () {
  162. if ($scope.tabs[chooseIndex].currentPage >= response.totalPage) {
  163. $scope.tabs[chooseIndex].isload = false;
  164. } else {
  165. $scope.tabs[chooseIndex].isload = true;
  166. }
  167. }, 800);
  168. $scope.hideLoadingToast();
  169. $ionicScrollDelegate.$getByHandle("homeContent").scrollTo(0, 0);
  170. $ionicSlideBoxDelegate.update();
  171. }, 50);
  172. }, function () {
  173. $scope.hideLoadingToast();
  174. }).finally(function () {
  175. $rootScope.homeBadge = 0;
  176. $scope.$broadcast('scroll.refreshComplete');
  177. });
  178. };
  179. $scope.loadMore = function () {
  180. $scope.tabs[chooseIndex].isload = false;
  181. //加载动画Loading
  182. /* $scope.showLoadingToast();*/
  183. $scope.loadmore = true;
  184. HomeService.loadMoreClassify($scope.tabs[chooseIndex], "", $scope.city).then(function (response) {
  185. //加载数据,更新当前页数
  186. $timeout(function () {
  187. if (response.tngou.length > 0) {
  188. $scope.tabs[chooseIndex].currentPage++;
  189. $scope.tabs[chooseIndex].items = $scope.tabs[chooseIndex].items.concat(response.tngou);
  190. }
  191. $scope.loadmore = false;
  192. }, 2000);
  193. // $scope.hideLoadingToast();
  194. //判断是否有下一页
  195. $timeout(function () {
  196. if ($scope.tabs[chooseIndex].currentPage >= response.totalPage) {
  197. $scope.tabs[chooseIndex].isload = false;
  198. } else {
  199. $scope.tabs[chooseIndex].isload = true;
  200. }
  201. }, 2000);
  202. }, function () {
  203. // $scope.hideLoadingToast();
  204. $scope.loadmore = false;
  205. }).finally(function () {
  206. $scope.$broadcast('scroll.infiniteScrollComplete');
  207. });
  208. };
  209. //进入详情
  210. $scope.goDetails = function (item, type) {
  211. var module = item.moduleId;
  212. var id = "";
  213. var url = "";
  214. if (module == 9 || module == 10 || module == 100 || module > 50) {
  215. url = item.siteUrl;
  216. id = item.id;
  217. } else {
  218. id = item.infoId;
  219. }
  220. $scope.go('detail', {
  221. url: url,
  222. creatorId: item.creator,
  223. id: id,
  224. title: item.title,
  225. type: type,
  226. siteName: item.siteName,
  227. time: item.time,
  228. content: item.content,
  229. moduleId: item.moduleId,
  230. favourCount: item.favourCount
  231. });
  232. };
  233. //上传我的频道
  234. var uploadMyChannel = function () {
  235. var templist = angular.copy($scope.tabs);
  236. var moduleIdStr = "";
  237. angular.forEach(templist, function (value, key) {
  238. moduleIdStr = moduleIdStr + value.id + ",";
  239. });
  240. moduleIdStr = moduleIdStr.substr(0, moduleIdStr.length - 1);
  241. HomeService.uploadMyChannel(moduleIdStr).then(function (response) {
  242. changedflg = false;
  243. }, function () {
  244. });
  245. };
  246. $ionicModal.fromTemplateUrl("templates/modal.html", {
  247. scope: $scope,
  248. animation: 'slide-in-left'
  249. }).then(function (modal) {
  250. $scope.categoryModal = modal;
  251. });
  252. $scope.openCategoryModal = function () {
  253. if($scope.app){
  254. $scope.setStatusBar(0);
  255. }
  256. $scope.categoryModal.show();
  257. };
  258. $scope.closeCategoryModal = function () {
  259. if($scope.app){
  260. $scope.setStatusBar(1);
  261. }
  262. var templist = angular.copy($scope.tabs);
  263. var channellist = [];
  264. angular.forEach(templist, function (value, key) {
  265. value.items = [];
  266. value.isload = false;
  267. value.currentPage = 1;
  268. channellist.push(value);
  269. });
  270. var str = JSON.stringify(channellist);
  271. localStorage.setItem("localchannel", str);
  272. $ionicSlideBoxDelegate.update();
  273. $scope.categoryModal.hide();
  274. };
  275. $scope.deltab = function (appModuleName) {
  276. delorAdditem($scope.tabs, appModuleName, $scope.categoryListOtherData)
  277. };
  278. $scope.addtab = function (appModuleName) {
  279. delorAdditem($scope.categoryListOtherData, appModuleName, $scope.tabs)
  280. };
  281. var changedflg = false;
  282. var delorAdditem = function (objarry, appModuleName, targetarry) {
  283. angular.forEach(objarry, function (data, index) {
  284. if (data.appModuleName == appModuleName) {
  285. targetarry.push(data);
  286. objarry.splice(index, 1);
  287. }
  288. });
  289. changedflg = true;
  290. };
  291. //离开页面时,当且仅当已登录/频道修改时,上传我的频道信息
  292. $scope.$on("$ionicView.beforeLeave", function () {
  293. if (ConfigService.islogin && changedflg) {
  294. uploadMyChannel();
  295. }
  296. });
  297. $scope.scrollTop = function () {
  298. $ionicScrollDelegate.scrollTop();
  299. };
  300. $scope.toptask = false;
  301. $scope.getScrollPosition = function () {
  302. var domScroll = $(".home_refresh").eq(chooseIndex).offset().top;//距离页面顶部的距离
  303. if (domScroll < -250) {
  304. $scope.toptask = true;
  305. } else {
  306. $scope.toptask = false;
  307. }
  308. $scope.$apply();//触发用以更新view
  309. };
  310. $scope.goProvince = function () {
  311. ConfigService.cityflg = 1;
  312. $scope.go('chooseProvince');
  313. }
  314. })
  315. ;