home.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. (function (app) {
  2. app.controller('homeCtrl', ["$scope", "$http", 'config', "$state", "msg", "$ionicPopover", "homeService"
  3. , function ($scope, $http, config, $state, msg, $ionicPopover, homeService) {
  4. $scope.type = 'hot';//tab切换
  5. $ionicPopover.fromTemplateUrl('my-popover.html', {
  6. scope: $scope
  7. }).then(function (popover) {
  8. $scope.popover = popover;
  9. });
  10. $scope.openPopover = function ($event) {
  11. $scope.popover.show($event);
  12. };
  13. $scope.closePopover = function () {
  14. $scope.popover.hide();
  15. };
  16. $scope.changetype = function (type) {
  17. $scope.type = type;
  18. $scope.load(true);
  19. }
  20. $scope.toDetail = function (id) {
  21. $state.go('app.home_dreamdetail',{id:id});
  22. };
  23. $scope.filter = {
  24. hasMore: false,
  25. pageIndex: 1,
  26. pageSize: 20,
  27. }
  28. $scope.index = {
  29. banner: [],
  30. users: [],
  31. dreams:[]
  32. };
  33. $scope.load = function (init) {
  34. if (init) {
  35. $scope.filter.pageIndex = 1;
  36. $scope.index.dreams = [];
  37. }
  38. msg.loading();
  39. homeService.index($scope.type, $scope.filter.pageIndex).then(function (result) {
  40. msg.hide();
  41. $scope.index.banners = result.data.data.banners;
  42. $scope.index.users = result.data.data.users;
  43. $scope.filter.pageIndex++;
  44. var more = (result.data.data.dreams.data.length >= $scope.filter.pageSize);
  45. $scope.filter.hasMore = more;
  46. $scope.index.dreams = $scope.index.dreams.concat(result.data.data.dreams.data);
  47. if (init) {
  48. $scope.$broadcast('scroll.refreshComplete');
  49. } else {
  50. $scope.$broadcast('scroll.infiniteScrollComplete');
  51. }
  52. }, function (error) {
  53. msg.hide();
  54. });
  55. }
  56. $scope.data = {};
  57. $scope.load(true);
  58. $scope.$on('$ionicView.beforeLeave', function () {
  59. $scope.popover.hide();
  60. });
  61. }]);
  62. app.controller('dreamDetailCtrl', ["$scope", "$state", "storage","msg", "common", "config", "homeService", "$ionicTabsDelegate", "$stateParams", "$ionicModal", "$timeout", "$ionicScrollDelegate", "$interval"
  63. , function ($scope, $state, storage , msg, common, config, homeService, $ionicTabsDelegate, $stateParams, $ionicModal, $timeout, $ionicScrollDelegate, $interval) {
  64. var id = $stateParams.id;
  65. id=5;
  66. $scope.load = function (id) {
  67. msg.loading();
  68. homeService.dreamDetail(id).then(function (result) {
  69. msg.hide();
  70. console.log(result);
  71. $scope.dream = result.data.data
  72. var end_time = new Date($scope.dream.end_time);
  73. var date = new Date();
  74. var inter = (end_time.getTime() - date.getTime()) / 1000
  75. leftTimer(inter);
  76. $scope.multi.a = $scope.dream.a;
  77. $scope.multi.b = new Date($scope.dream.created_at.replace(/-/g, "/"));
  78. $scope.multi.c = $scope.dream.c;
  79. $scope.multi.promise = $scope.dream.a;
  80. $top3user = storage.getObject('top3user');
  81. if($scope.dream.top3user != $top3user&&$top3user != null){
  82. //测试动画切换
  83. if($scope.dream.top3user[0])$scope.sort.first = $scope.dream.top3user[0].avatar;
  84. if($scope.dream.top3user[1])$scope.sort.second = $scope.dream.top3user[1].avatar;
  85. if($scope.dream.top3user[2])$scope.sort.third = $scope.dream.top3user[2].avatar;
  86. $scope.sort.slide1 = 'list-grow-animation';
  87. $scope.sort.slide2 = 'slide-in-both-ways';
  88. $scope.sort.slide3 = 'bounce-animation';
  89. storage.setObject('top3user',$scope.dream.top3user)
  90. }
  91. $scope.calcmultiplier();
  92. }, function (error) {
  93. msg.hide();
  94. });
  95. }
  96. $scope.multi = {
  97. a: '',
  98. b: '',
  99. c: '',
  100. result: 1,
  101. promise:null,
  102. }
  103. $scope.type = 1;//tab切换
  104. $scope.tosupport = false;
  105. $scope.support = function ($event) {
  106. $event.stopPropagation();
  107. $scope.tosupport = true;
  108. $scope.vm.coin = 0;
  109. $scope.index = 0;
  110. }
  111. $scope.cancelSupport = function () {
  112. $scope.tosupport = false;
  113. }
  114. $scope.changeType = function (type) {
  115. $ionicScrollDelegate.scrollTop(true);
  116. $scope.type = type;
  117. }
  118. $scope.index = 0;
  119. $scope.$on('$ionicView.beforeEnter', function () {
  120. $scope.load(id);
  121. $ionicTabsDelegate.showBar(false);
  122. $scope.thisUser = storage.getObject('user');
  123. console.log($scope.user);
  124. });
  125. $scope.$on('$ionicView.leave', function () {
  126. $ionicTabsDelegate.showBar(true);
  127. if($scope.multi.promise)$interval.cancel($scope.multi.promise);
  128. if($scope.leftTimer)$interval.cancel($scope.leftTimer);
  129. });
  130. $scope.vm = {
  131. coin:"",
  132. title:""
  133. }
  134. $scope.timer = '';
  135. var leftTimer = function (countDown) {
  136. if (isNaN(countDown)) {
  137. $scope.timer = '结束';
  138. return;
  139. }
  140. var day=parseInt(countDown/(24*60*60));
  141. var h=parseInt(countDown/(60*60)%24);
  142. var m=parseInt(countDown/60%60);
  143. var s=parseInt(countDown%60);
  144. $scope.timer=(h<10?'0'+h:h)+'时'+(m<10?'0'+m:m)+'分'+(s<10?'0'+s:s)+'秒';
  145. if(day>0) $scope.timer = day+'天'+ $scope.timer;
  146. if($scope.leftTimer)$interval.cancel($scope.leftTimer);
  147. $scope.leftTimer = $interval(function () {
  148. if (countDown >= 1) leftTimer(countDown - 1);
  149. },1000);
  150. if(countDown<=0){
  151. $scope.timer='结束';
  152. }
  153. }
  154. $scope.changeIndex = function (index) {
  155. $scope.index = index;
  156. $scope.vm.coin = index*5;
  157. }
  158. //实时排行
  159. $scope.sort = {
  160. slide: ''
  161. }
  162. $scope.vidEnded = function () {
  163. alert('播放完毕');
  164. }
  165. //实时计算支持乘数
  166. $scope.calcmultiplier = function () {
  167. $scope.multi.promise = $interval(function () {
  168. var date = new Date();
  169. var inter = date.getTime() - $scope.multi.b.getTime();
  170. var minutes = Math.floor(inter / (60 * 1000));
  171. var number = $scope.multi.a * minutes + $scope.multi.c;//js浮点运算会失真,根据muti.a的可能值范围,比如乘以100再除以100
  172. if (number<=1) {
  173. number = 1;
  174. }
  175. $scope.multi.result = Math.round(number * 100) / 100;
  176. },1000);
  177. // 点击支持取消 $interval.cancel($scope.multi.promise); 获取数据后重新开始执行
  178. }
  179. $scope.supportDream = function (data) {
  180. if(!$scope.vm.coin){
  181. msg.error("请选择梦想币数量!");
  182. return ;
  183. }
  184. var data = {
  185. id:id,
  186. coin:$scope.vm.coin
  187. };
  188. homeService.supportDream(data).then(function(result){
  189. $scope.tosupport = false;
  190. $scope.load(id);
  191. $interval.cancel($scope.multi.promise);
  192. })
  193. };
  194. $scope.collectionDream = function(is_collection){
  195. homeService.collectionDream(id,is_collection).then(function(result){
  196. $scope.dream.is_collection=result.data.data;
  197. })
  198. }
  199. $scope.add = function(is_collection){
  200. var data = {
  201. id:id,
  202. title:$scope.vm.title,
  203. pics:$scope.imgs,
  204. };
  205. homeService.add_interaction(data).then(function(){
  206. $scope.load(id);
  207. $scope.closeModal();
  208. })
  209. }
  210. $scope.showText = function(){
  211. $scope.aboutStyle = {
  212. "white-space" : "normal"
  213. }
  214. $scope.ionDownStyle = {
  215. "display" : "none"
  216. }
  217. }
  218. $scope.input = {
  219. placeholder: '评论',
  220. focus:false
  221. }
  222. $scope.replay = function ($event, name) {
  223. $event.stopPropagation();
  224. $scope.input.focus = true;
  225. $scope.input.placeholder = "回复" + name;
  226. $scope.vm.title='';
  227. }
  228. $scope.comment = function(){
  229. // $event.stopPropagation();
  230. //$scope.input.placeholder = "评论";
  231. $scope.input.focus = true;
  232. $scope.vm.title='';
  233. }
  234. $scope.submitComment = function(iid){
  235. if($scope.vm.comment&&$scope.vm.comment==''||!iid)return;
  236. var data = {
  237. id:iid,
  238. content:$scope.vm.comment,
  239. };
  240. console.log($scope.vm.comment+" "+iid)
  241. homeService.add_comment(data).then(function(){
  242. $scope.vm.comment = '' ;
  243. $scope.load(id);
  244. $scope.closeModal();
  245. })
  246. }
  247. $scope.addpict = function () {
  248. common.chooseImage().then(function (img) {
  249. common.uploadFiles(img,1).then(function (result) {
  250. var response = JSON.parse(result.response);
  251. var file = response.data.file;
  252. $scope.imgs.push(config.imgServer+file);
  253. console.log(JSON.stringify(config.imgServer+file));
  254. }, function (error) {
  255. msg.error('图片上传失败');
  256. });
  257. }, function (error) {
  258. console.log('图片选择失败');
  259. });
  260. };
  261. $ionicModal.fromTemplateUrl('recharge-modal.html', {
  262. scope: $scope,
  263. animation: 'slide-in-up'
  264. }).then(function(modal) {
  265. $scope.rechagemodal = modal;
  266. });
  267. $scope.openRechargeModal = function() {
  268. $scope.rechagemodal.show();
  269. $scope.vm.money = '';
  270. };
  271. $scope.closeRechargeModal = function() {
  272. $scope.rechagemodal.hide();
  273. };
  274. $ionicModal.fromTemplateUrl('interaction-modal.html', {
  275. scope: $scope,
  276. animation: 'slide-in-up'
  277. }).then(function(modal) {
  278. $scope.modal = modal;
  279. });
  280. $scope.openModal = function() {
  281. $scope.modal.show();
  282. $scope.vm.title = '';
  283. $scope.imgs = [];
  284. };
  285. $scope.closeModal = function() {
  286. $scope.modal.hide();
  287. };
  288. //当我们用到模型时,清除它!
  289. $scope.$on('$destroy', function() {
  290. $scope.modal.remove();
  291. });
  292. }]);
  293. app.controller('searchCtrl', ["$scope","homeService", "$state", "msg", "$ionicTabsDelegate", "$ionicNavBarDelegate"
  294. , function ($scope,homeService, $state, msg, $ionicTabsDelegate, $ionicNavBarDelegate) {
  295. $scope.showMore = function (keyword,index) {
  296. if(!keyword)return;
  297. $scope.index = index;
  298. // $scope.vm.keyword = keyword;
  299. homeService.search(keyword).then(function(result){
  300. $scope.isResult = true;
  301. $scope.dream_infos = result.data.data.dream_infos;
  302. $scope.user_infos = result.data.data.user_infos;
  303. });
  304. }
  305. $scope.search = function (keyword) {
  306. $scope.vm.keyword = keyword;
  307. homeService.search(keyword).then(function(result){
  308. $scope.isResult = true;
  309. $scope.dream_infos = result.data.data.dream_infos;
  310. $scope.user_infos = result.data.data.user_infos;
  311. });
  312. }
  313. $scope.toDetail = function (id) {
  314. $state.go('app.home_dreamdetail',{id:id});
  315. };
  316. $scope.toUser = function (id) {
  317. $state.go('app.home_dreamdetail',{id:id});
  318. };
  319. $scope.$on('$ionicView.beforeEnter', function () {
  320. $scope.isResult = false;
  321. $scope.index = 0;
  322. $scope.vm = {
  323. keyword : ""
  324. }
  325. $ionicTabsDelegate.showBar(false);
  326. // $ionicNavBarDelegate.showBackButton(false);
  327. homeService.search('').then(function(result){
  328. $scope.hot_searches = result.data.data.hot_searches;
  329. $scope.history_searches = result.data.data.history_searches;
  330. });
  331. });
  332. $scope.$on('$ionicView.leave', function () {
  333. $ionicTabsDelegate.showBar(true);
  334. });
  335. }]);
  336. app.controller('interactionAddCtrl', ["$scope","homeService", "$state", "msg","config","common", "$ionicTabsDelegate", "$ionicNavBarDelegate"
  337. , function ($scope,homeService, $state, msg,config,common, $ionicTabsDelegate, $ionicNavBarDelegate) {
  338. $scope.$on('$ionicView.beforeEnter', function () {
  339. });
  340. }]);
  341. app.controller('homeDreamsCtrl', ["$scope", "$state", "myService", "msg"
  342. , function ($scope, $state, myService, msg) {
  343. $scope.$on('$ionicView.beforeEnter', function () {
  344. myService.myDream().then(function(result){
  345. console.log(result.data.data);
  346. $scope.dreams = result.data.data;
  347. });
  348. });
  349. $scope.toDetail = function (id) {
  350. $state.go('app.home_dreamdetail',{id:id});
  351. };
  352. }]);
  353. })(angular.module('app.controllers'));