home.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. $scope.chosedIndex = 0;
  6. $scope.clickAvatar = function(index){
  7. $scope.chosedIndex = index;
  8. };
  9. $ionicPopover.fromTemplateUrl('my-popover.html', {
  10. scope: $scope
  11. }).then(function (popover) {
  12. $scope.popover = popover;
  13. });
  14. $scope.openPopover = function ($event) {
  15. $scope.popover.show($event);
  16. };
  17. $scope.closePopover = function () {
  18. $scope.popover.hide();
  19. };
  20. $scope.changetype = function (type) {
  21. $scope.type = type;
  22. $scope.load(true);
  23. };
  24. $scope.toDetail = function (id) {
  25. $state.go('app.home_dreamdetail',{id:id});
  26. };
  27. $scope.toUserDetail = function (id) {
  28. $state.go('app.userDetail',{id:id});
  29. };
  30. $scope.filter = {
  31. hasMore: false,
  32. pageIndex: 1,
  33. pageSize: 20
  34. }
  35. $scope.index = {
  36. banner: [],
  37. users: [],
  38. dreams:[]
  39. };
  40. $scope.load = function (init) {
  41. if (init) {
  42. $scope.filter.pageIndex = 1;
  43. $scope.index.dreams = [];
  44. }
  45. msg.loading();
  46. homeService.index($scope.type, $scope.filter.pageIndex).then(function (result) {
  47. msg.hide();
  48. $scope.index.banners = result.data.data.banners;
  49. $scope.index.users = result.data.data.users;
  50. $scope.filter.pageIndex++;
  51. var more = (result.data.data.dreams.data.length >= $scope.filter.pageSize);
  52. $scope.filter.hasMore = more;
  53. $scope.index.dreams = $scope.index.dreams.concat(result.data.data.dreams.data);
  54. if (init) {
  55. $scope.$broadcast('scroll.refreshComplete');
  56. } else {
  57. $scope.$broadcast('scroll.infiniteScrollComplete');
  58. }
  59. }, function (error) {
  60. msg.hide();
  61. });
  62. }
  63. $scope.qrscan = function () {
  64. cordova.plugins.barcodeScanner.scan(
  65. function (result) {
  66. //扫码成功后执行的回调函数
  67. alert("收到一个二维码\n" +
  68. "扫码文字结果: " + result.text + "\n" +
  69. "格式: " + result.format + "\n" +
  70. "是否在扫码页面取消扫码: " + result.cancelled);
  71. },
  72. function (error) {
  73. //扫码失败执行的回调函数
  74. alert("Scanning failed: " + error);
  75. }, {
  76. preferFrontCamera: false, // iOS and Android 设置前置摄像头
  77. showFlipCameraButton: true, // iOS and Android 显示旋转摄像头按钮
  78. showTorchButton: true, // iOS and Android 显示打开闪光灯按钮
  79. torchOn: false, // Android, launch with the torch switched on (if available)打开手电筒
  80. prompt: "在扫描区域内放置二维码", // Android提示语
  81. resultDisplayDuration: 500, // Android, display scanned text for X ms.
  82. //0 suppresses it entirely, default 1500 设置扫码时间的参数
  83. formats: "QR_CODE", // 二维码格式可设置多种类型
  84. orientation: "portrait", // Android only (portrait|landscape),
  85. //default unset so it rotates with the device在安卓上 landscape 是横屏状态
  86. disableAnimations: true, // iOS 是否禁止动画
  87. disableSuccessBeep: false // iOS 禁止成功后提示声音 “滴”
  88. }
  89. );
  90. }
  91. $scope.data = {};
  92. $scope.load(true);
  93. $scope.$on('$ionicView.beforeLeave', function () {
  94. $scope.popover.hide();
  95. });
  96. }]);
  97. app.controller('dreamDetailCtrl', ["$scope", "$state", "$ionicModal","storage","msg", "common", "config", "homeService", "$ionicTabsDelegate", "$stateParams", "$ionicModal", "$timeout", "$ionicScrollDelegate", "$interval"
  98. , function ($scope, $state,$ionicModal, storage , msg, common, config, homeService, $ionicTabsDelegate, $stateParams, $ionicModal, $timeout, $ionicScrollDelegate, $interval) {
  99. var id = $stateParams.id;
  100. var top3user = '';
  101. // id=5;
  102. $scope.load = function (id) {
  103. msg.loading();
  104. homeService.dreamDetail(id).then(function (result) {
  105. msg.hide();
  106. console.log(result);
  107. $scope.dream = result.data.data;
  108. var end_time = new Date($scope.dream.end_time.replace(/-/g, "/"));
  109. var date = new Date();
  110. var inter = (end_time.getTime() - date.getTime()) / 1000;
  111. leftTimer(inter);
  112. $scope.multi.a = $scope.dream.a;
  113. $scope.multi.b = new Date($scope.dream.created_at.replace(/-/g, "/"));
  114. $scope.multi.c = $scope.dream.c;
  115. $scope.multi.promise = $scope.dream.a;
  116. debugger;
  117. var newtop3user = '' ;
  118. angular.forEach($scope.dream.top3user, function(user) {
  119. newtop3user= newtop3user +user.id+","
  120. });
  121. if(top3user!=''&&newtop3user!=top3user){
  122. //测试动画切换
  123. $timeout(function() {
  124. $scope.sort.slide1 = 'list-grow-animation';
  125. $scope.sort.slide2 = 'slide-in-both-ways';
  126. $scope.sort.slide3 = 'bounce-animation';
  127. }, 50);
  128. }
  129. top3user = newtop3user;
  130. if($scope.dream.top3user[0])$scope.sort.first = $scope.dream.top3user[0].avatar;
  131. if($scope.dream.top3user[1])$scope.sort.second = $scope.dream.top3user[1].avatar;
  132. if($scope.dream.top3user[2])$scope.sort.third = $scope.dream.top3user[2].avatar;
  133. $scope.calcmultiplier();
  134. }, function (error) {
  135. msg.hide();
  136. });
  137. };
  138. $ionicModal.fromTemplateUrl('supportExplain.html', {
  139. scope: $scope
  140. }).then(function(modal) {
  141. $scope.supportmodal = modal;
  142. });
  143. $scope.openSupportmodal = function() {
  144. $scope.supportmodal.show();
  145. };
  146. $scope.closeSupportmodal = function() {
  147. $scope.supportmodal.hide();
  148. };
  149. $scope.multi = {
  150. a: '',
  151. b: '',
  152. c: '',
  153. result: 1,
  154. promise:null
  155. };
  156. $scope.type = 1;//tab切换
  157. $scope.tosupport = false;
  158. $scope.support = function ($event) {
  159. $event.stopPropagation();
  160. $scope.tosupport = true;
  161. $scope.vm.coin = 0;
  162. $scope.index = 0;
  163. }
  164. $scope.cancelSupport = function () {
  165. $scope.tosupport = false;
  166. }
  167. //实时排行
  168. $scope.sort = {
  169. slide: ''
  170. };
  171. $scope.changeType = function (type) {
  172. $ionicScrollDelegate.scrollTop(true);
  173. $scope.type = type;
  174. };
  175. $scope.index = 0;
  176. $scope.$on('$ionicView.beforeEnter', function () {
  177. $scope.load(id);
  178. $ionicTabsDelegate.showBar(false);
  179. $scope.thisUser = storage.getObject('user');
  180. console.log($scope.user);
  181. });
  182. $scope.$on('$ionicView.leave', function () {
  183. $ionicTabsDelegate.showBar(true);
  184. if($scope.multi.promise)$interval.cancel($scope.multi.promise);
  185. if($scope.leftTimer)$interval.cancel($scope.leftTimer);
  186. });
  187. $scope.vm = {
  188. coin:"",
  189. title:""
  190. }
  191. $scope.timer = '';
  192. var leftTimer = function (countDown) {
  193. if (isNaN(countDown)) {
  194. $scope.timer = '结束';
  195. return;
  196. }
  197. var day=parseInt(countDown/(24*60*60));
  198. var h=parseInt(countDown/(60*60)%24);
  199. var m=parseInt(countDown/60%60);
  200. var s=parseInt(countDown%60);
  201. $scope.timer=(h<10?'0'+h:h)+'时'+(m<10?'0'+m:m)+'分'+(s<10?'0'+s:s)+'秒';
  202. if(day>0) $scope.timer = day+'天'+ $scope.timer;
  203. if($scope.leftTimer)$interval.cancel($scope.leftTimer);
  204. $scope.leftTimer = $interval(function () {
  205. if (countDown >= 1) leftTimer(countDown - 1);
  206. },1000);
  207. if(countDown<=0){
  208. $scope.timer='结束';
  209. }
  210. }
  211. $scope.changeIndex = function (index) {
  212. $scope.index = index;
  213. $scope.vm.coin = index*5;
  214. }
  215. $scope.vidEnded = function () {
  216. alert('播放完毕');
  217. }
  218. //实时计算支持乘数
  219. $scope.calcmultiplier = function () {
  220. $scope.multi.promise = $interval(function () {
  221. var date = new Date();
  222. var inter = date.getTime() - $scope.multi.b.getTime();
  223. var minutes = Math.floor(inter / (60 * 1000));
  224. var number = $scope.multi.a * minutes + $scope.multi.c;//js浮点运算会失真,根据muti.a的可能值范围,比如乘以100再除以100
  225. if (number<=1) {
  226. number = 1;
  227. }
  228. //$scope.multi.result = Math.round(number * 100) / 100;
  229. $scope.multi.result = Math.round(number * 100 / 100);
  230. },1000);
  231. // 点击支持取消 $interval.cancel($scope.multi.promise); 获取数据后重新开始执行
  232. }
  233. $scope.supportDream = function (data) {
  234. if(!$scope.vm.coin){
  235. msg.error("请选择梦想币数量!");
  236. return ;
  237. }
  238. var data = {
  239. id:id,
  240. coin:$scope.vm.coin
  241. };
  242. homeService.supportDream(data).then(function(result){
  243. $scope.tosupport = false;
  244. $scope.load(id);
  245. $interval.cancel($scope.multi.promise);
  246. })
  247. };
  248. $scope.collectionDream = function(is_collection){
  249. homeService.collectionDream(id,is_collection).then(function(result){
  250. $scope.dream.is_collection=result.data.data;
  251. })
  252. }
  253. $scope.add = function(is_collection){
  254. var data = {
  255. id:id,
  256. title:$scope.vm.title,
  257. pics:$scope.imgs,
  258. };
  259. homeService.add_interaction(data).then(function(){
  260. $scope.load(id);
  261. $scope.closeModal();
  262. })
  263. }
  264. $scope.showText = function(){
  265. $scope.aboutStyle = {
  266. "white-space" : "normal"
  267. };
  268. $scope.ionDownStyle = {
  269. "display" : "none"
  270. }
  271. };
  272. $scope.input = {
  273. placeholder: '评论',
  274. focus:false
  275. };
  276. $scope.replay = function ($event, name) {
  277. $event.stopPropagation();
  278. $scope.input.focus = true;
  279. $scope.input.placeholder = "回复" + name;
  280. $scope.vm.title='';
  281. }
  282. $scope.comment = function(){
  283. // $event.stopPropagation();
  284. //$scope.input.placeholder = "评论";
  285. $scope.input.focus = true;
  286. $scope.vm.title='';
  287. }
  288. $scope.submitComment = function(iid){
  289. if($scope.vm.comment&&$scope.vm.comment==''||!iid)return;
  290. var data = {
  291. id:iid,
  292. content:$scope.vm.comment,
  293. };
  294. console.log($scope.vm.comment+" "+iid)
  295. homeService.add_comment(data).then(function(){
  296. $scope.vm.comment = '' ;
  297. $scope.load(id);
  298. $scope.closeModal();
  299. })
  300. }
  301. $scope.addpict = function () {
  302. common.chooseImage().then(function (img) {
  303. common.uploadFiles(img,1).then(function (result) {
  304. var response = JSON.parse(result.response);
  305. var file = response.data.file;
  306. $scope.imgs.push(config.imgServer+file);
  307. console.log(JSON.stringify(config.imgServer+file));
  308. }, function (error) {
  309. msg.error('图片上传失败');
  310. });
  311. }, function (error) {
  312. console.log('图片选择失败');
  313. });
  314. };
  315. $ionicModal.fromTemplateUrl('recharge-modal.html', {
  316. scope: $scope,
  317. animation: 'slide-in-up'
  318. }).then(function(modal) {
  319. $scope.rechagemodal = modal;
  320. });
  321. $scope.openRechargeModal = function() {
  322. $scope.rechagemodal.show();
  323. $scope.vm.money = '';
  324. };
  325. $scope.closeRechargeModal = function() {
  326. $scope.rechagemodal.hide();
  327. };
  328. $ionicModal.fromTemplateUrl('interaction-modal.html', {
  329. scope: $scope,
  330. animation: 'slide-in-up'
  331. }).then(function(modal) {
  332. $scope.modal = modal;
  333. });
  334. $scope.openModal = function() {
  335. $scope.modal.show();
  336. $scope.vm.title = '';
  337. $scope.imgs = [];
  338. };
  339. $scope.closeModal = function() {
  340. $scope.modal.hide();
  341. };
  342. //当我们用到模型时,清除它!
  343. $scope.$on('$destroy', function() {
  344. $scope.modal.remove();
  345. });
  346. }]);
  347. app.controller('userDetailCtrl', ["$scope","homeService", "$stateParams","$state", "msg"
  348. , function ($scope,homeService,$stateParams, $state, msg) {
  349. var id = $stateParams.id;
  350. $scope.load = function (id) {
  351. msg.loading();
  352. homeService.dreamDetail(id).then(function (result) {
  353. msg.hide();
  354. $scope.user = result.data.data;
  355. }, function (error) {
  356. msg.hide();
  357. });
  358. };
  359. $scope.load(id);
  360. $scope.toDetail = function (id) {
  361. $state.go('app.home_dreamdetail',{id:id});
  362. };
  363. $scope.toOldDream = function(){
  364. $state.go("app.oldDream");
  365. }
  366. $scope.toSupportDream = function(){
  367. $state.go("app.supportDream");
  368. }
  369. }]);
  370. app.controller('oldDreamCtrl', ["$scope","homeService", "$stateParams","$state", "msg"
  371. , function ($scope,homeService,$stateParams, $state, msg) {
  372. }]);
  373. app.controller('supportDreamCtrl', ["$scope","homeService", "$stateParams","$state", "msg"
  374. , function ($scope,homeService,$stateParams, $state, msg) {
  375. }]);
  376. app.controller('searchCtrl', ["$scope","homeService", "$state", "msg", "$ionicTabsDelegate", "$ionicNavBarDelegate"
  377. , function ($scope,homeService, $state, msg, $ionicTabsDelegate, $ionicNavBarDelegate) {
  378. $scope.showMore = function (keyword,index) {
  379. if(!keyword)return;
  380. $scope.index = index;
  381. // $scope.vm.keyword = keyword;
  382. homeService.search(keyword).then(function(result){
  383. $scope.isResult = true;
  384. $scope.dream_infos = result.data.data.dream_infos;
  385. $scope.user_infos = result.data.data.user_infos;
  386. });
  387. };
  388. $scope.search = function (keyword) {
  389. $scope.vm.keyword = keyword;
  390. homeService.search(keyword).then(function(result){
  391. $scope.isResult = true;
  392. $scope.dream_infos = result.data.data.dream_infos;
  393. $scope.user_infos = result.data.data.user_infos;
  394. });
  395. };
  396. $scope.toDetail = function (id) {
  397. $state.go('app.home_dreamdetail',{id:id});
  398. };
  399. $scope.toUser = function (id) {
  400. $state.go('app.home_dreamdetail',{id:id});
  401. };
  402. $scope.$on('$ionicView.beforeEnter', function () {
  403. $scope.isResult = false;
  404. $scope.index = 0;
  405. $scope.vm = {
  406. keyword : ""
  407. }
  408. $ionicTabsDelegate.showBar(false);
  409. // $ionicNavBarDelegate.showBackButton(false);
  410. homeService.search('').then(function(result){
  411. $scope.hot_searches = result.data.data.hot_searches;
  412. $scope.history_searches = result.data.data.history_searches;
  413. });
  414. });
  415. $scope.$on('$ionicView.leave', function () {
  416. $ionicTabsDelegate.showBar(true);
  417. });
  418. }]);
  419. app.controller('interactionAddCtrl', ["$scope","homeService", "$state", "msg","config","common", "$ionicTabsDelegate", "$ionicNavBarDelegate"
  420. , function ($scope,homeService, $state, msg,config,common, $ionicTabsDelegate, $ionicNavBarDelegate) {
  421. $scope.$on('$ionicView.beforeEnter', function () {
  422. });
  423. }]);
  424. app.controller('homeDreamsCtrl', ["$scope", "$state", "myService", "msg"
  425. , function ($scope, $state, myService, msg) {
  426. $scope.$on('$ionicView.beforeEnter', function () {
  427. myService.myDream().then(function(result){
  428. console.log(result.data.data);
  429. $scope.dreams = result.data.data;
  430. });
  431. });
  432. $scope.toDetail = function (id) {
  433. $state.go('app.home_dreamdetail',{id:id});
  434. };
  435. }]);
  436. })(angular.module('app.controllers'));