home.js 14 KB

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